Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
file.php
1<?php
2
4
8
9Loc::loadMessages(__FILE__);
10
15class File extends Base
16{
17
21 public static function getType()
22 {
23 return FieldType::FILE;
24 }
25
30 public static function getFormats()
31 {
32 $formats = parent::getFormats();
33 $formats['src'] = [
34 'callable' => 'formatValueSrc',
35 'separator' => ', ',
36 ];
37
38 $formats['publink'] = [
39 'callable' => 'formatValuePublicLink',
40 'separator' => ', ',
41 ];
42
43 $formats['shortlink'] = [
44 'callable' => 'formatValueShortLink',
45 'separator' => ', ',
46 ];
47
48 $formats['name'] = [
49 'callable' => 'formatValueName',
50 'separator' => ', ',
51 ];
52
53 return $formats;
54 }
55
63 public static function toSingleValue(FieldType $fieldType, $value)
64 {
65 if (is_array($value))
66 {
67 if (\CBPHelper::isAssociativeArray($value))
68 {
69 $value = array_keys($value);
70 }
71 reset($value);
72 $value = current($value);
73 }
74
75 return $value;
76 }
77
83 protected static function formatValuePrintable(FieldType $fieldType, $value)
84 {
85 $value = (int) $value;
86 $iterator = \CFile::getByID($value);
87 if ($file = $iterator->fetch())
88 {
89 return '[url=/bitrix/tools/bizproc_show_file.php?f='.urlencode($file['FILE_NAME']).'&hash='
90 .md5($file['FILE_NAME'])
91 .'&i='.$value.'&h='.md5($file['SUBDIR']).']'
92 .htmlspecialcharsbx($file['ORIGINAL_NAME'])
93 .'[/url]';
94 }
95
96 return '';
97 }
98
104 protected static function formatValueSrc(FieldType $fieldType, $value)
105 {
106 $value = (int) $value;
107 $file = \CFile::getFileArray($value);
108 if ($file && $file['SRC'])
109 {
110 return $file['SRC'];
111 }
112 return '';
113 }
114
120 protected static function formatValueName(FieldType $fieldType, $value)
121 {
122 $value = (int) $value;
123 $file = \CFile::getFileArray($value);
124 if ($file && ($file['ORIGINAL_NAME'] || $file['FILE_NAME']))
125 {
126 return $file['ORIGINAL_NAME'] ?: $file['FILE_NAME'];
127 }
128 return '';
129 }
130
136 protected static function formatValuePublicLink(FieldType $fieldType, $value)
137 {
138 $fileId = (int) $value;
139 if ($fileId)
140 {
141 return \Bitrix\Bizproc\Controller\File::getPublicLink($fileId);
142 }
143 return '';
144 }
145
151 protected static function formatValueShortLink(FieldType $fieldType, $value)
152 {
153 $pubLink = static::formatValuePublicLink($fieldType, $value);
154 if ($pubLink)
155 {
156 return Main\Engine\UrlManager::getInstance()->getHostUrl().\CBXShortUri::getShortUri($pubLink);
157 }
158 return '';
159 }
160
167 public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
168 {
170 $type = $toTypeClass::getType();
171 switch ($type)
172 {
173 case FieldType::FILE:
174 $value = (int) $value;
175 break;
176 default:
177 $value = null;
178 }
179
180 return $value;
181 }
182
187 public static function getConversionMap()
188 {
189 return [
190 [
191 FieldType::FILE
192 ]
193 ];
194 }
195
202 public static function convertValueMultiple(FieldType $fieldType, $value, $toTypeClass)
203 {
204 $value = (array) $value;
205 if (\CBPHelper::isAssociativeArray($value))
206 {
207 $value = array_keys($value);
208 }
209
210 return parent::convertValueMultiple($fieldType, $value, $toTypeClass);
211 }
212
217 public static function canRenderControl($renderMode)
218 {
219 return true;
220 }
221
230 protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
231 {
232 if ($renderMode & FieldType::RENDER_MODE_DESIGNER)
233 {
234 return '';
235 }
236
237 $classNameHtml = htmlspecialcharsbx(static::generateControlClassName($fieldType, $field));
238 $idHtml = htmlspecialcharsbx(static::generateControlId($field));
239 $nameHtml = htmlspecialcharsbx(static::generateControlName($field));
240
241 if ($renderMode & FieldType::RENDER_MODE_PUBLIC)
242 {
243 $msg = htmlspecialcharsbx(Loc::getMessage('BPDT_FILE_CHOOSE_FILE'));
244 $onchange = 'this.nextSibling.textContent = BX.Bizproc.FieldType.File.parseLabel(this.value);';
245 $onchange = htmlspecialcharsbx($onchange);
246
247 return <<<HTML
248 <div class="{$classNameHtml}">
249 <span>
250 <span class="webform-small-button">{$msg}</span>
251 </span>
252 <input type="file" id="{$idHtml}" name="{$nameHtml}" onchange="{$onchange}">
253 <span class="bizproc-type-control-file-label"></span>
254 </div>
255HTML;
256 }
257
258 return '<input type="file" class="'.$classNameHtml.'" id="'.$idHtml.'" name="'.$nameHtml.'">';
259 }
260
269 public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
270 {
271 if ($allowSelection && $renderMode & FieldType::RENDER_MODE_PUBLIC)
272 {
273 return self::renderPublicSelectableControlSingle($fieldType, $field, $value);
274 }
275
276 if ($renderMode & FieldType::RENDER_MODE_MOBILE)
277 {
278 return self::renderMobileControl($fieldType, $field, $value);
279 }
280
281 return parent::renderControlSingle($fieldType, $field, $value, $allowSelection, $renderMode);
282 }
283
292 public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
293 {
294 if ($renderMode & FieldType::RENDER_MODE_DESIGNER && !$allowSelection)
295 {
296 return '';
297 }
298
299 if ($allowSelection && $renderMode & FieldType::RENDER_MODE_PUBLIC)
300 {
301 return self::renderPublicSelectableControlMultiple($fieldType, $field, $value);
302 }
303
304 if ($renderMode & FieldType::RENDER_MODE_MOBILE)
305 {
306 return self::renderMobileControl($fieldType, $field, $value);
307 }
308
309 if ($renderMode & FieldType::RENDER_MODE_DESIGNER)
310 {
311 if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
312 {
313 $value = [$value];
314 }
315
316 // need to show at least one control
317 if (empty($value))
318 {
319 $value[] = null;
320 }
321
322 $controls = [];
323
324 foreach ($value as $k => $v)
325 {
326 $singleField = $field;
327 $singleField['Index'] = $k;
328 $controls[] = parent::renderControlSingle($fieldType, $singleField, $v, $allowSelection, $renderMode);
329 }
330
331 return static::wrapCloneableControls($controls, static::generateControlName($field));
332 }
333
334 return parent::renderControlMultiple($fieldType, $field, $value, $allowSelection, $renderMode);
335 }
336
337 private static function renderPublicSelectableControlSingle(FieldType $fieldType, array $field, $value)
338 {
339 $name = static::generateControlName($field);
340 $className = static::generateControlClassName($fieldType, $field);
341 $className = str_replace('file', 'file-selectable', $className);
342
343 return sprintf(
344 '<input type="text" class="%s" name="%s" value="%s" placeholder="%s" data-role="inline-selector-target" data-selector-type="file" data-property="%s"/>',
345 htmlspecialcharsbx($className),
346 htmlspecialcharsbx($name),
347 htmlspecialcharsbx((string)$value),
348 htmlspecialcharsbx($fieldType->getDescription()),
349 htmlspecialcharsbx(Main\Web\Json::encode($fieldType->getProperty()))
350 );
351 }
352
353 private static function renderPublicSelectableControlMultiple(FieldType $fieldType, array $field, $value)
354 {
355 if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
356 {
357 $value = [$value];
358 }
359
360 // need to show at least one control
361 if (empty($value))
362 {
363 $value[] = null;
364 }
365
366 $controls = [];
367
368 foreach ($value as $k => $v)
369 {
370 $singleField = $field;
371 $singleField['Index'] = $k;
372 $controls[] = static::renderPublicSelectableControlSingle(
373 $fieldType,
374 $singleField,
375 $v
376 );
377 }
378
379 return static::renderPublicMultipleWrapper($fieldType, $field, $controls);
380 }
381
382 private static function renderMobileControl(FieldType $fieldType, array $field, $value)
383 {
385 global $APPLICATION;
386 ob_start();
387 $APPLICATION->IncludeComponent(
388 'bitrix:main.file.input',
389 'mobile',
390 [
391 'MODULE_ID' => 'bizproc',
392 'CONTROL_ID' => static::generateControlId($field),
393 'ALLOW_UPLOAD' => 'A',
394 'INPUT_NAME' => static::generateControlName($field),
395 'INPUT_VALUE' => $value,
396 'MULTIPLE' => $fieldType->isMultiple() ? 'Y' : 'N'
397 ]
398 );
399
400 return ob_get_clean();
401 }
402
409 protected static function extractValue(FieldType $fieldType, array $field, array $request)
410 {
411 $value = parent::extractValue($fieldType, $field, $request);
412
413 if (is_array($value) && !empty($value['name']) && !empty($value['tmp_name']))
414 {
415 if (!is_uploaded_file($value['tmp_name']))
416 {
417 $value = null;
418 static::addError([
419 'code' => 'ErrorValue',
420 'message' => Loc::getMessage('BPDT_FILE_SECURITY_ERROR'),
421 'parameter' => static::generateControlName($field),
422 ]);
423 }
424 else
425 {
426 if (!array_key_exists('MODULE_ID', $value) || $value['MODULE_ID'] == '')
427 $value['MODULE_ID'] = 'bizproc';
428
429 $value = \CFile::saveFile($value, 'bizproc_wf');
430 if (!$value)
431 {
432 $value = null;
433 static::addError([
434 'code' => 'ErrorValue',
435 'message' => Loc::getMessage('BPDT_FILE_INVALID'),
436 'parameter' => static::generateControlName($field),
437 ]);
438 }
439 }
440 }
441 elseif (\CBPActivity::isExpression($value))
442 {
443 //It`s OK
444 }
445 elseif (is_numeric($value) && defined('BX_MOBILE'))
446 {
447 $file = \CFile::getById($value)->fetch();
448 if (!$file || $file['MODULE_ID'] !== 'bizproc')
449 {
450 $value = null;
451 }
452 }
453 elseif (is_string($value) && $value !== '')
454 {
455 // from mobile
456
457 $unsignedValue = \CBPDocument::unSignParameters($value);
458 if (isset($unsignedValue[0]) && is_numeric($unsignedValue[0]) && (int)$unsignedValue[0] > 0)
459 {
460 $value = (int)$unsignedValue[0];
461 }
462 else
463 {
464 $value = null;
465 }
466 }
467 else
468 {
469 $value = null;
470 }
471
472 return $value;
473 }
474
475 public static function externalizeValue(FieldType $fieldType, $context, $value)
476 {
477 if ($context === 'rest' && is_numeric($value))
478 {
479 return \CRestUtil::GetFile($value);
480 }
481
482 return parent::externalizeValue($fieldType, $context, $value);
483 }
484
485 public static function internalizeValue(FieldType $fieldType, $context, $value)
486 {
487 if ($context === 'rest')
488 {
489 $fileFields = \CRestUtil::saveFile($value);
490
491 if ($fileFields)
492 {
493 $fileFields['MODULE_ID'] = 'bizproc';
494 return (int) \CFile::saveFile($fileFields, 'bizproc_rest');
495 }
496 }
497
498 return parent::internalizeValue($fieldType, $context, $value);
499 }
500
501}
static formatValueSrc(FieldType $fieldType, $value)
Definition file.php:104
static renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition file.php:230
static formatValuePublicLink(FieldType $fieldType, $value)
Definition file.php:136
static formatValueShortLink(FieldType $fieldType, $value)
Definition file.php:151
static extractValue(FieldType $fieldType, array $field, array $request)
Definition file.php:409
static formatValueName(FieldType $fieldType, $value)
Definition file.php:120
static externalizeValue(FieldType $fieldType, $context, $value)
Definition file.php:475
static renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition file.php:269
static formatValuePrintable(FieldType $fieldType, $value)
Definition file.php:83
static internalizeValue(FieldType $fieldType, $context, $value)
Definition file.php:485
static toSingleValue(FieldType $fieldType, $value)
Definition file.php:63
static convertValueMultiple(FieldType $fieldType, $value, $toTypeClass)
Definition file.php:202
static renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
Definition file.php:292
static canRenderControl($renderMode)
Definition file.php:217
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29