Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
propertytypesettings.php
1<?php
2
4
6use CIBlockProperty;
7
9{
10 private string $propertyType;
11 private ?string $userType;
12 private array $setValues = [];
13 private array $showedFields = [];
14 private array $hiddenFields = [];
15 private ?string $settingsHtml = null;
16 private ?string $defaultValueHtml = null;
17
22 public function __construct(string $propertyType, ?string $userType = null)
23 {
24 $this->propertyType = $propertyType;
25 $this->userType = $userType;
26
27 $this->initHiddenFields();
28 }
29
37 public function getPropertyType(): string
38 {
39 return $this->propertyType;
40 }
41
47 public function getUserType(): ?string
48 {
49 return $this->userType;
50 }
51
57 private function initHiddenFields(): void
58 {
59 if ($this->propertyType === PropertyTable::TYPE_ELEMENT)
60 {
61 $this->hiddenFields[] = 'SEARCHABLE';
62 $this->hiddenFields[] = 'WITH_DESCRIPTION';
63 $this->hiddenFields[] = 'COL_COUNT';
64 $this->hiddenFields[] = 'DEFAULT_VALUE';
65 }
66 elseif ($this->propertyType === PropertyTable::TYPE_SECTION)
67 {
68 $this->hiddenFields[] = 'SEARCHABLE';
69 $this->hiddenFields[] = 'WITH_DESCRIPTION';
70 $this->hiddenFields[] = 'COL_COUNT';
71 $this->hiddenFields[] = 'DEFAULT_VALUE';
72 }
73 elseif ($this->propertyType === PropertyTable::TYPE_LIST)
74 {
75 $this->hiddenFields[] = 'WITH_DESCRIPTION';
76 $this->hiddenFields[] = 'MULTIPLE_CNT';
77 $this->hiddenFields[] = 'COL_COUNT';
78 $this->hiddenFields[] = 'DEFAULT_VALUE';
79 }
80 elseif ($this->propertyType === PropertyTable::TYPE_FILE)
81 {
82 $this->hiddenFields[] = 'FILTERABLE';
83 $this->hiddenFields[] = 'MULTIPLE_CNT';
84 $this->hiddenFields[] = 'SMART_FILTER';
85 $this->hiddenFields[] = 'DISPLAY_TYPE';
86 $this->hiddenFields[] = 'DISPLAY_EXPANDED';
87 $this->hiddenFields[] = 'FILTER_HINT';
88 $this->hiddenFields[] = 'DEFAULT_VALUE';
89 $this->hiddenFields[] = 'ROW_COUNT';
90 }
91 elseif ($this->userType === PropertyTable::USER_TYPE_HTML)
92 {
93 $this->hiddenFields[] = 'MULTIPLE';
94 }
95
96 // hidden for NOT need type
97
98 if ($this->propertyType !== PropertyTable::TYPE_LIST)
99 {
100 $this->hiddenFields[] = 'LIST_TYPE';
101 }
102
103 if ($this->propertyType !== PropertyTable::TYPE_FILE)
104 {
105 $this->hiddenFields[] = 'FILE_TYPE';
106 }
107
108 if (
109 !in_array($this->propertyType, [PropertyTable::TYPE_SECTION, PropertyTable::TYPE_ELEMENT], true)
110 && $this->userType !== PropertyTable::USER_TYPE_SKU
111 )
112 {
113 $this->hiddenFields[] = 'LINK_IBLOCK_ID';
114 }
115 }
116
117 public function appendShowedFields(array $fields): void
118 {
119 array_push($this->showedFields, ...$fields);
120 }
121
122 public function appendHiddenFields(array $fields): void
123 {
124 foreach ($fields as $name)
125 {
126 $this->hiddenFields[] = $name;
127
128 if ($name === 'SMART_FILTER')
129 {
130 $this->hiddenFields[] = 'DISPLAY_TYPE';
131 $this->hiddenFields[] = 'DISPLAY_EXPANDED';
132 $this->hiddenFields[] = 'FILTER_HINT';
133 }
134 }
135 }
136
147 public static function createByUserType(string $propertyType, string $userType, array $propertyFields, ?string $htmlName = 'USER_TYPE_SETTINGS'): self
148 {
149 $self = new self($propertyType, $userType);
150 $userTypeFields = CIBlockProperty::GetUserType($userType);
151
152 $excludeSettingsHtmlUserTypes = [
154 ];
155
156 if (
157 isset($userTypeFields["GetSettingsHTML"])
158 && !in_array($userType, $excludeSettingsHtmlUserTypes, true)
159 && is_callable($userTypeFields["GetSettingsHTML"])
160 )
161 {
162 $config = [];
163 $htmlCode = call_user_func_array(
164 $userTypeFields["GetSettingsHTML"],
165 [
166 $propertyFields,
167 [
168 'NAME' => $htmlName,
169 ],
170 &$config,
171 ]
172 );
173
174 if (!empty($htmlCode) && is_string($htmlCode))
175 {
176 $self->settingsHtml = $htmlCode;
177 }
178
179 if (isset($config['SHOW']) && is_array($config['SHOW']))
180 {
181 $self->appendShowedFields($config['SHOW']);
182 }
183
184 if (isset($config['HIDE']) && is_array($config['HIDE']))
185 {
186 $self->appendHiddenFields($config['HIDE']);
187 }
188
189 if (isset($config['SET']) && is_array($config['SET']))
190 {
191 $self->setValues = $config['SET'];
192 }
193 }
194
195 if (isset($userTypeFields["GetPropertyFieldHtml"]) && is_callable($userTypeFields["GetPropertyFieldHtml"]))
196 {
197 $htmlCode = call_user_func_array(
198 $userTypeFields["GetPropertyFieldHtml"],
199 [
200 $propertyFields,
201 [
202 'VALUE' => $propertyFields['DEFAULT_VALUE'] ?? null,
203 'DESCRIPTION' => '',
204 ],
205 [
206 'VALUE' => 'DEFAULT_VALUE',
207 'DESCRIPTION' => '',
208 'MODE' => 'EDIT_FORM',
209 'FORM_NAME' => '',
210 ],
211 ]
212 );
213
214 if (!empty($htmlCode) && is_string($htmlCode))
215 {
216 $self->defaultValueHtml = $htmlCode;
217 }
218 }
219
220 return $self;
221 }
222
230 public function isShownField(string $fieldName): bool
231 {
232 $hide = false;
233
234 if (!empty($this->hiddenFields))
235 {
236 $hide =
237 in_array($fieldName, $this->hiddenFields, true)
238 && !in_array($fieldName, $this->showedFields, true)
239 ;
240 }
241
242 return !$hide;
243 }
244
245 public function getSettingsHtml(): ?string
246 {
247 return $this->settingsHtml;
248 }
249
250 public function getDefaultValueHtml(): ?string
251 {
252 return $this->defaultValueHtml;
253 }
254
255 public function getSetValues(): array
256 {
257 return $this->setValues;
258 }
259}
static createByUserType(string $propertyType, string $userType, array $propertyFields, ?string $htmlName='USER_TYPE_SETTINGS')
__construct(string $propertyType, ?string $userType=null)