Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usertypepropertydiskfile.php
1<?php
2
4
7use Bitrix\Disk\File;
9
10if (Loader::requireModule('bizproc'))
11{
12 class UserTypePropertyDiskFile extends UserTypeProperty
13 {
14 private const MOBILE_COMPONENT_NAME = 'bitrix:mobile.iblock.bizproctype.diskfile';
15
19 public static function getType()
20 {
21 return FieldType::INT;
22 }
23
24 public static function formatValueMultiple(FieldType $fieldType, $value, $format = 'printable')
25 {
26 if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
27 $value = array($value);
28
29 foreach ($value as $k => $v)
30 {
31 $value[$k] = static::formatValuePrintable($fieldType, $v);
32 }
33
34 return implode(static::getFormatSeparator($format), $value);
35 }
36
37 public static function formatValueSingle(FieldType $fieldType, $value, $format = 'printable')
38 {
39 return static::formatValueMultiple($fieldType, $value, $format);
40 }
41
47 protected static function formatValuePrintable(FieldType $fieldType, $value)
48 {
49 $iblockId = self::getIblockId($fieldType);
50
51 $property = static::getUserType($fieldType);
52 if (array_key_exists('GetUrlAttachedFileWorkflow', $property))
53 {
54 return call_user_func_array($property['GetUrlAttachedFileWorkflow'], array($iblockId, $value));
55 }
56 else
57 {
58 return '';
59 }
60 }
61
68 public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
69 {
70 if (is_array($value) && isset($value['VALUE']))
71 $value = $value['VALUE'];
72
73 $value = (int) $value;
74
76 $type = $toTypeClass::getType();
77 switch ($type)
78 {
79 case FieldType::FILE:
80 $diskFile = File::getById($value);
81 $value = $diskFile? $diskFile->getFileId() : null;
82 break;
83 default:
84 $value = null;
85 }
86
87 return $value;
88 }
89
90 public static function convertValueMultiple(FieldType $fieldType, $value, $toTypeClass)
91 {
92 return array_values(parent::convertValueMultiple($fieldType, $value, $toTypeClass));
93 }
94
99 public static function getConversionMap()
100 {
101 return array(
102 array(
103 FieldType::FILE
104 )
105 );
106 }
107
108 public static function canRenderControl($renderMode)
109 {
110 if ($renderMode & FieldType::RENDER_MODE_MOBILE)
111 {
112 return (static::getMobileControlRenderer() !== null);
113 }
114
115 return parent::canRenderControl($renderMode);
116 }
117
126 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
127 {
128 return static::renderControlMultiple($fieldType, $field, $value, $allowSelection, $renderMode);
129 }
130
139 public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
140 {
141 if ($allowSelection)
142 {
143 $selectorValue = null;
144 if(is_array($value))
145 {
146 $value = current($value);
147 }
148 if (\CBPActivity::isExpression($value))
149 {
150 $selectorValue = $value;
151 $value = null;
152 }
153 return static::renderControlSelector($field, $selectorValue, true, '', $fieldType);
154 }
155
156 if ($renderMode & FieldType::RENDER_MODE_DESIGNER)
157 return '';
158
159 if ($renderMode & FieldType::RENDER_MODE_MOBILE)
160 {
161 return self::renderMobileControl($fieldType, $field, $value);
162 }
163
164 $userType = static::getUserType($fieldType);
165 $iblockId = self::getIblockId($fieldType);
166
167 if (!empty($userType['GetPublicEditHTML']))
168 {
169 if (is_array($value) && isset($value['VALUE']))
170 $value = $value['VALUE'];
171
172 $fieldName = static::generateControlName($field);
174 $userType['GetPublicEditHTML'],
175 array(
176 array(
177 'IBLOCK_ID' => $iblockId,
178 'IS_REQUIRED' => $fieldType->isRequired()? 'Y' : 'N',
179 'PROPERTY_USER_TYPE' => $userType
180 ),
181 array('VALUE' => $value),
182 array(
183 'FORM_NAME' => $field['Form'],
184 'VALUE' => $fieldName,
185 'DESCRIPTION' => '',
186 ),
187 true
188 )
189 );
190 }
191 else
192 $renderResult = static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
193
194 return $renderResult;
195 }
196
197 public static function extractValueSingle(FieldType $fieldType, array $field, array $request)
198 {
199 return static::extractValueMultiple($fieldType, $field, $request);
200 }
201
202 public static function extractValueMultiple(FieldType $fieldType, array $field, array $request)
203 {
204 if (defined('BX_MOBILE'))
205 {
206 return self::extractValueMobile($fieldType, $field, $request);
207 }
208
209 return parent::extractValueMultiple($fieldType, $field, $request);
210 }
211
212 private static function getIblockId(FieldType $fieldType)
213 {
214 $documentType = $fieldType->getDocumentType();
215 $type = explode('_', $documentType[2]);
216 return intval($type[1]);
217 }
218
219 public static function extractValue(FieldType $fieldType, array $field, array $request)
220 {
221 $value = parent::extractValue($fieldType, $field, $request);
222 if (is_array($value) && isset($value['VALUE']))
223 {
224 $value = $value['VALUE'];
225 }
226
227 if(!$value)
228 {
229 return null;
230 }
231
232 $property = static::getUserType($fieldType);
233 $iblockId = self::getIblockId($fieldType);
234
235 if (array_key_exists('AttachFilesWorkflow', $property))
236 {
237 return call_user_func_array($property['AttachFilesWorkflow'], array($iblockId, $value));
238 }
239
240 return null;
241 }
242
243 private static function extractValueMobile(FieldType $fieldType, array $field, array $request)
244 {
245 $renderer = self::getMobileControlRenderer();
246 if (!$renderer)
247 {
248 return null;
249 }
250
252 [$renderer, 'extractValues'],
253 static::generateControlName($field),
254 $request
255 );
256
257 $property = static::getUserType($fieldType);
258 $iblockId = self::getIblockId($fieldType);
259
260 if (array_key_exists('AttachFilesWorkflow', $property))
261 {
262 foreach ($diskFileIds as $i => $diskFileId)
263 {
265 $property['AttachFilesWorkflow'], [$iblockId, $diskFileId]
266 );
267 }
268 }
269
270 if (!$fieldType->isMultiple())
271 {
272 return $diskFileIds ? end($diskFileIds) : null;
273 }
274
275 return $diskFileIds;
276 }
277
278 public static function clearValueSingle(FieldType $fieldType, $value)
279 {
280 static::clearValueMultiple($fieldType, $value);
281 }
282
283 public static function clearValueMultiple(FieldType $fieldType, $values)
284 {
285 if(!is_array($values))
286 {
287 $values = array($values);
288 }
289
290 $property = static::getUserType($fieldType);
291 $iblockId = self::getIblockId($fieldType);
292
293 if (array_key_exists('DeleteAttachedFiles', $property))
294 {
295 call_user_func_array($property['DeleteAttachedFiles'], array($iblockId, $values));
296 }
297 }
298
299 public static function toSingleValue(FieldType $fieldType, $value)
300 {
301 if (is_array($value) && isset($value['VALUE']))
302 {
303 $value = $value['VALUE'];
304 }
305 if (is_array($value) && isset($value[0]))
306 {
307 $value = $value[0];
308 }
309
310 return $value;
311 }
312
313 protected static function getMobileControlRenderer(): ?string
314 {
315 ob_start();
316 $className = \CBitrixComponent::includeComponentClass(self::MOBILE_COMPONENT_NAME);
317 ob_end_clean();
318
319 return $className ?: null;
320 }
321
322 protected static function renderMobileControl(FieldType $fieldType, array $field, $value): string
323 {
326 ob_start();
327 $APPLICATION->IncludeComponent(
328 self::MOBILE_COMPONENT_NAME,
329 '',
330 [
331 'INPUT_NAME' => static::generateControlName($field),
332 'INPUT_VALUE' => $value,
333 'MULTIPLE' => $fieldType->isMultiple() ? 'Y' : 'N'
334 ]
335 );
336
337 return ob_get_clean();
338 }
339 }
340}
extractValue(array $fieldProperties, $value)