Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
elementsku.php
1<?php
8
9class ElementSku extends Base
10{
11 protected $elementFields = null;
12 protected $skuIblockId = null;
13 protected $skuList = null;
14 protected $property = null;
15 protected $price = null;
19 public function __construct($id)
20 {
21 parent::__construct($id);
22 $this->fieldMap = array(
23 "name" => "NAME",
24 "previewtext" => "PREVIEW_TEXT",
25 "detailtext" => "DETAIL_TEXT",
26 "code" => "CODE",
27 //not accessible from template engine
28 "ID" => "ID",
29 "IBLOCK_ID" => "IBLOCK_ID",
30 "IBLOCK_SECTION_ID" => "IBLOCK_SECTION_ID",
31 );
32 }
33
41 public function resolve($entity)
42 {
43 if ($entity === "property")
44 {
45 if (!$this->property && $this->loadFromDatabase())
46 {
47 if ($this->skuIblockId && $this->skuList)
48 {
49 $this->property = new ElementSkuProperty($this->fields["ID"]);
50 $this->property->setIblockId($this->skuIblockId);
51 }
52 }
53
54 if ($this->property)
55 return $this->property;
56 }
57 elseif ($entity === "price")
58 {
59 if (!$this->price && $this->loadFromDatabase())
60 {
61 if ($this->skuIblockId && $this->skuList)
62 {
63 $this->price = new ElementSkuPrice($this->fields["ID"]);
64 }
65 }
66
67 if ($this->price)
68 return $this->price;
69 }
70 return parent::resolve($entity);
71 }
72
79 public function loadFromDatabase()
80 {
81 if (!isset($this->fields))
82 {
83 $this->fields = array();
84 $select = array_values($this->fieldMap);
85
86 $elementList = \Bitrix\Iblock\ElementTable::getList(array(
87 "select" => $select,
88 "filter" => array("=ID" => $this->id),
89 ));
90 $this->elementFields = $elementList->fetch();
91 if ($this->elementFields)
92 {
93 $catalog = \CCatalogSKU::getInfoByProductIBlock($this->elementFields["IBLOCK_ID"]);
94 if (!empty($catalog))
95 {
96 $this->skuIblockId = $catalog["IBLOCK_ID"];
97 $skuList = \CIBlockElement::getList(array(), array(
98 "IBLOCK_ID" => $catalog["IBLOCK_ID"],
99 "=PROPERTY_".$catalog["SKU_PROPERTY_ID"] => $this->id,
100 ), false, false, $select);
101 while ($sku = $skuList->fetch())
102 {
103 $this->skuList[] = $sku;
104 foreach($sku as $fieldName => $fieldValue)
105 {
106 $this->fields[$fieldName][] = $fieldValue;
107 }
108 }
109 }
110 }
111 }
112
113 return is_array($this->fields);
114 }
115}