1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
listfield.php
См. документацию.
1<?php
2
4
7
8abstract class CListField
9{
11 protected $_iblock_id;
13 protected $_field_id;
15 protected $_label;
17 protected $_type;
19 protected $_sort;
20
21 private static $prop_cache = array();
22
23 public function __construct($iblock_id, $field_id, $label, $sort)
24 {
25 global $DB;
26
27 $this->_iblock_id = intval($iblock_id);
28 $this->_field_id = $field_id;
29 $this->_label = $label;
30 $this->_sort = intval($sort);
31
32 if($this->_iblock_id > 0 && mb_strlen($this->_field_id))
33 {
34 $arField = $this->_read_from_cache($this->_field_id);
35 if(!$arField)
36 {
37 $DB->Add("b_lists_field", array(
38 "ID" => 1, //This makes Oracle version happy
39 "IBLOCK_ID" => $this->_iblock_id,
40 "FIELD_ID" => $this->_field_id,
41 "SORT" => $this->_sort,
42 "NAME" => $this->_label,
43 ));
44 $this->_clear_cache();
45 }
46 elseif(
47 $arField["SORT"] != $this->_sort
48 || $arField["NAME"] != $this->_label
49 )
50 {
51 $DB->Query("
52 UPDATE b_lists_field
53 SET SORT = ".$this->_sort."
54 ,NAME = '".$DB->ForSQL($this->_label)."'
55 WHERE IBLOCK_ID = ".$this->_iblock_id."
56 AND FIELD_ID = '".$DB->ForSQL($this->_field_id)."'
57 ");
58 $this->_clear_cache();
59 }
60 }
61 }
62
63 private function _read_from_cache($field_id)
64 {
65 global $DB;
66
67 if($this->_iblock_id > 0 && !isset(self::$prop_cache[$this->_iblock_id]))
68 {
69 $rsFields = $DB->Query("
70 SELECT * FROM b_lists_field
71 WHERE IBLOCK_ID = ".$this->_iblock_id."
72 ");
73
74 self::$prop_cache[$this->_iblock_id] = array();
75 while($arField = $rsFields->Fetch())
76 self::$prop_cache[$this->_iblock_id][$arField["FIELD_ID"]] = $arField;
77 }
78
79 if(isset(self::$prop_cache[$this->_iblock_id][$field_id]))
80 return self::$prop_cache[$this->_iblock_id][$field_id];
81 else
82 return false;
83 }
84
85 private function _clear_cache()
86 {
87 if (CACHED_b_lists_field !== false)
88 {
89 $cache = Bitrix\Main\Data\Cache::createInstance();
90 $cache->clean(CACHED_b_lists_field_prefix . $this->_iblock_id, 'b_lists_field');
91 }
92
93 if(isset(self::$prop_cache[$this->_iblock_id]))
94 unset(self::$prop_cache[$this->_iblock_id]);
95 }
96
97 public function GetID()
98 {
99 return $this->_field_id;
100 }
101
102 public function GetLabel()
103 {
104 return $this->_label;
105 }
106
107 public function GetTypeID()
108 {
109 return $this->_type->GetID();
110 }
111
112 public function IsReadOnly()
113 {
114 return $this->_type->IsReadonly();
115 }
116
117 public function GetSort()
118 {
119 return $this->_sort;
120 }
121
122 public function GetSettingsDefaults()
123 {
124 switch($this->_field_id)
125 {
126 case "PREVIEW_TEXT":
127 return array(
128 "USE_EDITOR" => "N",
129 "WIDTH" => "40",
130 "HEIGHT" => "3",
131 );
132 default:
133 return false;
134 }
135 }
136
137 public function GetSettings()
138 {
139 $arField = $this->_read_from_cache($this->_field_id);
140 if($arField)
141 {
142 $res = unserialize($arField["SETTINGS"], ['allowed_classes' => false]);
143 if(is_array($res))
144 return $res;
145 }
146 return $this->GetSettingsDefaults();
147 }
148
149 public function SetSettings($arSettings)
150 {
151 global $DB;
152
153 $arStore = false;
154 switch($this->_field_id)
155 {
156 case "PREVIEW_TEXT":
157 if(preg_match('/\s*(\d+)\s*(px|%|)/', $arSettings["WIDTH"], $match) && ($match[1] > 0))
158 $width = $match[1].$match[2];
159 else
160 $width = "40";
161
162 if(preg_match('/\s*(\d+)\s*(px|%|)/', $arSettings["HEIGHT"], $match) && ($match[1] > 0))
163 $height = $match[1].$match[2];
164 else
165 $height = "3";
166
167 $arStore = array(
168 "USE_EDITOR" => $arSettings["USE_EDITOR"]=="Y"? "Y": "N",
169 "WIDTH" => $width,
170 "HEIGHT" => $height,
171 "SHOW_ADD_FORM" => $arSettings["SHOW_ADD_FORM"],
172 "SHOW_EDIT_FORM" => $arSettings["SHOW_EDIT_FORM"],
173 "ADD_READ_ONLY_FIELD" => $arSettings["ADD_READ_ONLY_FIELD"],
174 "EDIT_READ_ONLY_FIELD" => $arSettings["EDIT_READ_ONLY_FIELD"],
175 "SHOW_FIELD_PREVIEW" => $arSettings["SHOW_FIELD_PREVIEW"]
176 );
177 break;
178 default:
179 $arStore = $arSettings;
180 }
181
182 $arFields = array();
183 if(is_array($arStore))
184 $arFields["SETTINGS"] = serialize($arStore);
185 else
186 $arFields["SETTINGS"] = false;
187
188 $strUpdate = $DB->PrepareUpdate("b_lists_field", $arFields);
189 if($strUpdate!="")
190 {
191 $strSql = "
192 UPDATE b_lists_field
193 SET ".$strUpdate."
194 WHERE IBLOCK_ID = ".$this->_iblock_id."
195 AND FIELD_ID = '".$DB->ForSQL($this->_field_id)."'
196 ";
197 $arBinds = array(
198 "SETTINGS" => $arFields["SETTINGS"],
199 );
200 $DB->QueryBind($strSql, $arBinds);
201
202 $this->_clear_cache();
203 }
204 }
205
206 abstract public function IsRequired();
207 abstract public function IsMultiple();
208 abstract public function GetDefaultValue();
209 abstract public function SetSort($sort);
210 abstract public function GetArray();
211
212 public function Delete()
213 {
214 global $DB;
215 $DB->Query("
216 DELETE FROM b_lists_field
217 WHERE IBLOCK_ID = ".$this->_iblock_id."
218 AND FIELD_ID = '".$DB->ForSQL($this->_field_id)."'
219 ");
220 }
221
222 abstract public function Update($arFields);
223 static public function Add($iblock_id, $arFields)
224 {
225 }
226}
227
229{
230 private $_iblock_field;
231
232 public function __construct($iblock_id, $field_id, $label, $sort)
233 {
234 parent::__construct($iblock_id, $field_id, $label, $sort);
235
236 $this->_type = CListFieldTypeList::GetByID($field_id);
237
238 if($this->_iblock_id > 0)
239 $arIBlockFields = CIBlock::GetArrayByID($this->_iblock_id, "FIELDS");
240 else
241 $arIBlockFields = CIBlock::GetFieldsDefaults();
242
243 $this->_iblock_field = $arIBlockFields[$field_id] ?? null;
244 }
245
246 public function IsRequired()
247 {
248 return isset($this->_iblock_field["IS_REQUIRED"]) && $this->_iblock_field["IS_REQUIRED"] == "Y";
249 }
250
251 public function IsMultiple()
252 {
253 return false;
254 }
255
256 public function GetDefaultValue()
257 {
258 return $this->_iblock_field["DEFAULT_VALUE"];
259 }
260
261 public function SetSort($sort)
262 {
263 $this->_sort = intval($sort);
264 }
265
266 //This is only backward compatibility method
267 public function GetArray()
268 {
269 return array(
270 "FIELD_ID" => $this->_field_id,
271 "SORT" => $this->_sort,
272 "NAME" => $this->_label,
273 "IS_REQUIRED" => $this->_iblock_field["IS_REQUIRED"] ?? 'N',
274 "MULTIPLE" => "N",
275 "DEFAULT_VALUE" => $this->_iblock_field["DEFAULT_VALUE"] ?? null,
276 "TYPE" => $this->GetTypeID(),
277 "PROPERTY_TYPE" => false,
278 "PROPERTY_USER_TYPE" => false,
279 "SETTINGS" => $this->GetSettings(),
280 );
281 }
282
283 public function Delete()
284 {
286 global $stackCacheManager;
287 if (isset($this->_iblock_field['IS_REQUIRED']) && $this->_iblock_field['IS_REQUIRED'] == 'Y')
288 {
289 if($this->_iblock_id > 0)
290 {
291 $arIBlockFields = CIBlock::GetArrayByID($this->_iblock_id, "FIELDS");
292 $arIBlockFields[$this->_field_id]["IS_REQUIRED"] = "N";
293 CIBlock::SetFields($this->_iblock_id, $arIBlockFields);
294 $stackCacheManager->Clear("b_iblock");
295 }
296 $this->_iblock_field["IS_REQUIRED"] = "N";
297 }
298
299 parent::Delete();
300 return true;
301 }
302
303 public function Update($arFields)
304 {
306 global $stackCacheManager;
307 if(isset($arFields["TYPE"]))
308 $newType = $arFields["TYPE"];
309 else
310 $newType = $this->GetTypeID();
311
312 if($this->_iblock_id > 0 && CListFieldTypeList::IsField($newType))
313 {
314 $arIBlockFields = CIBlock::GetArrayByID($this->_iblock_id, "FIELDS");
315 $arIBlockFields[$newType] = $arFields;
316 CIBlock::SetFields($this->_iblock_id, $arIBlockFields);
317 $stackCacheManager->Clear("b_iblock");
318
319 if($newType != $this->GetTypeID())
320 $this->Delete();
321
322 return new CListElementField($this->_iblock_id, $newType, $arFields["NAME"], $arFields["SORT"]);
323 }
324
325 return null;
326 }
327
328 static public function Add($iblock_id, $arFields)
329 {
331 global $stackCacheManager;
332 if($iblock_id > 0)
333 {
334 $arIBlockFields = CIBlock::GetArrayByID($iblock_id, "FIELDS");
335 $arIBlockFields[$arFields["TYPE"]] = $arFields;
336 CIBlock::SetFields($iblock_id, $arIBlockFields);
337 $stackCacheManager->Clear("b_iblock");
338 }
339 return new CListElementField($iblock_id, $arFields["TYPE"], $arFields["NAME"], $arFields["SORT"]);
340 }
341}
342
344{
345 private $_property = false;
346 private static $prop_cache = array();
347
348 public function __construct($iblock_id, $field_id, $label, $sort)
349 {
350 parent::__construct($iblock_id, $field_id, $label, $sort);
351
352 if(preg_match("/^PROPERTY_(\\d+)$/", $field_id, $match))
353 {
354 $this->_property = $this->getPropertyArrayFromCache($match[1]);
355 }
356
357 if ($this->_property)
358 {
359 if($this->_property["USER_TYPE"])
360 $this->_type = CListFieldTypeList::GetByID($this->_property["PROPERTY_TYPE"].":".$this->_property["USER_TYPE"]);
361 else
362 $this->_type = CListFieldTypeList::GetByID($this->_property["PROPERTY_TYPE"]);
363 }
364
365 if(!is_object($this->_type))
366 $this->_type = CListFieldTypeList::GetByID("S");
367 }
368
369 private function getPropertyArrayFromCache($id)
370 {
371 //Cache iblock metadata in order to reduce queries
372 if(!array_key_exists($this->_iblock_id, self::$prop_cache))
373 {
374 self::$prop_cache[$this->_iblock_id] = array();
375
376 $rsProperties = CIBlockProperty::GetList(array(), array(
377 "IBLOCK_ID" => $this->_iblock_id,
378 "CHECK_PERMISSIONS" => "N",
379 "ACTIVE" => "Y",
380 ));
381 while($arProperty = $rsProperties->Fetch())
382 self::$prop_cache[$this->_iblock_id][$arProperty["ID"]] = $arProperty;
383 }
384 return self::$prop_cache[$this->_iblock_id][$id];
385 }
386
387 private static function resetPropertyArrayCache()
388 {
389 self::$prop_cache = array();
390 }
391
392 public function IsRequired()
393 {
394 return is_array($this->_property) && $this->_property["IS_REQUIRED"] == "Y";
395 }
396
397 public function IsMultiple()
398 {
399 return is_array($this->_property) && $this->_property["MULTIPLE"] == "Y";
400 }
401
402 public function GetDefaultValue()
403 {
404 return is_array($this->_property) && $this->_property["DEFAULT_VALUE"];
405 }
406
407 public function SetSort($sort)
408 {
409 if(is_array($this->_property))
410 {
411 $old_sort = intval($this->_property["SORT"]);
412 $new_sort = intval($sort);
413
414 if($old_sort != $new_sort)
415 {
416 $this->_property["SORT"] = $new_sort;
417 $obProperty = new CIBlockProperty;
418 $obProperty->Update($this->_property["ID"], $this->_property);
419 $this->_sort = $new_sort;
420 }
421 }
422 }
423
424 //This is only backward compatibility method
425 public function GetArray()
426 {
427 if(is_array($this->_property))
428 {
429 return array(
430 "FIELD_ID" => $this->_field_id,
431 "SORT" => $this->_sort,
432 "NAME" => $this->_property["NAME"],
433 "IS_REQUIRED" => $this->_property["IS_REQUIRED"],
434 "MULTIPLE" => $this->_property["MULTIPLE"],
435 "DEFAULT_VALUE" => $this->_property["DEFAULT_VALUE"],
436 "TYPE" => $this->GetTypeID(),
437 "PROPERTY_TYPE" => $this->_property["PROPERTY_TYPE"],
438 "PROPERTY_USER_TYPE" => $this->_property["USER_TYPE"]? CIBlockProperty::GetUserType($this->_property["USER_TYPE"]): false,
439 "CODE" => $this->_property["CODE"],
440 "ID" => $this->_property["ID"],
441 "LINK_IBLOCK_ID" => $this->_property["LINK_IBLOCK_ID"],
442 "ROW_COUNT" => $this->_property["ROW_COUNT"],
443 "COL_COUNT" => $this->_property["COL_COUNT"],
444 "USER_TYPE_SETTINGS" => $this->_property["USER_TYPE_SETTINGS"],
445 "SETTINGS" => $this->GetSettings(),
446 );
447 }
448 else
449 {
450 return false;
451 }
452 }
453
454 public function Delete()
455 {
456 if(is_array($this->_property))
457 {
458 $obProperty = new CIBlockProperty;
459 if($obProperty->Delete($this->_property["ID"]))
460 {
461 $this->resetPropertyArrayCache();
462 $this->_property = false;
463 }
464 }
465
466 parent::Delete();
467 return true;
468 }
469
470 private static function generatePropertyCode($name, $code, $iblockId, $propertyId = 0)
471 {
472 if(empty($code))
473 {
474 $code = CUtil::translit($name, LANGUAGE_ID, array("change_case" => "U"));
475 }
476
477 $object = CIBlockProperty::getList(array(), array("IBLOCK_ID" => $iblockId));
478 while($property = $object->fetch())
479 {
480 if($property["CODE"] == $code && $property["ID"] != $propertyId)
481 {
483 break;
484 }
485 }
486
487 return $code;
488 }
489
490 public function Update($arFields)
491 {
492 if(isset($arFields["TYPE"]))
493 $newType = $arFields["TYPE"];
494 else
495 $newType = $this->GetTypeID();
496
497 if(is_array($this->_property) && !CListFieldTypeList::IsField($newType))
498 {
499 if (self::existPropertyCode($this->_iblock_id, $arFields["CODE"], $this->_property["ID"]))
500 {
501 throw new NotSupportedException(GetMessage("LIST_PROPERTY_FIELD_DUPLICATE_CODE"));
502 }
503
504 foreach($this->GetArray() as $id => $val)
505 if(array_key_exists($id, $arFields) && $id != "IBLOCK_ID")
506 $this->_property[$id] = $arFields[$id];
507
508 if(mb_strpos($newType, ":") !== false)
509 list($this->_property["PROPERTY_TYPE"], $this->_property["USER_TYPE"]) = explode(":", $newType);
510 else
511 {
512 $this->_property["PROPERTY_TYPE"] = $newType;
513 $this->_property["USER_TYPE"] = "";
514 }
515
516 $this->_property["CHECK_PERMISSIONS"] = "N";
517 $this->_property["ACTIVE"] = "Y";
518
519 $obProperty = new CIBlockProperty;
520 if($obProperty->Update($this->_property["ID"], $this->_property))
521 {
522 self::resetPropertyArrayCache();
523
524 if($this->_property["PROPERTY_TYPE"] == "L" && is_array($arFields["LIST"] ?? null))
525 CList::UpdatePropertyList($this->_property["ID"], $arFields["LIST"]);
526
527 return new CListPropertyField($this->_property["IBLOCK_ID"], "PROPERTY_".$this->_property["ID"], $arFields["NAME"], $arFields["SORT"]);
528 }
529 elseif (!empty($obProperty->LAST_ERROR))
530 {
531 throw new ArgumentException($obProperty->LAST_ERROR);
532 }
533 }
534
535 return null;
536 }
537
538 static public function Add($iblock_id, $arFields)
539 {
540 if($iblock_id > 0)
541 {
542 if (self::existPropertyCode($iblock_id, $arFields["CODE"]))
543 {
544 throw new NotSupportedException(GetMessage("LIST_PROPERTY_FIELD_DUPLICATE_CODE"));
545 }
546 $property_id = intval($arFields["ID"] ?? 0);
547 if($property_id > 0)
548 {
549 return new CListPropertyField($iblock_id, "PROPERTY_".$property_id, $arFields["NAME"], $arFields["SORT"]);
550 }
551 else
552 {
553 $arFields["IBLOCK_ID"] = $iblock_id;
554 if(mb_strpos($arFields["TYPE"], ":") !== false)
555 list($arFields["PROPERTY_TYPE"], $arFields["USER_TYPE"]) = explode(":", $arFields["TYPE"]);
556 else
557 $arFields["PROPERTY_TYPE"] = $arFields["TYPE"];
558 $arFields["MULTIPLE_CNT"] = 1;
559 $arFields["CHECK_PERMISSIONS"] = "N";
560
561 $obProperty = new CIBlockProperty;
562 $res = $obProperty->Add($arFields);
563 if (!empty($obProperty->LAST_ERROR))
564 {
565 throw new ArgumentException($obProperty->LAST_ERROR);
566 }
567 if($res)
568 {
569 self::resetPropertyArrayCache();
570
571 if($arFields["PROPERTY_TYPE"] == "L" && is_array($arFields["LIST"] ?? null))
572 {
574 }
575
576 return new CListPropertyField($iblock_id, "PROPERTY_".$res, $arFields["NAME"], $arFields["SORT"]);
577 }
578 }
579 }
580
581 return null;
582 }
583
584 private static function existPropertyCode($iblockId, $code, $propertyId = 0)
585 {
586 $iblockId = intval($iblockId);
587 if (!$iblockId)
588 {
589 throw new ArgumentException("Required parameter \"iblockId\" is missing.");
590 }
591 if (empty($code))
592 {
593 return false;
594 }
595
596 $queryObject = \CIBlockProperty::getList(array(), array("IBLOCK_ID" => $iblockId, "CODE" => $code));
597 $property = $queryObject->fetch();
598
599 if (!empty($property) && is_array($property))
600 {
601 return $property["ID"] != $propertyId;
602 }
603 else
604 {
605 return false;
606 }
607 }
608}
Update($ID, $arFields, $bCheckDescription=false)
Определения iblockproperty.php:516
Определения listfield.php:229
SetSort($sort)
Определения listfield.php:261
GetDefaultValue()
Определения listfield.php:256
GetArray()
Определения listfield.php:267
Delete()
Определения listfield.php:283
Update($arFields)
Определения listfield.php:303
IsMultiple()
Определения listfield.php:251
static Add($iblock_id, $arFields)
Определения listfield.php:328
__construct($iblock_id, $field_id, $label, $sort)
Определения listfield.php:232
IsRequired()
Определения listfield.php:246
Определения listfield.php:9
GetTypeID()
Определения listfield.php:107
GetLabel()
Определения listfield.php:102
SetSort($sort)
$_sort
Определения listfield.php:19
GetSettings()
Определения listfield.php:137
$_type
Определения listfield.php:17
Delete()
Определения listfield.php:212
Update($arFields)
$_field_id
Определения listfield.php:13
IsReadOnly()
Определения listfield.php:112
$_label
Определения listfield.php:15
$_iblock_id
Определения listfield.php:11
static Add($iblock_id, $arFields)
Определения listfield.php:223
GetSettingsDefaults()
Определения listfield.php:122
SetSettings($arSettings)
Определения listfield.php:149
__construct($iblock_id, $field_id, $label, $sort)
Определения listfield.php:23
GetID()
Определения listfield.php:97
GetSort()
Определения listfield.php:117
static IsField($type_id)
Определения listfieldtypes.php:26
static GetByID($type_id)
Определения listfieldtypes.php:17
static UpdatePropertyList($prop_id, $list)
Определения list.php:82
SetSort($sort)
Определения listfield.php:407
GetDefaultValue()
Определения listfield.php:402
GetArray()
Определения listfield.php:425
Delete()
Определения listfield.php:454
Update($arFields)
Определения listfield.php:490
IsMultiple()
Определения listfield.php:397
static Add($iblock_id, $arFields)
Определения listfield.php:538
__construct($iblock_id, $field_id, $label, $sort)
Определения listfield.php:348
IsRequired()
Определения listfield.php:392
static generateMnemonicCode($integerCode=0)
Определения lists.php:445
$arFields
Определения dblapprove.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$iblockId
Определения iblock_catalog_edit.php:30
global $DB
Определения cron_frame.php:29
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$name
Определения menu_edit.php:35
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$width
Определения html.php:68
$val
Определения options.php:1793