Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
elementproperty.php
1<?php
8
9class ElementProperty extends Base
10{
11 protected $iblockId = 0;
12 protected $properties = array();
13 protected $elementLinkProperties = array();
14 protected $sectionLinkProperties = array();
18 public function __construct($id)
19 {
20 parent::__construct($id);
21 }
22
30 public function setIblockId($iblockId)
31 {
32 $this->iblockId = intval($iblockId);
33 }
34
42 public function resolve($entity)
43 {
44 if ($this->loadFromDatabase())
45 {
46 if (isset($this->elementLinkProperties[$entity]))
47 {
48 if (!is_object($this->elementLinkProperties[$entity]))
49 $this->elementLinkProperties[$entity] = new Element($this->elementLinkProperties[$entity]);
50 return $this->elementLinkProperties[$entity];
51 }
52 elseif (isset($this->sectionLinkProperties[$entity]))
53 {
54 if (!is_object($this->sectionLinkProperties[$entity]))
55 $this->sectionLinkProperties[$entity] = new Element($this->sectionLinkProperties[$entity]);
56 return $this->sectionLinkProperties[$entity];
57 }
58 }
59 return parent::resolve($entity);
60 }
61
69 public function setFields(array $fields)
70 {
71 parent::setFields($fields);
72 if (
73 is_array($this->fields)
74 && $this->iblockId > 0
75 )
76 {
77 $properties = array();
78 $propertyList = \Bitrix\Iblock\PropertyTable::getList(array(
79 "select" => array("*"),
80 "filter" => array("=IBLOCK_ID" => $this->iblockId),
81 ));
82 while ($row = $propertyList->fetch())
83 {
84 if ($row["USER_TYPE_SETTINGS"])
85 {
86 $row["USER_TYPE_SETTINGS"] = unserialize(
87 $row["USER_TYPE_SETTINGS"],
88 array('allowed_classes' => false)
89 );
90 }
91
92 $properties[$row["ID"]] = $row;
93 if ($row["CODE"] != "")
94 $properties[$row["CODE"]] = &$properties[$row["ID"]];
95 }
96
97 foreach ($fields as $propertyCode => $propertyValues)
98 {
99 if (is_array($propertyValues))
100 {
101 foreach ($propertyValues as $i => $propertyValue)
102 {
103 if (is_array($propertyValue) && array_key_exists("VALUE", $propertyValue))
104 {
105 if ($propertyValue["VALUE"] != "")
106 $propertyValues[$i] = $propertyValue["VALUE"];
107 else
108 unset($propertyValues[$i]);
109 }
110 }
111 }
112
113 if (isset($properties[$propertyCode]))
114 {
115 $property = $properties[$propertyCode];
116 $fieldCode = mb_strtolower($propertyCode);
117
118 if ($property["PROPERTY_TYPE"] === "L")
119 {
120 if (is_numeric($propertyValues))
121 {
122 $value = new ElementPropertyEnum($propertyValues);
123 }
124 elseif (is_array($propertyValues))
125 {
126 $value = array();
127 foreach ($propertyValues as $propertyValue)
128 {
129 if (is_numeric($propertyValue))
130 $value[] = new ElementPropertyEnum($propertyValue);
131 }
132 }
133 else
134 {
135 $value = $propertyValues;
136 }
137 }
138 elseif ($property["PROPERTY_TYPE"] === "E")
139 {
140 if ($propertyValues instanceof Element)
141 {
142 $this->elementLinkProperties[$fieldCode] = $propertyValues;
143 $value = $propertyValues->getField("name");
144 }
145 elseif (is_numeric($propertyValues))
146 {
147 $this->elementLinkProperties[$fieldCode] = $propertyValues;
148 $value = new ElementPropertyElement($propertyValues);
149 }
150 elseif (is_array($propertyValues))
151 {
152 $value = array();
153 foreach ($propertyValues as $propertyValue)
154 {
155 if (is_numeric($propertyValue))
156 $value[] = new ElementPropertyElement($propertyValue);
157 }
158 }
159 else
160 {
161 $value = $propertyValues;
162 }
163 }
164 elseif ($property["PROPERTY_TYPE"] === "G")
165 {
166 if ($propertyValues instanceof Section)
167 {
168 $this->sectionLinkProperties[$fieldCode] = $propertyValues;
169 $value = $propertyValues->getField("name");
170 }
171 elseif (is_numeric($propertyValues))
172 {
173 $this->sectionLinkProperties[$fieldCode] = $propertyValues;
174 $value = new ElementPropertySection($propertyValues);
175 }
176 elseif (is_array($propertyValues))
177 {
178 $value = array();
179 foreach ($propertyValues as $propertyValue)
180 {
181 if (is_numeric($propertyValue))
182 $value[] = new ElementPropertySection($propertyValue);
183 }
184 }
185 else
186 {
187 $value = $propertyValues;
188 }
189 }
190 else
191 {
192 if($property["USER_TYPE"] <> '')
193 {
194 if(is_array($propertyValues))
195 {
196 $value = array();
197 foreach($propertyValues as $propertyValue)
198 {
199 $value[] = new ElementPropertyUserField($propertyValue, $property);
200 }
201 }
202 else
203 {
204 $value = new ElementPropertyUserField($propertyValues, $property);
205 }
206 }
207 else
208 {
209 $value = $propertyValues;
210 }
211 }
212
213 $this->fieldMap[$fieldCode] = $property["ID"];
214 $this->fieldMap[$property["ID"]] = $property["ID"];
215 if ($property["CODE"] != "")
216 $this->fieldMap[mb_strtolower($property["CODE"])] = $property["ID"];
217
218 $this->fields[$property["ID"]] = $value;
219 }
220 }
221 }
222 }
223
230 protected function loadFromDatabase()
231 {
232 if (!isset($this->fields) && $this->iblockId > 0)
233 {
234 $this->fields = array();
235 $this->fieldMap = array();
236
237 $propertyList = \CIBlockElement::getProperty(
238 $this->iblockId,
239 $this->id,
240 array("sort" => "asc"),
241 array("EMPTY" => "N")
242 );
243 while ($property = $propertyList->fetch())
244 {
245 if ($property["VALUE_ENUM"] != "")
246 {
247 $value = $property["VALUE_ENUM"];
248 }
249 elseif ($property["PROPERTY_TYPE"] === "E")
250 {
251 $this->elementLinkProperties[$property["ID"]] = $property["VALUE"];
252 if ($property["CODE"] != "")
253 $this->elementLinkProperties[mb_strtolower($property["CODE"])] = $property["VALUE"];
254 $value = new ElementPropertyElement($property["VALUE"]);
255 }
256 elseif ($property["PROPERTY_TYPE"] === "G")
257 {
258 $this->sectionLinkProperties[$property["ID"]] = $property["VALUE"];
259 if ($property["CODE"] != "")
260 $this->sectionLinkProperties[mb_strtolower($property["CODE"])] = $property["VALUE"];
261 $value = new ElementPropertySection($property["VALUE"]);
262 }
263 else
264 {
265 if($property["USER_TYPE"] <> '')
266 {
267 $value = new ElementPropertyUserField($property["VALUE"], $property);
268 }
269 else
270 {
271 $value = $property["VALUE"];
272 }
273 }
274
275 $this->fieldMap[$property["ID"]] = $property["ID"];
276 if ($property["CODE"] != "")
277 $this->fieldMap[mb_strtolower($property["CODE"])] = $property["ID"];
278
279 if ($property["MULTIPLE"] == "Y")
280 $this->fields[$property["ID"]][] = $value;
281 else
282 $this->fields[$property["ID"]] = $value;
283 }
284 }
285 return is_array($this->fields);
286 }
287}
288
290{
292 private $property = null;
293
298 function __construct($key, $property)
299 {
300 parent::__construct($key);
301 if (is_array(($property)))
302 {
303 $this->property = $property;
304 }
305 }
311 protected function load()
312 {
313 $propertyFormatFunction = $this->getFormatFunction();
314 if ($propertyFormatFunction)
315 {
316 return call_user_func_array($propertyFormatFunction,
317 array(
318 $this->property,
319 array("VALUE" => $this->key),
320 array("MODE" => "ELEMENT_TEMPLATE"),
321 )
322 );
323 }
324 else
325 {
326 return $this->key;
327 }
328 }
335 protected function getFormatFunction()
336 {
337 static $propertyFormatFunction = array();
338 if (!isset($propertyFormatFunction[$this->property["ID"]]))
339 {
340 $propertyFormatFunction[$this->property["ID"]] = false;
341 if ($this->property && mb_strlen($this->property["USER_TYPE"]))
342 {
343 $propertyUserType = \CIBlockProperty::getUserType($this->property["USER_TYPE"]);
344 if(
345 array_key_exists("GetPublicViewHTML", $propertyUserType)
346 && is_callable($propertyUserType["GetPublicViewHTML"])
347 )
348 {
349 $propertyFormatFunction[$this->property["ID"]] = $propertyUserType["GetPublicViewHTML"];
350 }
351 }
352 }
353 return $propertyFormatFunction[$this->property["ID"]];
354 }
355}
356
358{
364 protected function load()
365 {
366 $enumList = \Bitrix\Iblock\PropertyEnumerationTable::getList(array(
367 "select" => array("VALUE"),
368 "filter" => array("=ID" => $this->key),
369 ));
370 $enum = $enumList->fetch();
371 if ($enum)
372 return $enum["VALUE"];
373 else
374 return "";
375 }
376}
377
379{
385 protected function load()
386 {
387 $elementList = \Bitrix\Iblock\ElementTable::getList(array(
388 "select" => array("NAME"),
389 "filter" => array("=ID" => $this->key),
390 ));
391 $element = $elementList->fetch();
392 if ($element)
393 return $element["NAME"];
394 else
395 return "";
396 }
397}
398
400{
406 protected function load()
407 {
408 $sectionList = \Bitrix\Iblock\SectionTable::getList(array(
409 "select" => array("NAME"),
410 "filter" => array("=ID" => $this->key),
411 ));
412 $section = $sectionList->fetch();
413 if ($section)
414 return $section["NAME"];
415 else
416 return "";
417 }
418}