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/sproduction.datasync/lib/ |
Upload File : |
<?php /** * ProductsEdit * * @mail support@s-production.online * @link s-production.online */ namespace SProduction\Datasync; \Bitrix\Main\Loader::includeModule("catalog"); use Bitrix\Main, Bitrix\Main\Entity, Bitrix\Main\Type, Bitrix\Main\Localization\Loc; Loc::loadMessages(__FILE__); class StoreProducts { const FIELD_TYPE_STRING = 'string'; const FIELD_TYPE_CHAR = 'char'; const FIELD_TYPE_BOOLEAN = 'boolean'; const FIELD_TYPE_INTEGER = 'integer'; const FIELD_TYPE_FILE = 'file'; const FIELD_TYPE_TEXT = 'text'; const FIELD_TYPE_DATETIME = 'datetime'; /** * Get product data */ public static function getById($product_id) { // $product = \Bitrix\Iblock\ElementTable::getByPrimary($product_id)->fetch(); // foreach ($product as $k => $value) { // if (gettype($value) == 'object' && get_class($value) == 'Bitrix\Main\Type\DateTime') { // $product[$k] = $value->getTimestamp(); // } // } $values = false; // General data if ($product_id) { $filter = ["ID" => $product_id]; $res = \CIBlockElement::GetList(["SORT" => "ASC"], $filter); if ($ob = $res->GetNextElement()) { $values = $ob->GetFields(); if ($values['PREVIEW_PICTURE']) { $file = \CFile::GetFileArray($values['PREVIEW_PICTURE']); $values['PREVIEW_PICTURE'] = $file['SRC']; } if ($values['DETAIL_PICTURE']) { $file = \CFile::GetFileArray($values['DETAIL_PICTURE']); $values['DETAIL_PICTURE'] = $file['SRC']; } // Properties $properties = $ob->GetProperties(); foreach ($properties as $property) { $values['properties'][$property['ID']] = $property; } // Catalog data $res_catalog = \Bitrix\Catalog\ProductTable::getList(array( 'filter' => [ '=ID' => $product_id ], )); if ($catalog_info = $res_catalog->fetch()) { $values['catalog'] = $catalog_info; } // Prices $res_price = \Bitrix\Catalog\PriceTable::getList([ 'filter' => ['PRODUCT_ID' => $product_id] ]); $prices = []; while ($price = $res_price->fetch()) { $prices[] = $price; } if (count($prices) == 1) { $values['prices'][$prices[0]['CATALOG_GROUP_ID']] = $prices[0]; } else { $quantity = $values['catalog']['QUANTITY']; foreach ($prices as $price) { $q_from = $price['QUANTITY_FROM']; $q_to = $price['QUANTITY_TO']; if ((strlen($q_from) == 0 || $quantity >= (int)$q_from) && (strlen($q_to) == 0 || $quantity <= (int)$q_to)) { $values['prices'][$price['CATALOG_GROUP_ID']] = $price; } } } // Product parent data $parent_product = false; $store_parent_prod_info = \CCatalogSKU::GetProductInfo($product_id); if ($store_parent_prod_info && $store_parent_prod_info['ID']) { $filter = ["ID" => $store_parent_prod_info['ID']]; $res = \CIBlockElement::GetList(["SORT" => "ASC"], $filter); if ($ob = $res->GetNextElement()) { $parent_product = $ob->GetFields(); if ($parent_product['PREVIEW_PICTURE']) { $file = \CFile::GetFileArray($parent_product['PREVIEW_PICTURE']); $parent_product['PREVIEW_PICTURE'] = $file['SRC']; } if ($parent_product['DETAIL_PICTURE']) { $file = \CFile::GetFileArray($parent_product['DETAIL_PICTURE']); $parent_product['DETAIL_PICTURE'] = $file['SRC']; } $properties = $ob->GetProperties(); foreach ($properties as $property) { $parent_product['properties'][$property['ID']] = $property; } } } $values['parent'] = $parent_product; } } return $values; } /** * Get sections list */ public static function getList($iblock_id, int $after_id=0, array $select=['ID'], $limit=0) { $products = []; $filter = [ 'IBLOCK_ID' => $iblock_id, ]; if ($after_id) { $filter['>ID'] = $after_id; } $sections_db = \Bitrix\Iblock\ElementTable::getList([ 'order' => ['ID' => 'asc'], 'filter' => $filter, 'select' => $select, 'limit' => $limit, ]); while ($section = $sections_db->fetch()) { $products[] = $section; } return $products; } /** * Get sections list count */ public static function getCount($iblock_id) { $filter = [ 'IBLOCK_ID' => $iblock_id, ]; return \Bitrix\Iblock\ElementTable::getCount($filter); } // TODO public static function getHLValue($hl_table, $value_code) { $hl_value = false; $hl_block = \Bitrix\Highloadblock\HighloadBlockTable::getList( array( "filter" => array( 'TABLE_NAME' => $hl_table ) ) )->fetch(); if (isset($hl_block['ID'])) { $entity = \Bitrix\Highloadblock\HighloadBlockTable::compileEntity($hl_block); $entity_data_class = $entity->getDataClass(); $res = $entity_data_class::getList(['filter' => ['UF_XML_ID' => $value_code]]); if ($item = $res->fetch()) { $hl_value = $item['UF_NAME']; } } return $hl_value; } /** * Get search value for searching CRM products by store products */ public static function getSearchValuesForCrm($store_prod_ids) { $store_prod_cmpr = []; foreach ($store_prod_ids as $store_prod_id) { $search_value = self::getSearchValue($store_prod_id); if ($search_value) { $store_prod_cmpr[$store_prod_id] = $search_value; } } return $store_prod_cmpr; } /** * Get search value of product */ public static function getSearchValue($store_prod_id) { $search_value = false; $store_product = self::getById($store_prod_id); if ($store_product) { $store_search_field = SettingsIblocksProfiles::getStoreSearchField($store_product['IBLOCK_ID']); if (strpos($store_search_field, 'PROP_') !== false) { $prop_id = str_replace('PROP_', '', $store_search_field); $search_value = $store_product['properties'][$prop_id]['VALUE'] ?? false; } else { $search_value = $store_product[$store_search_field] ?? false; } } return $search_value; } /** * Find product by search value */ public static function find($iblock_id, $search_value, $search_field='') { $product_id = false; if (!$search_field) { $search_field = SettingsIblocksProfiles::getStoreSearchField($iblock_id); } $search_field = str_replace('PROP_', 'PROPERTY_', $search_field); $res = \CIBlockElement::GetList([], [ 'IBLOCK_ID' => $iblock_id, $search_field => $search_value, ], false, false, ['ID']); if ($ob = $res->GetNextElement()) { $fields = $ob->GetFields(); $product_id = $fields['ID']; } // if ($product = \Bitrix\Iblock\ElementTable::getList([ // 'filter' => [ // 'IBLOCK_ID' => $iblock_id, // $search_field => $search_value, // ] // ])->fetch()) { // $product_id = $product['ID']; // } return $product_id; } /** * Fields of products: base fields */ public static function getFieldsBase() { $list = [ [ 'id' => 'ID', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_ID"), 'type' => self::FIELD_TYPE_INTEGER, ], [ 'id' => 'SORT', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_INDEKS_SORTIROVKI"), 'type' => self::FIELD_TYPE_INTEGER, ], [ 'id' => 'NAME', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_IMA_ELEMENTA"), 'type' => self::FIELD_TYPE_STRING, ], [ 'id' => 'CODE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_KOD_ELEMENTA"), 'type' => self::FIELD_TYPE_STRING, ], [ 'id' => 'XML_ID', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_XML_ID"), 'type' => self::FIELD_TYPE_STRING, ], [ 'id' => 'ACTIVE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_ACTIVE"), 'type' => self::FIELD_TYPE_CHAR ], [ 'id' => 'DATE_ACTIVE_FROM', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_NACALO_AKTIVNOSTI"), 'type' => self::FIELD_TYPE_DATETIME, ], [ 'id' => 'DATE_ACTIVE_TO', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_OKONCANIE_AKTIVNOSTI"), 'type' => self::FIELD_TYPE_DATETIME, ], [ 'id' => 'TAGS', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_TEGI"), 'type' => self::FIELD_TYPE_STRING, ], [ 'id' => 'PREVIEW_PICTURE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_IZOBRAJENIE_DLA_ANON"), 'type' => self::FIELD_TYPE_FILE, ], [ 'id' => 'DETAIL_PICTURE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_DETALQNOE_IZOBRAJENI"), 'type' => self::FIELD_TYPE_FILE, ], [ 'id' => 'PREVIEW_TEXT_TYPE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_TIP_OPISANIA_DLA_ANO"), 'type' => self::FIELD_TYPE_STRING, ], [ 'id' => 'PREVIEW_TEXT', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_OPISANIE_DLA_ANONSA"), 'type' => self::FIELD_TYPE_TEXT, ], [ 'id' => 'DETAIL_TEXT_TYPE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_TIP_DETALQNOGO_OPISA"), 'type' => self::FIELD_TYPE_STRING, ], [ 'id' => 'DETAIL_TEXT', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_DETALQNOE_OPISANIE"), 'type' => self::FIELD_TYPE_TEXT, ], [ 'id' => 'DETAIL_PAGE_URL', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_DETAIL_PAGE_URL"), 'type' => self::FIELD_TYPE_STRING, ], [ 'id' => 'DETAIL_PAGE_URL_HTML', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_DETAIL_PAGE_URL_HTML"), 'type' => self::FIELD_TYPE_TEXT, ], ]; return $list; } /** * Fields of products: meta fields */ public static function getFieldsMeta() { $list = [ [ 'id' => 'SEO_ELEMENT_META_TITLE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_ELEMENT_META_TITLE"), 'type' => self::FIELD_TYPE_STRING, ], [ 'id' => 'SEO_ELEMENT_META_KEYWORDS', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_ELEMENT_META_KEYWORDS"), 'type' => self::FIELD_TYPE_STRING, ], [ 'id' => 'SEO_ELEMENT_META_DESCRIPTION', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_ELEMENT_META_DESCRIPTION"), 'type' => self::FIELD_TYPE_STRING, ], [ 'id' => 'SEO_ELEMENT_PAGE_TITLE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_ELEMENT_PAGE_TITLE"), 'type' => self::FIELD_TYPE_STRING, ], ]; return $list; } /** * Fields of products: properties */ public static function getIblockProps(int $iblock_id, $get_enum_values=false) { $list = []; if ($iblock_id) { $props_db = \Bitrix\Iblock\PropertyTable::getList(array( 'select' => ['ID', 'NAME', 'ACTIVE', 'CODE', 'PROPERTY_TYPE', 'LIST_TYPE', 'MULTIPLE', 'XML_ID', 'FILE_TYPE', 'LINK_IBLOCK_ID', 'IS_REQUIRED'], 'filter' => ['IBLOCK_ID' => $iblock_id, 'ACTIVE' => 'Y'] )); while ($prop = $props_db->fetch()) { if ($prop['PROPERTY_TYPE'] == 'L' && $get_enum_values) { $prop['values'] = \Bitrix\Iblock\PropertyEnumerationTable::getList([ 'filter' => ['PROPERTY_ID' => $prop['ID']], 'select' => ['ID', 'VALUE', 'XML_ID'] ])->fetchAll(); } $list[] = $prop; } } return $list; } /** * Properties of iblock */ public static function getFieldsProps(int $iblock_id) { $list = []; if ($iblock_id) { $props = self::getIblockProps($iblock_id); foreach ($props as $prop) { $list[] = [ 'id' => $prop['ID'], 'name' => $prop['NAME'], 'type' => self::FIELD_TYPE_STRING, ]; } } return $list; } /** * Fields of products: catalog fields */ public static function getFieldsCatalog() { $list = []; $list[] = [ 'id' => 'PURCHASING_PRICE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_PURCHASING_PRICE"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'PURCHASING_CURRENCY', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_PURCHASING_CURRENCY"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'VAT_ID', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_VAT_ID"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'VAT_INCLUDED', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_VAT_INCLUDED"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'WIDTH', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_WIDTH"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'HEIGHT', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_HEIGHT"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'LENGTH', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_LENGTH"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'WEIGHT', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_WEIGHT"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'MEASURE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_MEASURE"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'QUANTITY', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_QUANTITY"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'QUANTITY_RESERVED', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_QUANTITY_RESERVED"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'QUANTITY_TRACE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_QUANTITY_TRACE"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'RESERVED', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_RESERVED"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'SUBSCRIBE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_SUBSCRIBE"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'AVAILABLE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_AVAILABLE"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'CAN_BUY_ZERO', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_CAN_BUY_ZERO"), 'type' => self::FIELD_TYPE_STRING, ]; $list[] = [ 'id' => 'BUNDLE', 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_BUNDLE"), 'type' => self::FIELD_TYPE_STRING, ]; // $list[] = [ // 'id' => 'TYPE', // 'name' => GetMessage("SP_DS_STORE_PRODUCTS_FIELDS_TYPE"), // 'type' => self::FIELD_TYPE_STRING, // ]; return $list; } public static function getIblocks($offers=false) { $list = []; $catalog_iblocks_ids = []; $filter = []; if (!$offers) { $filter['PRODUCT_IBLOCK_ID'] = 0; } $catalog_iblocks = \Bitrix\Catalog\CatalogIblockTable::getList([ 'filter' => $filter, ])->fetchAll(); foreach ($catalog_iblocks as $catalog_iblock) { $catalog_iblocks_ids[] = $catalog_iblock['IBLOCK_ID']; } $res = \Bitrix\Iblock\IblockTable::getList([ 'select' => ['ID','NAME','IBLOCK_TYPE_ID','LID','CODE','ACTIVE'], ]); while ($iblock = $res->fetch()) { if (in_array($iblock['ID'], $catalog_iblocks_ids)) { $list[] = $iblock; } } return $list; } }