Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
manager.php
1<?php
8
13
15{
16 private static $catalog = null;
17
18 private static $deferredIndexing = -1;
19
20 private static $elementQueue = array();
21
22 private static $indexerInstances = array();
23
32 public static function resolveIblock($iblockId)
33 {
34 if (self::$catalog === null)
35 {
36 self::$catalog = Loader::includeModule("catalog");
37 }
38
39 if (self::$catalog)
40 {
41 $catalog = \CCatalogSKU::getInfoByOfferIBlock($iblockId);
42 if (!empty($catalog) && is_array($catalog))
43 {
44 return $catalog["PRODUCT_IBLOCK_ID"];
45 }
46 }
47
48 return $iblockId;
49 }
50
60 public static function resolveElement($iblockId, $elementId)
61 {
62 if (self::$catalog === null)
63 {
64 self::$catalog = Loader::includeModule("catalog");
65 }
66
67 if (self::$catalog)
68 {
69 $catalog = \CCatalogSKU::getProductInfo($elementId, $iblockId);
70 if (!empty($catalog) && is_array($catalog))
71 {
72 return $catalog["ID"];
73 }
74 }
75
76 return $elementId;
77 }
78
86 public static function dropIfExists($iblockId)
87 {
88 $storage = new Storage($iblockId);
89 if ($storage->isExists())
90 $storage->drop();
91
92 $dictionary = new Dictionary($iblockId);
93 if ($dictionary->isExists())
94 $dictionary->drop();
95 }
96
104 public static function createIndexer($iblockId)
105 {
106 $iblockId = (int)$iblockId;
107 $productIblock = self::resolveIblock($iblockId);
108 if (!isset(self::$indexerInstances[$productIblock]))
109 {
110 $indexer = new Indexer($productIblock);
111 $indexer->init();
112 self::$indexerInstances[$productIblock] = $indexer;
113 unset($indexer);
114 }
115 return self::$indexerInstances[$productIblock];
116 }
117
125 public static function markAsInvalid($iblockId)
126 {
127 Iblock\IblockTable::update($iblockId, array(
128 "PROPERTY_INDEX" => "I",
129 ));
130
131 $productIblock = self::resolveIblock($iblockId);
132 if ($iblockId != $productIblock)
133 {
134 Iblock\IblockTable::update($productIblock, array(
135 "PROPERTY_INDEX" => "I",
136 ));
137 }
138
140 }
141
151 public static function onPropertyUpdate($iblockId, $propertyOld, $propertyNew)
152 {
153 if (array_key_exists("USER_TYPE", $propertyNew))
154 {
155 $storageOld = Iblock\PropertyIndex\Indexer::getPropertyStorageType($propertyOld);
156 $storageNew = Iblock\PropertyIndex\Indexer::getPropertyStorageType($propertyNew);
157 if ($storageOld !== $storageNew)
158 {
159 self::markAsInvalid($iblockId);
160 }
161 }
162 }
163
171 public static function checkAdminNotification($force = false)
172 {
173 if ($force)
174 {
175 $add = true;
176 }
177 else
178 {
179 $iblockList = Iblock\IblockTable::getList(array(
180 'select' => array('ID'),
181 'filter' => array('=PROPERTY_INDEX' => 'I'),
182 ));
183 $add = (bool)$iblockList->fetch();
184 }
185
186 if (ModuleManager::isModuleInstalled('bitrix24'))
187 {
188 $add = false;
189 }
190
191 if ($add)
192 {
193 $notifyList = \CAdminNotify::getList(array(), array(
194 "TAG" => "iblock_property_reindex",
195 ));
196 if (!$notifyList->fetch())
197 {
198 \CAdminNotify::add(array(
199 "MESSAGE" => Loc::getMessage("IBLOCK_NOTIFY_PROPERTY_REINDEX", array(
200 "#LINK#" => "/bitrix/admin/iblock_reindex.php?lang=".\Bitrix\Main\Application::getInstance()->getContext()->getLanguage(),
201 )),
202 "TAG" => "iblock_property_reindex",
203 "MODULE_ID" => "iblock",
204 "ENABLE_CLOSE" => "Y",
205 "PUBLIC_SECTION" => "N",
206 ));
207 }
208 }
209 else
210 {
211 \CAdminNotify::deleteByTag("iblock_property_reindex");
212 }
213 }
221 public static function deleteIndex($iblockId)
222 {
223 self::dropIfExists($iblockId);
224 Iblock\IblockTable::update($iblockId, array(
225 "PROPERTY_INDEX" => "N",
226 ));
227 }
228
237 public static function deleteElementIndex($iblockId, $elementId)
238 {
239 $elementId = (int)$elementId;
240 $productIblock = self::resolveIblock($iblockId);
241 $indexer = self::createIndexer($productIblock);
242
243 if ($indexer->isExists())
244 {
245 if ($iblockId != $productIblock)
246 {
247 self::updateElementIndex($iblockId, $elementId);
248 }
249 else
250 {
251 $indexer->deleteElement($elementId);
252 }
253 }
254 }
255
264 public static function updateElementIndex($iblockId, $elementId)
265 {
266 $elementId = (int)$elementId;
267 $productIblock = self::resolveIblock($iblockId);
268 if ($iblockId != $productIblock)
269 $elementId = self::resolveElement($iblockId, $elementId);
270 if (self::usedDeferredIndexing())
271 {
272 self::pushToQueue($productIblock, $elementId);
273 }
274 else
275 {
276 $indexer = self::createIndexer($productIblock);
277 if ($indexer->isExists())
278 {
279 self::elementIndexing($indexer, $elementId);
280 }
281 unset($indexer);
282 }
283 }
284
290 public static function enableDeferredIndexing()
291 {
292 self::$deferredIndexing++;
293 }
294
300 public static function disableDeferredIndexing()
301 {
302 self::$deferredIndexing--;
303 }
304
310 public static function usedDeferredIndexing()
311 {
312 return (self::$deferredIndexing >= 0);
313 }
314
321 public static function runDeferredIndexing($iblockId)
322 {
323 $iblockId = (int)$iblockId;
324 $productIblock = self::resolveIblock($iblockId);
325 if (empty(self::$elementQueue[$productIblock]))
326 return;
327 $indexer = self::createIndexer($productIblock);
328 if ($indexer->isExists())
329 {
330 sort(self::$elementQueue[$productIblock]);
331
332 foreach (self::$elementQueue[$productIblock] as $elementId)
333 self::elementIndexing($indexer, $elementId);
334 unset($elementId);
335 }
336 unset($indexer);
337 unset(self::$elementQueue[$productIblock]);
338 }
339
340 private static function pushToQueue($iblockId, $elementId)
341 {
342 if (!isset(self::$elementQueue[$iblockId]))
343 self::$elementQueue[$iblockId] = [];
344 self::$elementQueue[$iblockId][$elementId] = $elementId;
345 }
346
347 private static function elementIndexing(Iblock\PropertyIndex\Indexer $indexer, $elementId)
348 {
349 $indexer->deleteElement($elementId);
350 $connection = \Bitrix\Main\Application::getConnection();
351 $elementCheck = $connection->query("
352 SELECT BE.ID
353 FROM b_iblock_element BE
354 WHERE BE.ACTIVE = 'Y' AND BE.ID = ".$elementId.
355 \CIBlockElement::wf_getSqlLimit("BE.", "N")
356 );
357 if ($elementCheck->fetch())
358 {
359 $indexer->indexElement($elementId);
360 }
361 unset($elementCheck, $connection);
362 }
363}
static deleteElementIndex($iblockId, $elementId)
Definition manager.php:237
static checkAdminNotification($force=false)
Definition manager.php:171
static resolveIblock($iblockId)
Definition manager.php:32
static updateElementIndex($iblockId, $elementId)
Definition manager.php:264
static onPropertyUpdate($iblockId, $propertyOld, $propertyNew)
Definition manager.php:151
static runDeferredIndexing($iblockId)
Definition manager.php:321
static resolveElement($iblockId, $elementId)
Definition manager.php:60
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static isModuleInstalled($moduleName)