Bitrix-D7  20.0.0
Класс SitemapIblock

Открытые статические члены

static __callStatic ($name, $arguments)
 Event handler for multiple IBlock events. Подробнее...
 
static prepareUrlToReplace ($url, $siteId=NULL)
 Replace some parts of URL-template, then not correct processing in replaceDetailUrl. Подробнее...
 

Защищенные статические члены

static checkElement (&$fields)
 Checks if element is a real element, not a workflow item. Подробнее...
 
static actionUpdate ($data, $element)
 Processes actions on IBlock element or section update. Подробнее...
 
static actionDelete ($data)
 Processes actions on IBlock element or section delete. Подробнее...
 
static actionAdd ($name, $fields)
 Processes actions on IBlock element or section add. Подробнее...
 

Подробное описание

См. определение в файле sitemapiblock.php строка 212

Методы

◆ __callStatic()

static __callStatic (   $name,
  $arguments 
)
static

Event handler for multiple IBlock events.

См. определение в файле sitemapiblock.php строка 224

225  {
226  $name = ToUpper($name);
227 
228  switch($name)
229  {
230  case 'ADDELEMENT':
231  case 'ADDSECTION':
232  if(
233  $arguments[0]["ID"] > 0
234  && $arguments[0]['IBLOCK_ID'] > 0
235  && (
236  !isset($arguments[0]['ACTIVE'])
237  || $arguments[0]['ACTIVE'] == 'Y'
238  )
239  )
240  {
241  // we recieve array reference here
242  $fields = array();
243  foreach($arguments[0] as $key => $value)
244  {
245  $fields[$key] = $value;
246  }
247 
248  if(!isset($fields['EXTERNAL_ID']) && isset($fields['XML_ID']))
249  {
250  $fields['EXTERNAL_ID'] = $fields['XML_ID'];
251  }
252 
253  self::actionAdd($name, $fields);
254  }
255  break;
256 
257  case 'BEFOREDELETEELEMENT':
258  case 'BEFOREDELETESECTION':
259  case 'BEFOREUPDATEELEMENT':
260  case 'BEFOREUPDATESECTION':
261  $ID = $arguments[0];
262  if(is_array($ID))
263  $ID = $ID['ID'];
264 
265  if($ID > 0)
266  {
267  $element = $name == 'BEFOREDELETEELEMENT' || $name == 'BEFOREUPDATEELEMENT';
268 
269  $dbFields = $element
270  ? \CIBlockElement::getByID($ID)
271  : \CIBlockSection::getByID($ID);
272 
273  $fields = $dbFields->getNext();
274  if($fields)
275  {
276  if($element && !self::checkElement($fields))
277  {
278  return;
279  }
280 
282  $fields,
284  );
285 
286  if (count($sitemaps) > 0)
287  {
288 // URL from $fields may be incorrect, if using #SERVER_NAME# template in iblock URL-template
289 // And we must generate true URL
290  $dbIblock = \CIBlock::GetByID($fields['IBLOCK_ID']);
291  $iblock = $dbIblock->GetNext();
292  $url = $element
293  ? $iblock['~DETAIL_PAGE_URL']
294  : $iblock['~SECTION_PAGE_URL'];
295  $url = self::prepareUrlToReplace($url);
296  $url = \CIBlock::replaceDetailUrl($url, $fields, false, $element ? 'E' : 'S');
297 
298  self::$beforeActions[$name][intval($element)][$ID] = array(
299  'URL' => $url,
300  'FIELDS' => $fields,
301  'SITEMAPS' => $sitemaps,
302  );
303  }
304  }
305  }
306  break;
307 
308  case 'DELETEELEMENT':
309  case 'DELETESECTION':
310  case 'UPDATEELEMENT':
311  case 'UPDATESECTION':
312 
313  $fields = $arguments[0];
314  $element = $name == 'DELETEELEMENT' || $name == 'UPDATEELEMENT';
315 
316  if(
317  is_array($fields)
318  && $fields['ID'] > 0
319  && isset(self::$beforeActions['BEFORE'.$name][intval($element)][$fields['ID']])
320  )
321  {
322  if($fields['RESULT'] !== false)
323  {
324  if($name == 'DELETEELEMENT' || $name == 'DELETESECTION')
325  {
326  self::actionDelete(self::$beforeActions['BEFORE'.$name][intval($element)][$fields['ID']]);
327  }
328  else
329  {
330  self::actionUpdate(self::$beforeActions['BEFORE'.$name][intval($element)][$fields['ID']], $element);
331  }
332  }
333 
334  unset(self::$beforeActions['BEFORE'.$name][intval($element)][$fields['ID']]);
335  }
336 
337  break;
338 
339  }
340  }

◆ actionAdd()

static actionAdd (   $name,
  $fields 
)
staticprotected

Processes actions on IBlock element or section add.

Аргументы
string$nameEvent handler name.
array$fieldsElement/section fields.

См. определение в файле sitemapiblock.php строка 477

478  {
479  if($name == 'ADDELEMENT')
480  {
481  if(!self::checkElement($fields))
482  {
483  return;
484  }
485 
486  // we don't have the GLOBAL_ACTIVE flag in fields so we should check it manually
487  if(is_array($fields['IBLOCK_SECTION']) && count($fields['IBLOCK_SECTION']) > 0)
488  {
489  $newSections = array();
490  $filter = array(
491  'ID' => $fields['IBLOCK_SECTION'],
492  'IBLOCK_ID' => $fields['IBLOCK_ID'],
493  'GLOBAL_ACTIVE' => 'Y'
494  );
495 
496  $dbRes = \CIBlockSection::getList(array(), $filter, false, array('ID', 'IBLOCK_TYPE_ID', 'IBLOCK_CODE'));
497  while ($ar = $dbRes->fetch())
498  {
499  $newSections[] = $ar['ID'];
500  $iblockTypeId = $ar['IBLOCK_TYPE_ID'] ? $ar['IBLOCK_TYPE_ID'] : null;
501  $iblockCode = $ar['IBLOCK_CODE'] ? $ar['IBLOCK_CODE'] : null;
502  }
503 
504  if(count($newSections) <= 0)
505  {
506  // element is added to inactive sections
507  return;
508  }
509 
510  $fields['IBLOCK_SECTION'] = $newSections;
511  }
512  }
513  elseif($name == 'ADDSECTION')
514  {
515  $dbRes = \CIBlockSection::getList(
516  array(),
517  array('ID' => $fields['ID'], 'GLOBAL_ACTIVE' => 'Y'),
518  false,
519  array('ID', 'IBLOCK_TYPE_ID', 'IBLOCK_CODE')
520  );
521 
522  $inactiveBranch = true;
523  while ($ar = $dbRes->fetch())
524  {
525  $iblockTypeId = $ar['IBLOCK_TYPE_ID'] ? $ar['IBLOCK_TYPE_ID'] : null;
526  $iblockCode = $ar['IBLOCK_CODE'] ? $ar['IBLOCK_CODE'] : null;
527  $inactiveBranch = false;
528  }
529 
530  if ($inactiveBranch)
531  {
532  // section is added to inactive branch
533  return;
534  }
535  }
536 
537  $fields['IBLOCK_TYPE_ID'] = $iblockTypeId;
538  $fields['IBLOCK_CODE'] = $iblockCode;
539 
541  $fields,
543  );
544 
545  $fields['TIMESTAMP_X'] = ConvertTimeStamp(false, "FULL");
546 
547  if(isset($fields['IBLOCK_SECTION']) && is_array($fields['IBLOCK_SECTION']) && count($fields['IBLOCK_SECTION']) > 0)
548  {
549  $fields['IBLOCK_SECTION_ID'] = min($fields['IBLOCK_SECTION']);
550  }
551 
552  if(count($sitemaps) > 0)
553  {
554  $siteDirs = self::createSiteDirs();
555 
556  foreach ($sitemaps as $sitemap)
557  {
558  $fileName = str_replace(
559  array('#IBLOCK_ID#', '#IBLOCK_CODE#', '#IBLOCK_XML_ID#'),
560  array($fields['IBLOCK_ID'], $sitemap['IBLOCK_CODE'], $sitemap['IBLOCK_XML_ID']),
561  $sitemap['SITEMAP_FILE_IBLOCK']
562  );
563 
564  $sitemapFile = new SitemapFile($fileName, $sitemap);
565 
566 // write changes to temp file to preserve collisions
567  $sitemapRuntimeId = $sitemap['SITE_ID'] . '-' . uniqid();
568  $sitemapRuntimeId .= isset($fields['ID']) ? '-' . $fields['ID'] . '-' : '';
569  $sitemapRuntimeFile = new SitemapRuntime($sitemapRuntimeId, $fileName, $sitemap);
570 
571  if(self::checkActivity($name == 'ADDELEMENT' ? true : false, $fields))
572  {
573  $fields['LANG_DIR'] = $siteDirs[$sitemap['SITE_ID']];
574 
575  $url = $name == 'ADDSECTION' ? $sitemap['SECTION_PAGE_URL'] : $sitemap['DETAIL_PAGE_URL'];
576  $urlType = $name == 'ADDSECTION' ? 'S' : 'E';
577 // remove or replace SERVER_NAME
578  $url = self::prepareUrlToReplace($url, $sitemap['SITE_ID']);
579  $rule = array(
580  'url' => \CIBlock::replaceDetailUrl($url, $fields, false, $urlType),
581  'lastmod' => MakeTimeStamp($fields['TIMESTAMP_X']),
582  );
583 
584  $sitemapRuntimeFile->setOriginalFile($sitemapFile);
585  $sitemapRuntimeFile->appendIblockEntry($rule['url'], $rule['lastmod']);
586  }
587 
588 // after this in original file will be added always changes
589  if ($sitemapRuntimeFile->isNotEmpty() && $sitemapRuntimeFile->isCurrentPartNotEmpty())
590  $sitemapRuntimeFile->finish();
591  else
592  $sitemapRuntimeFile->delete();
593 
594  $sitemapIndex = new SitemapIndex($sitemap['SITEMAP_FILE'], $sitemap);
595  $sitemapIndex->appendIndexEntry($sitemapFile);
596 
597  if ($sitemap['ROBOTS'] == 'Y')
598  {
599  $robotsFile = new RobotsFile($sitemap['SITE_ID']);
600  $robotsFile->addRule(
601  array(RobotsFile::SITEMAP_RULE, $sitemapIndex->getUrl())
602  );
603  }
604  }
605  }
606  }

◆ actionDelete()

static actionDelete (   $data)
staticprotected

Processes actions on IBlock element or section delete.

Аргументы
array$dataData got from SitemapIblockTable::getByIblock() + element/section data + prev link data got from event handler.

См. определение в файле sitemapiblock.php строка 452

453  {
454  $fields = $data['FIELDS'];
455  foreach ($data['SITEMAPS'] as $sitemap)
456  {
457  $fileName = str_replace(
458  array('#IBLOCK_ID#', '#IBLOCK_CODE#', '#IBLOCK_XML_ID#'),
459  array($fields['IBLOCK_ID'], $sitemap['IBLOCK_CODE'], $sitemap['IBLOCK_XML_ID']),
460  $sitemap['SITEMAP_FILE_IBLOCK']
461  );
462 
463  $sitemapFile = new SitemapFile($fileName, $sitemap);
464  $sitemapFile->removeEntry($data['URL']);
465 
466  $sitemapIndex = new SitemapIndex($sitemap['SITEMAP_FILE'], $sitemap);
467  $sitemapIndex->appendIndexEntry($sitemapFile);
468  }
469  }

◆ actionUpdate()

static actionUpdate (   $data,
  $element 
)
staticprotected

Processes actions on IBlock element or section update.

Аргументы
array$dataData got from SitemapIblockTable::getByIblock() + element/section data + prev link data got from event handler.
bool$elementElement or section.

См. определение в файле sitemapiblock.php строка 376

377  {
378  $fields = $data['FIELDS'];
379  $siteDirs = self::createSiteDirs();
380 
381  foreach ($data['SITEMAPS'] as $sitemap)
382  {
383  $fileName = str_replace(
384  array('#IBLOCK_ID#', '#IBLOCK_CODE#', '#IBLOCK_XML_ID#'),
385  array($fields['IBLOCK_ID'], $sitemap['IBLOCK_CODE'], $sitemap['IBLOCK_XML_ID']),
386  $sitemap['SITEMAP_FILE_IBLOCK']
387  );
388 
389  if($element)
390  $dbRes = \CIBlockElement::getByID($fields["ID"]);
391  else
392  $dbRes = \CIBlockSection::getByID($fields["ID"]);
393 
394  $newFields = $dbRes->fetch();
395 
396  $sitemapFile = new SitemapFile($fileName, $sitemap);
397 // try remove entry from original file, to not create temp files to all parts
398 // name may was changed in removeEntry
399  $fileName = $sitemapFile->removeEntry($data['URL']);
400 
401 // write changes to temp file to preserve collisions
402  $sitemapRuntimeId = $sitemap['SITE_ID'] . '-' . uniqid();
403  $sitemapRuntimeId .= isset($fields['ID']) ? '-' . $fields['ID'] . '-' : '';
404  $sitemapRuntimeFile = new SitemapRuntime($sitemapRuntimeId, $fileName, $sitemap);
405 
406 // check ACTIVITY by active, date or etc, add entry only for active
407  if(self::checkActivity($element, $newFields))
408  {
409  $newFields['LANG_DIR'] = $siteDirs[$sitemap['SITE_ID']];
410 
411 // use just date(). it is not true, but because we use BEFORE event, we cant use real lastmod date, only previous value
412  $date = date('d.m.Y H:i:s');
413 
414  $url = $element ? $sitemap['DETAIL_PAGE_URL'] : $sitemap['SECTION_PAGE_URL'];
415  $urlType = $element ? 'E' : 'S';
416 // remove or replace SERVER_NAME
417  $url = self::prepareUrlToReplace($url, $sitemap['SITE_ID']);
418  $rule = array(
419  'url' => \CIBlock::replaceDetailUrl($url, $newFields, false, $urlType),
420  'lastmod' => MakeTimeStamp($date),
421  );
422 
423  $sitemapRuntimeFile->setOriginalFile($sitemapFile);
424  $sitemapRuntimeFile->appendIblockEntry($rule['url'], $rule['lastmod']);
425  }
426 
427 // rename RUNTIME file to original SITEMAPFILE name, or just remove TMP file
428 // after this in original file will be added always changes
429  if ($sitemapRuntimeFile->isNotEmpty() && $sitemapRuntimeFile->isCurrentPartNotEmpty())
430  $sitemapRuntimeFile->finish();
431  else
432  $sitemapRuntimeFile->delete();
433 
434  $sitemapIndex = new SitemapIndex($sitemap['SITEMAP_FILE'], $sitemap);
435  $sitemapIndex->appendIndexEntry($sitemapFile);
436 
437  if ($sitemap['ROBOTS'] == 'Y')
438  {
439  $robotsFile = new RobotsFile($sitemap['SITE_ID']);
440  $robotsFile->addRule(
441  array(RobotsFile::SITEMAP_RULE, $sitemapIndex->getUrl())
442  );
443  }
444  }
445  }

◆ checkElement()

static checkElement ( $fields)
staticprotected

Checks if element is a real element, not a workflow item.

Аргументы
array$fieldsElement fields.
Возвращает
bool

См. определение в файле sitemapiblock.php строка 349

350  {
351  if($fields['WF'] === 'Y')
352  {
353  if(
354  $fields['WF_PARENT_ELEMENT_ID'] > 0
355  && $fields['WF_PARENT_ELEMENT_ID'] != $fields['ID']
356  && $fields['WF_STATUS_ID'] == 1
357  )
358  {
359  $fields['ID'] = $fields['WF_PARENT_ELEMENT_ID'];
360  }
361  else
362  {
363  return false;
364  }
365  }
366 
367  return true;
368  }

◆ prepareUrlToReplace()

static prepareUrlToReplace (   $url,
  $siteId = NULL 
)
static

Replace some parts of URL-template, then not correct processing in replaceDetailUrl.

Аргументы
string$url- String of URL-template.
null$siteId- In NULL - #SERVER_NAME# will not replaced.
Возвращает
mixed|string

См. определение в файле sitemapiblock.php строка 670

671  {
672 // REMOVE PROTOCOL - we put them later, based on user settings
673  $url = str_replace('http://', '', $url);
674  $url = str_replace('https://', '', $url);
675 
676 // REMOVE SERVER_NAME from start position, because we put server_url later
677  if (substr($url, 0, strlen('#SERVER_NAME#')) == '#SERVER_NAME#')
678  $url = substr($url, strlen('#SERVER_NAME#'));
679 
680 // get correct SERVER_URL
681  if ($siteId)
682  {
683  $filter = array('=LID' => $siteId);
684  $dbSite = SiteTable::getList(array(
685  'filter' => $filter,
686  'select' => array('LID', 'DIR', 'SERVER_NAME'),
687  ));
688  $currentSite = $dbSite->fetch();
689  $serverName = $currentSite['SERVER_NAME'];
690  $url = str_replace('#SERVER_NAME#', $serverName, $url);
691  }
692 
693  return $url;
694  }

Объявления и описания членов класса находятся в файле:
Bitrix\Seo\SitemapIblock\actionDelete
static actionDelete($data)
Processes actions on IBlock element or section delete.
Definition: sitemapiblock.php:452
Bitrix\Seo\SitemapIblockTable\getByIblock
static getByIblock($fields, $itemType)
Returns array of data for sitemap update due to some iblock action.
Definition: sitemapiblock.php:93
Bitrix\Seo\RobotsFile\SITEMAP_RULE
const SITEMAP_RULE
Definition: robotsfile.php:18
Bitrix\Seo\SitemapIblock\actionUpdate
static actionUpdate($data, $element)
Processes actions on IBlock element or section update.
Definition: sitemapiblock.php:376
Bitrix\Seo\SitemapIblock\prepareUrlToReplace
static prepareUrlToReplace($url, $siteId=NULL)
Replace some parts of URL-template, then not correct processing in replaceDetailUrl.
Definition: sitemapiblock.php:670
Bitrix\Seo\SitemapIblock\actionAdd
static actionAdd($name, $fields)
Processes actions on IBlock element or section add.
Definition: sitemapiblock.php:477
Bitrix\Seo\SitemapIblockTable\TYPE_SECTION
const TYPE_SECTION
Definition: sitemapiblock.php:19
Bitrix\Main\ORM\Data\DataManager\getList
static getList(array $parameters=array())
Executes the query and returns selection by parameters of the query.
Definition: main/lib/orm/data/datamanager.php:438
Bitrix\Seo\SitemapIblockTable\TYPE_ELEMENT
const TYPE_ELEMENT
Definition: sitemapiblock.php:18