Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/wbs24.ozonexport/lib/ |
Upload File : |
<?php namespace Wbs24\Ozonexport; use Bitrix\Main\Localization\Loc; class Admin { protected $wrappers; protected $cachedProperties; protected $cachedPriceTypes; public function __construct($param = []) { $objects = $param['objects'] ?? []; $this->wrappers = new Wrappers($objects); } public function loadJs() { echo '<script src="/bitrix/js/wbs24.ozonexport/admin.js?'.time().'"></script>'; } public function getSelectProperties($iblockId, $field, $currentValue, $props = []) { $propertiesOptions = $this->getAllValidProperties(); foreach ($propertiesOptions as $key => $option) { $propertiesOptions[$key]['VALUE'] = $option['ID']; $propertiesOptions[$key]['FOR_JS'] = 'data-iblock-id="'.$option['IBLOCK_ID'].'"'; } $options = []; if ($field == 'BLOB[packageRatioProduct]') { $options[] = [ 'NAME' => Loc::getMessage("BX_CATALOG_EXPORT_PACKAGING_RATIO_DEFAULT_VALUE"), 'VALUE' => '', 'FOR_JS' => '', ]; } else { $options[] = [ 'NAME' => '-', 'VALUE' => '', 'FOR_JS' => '', ]; } $options = array_merge($options, $propertiesOptions); $selectCode = $this->getSelect($field, $options, $currentValue, $props); if ($iblockId) { if ($field == 'SET_OFFER_ID') { $iblockInfo = $this->getInfoByIblockId($iblockId); $iblockId = $iblockInfo['OFFERS_IBLOCK_ID']; } $jsCode = '<script>' .'document.addEventListener("DOMContentLoaded", function () {' .'let ozon = new Wbs24Ozonexport();' .( $this->isContainBrackets($field) ? 'ozon.activateOptionsForCurrentIblockById("'.$this->getFieldId($field).'", '.$iblockId.');' : 'ozon.activateOptionsForCurrentIblock("'.$field.'", '.$iblockId.');' ) .'});' .'</script>' ; } return $selectCode.$jsCode; } public function getSelectPropertiesForOffersIblock($iblockId, $field, $currentValue, $props = []) { $propertiesOptions = $this->getAllValidProperties(); foreach ($propertiesOptions as $key => $option) { $propertiesOptions[$key]['VALUE'] = $option['ID']; $propertiesOptions[$key]['FOR_JS'] = 'data-iblock-id="'.$option['IBLOCK_ID'].'"'; } $options = []; $options[] = [ 'NAME' => '-', 'VALUE' => '', 'FOR_JS' => '', ]; $options = array_merge($options, $propertiesOptions); $selectCode = $this->getSelect($field, $options, $currentValue, $props); if ($iblockId) { $iblockInfo = $this->getInfoByIblockId($iblockId); $iblockId = $iblockInfo['OFFERS_IBLOCK_ID']; $jsCode = '<script>' .'document.addEventListener("DOMContentLoaded", function () {' .'let ozon = new Wbs24Ozonexport();' .( $this->isContainBrackets($field) ? 'ozon.activateOptionsForCurrentIblockById("'.$this->getFieldId($field).'", '.$iblockId.');' : 'ozon.activateOptionsForCurrentIblock("'.$field.'", '.$iblockId.');' ) .'});' .'</script>' ; } return $selectCode.$jsCode; } public function getSelectPropertiesForOfferIblockAndParentProperties($iblockId, $field, $currentValue) { $propertiesOptions = $this->getAllValidProperties(); $catalogIblockIdsToOffersIblockIds = $this->getCatalogIblockIdsToOffersIblockIds(); $catalogIblockIds = array_keys($catalogIblockIdsToOffersIblockIds); $offersIblockIds = array_values($catalogIblockIdsToOffersIblockIds); $simpleProductTitle = []; $simpleProductTitle[] = [ 'NAME' => Loc::getMessage("BX_CATALOG_EXPORT_PACKAGING_PRODUCT_TITLE"), 'VALUE' => 'title_product', 'FOR_JS' => '', 'NO_CHECK' => true, ]; $simpleProductOptions = $this->getPropertiesOptionsForIblock($propertiesOptions, $catalogIblockIds); $offerProductTitle = []; $offerProductTitle[] = [ 'NAME' => Loc::getMessage("BX_CATALOG_EXPORT_PACKAGING_OFFER_TITLE"), 'VALUE' => 'title_offer', 'FOR_JS' => '', 'NO_CHECK' => true, ]; $offerProductOptions = $this->getPropertiesOptionsForIblock($propertiesOptions, $offersIblockIds); $simpleProductOptions = array_merge($simpleProductTitle, $simpleProductOptions); $offerProductOptions = array_merge($offerProductTitle, $offerProductOptions); $propertiesOptions = array_merge($simpleProductOptions, $offerProductOptions); foreach ($propertiesOptions as $key => $option) { if ($option['NO_CHECK'] == true) continue; $propertiesOptions[$key]['VALUE'] = $option['ID']; $propertiesOptions[$key]['FOR_JS'] = 'data-iblock-id="'.$option['IBLOCK_ID'].'"'; } $options = []; $options[] = [ 'NAME' => Loc::getMessage("BX_CATALOG_EXPORT_PACKAGING_RATIO_DEFAULT_VALUE"), 'VALUE' => '', 'FOR_JS' => '', ]; $options = array_merge($options, $propertiesOptions); $selectCode = $this->getSelect($field, $options, $currentValue); $jsCode = '<script>' .'document.addEventListener("DOMContentLoaded", function () {' .'let ozon = new Wbs24Ozonexport();' .'ozon.activateOptionsForOfferIblocks("'.$this->getFieldId($field).'", '.\CUtil::PhpToJSObject($catalogIblockIdsToOffersIblockIds).','. $iblockId .');' .'});' .'</script>' ; return $selectCode.$jsCode; } public function getPropertiesOptionsForIblock($propertiesOptions, $iblockIds) { $result = array_filter($propertiesOptions, function($propertyOption) use ($iblockIds) { return in_array($propertyOption['IBLOCK_ID'], $iblockIds); }); return $result; } public function getSelectForPriceTypes($currentPriceId, $labelDefaultPriceType) { $selectCode = '<select class="select_price_type" name="BLOB[priceType]">' .'<option value=""'.($currentPriceId == "" || $currentPriceId == 0 ? ' selected' : '').'>' .$labelDefaultPriceType .'</option>' ; $selectCode .= $this->getOptionForPriceTypes($currentPriceId) .'<option value="-1"' . ("-1" == $currentPriceId ? ' selected' : '') . '>' . GetMessage('WBS24_OZONEXPORT_PRICE_FROM_PROPERTY') . '</option>' ; $selectCode .= '</select>'; return $selectCode; } public function getSelectForOldPriceTypes($currentPriceId, $currentOldPriceId, $labelDefaultPriceType) { $selectCode = '<select class="select_old_price_type" name="BLOB[oldPriceType]">' .'<option value=""'.($currentOldPriceId == "" || $currentOldPriceId == 0 ? ' selected' : '').'>' .$labelDefaultPriceType .'</option>' .'<option value="-2"' .("" == $currentPriceId || 0 == $currentPriceId ? '' : ' hidden') .("-2" == $currentOldPriceId ? ' selected' : '').'>'.GetMessage('WBS24_OZONEXPORT_OLD_PRICE_BEFORE_DISCOUNT') .'</option>' .$this->getOptionForPriceTypes($currentOldPriceId) .'<option value="-1"'.("-1" == $currentOldPriceId ? ' selected' : '').'>'.GetMessage('WBS24_OZONEXPORT_PRICE_FROM_PROPERTY').'</option>' .'</select>' ; return $selectCode; } protected function getOptionForPriceTypes($currentPriceId) { $selectCode = ''; $priceTypes = $this->getPriceTypes(); foreach ($priceTypes as $arRes) { $priceNameLang = htmlspecialcharsEx($arRes['NAME_LANG']); if ($priceNameLang) $priceNameLang .= ' '; $selectCode .= '<option value="'.$arRes['ID'].'"'.($currentPriceId == $arRes['ID'] ? ' selected' : '').'>' .'['.$arRes['ID'].'] '.$priceNameLang.'('.htmlspecialcharsEx($arRes['NAME']).')' .'</option>' ; } return $selectCode; } protected function getPriceTypes() { if (!$this->cachedPriceTypes) { $priceTypes = []; $dbRes = $this->wrappers->CCatalogGroup->GetListEx( array('SORT' => 'ASC'), array(), false, false, array('ID', 'NAME_LANG', 'NAME', 'BASE') ); while ($arRes = $dbRes->Fetch()) $priceTypes[] = $arRes; $this->cachedPriceTypes = $priceTypes; } return $this->cachedPriceTypes; } public function getSelectForOfferId($iblockId, $field, $currentValue) { Loc::loadMessages(__FILE__); $options = [ [ 'ID' => '', 'NAME' => 'ID', 'CODE' => 'ID', 'VALUE' => 'ID', 'IBLOCK_ID' => 'all', 'FOR_JS' => '', ], [ 'ID' => '', 'NAME' => Loc::getMessage("XML_ID_LABEL"), 'CODE' => 'XML_ID', 'VALUE' => 'XML_ID', 'IBLOCK_ID' => 'all', 'FOR_JS' => '', ], ]; $propertiesOptions = $this->getAllValidProperties(); foreach ($propertiesOptions as $key => $option) { $propertiesOptions[$key]['VALUE'] = $option['CODE']; $propertiesOptions[$key]['FOR_JS'] = 'data-iblock-id="'.$option['IBLOCK_ID'].'"'; } $options = array_merge($options, $propertiesOptions); $selectCode = $this->getSelect($field, $options, $currentValue); if ($iblockId) { if ($field == 'SET_OFFER_ID') { $iblockInfo = $this->getInfoByIblockId($iblockId); $iblockId = $iblockInfo['OFFERS_IBLOCK_ID']; } $jsCode = '<script>' .'document.addEventListener("DOMContentLoaded", function () {' .'let ozon = new Wbs24Ozonexport();' .'ozon.activateOptionsForCurrentIblock("'.$field.'", '.$iblockId.');' .'});' .'</script>' ; } return $selectCode.$jsCode; } public function getCatalogIblockIdsToOffersIblockIds() { $iblockList = []; $res = $this->wrappers->CIBlock->GetList(); while ($iblock = $res->Fetch()) { $info = $this->getInfoByIblockId($iblock['ID']); if ($info['CATALOG_TYPE'] == 'X') { $iblockList[$iblock['ID']] = $info['OFFERS_IBLOCK_ID']; } } return $iblockList; } public function getInfoByIblockId($catalogIblockId) { $iblockInfo = $this->wrappers->CCatalog->GetByIDExt($catalogIblockId); return $iblockInfo; } protected function getAllValidProperties() { if ($this->cachedProperties !== null) return $this->cachedProperties; $properties = []; $res = $this->wrappers->CIBlockProperty->GetList(); while ($property = $res->Fetch()) { $type = $property['PROPERTY_TYPE']; if ($type != 'S' && $type != 'N') continue; $properties[] = [ 'ID' => $property['ID'], 'NAME' => $property['NAME'], 'CODE' => $property['CODE'], 'IBLOCK_ID' => $property['IBLOCK_ID'], ]; } $this->cachedProperties = $properties; return $properties; } public function getSelect($name, $options, $currentValue, $param = []) { $selectId = $this->getFieldId($name); $disabled = $param['disabled'] ?: false; $code = '<select' .' id="'.$selectId.'"' .' name="'.$name.'"' .($disabled ? ' disabled' : '') .'>'; foreach ($options as $option) { $code .= '<option '.$option['FOR_JS'].' value="'.$option['VALUE'].'"' .($currentValue == $option['VALUE'] ? ' selected' : '') .' data-selected="'.($currentValue == $option['VALUE'] ? 'Y' : 'N').'"' .'>'.$option['NAME'].'</option>' ; } $code .= '</select>'; return $code; } public function getPriceMarksToLang() { $priceMarksToLang = []; $priceTypes = $this->getPriceTypes(); foreach ($priceTypes as $price) { $key = 'PRICE_'.$price['ID']; $value = '['.$price['ID'].'] '.$price['NAME'] .($price['NAME_LANG'] ? ' - '.$price['NAME_LANG'] : '') ; $priceMarksToLang[$key] = $value; } return $priceMarksToLang; } public function getPriceMarks($priceMarksToLang) { return array_keys($priceMarksToLang); } public function getFormulaMarks($type, $marks = [], $lang = []) { $code = ''; foreach ($marks as $mark) { $code .= '<a href="#" data-mark="{'.$mark.'}" data-type="'.$type.'" class="ozonexport-mark js-add-mark-'.$type.'">' .($lang[$mark] ?? GetMessage('WBS24_OZONEXPORT_FORMULA_MARK_'.$mark)) .'</a>' ; } return $code; } protected function isContainBrackets($field) { return (strpos($field, '[') !== false); } protected function getFieldId($field) { return strtolower(strtr($field, [ "[" => "_", "]" => "", ])); } }