Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
yandexregion.php
1
<?php
8
namespace
Bitrix\Seo\Adv
;
9
10
use
Bitrix\Main\Application
;
11
use
Bitrix\Main\Config\Option
;
12
use
Bitrix\Main\Localization\Loc
;
13
use
Bitrix\Main\Type\Collection
;
14
use
Bitrix\Seo\AdvEntity
;
15
use
Bitrix\Seo\Engine
;
16
17
Loc::loadMessages
(__FILE__);
18
50
class
YandexRegionTable
extends
AdvEntity
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
}
Bitrix\Main\Application
Definition
application.php:28
Bitrix\Main\Application\getConnection
static getConnection($name="")
Definition
application.php:611
Bitrix\Main\Config\Option
Definition
option.php:15
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Definition
loc.php:64
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\ORM\Objectify\Collection
Definition
collection.php:33
Bitrix\Seo\Adv\YandexRegionTable
Definition
yandexregion.php:51
Bitrix\Seo\Adv\YandexRegionTable\setLastUpdate
static setLastUpdate($v=null)
Definition
yandexregion.php:184
Bitrix\Seo\Adv\YandexRegionTable\OPTION_LAST_UPDATE
const OPTION_LAST_UPDATE
Definition
yandexregion.php:54
Bitrix\Seo\Adv\YandexRegionTable\getMap
static getMap()
Definition
yandexregion.php:68
Bitrix\Seo\Adv\YandexRegionTable\needDatabaseUpdate
static needDatabaseUpdate()
Definition
yandexregion.php:179
Bitrix\Seo\Adv\YandexRegionTable\getEngine
static getEngine()
Definition
yandexregion.php:85
Bitrix\Seo\Adv\YandexRegionTable\getList
static getList(array $parameters=array())
Definition
yandexregion.php:94
Bitrix\Seo\Adv\YandexRegionTable\getLastUpdate
static getLastUpdate()
Definition
yandexregion.php:194
Bitrix\Seo\Adv\YandexRegionTable\getFilePath
static getFilePath()
Definition
yandexregion.php:58
Bitrix\Seo\Adv\YandexRegionTable\clearDatabase
static clearDatabase()
Definition
yandexregion.php:173
Bitrix\Seo\Adv\YandexRegionTable\updateDatabase
static updateDatabase()
Definition
yandexregion.php:104
Bitrix\Seo\Adv\YandexRegionTable\updateDatabaseItem
static updateDatabaseItem(array &$regionMap, $regionId)
Definition
yandexregion.php:125
Bitrix\Seo\Adv\YandexRegionTable\ENGINE
const ENGINE
Definition
yandexregion.php:52
Bitrix\Seo\Adv\YandexRegionTable\CACHE_LIFETIME
const CACHE_LIFETIME
Definition
yandexregion.php:53
Bitrix\Seo\Adv\YandexRegionTable\getTableName
static getTableName()
Definition
yandexregion.php:63
Bitrix\Seo\AdvEntity
Definition
adventity.php:35
Bitrix\Seo\Engine\YandexDirect
Definition
yandexdirect.php:32
Bitrix\Seo\Adv
Definition
auto.php:2
Bitrix\Seo\Engine
Definition
bitrix.php:9
modules
seo
lib
adv
yandexregion.php
Создано системой
1.10.0