Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
element.php
1<?php
8
9class Element extends Base
10{
11 protected $property = null;
12 protected $iblock = null;
13 protected $parent = null;
14 protected $sections = null;
15 protected $catalog = 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->fields["IBLOCK_ID"] > 0)
48 {
49 $this->property = new ElementProperty($this->id);
50 $this->property->setIblockId($this->fields["IBLOCK_ID"]);
51 }
52 }
53
54 if ($this->property)
55 return $this->property;
56 }
57 elseif ($entity === "iblock")
58 {
59 if (!$this->iblock && $this->loadFromDatabase())
60 {
61 if ($this->fields["IBLOCK_ID"] > 0)
62 $this->iblock = new Iblock($this->fields["IBLOCK_ID"]);
63 }
64
65 if ($this->iblock)
66 return $this->iblock;
67 }
68 elseif ($entity === "parent")
69 {
70 if (!$this->parent && $this->loadFromDatabase())
71 {
72 if ($this->fields["IBLOCK_SECTION_ID"] > 0)
73 $this->parent = new Section($this->fields["IBLOCK_SECTION_ID"]);
74 }
75
76 if ($this->parent)
77 return $this->parent;
78 }
79 elseif ($entity === "sections")
80 {
81 if (!$this->sections && $this->loadFromDatabase())
82 {
83 if ($this->fields["IBLOCK_SECTION_ID"] > 0)
84 $this->sections = new SectionPath($this->fields["IBLOCK_SECTION_ID"]);
85 }
86
87 if ($this->sections)
88 return $this->sections;
89 }
90 elseif ($entity === "catalog")
91 {
92 if (!$this->catalog && $this->loadFromDatabase())
93 {
94 if (\Bitrix\Main\Loader::includeModule('catalog'))
95 $this->catalog = new ElementCatalog($this->id);
96 }
97
98 if ($this->catalog)
99 return $this->catalog;
100 }
101 return parent::resolve($entity);
102 }
103
111 public function setFields(array $fields)
112 {
113 parent::setFields($fields);
114 if (
115 is_array($this->fields)
116 && $this->fields["IBLOCK_ID"] > 0
117 )
118 {
119 if (
120 isset($fields["PROPERTY_VALUES"])
121 && is_array($fields["PROPERTY_VALUES"])
122 )
123 {
124 $this->property = new ElementProperty($this->id);
125 $this->property->setIblockId($this->fields["IBLOCK_ID"]);
126 $this->property->setFields($fields["PROPERTY_VALUES"]);
127 }
128
129 $this->iblock = new Iblock($fields["IBLOCK_ID"]);
130
131 if (
132 isset($fields["IBLOCK_SECTION_ID"])
133 && $fields["IBLOCK_SECTION_ID"] > 0
134 )
135 {
136 $this->parent = new Section($fields["IBLOCK_SECTION_ID"]);
137 $this->sections = new SectionPath($fields["IBLOCK_SECTION_ID"]);
138 }
139 elseif (
140 isset($fields["IBLOCK_SECTION"])
141 && is_array($fields["IBLOCK_SECTION"])
142 )
143 {
144 $section = -1;
145 foreach ($fields["IBLOCK_SECTION"] as $sectionId)
146 {
147 if ($sectionId > 0)
148 {
149 if ($section < 0 || $section > $sectionId)
150 $section = $sectionId;
151 }
152 }
153
154 if ($section > 0)
155 {
156 $this->parent = new Section($section);
157 $this->sections = new SectionPath($section);
158 }
159 }
160
161 if (\Bitrix\Main\Loader::includeModule('catalog'))
162 $this->catalog = new ElementCatalog($this->id);
163 }
164 }
165
172 protected function loadFromDatabase()
173 {
174 static $cache = array();
175 if ($this->id > 0)
176 {
177 if (!isset($cache[$this->id]))
178 {
179 //Element fields
180 $elementList = \Bitrix\Iblock\ElementTable::getList(array(
181 "select" => array_values($this->fieldMap),
182 "filter" => array("=ID" => $this->id),
183 ));
184 $cache[$this->id] = $elementList->fetch();
185 }
186 $this->fields = $cache[$this->id];
187 }
188 return is_array($this->fields);
189 }
190}