%PDF- %PDF-
Server IP : 37.220.80.31 / Your IP : 18.217.141.52 Web Server : Apache/2.4.52 (Ubuntu) System : Linux 3051455-guretool.twc1.net 5.15.0-107-generic #117-Ubuntu SMP Fri Apr 26 12:26:49 UTC 2024 x86_64 User : www-root ( 1010) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/www-root/data/www/dev.artlot24.ru/local/php_interface/lib/Cron/Install/ |
Upload File : |
<?php namespace LocalLib\Cron\Install; use Bitrix\Iblock\SectionTable; use Bitrix\Main\Application; use Bitrix\Main\ORM\Fields\Relations\Reference; use Bitrix\Main\ORM\Query\Join; use CIBlock; use CIBlockElement; use Exception; use LocalLib\Cron\Base; use LocalLib\Excel\ExcelReader; use LocalLib\GetItems\GetItemsCatalog; use LocalLib\Helpers\Iblock\IblockHelper; use LocalLib\Helpers\Iblock\IblockPropertyHelper; use LocalLib\ORM\ElementPropertyTable; class ImportSectionsFromXlsx extends Base{ protected $pathToFile = '/upload/import_wort/item_rubrics_items.xlsx'; protected function init() { parent::init(); if (empty($this->pathToFile)) { throw new Exception('Путь к файлу не указан'); } $this->pathToFile = Application::getDocumentRoot().$this->pathToFile; if(!file_exists($this->pathToFile)){ throw new Exception('Файл не найден'); } $this->arPropsCatalog = IblockPropertyHelper::getProperties(IblockHelper::getIblockIdCatalog(), ['OLD_ID']); if($this->arPropsCatalog['OLD_ID']['ID'] <= 0){ throw new Exception('Не удалось найти свойство OLD_ID'); } } public function exec() { try { $this->init(); $this->parseFile(); $this->obGetItemsCatalog = new GetItemsCatalog(); $this->obGetItemsCatalog->arSelect = [ 'ID', 'IBLOCK_ID', 'ACTIVE', 'PROP__OLD_ID' => 'P__OLD_ID.VALUE' ]; $this->obGetItemsCatalog->arRuntimeFields[] = new Reference( 'P__OLD_ID', ElementPropertyTable::class, Join::on('this.ID', 'ref.IBLOCK_ELEMENT_ID') ->where('ref.IBLOCK_PROPERTY_ID', '=', $this->arPropsCatalog['OLD_ID']['ID']) ); $this->countItems = $this->obGetItemsCatalog->setCountItems(); $this->obLogFile->writeMessageToFile('countItems = '.$this->countItems); if($this->obGetItemsCatalog->countItems){ while($obItems = $this->obGetItemsCatalog->getItems()){ while($arItem = $obItems->fetch()){ $this->obGetItemsCatalog->id = $arItem['ID']; $this->execItem($arItem); } $this->obLogFile->writeHtmlToFile('<hr />'); $this->obLogFile->writeMessageToFile($this->getResume()); } CIBlock::clearIblockTagCache(IblockHelper::getIblockIdCatalog()); } $this->epilog(); } catch (Exception $e) { $this->exception($e); } } private function parseFile() { $this->obExcelReader = new ExcelReader(); $obSheet = $this->obExcelReader->getWorksheetIterator($this->pathToFile); $arData = $obSheet->toArray(); if($arData[0]){ unset($arData[0]); } if (empty($arData)) { throw new Exception('Файл пуст'); } $arSectionIds = []; $ob = SectionTable::getList([ 'select' => ['ID', 'XML_ID'], ]); while ($ar = $ob->fetch()) { if (empty($ar['XML_ID'])) { continue; } $arSectionIds[$ar['XML_ID']] = $ar['ID']; } foreach ($arData as $ar) { $cat = intval($ar[1]); $el = intval($ar[2]); if($el <= 0 || $cat <= 0 || $arSectionIds[$cat] <= 0){ continue; } $this->arData[$el][] = $arSectionIds[$cat]; } if (empty($this->arData)) { throw new Exception('$this->arData пуст'); } } private function execItem($arItem) { try { $this->processedItems++; $this->obLogFile->writeHtmlToFile('<hr />'); $this->obLogFile->writeDebugMessageToFile($arItem, 'arItem'); $oldId = trim((string)$arItem['PROP__OLD_ID']); if (empty($oldId)) { return; } if($this->arData[$oldId][0] > 0){ $arFields = [ 'IBLOCK_SECTION_ID' => $this->arData[$oldId][0], 'IBLOCK_SECTION' => $this->arData[$oldId], ]; }else{ $arFields = [ 'IBLOCK_SECTION_ID' => false, 'IBLOCK_SECTION' => false, ]; } $this->obLogFile->writeDebugMessageToFile($arFields, 'arFields', false); $el = new CIBlockElement(); if(!$el->Update($arItem['ID'], $arFields, false, false, false)){ throw new Exception('Не удалось обновить элемент. '.$el->LAST_ERROR); } $this->obLogFile->writeMessageToFile('Элемент успешно обновлён',false); $this->successfulItems++; } catch (Exception $e) { $this->obLogFile->writeExceptionToFile($e); } } }