Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
yandexregion.php
1<?php
8namespace Bitrix\Seo\Adv;
9
16
17Loc::loadMessages(__FILE__);
18
51{
52 const ENGINE = 'yandex_direct';
53 const CACHE_LIFETIME = 2592000;
54 const OPTION_LAST_UPDATE = 'yandex_direct_region_last_update';
55
56 private static $engine = null;
57
58 public static function getFilePath()
59 {
60 return __FILE__;
61 }
62
63 public static function getTableName()
64 {
65 return 'b_seo_adv_region';
66 }
67
68 public static function getMap()
69 {
70 return array_merge(
71 parent::getMap(),
72 array(
73 'PARENT_ID' => array(
74 'data_type' => 'integer',
75 'title' => Loc::getMessage('ADV_REGION_ENTITY_PARENT_ID_FIELD'),
76 ),
77 'PARENT' => array(
78 'data_type' => 'Bitrix\Seo\Adv\YandexRegionTable',
79 'reference' => array('=this.PARENT_ID' => 'ref.ID'),
80 ),
81 )
82 );
83 }
84
85 public static function getEngine()
86 {
87 if(!self::$engine)
88 {
89 self::$engine = new Engine\YandexDirect();
90 }
91 return self::$engine;
92 }
93
94 public static function getList(array $parameters = array())
95 {
96 if (static::needDatabaseUpdate())
97 {
98 static::updateDatabase();
99 }
100
101 return parent::getList($parameters);
102 }
103
104 public static function updateDatabase()
105 {
106 $engine = static::getEngine();
107 $regionList = $engine->getRegions();
108
109 $regionMap = array();
110 foreach($regionList as $region)
111 {
112 $regionMap[$region['RegionID']] = $region;
113 }
114
115 static::clearDatabase();
116
117 foreach($regionMap as $regionId => $region)
118 {
119 static::updateDatabaseItem($regionMap, $regionId);
120 }
121
122 static::setLastUpdate();
123 }
124
125 protected static function updateDatabaseItem(array &$regionMap, $regionId)
126 {
127 $region = $regionMap[$regionId];
128
129 if(!$regionMap[$region["RegionID"]]["ID"])
130 {
131 $engine = static::getEngine();
132 $ownerInfo = $engine->getCurrentUser();
133
134 $parentId = 0;
135 if($region["ParentID"] !== '')
136 {
137 if(array_key_exists($region["ParentID"], $regionMap))
138 {
139 if($regionMap[$region["ParentID"]]["ID"] > 0)
140 {
141 $parentId = $regionMap[$region["ParentID"]]["ID"];
142 }
143 else
144 {
145 $parentId = static::updateDatabaseItem(
146 $regionMap,
147 $region["ParentID"]
148 );
149 }
150 }
151 }
152
153 $regionData = array(
154 "ENGINE_ID" => $engine->getId(),
155 "OWNER_ID" => $ownerInfo['id'],
156 "OWNER_NAME" => $ownerInfo['login'],
157 "XML_ID" => $region["RegionID"],
158 "NAME" => $region["RegionName"],
159 "PARENT_ID" => $parentId
160 );
161
162 $result = static::add($regionData);
163
164 if($result->isSuccess())
165 {
166 $regionMap[$region["RegionID"]]["ID"] = $result->getId();
167 }
168 }
169
170 return $regionMap[$region["RegionID"]]["ID"];
171 }
172
173 protected static function clearDatabase()
174 {
175 $connection = Application::getConnection();
176 $connection->truncateTable(static::getTableName());
177 }
178
179 protected static function needDatabaseUpdate()
180 {
181 return time() - static::getLastUpdate() > static::CACHE_LIFETIME;
182 }
183
184 public static function setLastUpdate($v = null)
185 {
186 if($v === null)
187 {
188 $v = time();
189 }
190
191 Option::set('seo', static::OPTION_LAST_UPDATE, $v);
192 }
193
194 public static function getLastUpdate()
195 {
196 return Option::get('seo', static::OPTION_LAST_UPDATE, 0);
197 }
198
199}
static getConnection($name="")
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())
static updateDatabaseItem(array &$regionMap, $regionId)