Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
element.php
1<?php
8
9class Element
10{
11 protected $iblockId = 0;
12 protected $elementId = 0;
13 protected static $catalog = null;
14 protected static $filterPropertyID = array();
15 protected $skuIblockId = 0;
16 protected $skuPropertyId = 0;
17 protected $elementPropertyValues = array();
18 protected $elementPrices = array();
19 protected $elementSections = array();
20 protected static $sectionParents = array();
21
29 {
30 $this->iblockId = intval($iblockId);
31 $this->elementId = intval($elementId);
32
33 if (self::$catalog === null)
34 {
35 self::$catalog = \Bitrix\Main\Loader::includeModule("catalog");
36 }
37
38 if (self::$catalog)
39 {
40 $catalog = \CCatalogSKU::getInfoByProductIBlock($this->iblockId);
41 if (!empty($catalog) && is_array($catalog))
42 {
43 $this->skuIblockId = $catalog["IBLOCK_ID"];
44 $this->skuPropertyId = $catalog["SKU_PROPERTY_ID"];
45 }
46 }
47 }
48
54 public function getId()
55 {
56 return $this->elementId;
57 }
58
64 public function loadFromDatabase()
65 {
66 $this->elementPropertyValues = array();
67 $this->loadElementProperties($this->iblockId, array(
68 "IBLOCK_ID" => $this->iblockId,
69 "=ID" => $this->elementId,
70 ));
71 if ($this->skuIblockId > 0 && $this->skuPropertyId > 0)
72 {
73 $this->loadElementProperties($this->skuIblockId, array(
74 "IBLOCK_ID" => $this->iblockId,
75 "ACTIVE" => "Y",
76 "=PROPERTY_".$this->skuPropertyId => $this->elementId,
77 ));
78 }
79
80 $this->elementPrices = array();
81 if (self::$catalog)
82 {
83 $elements = $this->elementPropertyValues["IBLOCK_ELEMENT_ID"] ?? [$this->elementId];
84 $this->loadElementPrices($elements);
85 }
86
87 $this->elementSections = array();
88 $this->loadElementSections($this->elementId);
89 }
90
99 protected function loadElementProperties(int $iblockId, array $elementFilter)
100 {
101 if (!isset(self::$filterPropertyID[$iblockId]))
102 {
103 self::$filterPropertyID[$iblockId] = [];
104 $properties = \Bitrix\Iblock\SectionPropertyTable::getList(array(
105 "select" => array("PROPERTY_ID"),
106 "filter" => array(
107 "=IBLOCK_ID" => array($this->iblockId, $this->skuIblockId),
108 "=SMART_FILTER" => "Y",
109 ),
110 ));
111 while ($property = $properties->fetch())
112 {
113 self::$filterPropertyID[$iblockId][$property['PROPERTY_ID']] = $property['PROPERTY_ID'];
114 }
115 unset($property);
116 unset($properties);
117
118 self::$filterPropertyID[$iblockId] = array_values(self::$filterPropertyID[$iblockId]);
119 sort(self::$filterPropertyID[$iblockId]);
120 }
121
122 $elementList = \CIBlockElement::getPropertyValues(
123 $iblockId,
124 $elementFilter,
125 false,
126 array('ID' => self::$filterPropertyID[$iblockId])
127 );
128
129 while ($element = $elementList->fetch())
130 {
131 foreach ($element as $propertyId => $value)
132 {
133 if ($value !== false)
134 {
135 if (!isset($this->elementPropertyValues[$propertyId]))
136 $this->elementPropertyValues[$propertyId] = array();
137
138 if (is_array($value))
139 $this->elementPropertyValues[$propertyId] = array_merge($this->elementPropertyValues[$propertyId], $value);
140 else
141 $this->elementPropertyValues[$propertyId][] = $value;
142 }
143 }
144 }
145 }
146
154 protected function loadElementPrices(array $productList)
155 {
156 $priceList = \Bitrix\Catalog\PriceTable::getList(array(
157 'select' => array('ID', 'PRODUCT_ID', 'CATALOG_GROUP_ID', 'PRICE', 'CURRENCY', 'QUANTITY_FROM', 'QUANTITY_TO'),
158 'filter' => array('@PRODUCT_ID' => $productList)
159 ));
160 while($price = $priceList->fetch())
161 {
162 if (!isset($this->elementPrices[$price["CATALOG_GROUP_ID"]][$price["CURRENCY"]]))
163 $this->elementPrices[$price["CATALOG_GROUP_ID"]][$price["CURRENCY"]] = array();
164 $priceValue = (float)$price["PRICE"];
165 $this->elementPrices[$price["CATALOG_GROUP_ID"]][$price["CURRENCY"]][(string)$priceValue] = $priceValue;
166 }
167 unset($price);
168 unset($priceList);
169
170 foreach ($this->elementPrices as $catalogGroupId => $currencyPrices)
171 {
172 foreach ($currencyPrices as $currency => $prices)
173 {
174 if (count($prices) > 2)
175 {
176 $this->elementPrices[$catalogGroupId][$currency] = array(
177 min($prices),
178 max($prices),
179 );
180 }
181 }
182 unset($currency, $prices);
183 }
184 unset($catalogGroupId, $currencyPrices);
185 }
186
194 protected function loadElementSections($elementId)
195 {
196 $sectionList = \CIBlockElement::getElementGroups($elementId, true, array("ID"));
197 while ($section = $sectionList->fetch())
198 {
199 $this->elementSections[] = $section["ID"];
200 }
201 }
202
210 public function getPropertyValues($propertyId)
211 {
212 return $this->elementPropertyValues[$propertyId] ?? [];
213 }
214
222 public function getPriceValues($priceId)
223 {
224 return $this->elementPrices[$priceId] ?? [];
225 }
226
234 public function isElementSection($sectionId)
235 {
236 return in_array($sectionId, $this->elementSections);
237 }
238
244 public function getSections()
245 {
246 return array_unique($this->elementSections, SORT_NUMERIC);
247 }
248
254 public function getParentSections()
255 {
256 $sections = array();
257 foreach ($this->getSections() as $sectionId)
258 {
259 $sections = array_merge($sections, $this->getSectionParents($sectionId));
260 }
261 return array_unique($sections, SORT_NUMERIC);
262 }
263
271 public function getSectionParents($sectionId)
272 {
273 if (!isset(self::$sectionParents[$sectionId]))
274 {
275 $sections = array();
276 $sectionList = \CIBlockSection::getNavChain($this->iblockId, $sectionId, array("ID"));
277 while ($section = $sectionList->fetch())
278 {
279 $sections[] = $section["ID"];
280 }
281 self::$sectionParents[$sectionId] = $sections;
282 }
283 return self::$sectionParents[$sectionId];
284 }
285}
loadElementProperties(int $iblockId, array $elementFilter)
Definition element.php:99
loadElementPrices(array $productList)
Definition element.php:154
__construct($iblockId, $elementId)
Definition element.php:28