Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
property.php
1<?php
2
3
5
6
7
9use Bitrix\Main\Entity\ExpressionField;
20
21Loc::loadMessages($_SERVER["DOCUMENT_ROOT"].'/bitrix/modules/sale/admin/order_props_edit.php');
22
23class Property extends Controller
24{
25 protected $property;
26 protected $dbProperty;
28 protected $errors = [];
29
30 protected $personTypeId;
31 protected $siteId;
32
33 //region Actions
34 public function getFieldsByTypeAction($type)
35 {
36 $r = new Result();
37
38 if(!in_array($type, array_keys($this->getTypes()['ENUM'])))
39 {
40 $r->addError(new Error('type is out of range', 200850000008));
41 }
42
43 if(!$r->isSuccess())
44 {
45 $this->addErrors($r->getErrors());
46 return null;
47 }
48 else
49 {
50 $entity = new \Bitrix\Sale\Rest\Entity\Property();
51 return ['PROPERTY'=>$entity->prepareFieldInfos(
52 $entity->getFieldsByType($type)
53 )];
54 }
55 }
56
57 public function updateAction($id, array $fields)
58 {
59 $r = $this->exists($id);
60 if($r->isSuccess())
61 {
62 $fillFields = $this->get($id);
63 $fields['ID'] = $fillFields['ID'];
64 $fields['TYPE'] = $fillFields['TYPE'];
65 $fields['PROPS_GROUP_ID'] = $fillFields['PROPS_GROUP_ID'];
66 $fields['PERSON_TYPE_ID'] = $fillFields['PERSON_TYPE_ID'];
67
68 if(isset($fields['SETTINGS']))
69 {
70 $fields = array_merge($fields, $fields['SETTINGS']);
71 unset($fields['SETTINGS']);
72 }
73
74 $r = $this->checkFileds($fields);
75
76 if($r->isSuccess())
77 {
78 $this->personTypeId = $fields['PERSON_TYPE_ID'];
79 $this->property = $fields;
80
82 if ($this->validateProperty())
83 {
84 $this->saveProperty();
85 }
86
87 foreach ($this->errors as $error)
88 {
89 $r->addError(new Error($error, 200850000003));
90 }
91 }
92 }
93
94 if(!$r->isSuccess())
95 {
96 $this->addErrors($r->getErrors());
97 return null;
98 }
99 else
100 {
101 return ['PROPERTY'=>$this->get($this->property['ID'])];
102 }
103 }
104
105 public function getAction($id)
106 {
107 $r = $this->exists($id);
108 if($r->isSuccess())
109 {
110 return ['PROPERTY'=>$this->get($id)];
111 }
112 else
113 {
114 $this->addErrors($r->getErrors());
115 return null;
116 }
117 }
118
119 public function deleteAction($id)
120 {
121 $orderProps = new \CSaleOrderProps();
122
123 $r = $this->exists($id);
124 if($r->isSuccess())
125 {
126 if (!$orderProps->Delete($id))
127 {
128 if ($ex = self::getApplication()->GetException())
129 $r->addError(new Error($ex->GetString(), $ex->GetId()));
130 else
131 $r->addError(new Error('delete property error', 200850000004));
132 }
133 }
134
135 if($r->isSuccess())
136 {
137 return true;
138 }
139 else
140 {
141 $this->addErrors($r->getErrors());
142 return null;
143 }
144 }
145
146 public function listAction(PageNavigation $pageNavigation, $select=[], $filter=[], $order=[]): Page
147 {
148 $select = empty($select) ? ['*'] : $select;
149 $order = empty($order) ? ['ID' => 'ASC'] : $order;
150
152 $propertyClassName = $this->getPropertyClassName();
153
154 $items = $propertyClassName::getList(
155 [
156 'select' => $select,
157 'filter' => $filter,
158 'order' => $order,
159 'offset' => $pageNavigation->getOffset(),
160 'limit' => $pageNavigation->getLimit(),
161 ]
162 )->fetchAll();
163
164 return new Page('PROPERTIES', $items, function() use ($filter)
165 {
167 $propertyClassName = $this->getPropertyClassName();
168
169 return (int) $propertyClassName::getList([
170 'select' => ['CNT'],
171 'filter' => $filter,
172 'runtime' => [
173 new ExpressionField('CNT', 'COUNT(ID)')
174 ]
175 ])->fetch()['CNT'];
176 });
177 }
178
182 protected function getPropertyClassName(): string
183 {
184 return \Bitrix\Sale\Property::class;
185 }
186
190 protected function getEntityType(): string
191 {
192 return Registry::ENTITY_ORDER;
193 }
194
195 public function addAction($fields)
196 {
197 $fields = self::prepareFields($fields);
198 if (isset($fields['SETTINGS']) && is_array($fields['SETTINGS']))
199 {
200 $fields = array_merge($fields, $fields['SETTINGS']);
201 unset($fields['SETTINGS']);
202 }
203
204 $r = $this->checkFileds($fields);
205
206 if(!isset($fields['PERSON_TYPE_ID']) || trim($fields['PERSON_TYPE_ID'])=='')
207 $r->addError(new Error('person type id is empty', 200850000005));
208
209 if($r->isSuccess())
210 {
211 $this->personTypeId = $fields['PERSON_TYPE_ID'];
212
213 $this->property = $fields;
215 if ($this->validateProperty())
216 {
217 $this->saveProperty();
218 }
219
220 foreach ($this->errors as $error)
221 {
222 $r->addError(new Error($error, 200850000006));
223 }
224 }
225
226 if(!$r->isSuccess())
227 {
228 $this->addErrors($r->getErrors());
229 return null;
230 }
231 else
232 {
233 return ['PROPERTY'=>$this->get($this->property['ID'])];
234 }
235 }
236 //endregion
237
238 protected function checkFileds($fields)
239 {
240 $r = new Result();
241
242 if(isset($fields['MULTIPLE']) && $fields['MULTIPLE'] == 'Y')
243 {
244 if(isset($fields['IS_FILTERED']) == false)
245 {
246 $r->addError(new Error('Require fields: isFiltered.' , 200850000009));
247 }
248 elseif($fields['IS_FILTERED'] <> 'N')
249 {
250 $r->addError(new Error('Allowed values: isFiltered - [N]', 200850000010));
251 }
252 }
253
254 if ($fields['TYPE'] == \Bitrix\Sale\Rest\Entity\Property::PROPERTY_TYPE_LOCATION)
255 {
256 if(isset($fields['IS_LOCATION']) && $fields['IS_LOCATION'] == 'Y')
257 {
258 if(isset($fields['MULTIPLE']) == false)
259 {
260 $r->addError(new Error('Require fields: multiple.', 200850000011));
261 }
262 elseif ($fields['MULTIPLE'] <> 'N')
263 {
264 $r->addError(new Error('Allowed values: multiple - [N]', 200850000012));
265 }
266 }
267
268 if(isset($fields['IS_LOCATION4TAX']) && $fields['IS_LOCATION4TAX'] == 'Y')
269 {
270 if(isset($fields['MULTIPLE']) == false)
271 {
272 $r->addError(new Error('Require fields: multiple.', 200850000013));
273 }
274 elseif ($fields['MULTIPLE'] <> 'N')
275 {
276 $r->addError(new Error('Allowed values: multiple - [N]', 200850000014));
277 }
278 }
279 }
280
281 if ($fields['TYPE'] == \Bitrix\Sale\Rest\Entity\Property::PROPERTY_TYPE_STRING)
282 {
283 if(isset($fields['IS_PROFILE_NAME']) && $fields['IS_PROFILE_NAME'] == 'Y')
284 {
285 if(isset($fields['REQUIRED']) == false)
286 {
287 $r->addError(new Error('Require fields: require.', 200850000015));
288 }
289 elseif ($fields['REQUIRED'] <> 'Y')
290 {
291 $r->addError(new Error('Allowed values: require - [Y]', 200850000016));
292 }
293 }
294 }
295
296 return $r;
297 }
298
299 public function getTypes()
300 {
301 $r =[];
302 foreach(array_keys(Manager::getTypes()) as $type)
303 {
304 $fields = self::getCommonFields();
305
306 $fields += Manager::getCommonSettings(['TYPE'=>$type], null );
307
308 $fields['MULTIPLE']['DESCRIPTION'] = Loc::getMessage('MULTIPLE_DESCRIPTION');
309
310 $fields['DEFAULT_VALUE']=[
311 'REQUIRED'=>'N',
312 'DESCRIPTION'=>null,
313 'LABEL'=>Loc::getMessage('F_DEFAULT_VALUE')
314 ];
315
316 $fields += $this->getInputSettings(['TYPE'=>$type]);
317
318 if ($type === 'STRING')
319 {
320 $fields += $this->getStringSettings();
321 }
322 elseif ($type === 'LOCATION')
323 {
324 $fields += $this->getLocationSettings();
325 }
326 elseif ($type === 'ADDRESS')
327 {
328 $fields += $this->getAddressSettings();
329 }
330
331 foreach ($fields as $code=>&$v)
332 {
333 foreach ($v as $name=>$value)
334 {
335 if(in_array($name, ['ONCHANGE','ONCLICK', 'OPTIONS']))
336 {
337 unset($v[$name]);
338 }
339 }
340 }
341
342 $r[$type] = $fields;
343 }
344
345 return ['ENUM'=>$r];
346 }
347
348 protected function exists($id)
349 {
350 $r = new Result();
351 if($this->get($id)['ID']<=0)
352 $r->addError(new Error('property is not exists', 200840400001));
353
354 return $r;
355 }
356
357 protected function get($id)
358 {
359 return $fields = $this->loadProperty($id);
360 }
361
362 protected function getPropertyGroupOptions()
363 {
364 $groupOptions = [];
365 $orderPropsGroup = new \CSaleOrderPropsGroup();
366
367 $result = $orderPropsGroup->GetList(['NAME' => 'ASC'], ['PERSON_TYPE_ID' => $this->personTypeId]);
368 while ($row = $result->Fetch())
369 {
370 $groupOptions[$row['ID']] = $row['NAME'];
371 }
372
373 return $groupOptions;
374 }
375
376 protected function getPersonType($personTypeId)
377 {
378 $personTypeList = \Bitrix\Sale\PersonType::load($this->siteId, $personTypeId);
379
380 return isset($personTypeList[$personTypeId]) ? $personTypeList[$personTypeId] : null;
381 }
382
383 static protected function getCommonFields()
384 {
385 return [
386 'ID' => [
387 'TYPE' => 'NUMBER',
388 'LABEL' => 'ID',
389 'MIN' => 0,
390 'STEP' => 1,
391 'HIDDEN' => 'Y',
392 ],
393 'PERSON_TYPE_ID' => [
394 'TYPE' => 'NUMBER',
395 'LABEL' => Loc::getMessage('SALE_PERS_TYPE'),
396 'MIN' => 0,
397 'STEP' => 1,
398 'HIDDEN' => 'Y',
399 'REQUIRED' => 'Y'
400 ],
401 'PROPS_GROUP_ID' => [
402 'TYPE' => 'ENUM',
403 'LABEL' => Loc::getMessage('F_PROPS_GROUP_ID'),
404 ],
405 'NAME' => [
406 'TYPE' => 'STRING',
407 'LABEL' => Loc::getMessage('F_NAME'),
408 'MAXLENGTH' => 255,
409 'REQUIRED' => 'Y'
410 ],
411 'CODE' => [
412 'TYPE' => 'STRING',
413 'LABEL' => Loc::getMessage('F_CODE'),
414 'MAXLENGTH' => 50
415 ],
416 'ACTIVE' => [
417 'TYPE' => 'Y/N' ,
418 'LABEL' => Loc::getMessage('F_ACTIVE'),
419 'VALUE' => 'Y'
420 ],
421 'UTIL' => [
422 'TYPE' => 'Y/N',
423 'LABEL' => Loc::getMessage('F_UTIL')
424 ],
425 'USER_PROPS' => [
426 'TYPE' => 'Y/N',
427 'LABEL' => Loc::getMessage('F_USER_PROPS')
428 ],
429 'IS_FILTERED' => [
430 'TYPE' => 'Y/N',
431 'LABEL' => Loc::getMessage('F_IS_FILTERED'),
432 'DESCRIPTION' => Loc::getMessage('MULTIPLE_DESCRIPTION')
433 ],
434 'SORT' => [
435 'TYPE' => 'NUMBER',
436 'LABEL' => Loc::getMessage('F_SORT'),
437 'MIN' => 0,
438 'STEP' => 1,
439 'VALUE' => 100
440 ],
441 'DESCRIPTION' => [
442 'TYPE' => 'STRING',
443 'LABEL' => Loc::getMessage('F_DESCRIPTION'),
444 'MULTILINE' => 'Y',
445 'ROWS' => 3,
446 'COLS' => 40
447 ]
448 ];
449 }
450
451 protected function getCommonSettings()
452 {
453 $personType = $this->getPersonType($this->personTypeId);
454 $groupOptions = $this->getPropertyGroupOptions();
455
456 $commonSettings = [
457 'PERSON_TYPE_ID' => [
458 'TYPE' => 'NUMBER',
459 'LABEL' => Loc::getMessage('SALE_PERS_TYPE'),
460 'MIN' => 0,
461 'STEP' => 1,
462 'HIDDEN' => 'Y',
463 'REQUIRED' => 'Y',
464 'RLABEL' => $personType['NAME']
465 ],
466 'PROPS_GROUP_ID' => [
467 'TYPE' => 'ENUM',
468 'LABEL' => Loc::getMessage('F_PROPS_GROUP_ID'),
469 'OPTIONS' => $groupOptions
470 ],
471 'NAME' => [
472 'TYPE' => 'STRING',
473 'LABEL' => Loc::getMessage('F_NAME'),
474 'MAXLENGTH' => 255,
475 'REQUIRED' => 'Y'
476 ],
477 'CODE' => [
478 'TYPE' => 'STRING',
479 'LABEL' => Loc::getMessage('F_CODE'),
480 'MAXLENGTH' => 50
481 ],
482 'ACTIVE' => [
483 'TYPE' => 'Y/N' ,
484 'LABEL' => Loc::getMessage('F_ACTIVE'),
485 'VALUE' => 'Y'
486 ],
487 'UTIL' => [
488 'TYPE' => 'Y/N',
489 'LABEL' => Loc::getMessage('F_UTIL')
490 ],
491 'USER_PROPS' => [
492 'TYPE' => 'Y/N',
493 'LABEL' => Loc::getMessage('F_USER_PROPS')
494 ],
495 'IS_FILTERED' => [
496 'TYPE' => 'Y/N',
497 'LABEL' => Loc::getMessage('F_IS_FILTERED'),
498 'DESCRIPTION' => Loc::getMessage('MULTIPLE_DESCRIPTION')
499 ],
500 'SORT' => [
501 'TYPE' => 'NUMBER',
502 'LABEL' => Loc::getMessage('F_SORT'),
503 'MIN' => 0,
504 'STEP' => 1,
505 'VALUE' => 100
506 ],
507 'DESCRIPTION' => [
508 'TYPE' => 'STRING',
509 'LABEL' => Loc::getMessage('F_DESCRIPTION'),
510 'MULTILINE' => 'Y',
511 'ROWS' => 3,
512 'COLS' => 40
513 ],
514 'XML_ID' => [
515 'TYPE' => 'STRING',
516 'LABEL' => 'XML_ID',
517 'MAXLENGTH' => 255,
518 ],
519 ];
520
521 if (!empty($this->property['ID']))
522 {
523 $commonSettings = array_merge(
524 [
525 'ID' => [
526 'TYPE' => 'NUMBER',
527 'LABEL' => 'ID',
528 'MIN' => 0,
529 'STEP' => 1,
530 'HIDDEN' => 'Y',
531 'RLABEL' => $this->property['ID']
532 ]
533 ],
534 $commonSettings
535 );
536 }
537
538 $commonSettings += Manager::getCommonSettings($this->property, null );
539
540 if (!empty($commonSettings['TYPE']['OPTIONS']))
541 {
542 foreach ($commonSettings['TYPE']['OPTIONS'] as $key => $option)
543 {
544 $commonSettings['TYPE']['OPTIONS'][$key] = mb_substr($option, 0, mb_strpos($option, '[') - 1);
545 }
546 }
547
548 /*if (!$this->checkMultipleField($this->property))
549 {
550 $commonSettings['MULTIPLE']['DISABLED'] = 'Y';
551 $commonSettings['MULTIPLE']['NO_DISPLAY'] = 'Y';
552 unset($commonSettings['IS_FILTERED']['DESCRIPTION']);
553 }*/
554
555 $commonSettings['MULTIPLE']['DESCRIPTION'] = Loc::getMessage('MULTIPLE_DESCRIPTION');
556 unset($commonSettings['VALUE']);
557
558 $commonSettings['DEFAULT_VALUE'] = array(
559 'REQUIRED' => 'N',
560 'DESCRIPTION' => null,
561 'VALUE' => $this->property['DEFAULT_VALUE'],
562 'LABEL' => Loc::getMessage('F_DEFAULT_VALUE'),
563 ) + $this->property;
564
565 if ($this->property['TYPE'] === 'ENUM')
566 {
567 $defaultOptions = $this->property['MULTIPLE'] === 'Y'
568 ? []
569 : ['' => Loc::getMessage('NO_DEFAULT_VALUE')];
570
571 if (!empty($this->property['VARIANTS']))
572 {
573 foreach ($this->property['VARIANTS'] as $row)
574 {
575 $defaultOptions[$row['VALUE']] = $row['NAME'];
576 }
577 }
578
579 $commonSettings['DEFAULT_VALUE']['OPTIONS'] = $defaultOptions;
580 }
581 elseif ($this->property['TYPE'] === 'LOCATION')
582 {
583 if ($this->property['IS_LOCATION'] === 'Y' || $this->property['IS_LOCATION4TAX'] === 'Y')
584 {
585 unset($commonSettings['MULTIPLE']);
586 }
587 }
588
589 return $commonSettings;
590 }
591
592 protected function getInputSettings($property)
593 {
594 return Manager::getSettings($property,null);
595 }
596
597 protected function getStringSettings()
598 {
599 return array(
600 'IS_PROFILE_NAME' => [
601 'TYPE' => 'Y/N',
602 'LABEL' => Loc::getMessage('F_IS_PROFILE_NAME'),
603 'DESCRIPTION' => Loc::getMessage('F_IS_PROFILE_NAME_DESCR')
604 ],
605 'IS_PAYER' => [
606 'TYPE' => 'Y/N',
607 'LABEL' => Loc::getMessage('F_IS_PAYER'),
608 'DESCRIPTION' => Loc::getMessage('F_IS_PAYER_DESCR')
609 ],
610 'IS_EMAIL' => [
611 'TYPE' => 'Y/N',
612 'LABEL' => Loc::getMessage('F_IS_EMAIL'),
613 'DESCRIPTION' => Loc::getMessage('F_IS_EMAIL_DESCR')
614 ],
615 'IS_PHONE' => [
616 'TYPE' => 'Y/N',
617 'LABEL' => Loc::getMessage('F_IS_PHONE'),
618 'DESCRIPTION' => Loc::getMessage('F_IS_PHONE_DESCR')
619 ],
620 'IS_ZIP' => [
621 'TYPE' => 'Y/N',
622 'LABEL' => Loc::getMessage('F_IS_ZIP'),
623 'DESCRIPTION' => Loc::getMessage('F_IS_ZIP_DESCR')
624 ],
625 'IS_ADDRESS' => [
626 'TYPE' => 'Y/N',
627 'LABEL' => Loc::getMessage('F_IS_ADDRESS'),
628 'DESCRIPTION' => Loc::getMessage('F_IS_ADDRESS_DESCR')
629 ],
630 );
631 }
632
633 protected function getLocationSettings()
634 {
635 $orderProps = new \CSaleOrderProps();
636
637 $locationOptions = ['' => Loc::getMessage('NULL_ANOTHER_LOCATION')];
638
639 $result = $orderProps->GetList([],
640 [
641 'PERSON_TYPE_ID' => $this->personTypeId,
642 'TYPE' => 'STRING',
643 'ACTIVE' => 'Y'
644 ],
645 false, false, ['ID', 'NAME']
646 );
647 while ($row = $result->Fetch())
648 {
649 $locationOptions[$row['ID']] = $row['NAME'];
650 }
651
652 return [
653 'IS_LOCATION' => [
654 'TYPE' => 'Y/N' ,
655 'LABEL' => Loc::getMessage('F_IS_LOCATION'),
656 'DESCRIPTION' => Loc::getMessage('F_IS_LOCATION_DESCR'),
657 'ONCLICK' => null
658 ],
659 'INPUT_FIELD_LOCATION' => [
660 'TYPE' => 'ENUM',
661 'LABEL' => Loc::getMessage('F_ANOTHER_LOCATION'),
662 'DESCRIPTION' => Loc::getMessage('F_INPUT_FIELD_DESCR'),
663 'OPTIONS' => $locationOptions,
664 'VALUE' => 0
665 ],
666 'IS_LOCATION4TAX' => [
667 'TYPE' => 'Y/N',
668 'LABEL' => Loc::getMessage('F_IS_LOCATION4TAX'),
669 'DESCRIPTION' => Loc::getMessage('F_IS_LOCATION4TAX_DESCR'),
670 'ONCLICK' => null
671 ],
672 ];
673 }
674
675 protected function getAddressSettings()
676 {
677 return array(
678 'IS_ADDRESS_FROM' => [
679 'TYPE' => 'Y/N',
680 'LABEL' => 'IS_ADDRESS_FROM',
681 'DESCRIPTION' => 'IS_ADDRESS_FROM'
682 ],
683 'IS_ADDRESS_TO' => [
684 'TYPE' => 'Y/N',
685 'LABEL' => 'IS_ADDRESS_TO',
686 'DESCRIPTION' => 'IS_ADDRESS_TO'
687 ],
688 );
689 }
690
691 protected function getVariantSettings()
692 {
693 return [
694 'VALUE' => [
695 'TYPE' => 'STRING', 'LABEL' => Loc::getMessage('SALE_VARIANTS_CODE'), 'SIZE' => '5', 'MAXLENGTH' => 255, 'REQUIRED' => 'Y'
696 ],
697 'NAME' => [
698 'TYPE' => 'STRING', 'LABEL' => Loc::getMessage('SALE_VARIANTS_NAME'), 'SIZE' => '20', 'MAXLENGTH' => 255, 'REQUIRED' => 'Y'
699 ],
700 'SORT' => [
701 'TYPE' => 'NUMBER', 'LABEL' => Loc::getMessage('SALE_VARIANTS_SORT'), 'MIN' => 0, 'STEP' => 1, 'VALUE' => 100
702 ],
703 'DESCRIPTION' => [
704 'TYPE' => 'STRING', 'LABEL' => Loc::getMessage('SALE_VARIANTS_DESCR'), 'SIZE' => '30', 'MAXLENGTH' => 255
705 ],
706 'ID' => [
707 'TYPE' => 'NUMBER', 'MIN' => 0, 'STEP' => 1, 'HIDDEN' => 'Y'
708 ],
709 'XML_ID' => [
710 'TYPE' => 'STRING', 'LABEL' => 'XML_ID', 'SIZE' => '20', 'MAXLENGTH' => 255
711 ],
712 ];
713 }
714
715 protected function getRelationSettings()
716 {
717 $paymentOptions = array();
718 $result = \CSalePaySystem::GetList(
719 array("SORT"=>"ASC", "NAME"=>"ASC"),
720 array("ACTIVE" => "Y"),
721 false,
722 false,
723 array("ID", "NAME", "ACTIVE", "SORT", "LID")
724 );
725 while ($row = $result->Fetch())
726 $paymentOptions[$row['ID']] = $row['NAME'] . ($row['LID'] ? " ({$row['LID']}) " : ' ') . "[{$row['ID']}]";
727
728 // delivery system options
729 $deliveryOptions = array();
730
731 foreach(\Bitrix\Sale\Delivery\Services\Manager::getActiveList(true) as $deliveryId => $deliveryFields)
732 {
733 $name = $deliveryFields["NAME"]." [".$deliveryId."]";
734 $sites = \Bitrix\Sale\Delivery\Restrictions\Manager::getSitesByServiceId($deliveryId);
735
736 if(!empty($sites))
737 $name .= " (".implode(", ", $sites).")";
738
739 $deliveryOptions[$deliveryId] = $name;
740 }
741
742 return [
743 'P' => ['TYPE' => 'ENUM', 'LABEL' => Loc::getMessage('SALE_PROPERTY_PAYSYSTEM'), 'OPTIONS' => $paymentOptions , 'MULTIPLE' => 'Y', 'SIZE' => '5'],
744 'D' => ['TYPE' => 'ENUM', 'LABEL' => Loc::getMessage('SALE_PROPERTY_DELIVERY' ), 'OPTIONS' => $deliveryOptions, 'MULTIPLE' => 'Y', 'SIZE' => '5'],
745 ];
746 }
747
748 protected function checkMultipleField($property)
749 {
750 return true;
751 }
752
753 protected function modifyInputSettingsByType(&$propertySettings)
754 {
755 if ($this->property['MULTIPLE'] === 'Y')
756 {
757 $propertySettings['IS_FILTERED']['DISABLED'] = 'Y';
758 }
759
760 if ($this->property['TYPE'] === 'STRING')
761 {
763 }
764 elseif ($this->property['TYPE'] === 'LOCATION')
765 {
767
768 if ($this->property['IS_LOCATION'] !== 'Y' || $this->property['MULTIPLE'] === 'Y')
769 {
770 unset($propertySettings['INPUT_FIELD_LOCATION']);
771 }
772 }
773 elseif ($this->property['TYPE'] === 'ADDRESS')
774 {
776 }
777 }
778
779 protected function initializePropertySettings()
780 {
781 if ($this->propertySettings === null)
782 {
783 $this->propertySettings = $this->getCommonSettings();
784 $this->propertySettings += $this->getInputSettings($this->property);
785
786 $this->modifyInputSettingsByType($this->propertySettings);
787 }
788 }
789
790 protected function validateFields()
791 {
792 foreach ($this->propertySettings as $name => $input)
793 {
794 if ($error = Manager::getError($input, $this->property[$name]))
795 {
796 if ($input['MULTIPLE'] && $input['MULTIPLE'] === 'Y')
797 {
798 $errorString = '';
799
800 foreach ($error as $k => $v)
801 {
802 $errorString .= ' '.(++$k).': '.implode(', ', $v).';';
803 }
804
805 $this->errors[] = $input['LABEL'].$errorString;
806 }
807 else
808 {
809 $this->errors[] = $input['LABEL'].': '.implode(', ', $error);
810 }
811 }
812 }
813 }
814
815 protected function validateVariants()
816 {
817 if (!empty($this->property))
818 {
819 $index = 0;
820 $variantSettings = $this->getVariantSettings();
821
822 foreach ($this->property['VARIANTS'] as $row)
823 {
824 ++$index;
825
826 if (isset($row['DELETE']) && $row['DELETE'] === 'Y')
827 {
828 unset($this->propertySettings['DEFAULT_VALUE']['OPTIONS'][$row['VALUE']]);
829 }
830 else
831 {
832 $hasError = false;
833
834 foreach ($variantSettings as $name => $input)
835 {
836 if ($error = Manager::getError($input, $row[$name]))
837 {
838 $this->errors[] = Loc::getMessage('INPUT_ENUM')." $index: ".$input['LABEL'].': '.implode(', ', $error);
839 $hasError = true;
840 }
841 }
842
843 if ($hasError)
844 {
845 unset($this->propertySettings['DEFAULT_VALUE']['OPTIONS'][$row['VALUE']]);
846 }
847 }
848 }
849 }
850 }
851
852 protected function validateRelations()
853 {
854 $relationsSettings = $this->getRelationSettings();
855
856 foreach ($relationsSettings as $name => $input)
857 {
858 if (($value = $this->property['RELATIONS'][$name]) && $value != array(''))
859 {
860 if ($error = Manager::getError($input, $value))
861 $errors [] = $input['LABEL'].': '.implode(', ', $error);
862 }
863 else
864 {
865 $relations[$name] = array();
866 }
867 }
868 }
869
870 protected function validateProperty()
871 {
873 $this->validateFields();
874
875 if ($this->property['TYPE'] === 'ENUM')
876 {
877 $this->validateVariants();
878 }
879
880 $this->validateRelations();
881
882 return !$this->hasErrors();
883 }
884
885 protected function hasErrors()
886 {
887 return !empty($this->errors);
888 }
889
890 protected function saveRelations()
891 {
892 $orderProps = new \CSaleOrderProps();
893 $relationsSettings = $this->getRelationSettings();
894
895 foreach ($relationsSettings as $name => $input)
896 {
897 $orderProps->UpdateOrderPropsRelations( $this->property['ID'], $this->property['RELATIONS'][$name], $name);
898 }
899 }
900
901 protected function saveProperty()
902 {
903 if ($this->property['TYPE'] === 'FILE')
904 {
905 $savedFiles = $this->saveFiles($this->property);
906 }
907 else
908 {
909 $savedFiles = [];
910 }
911
912 $propertiesToSave = [];
913
914 foreach ($this->propertySettings as $name => $input)
915 {
916 $inputValue = Manager::getValue($input, $this->property[$name]);
917
918 if ($name === 'DEFAULT_VALUE' || $inputValue !== null)
919 {
920 $propertiesToSave[$name] = $inputValue;
921 }
922 }
923
924 $inputSettings = $this->getInputSettings($this->property);
925 $propertiesToSave['SETTINGS'] = array_intersect_key($propertiesToSave, $inputSettings);
926 $propertiesToSave = array_diff_key($propertiesToSave, $propertiesToSave['SETTINGS']);
927
928 if (!empty($this->property['ID']))
929 {
930 $this->initializeDbProperty($this->property['ID']);
931 }
932
933 if (!empty($this->property['ID']))
934 {
935 $this->updateProperty($propertiesToSave);
936 }
937 else
938 {
939 $this->property['ID'] = $this->addProperty($propertiesToSave);
940 }
941
942 $this->cleanUpFiles($savedFiles);
943
944 if (!$this->hasErrors())
945 {
946 $this->saveVariants();
947 $this->saveRelations();
948 }
949 }
950
951 protected function saveFiles(&$property)
952 {
953 $savedFiles = array();
954
955 $files = File::asMultiple($property['DEFAULT_VALUE']);
956 foreach ($files as $i => $file)
957 {
958 if (File::isDeletedSingle($file))
959 {
960 unset($files[$i]);
961 }
962 else
963 {
964 if (
965 File::isUploadedSingle($file)
966 && ($fileId = \CFile::SaveFile(array('MODULE_ID' => 'sale') + $file, 'sale/order/properties/default'))
967 && is_numeric($fileId)
968 )
969 {
970 $file = $fileId;
971 $savedFiles[] = $fileId;
972 }
973
974 $files[$i] = File::loadInfoSingle($file);
975 }
976 }
977
978 $property['DEFAULT_VALUE'] = $files;
979
980 return $savedFiles;
981 }
982
983 protected function initializeDbProperty($propertyId)
984 {
985 if ($this->dbProperty === null)
986 {
987 $this->dbProperty = $this->loadProperty($propertyId);
988 }
989 }
990
991 protected function loadProperty($propertyId)
992 {
993 if (empty($propertyId))
994 {
995 return [];
996 }
997
998 $property = \Bitrix\Sale\Internals\OrderPropsTable::getRow([
999 'filter' => [
1000 '=ID' => $propertyId
1001 ]
1002 ]);
1003 if (!empty($property))
1004 {
1005 $property += $property['SETTINGS'];
1008 }
1009
1010 return $property;
1011 }
1012
1013 protected function modifyDataDependedByType($property)
1014 {
1015 $propsVariant = new \CSaleOrderPropsVariant();
1016 if (!empty($property))
1017 {
1018 switch ($property['TYPE'])
1019 {
1020 case 'ENUM':
1021 $variants = [];
1022
1023 $result = $propsVariant->GetList([], [
1024 'ORDER_PROPS_ID' => $property['ID']
1025 ]);
1026 while ($row = $result->Fetch())
1027 {
1028 $variants[] = $row;
1029 }
1030
1031 $property['VARIANTS'] = $variants;
1032 break;
1033 case 'FILE':
1034 $property['DEFAULT_VALUE'] = File::loadInfo($property['DEFAULT_VALUE']);
1035 break;
1036 }
1037 }
1038
1039 return $property;
1040 }
1041
1042 protected function modifyRelationsDataDependedByType($property)
1043 {
1044 $orderProps = new \CSaleOrderProps();
1045 $result = $orderProps->GetOrderPropsRelations(['PROPERTY_ID' => $property['ID']]);
1046 while ($row = $result->Fetch())
1047 {
1048 if(RelationType::isDefined(RelationType::resolveID($row['ENTITY_TYPE'])))
1049 {
1050 $property['RELATIONS'][$row['ENTITY_TYPE']][] = $row['ENTITY_ID'];
1051 }
1052 }
1053
1054 return $property;
1055 }
1056
1057 protected function updateProperty($propertiesToSave)
1058 {
1059 $update = \Bitrix\Sale\Internals\OrderPropsTable::update(
1060 $this->property['ID'],
1061 array_diff_key($propertiesToSave, array('ID' => 1))
1062 );
1063 if ($update->isSuccess())
1064 {
1065 $propertyCode = $propertiesToSave['CODE'] ?: false;
1066
1067 $result = OrderPropsValueTable::getList(['filter'=>[
1068 'ORDER_PROPS_ID' => $this->property['ID'],
1069 '!CODE' => $propertyCode,
1070 ]]);
1071 while ($row = $result->Fetch())
1072 {
1073 OrderPropsValueTable::update($row['ID'], ['CODE' => $propertyCode]);
1074 }
1075 }
1076 else
1077 {
1078 foreach ($update->getErrorMessages() as $errorMessage)
1079 {
1080 $this->errors[] = $errorMessage;
1081 }
1082 }
1083 }
1084
1085 protected function addProperty($propertiesToSave)
1086 {
1087 $propertyId = null;
1088
1089 if(!isset($propertiesToSave['XML_ID']) && $propertiesToSave['XML_ID'] == '')
1090 {
1091 $propertiesToSave['XML_ID'] = \Bitrix\Sale\Internals\OrderPropsTable::generateXmlId();
1092 }
1093
1094 $propertiesToSave['ENTITY_REGISTRY_TYPE'] = \Bitrix\Sale\Registry::REGISTRY_TYPE_ORDER;
1095 $propertiesToSave['ENTITY_TYPE'] = $this->getEntityType();
1096 //
1097 $addResult = \Bitrix\Sale\Internals\OrderPropsTable::add($propertiesToSave);
1098 if ($addResult->isSuccess())
1099 {
1100 $propertyId = $addResult->getId();
1101 }
1102 else
1103 {
1104 foreach ($addResult->getErrorMessages() as $errorMessage)
1105 {
1106 $this->errors[] = $errorMessage;
1107 }
1108 }
1109
1110 return $propertyId;
1111 }
1112
1113 protected function cleanUpFiles($savedFiles)
1114 {
1115 $filesToDelete = [];
1116
1117 if ($this->hasErrors())
1118 {
1119 if (!empty($savedFiles))
1120 {
1121 $filesToDelete = $savedFiles;
1122 }
1123 }
1124 else
1125 {
1126 if (!empty($this->dbProperty) && $this->dbProperty['TYPE'] === 'FILE')
1127 {
1128 $filesToDelete = File::asMultiple(File::getValue(
1129 $this->dbProperty, $this->dbProperty['DEFAULT_VALUE']
1130 ));
1131
1132 if (!empty($savedFiles))
1133 {
1134 $filesToDelete = array_diff(
1135 $filesToDelete,
1136 File::asMultiple(File::getValue($this->property, $this->property['DEFAULT_VALUE']))
1137 );
1138 }
1139 }
1140 }
1141
1142 foreach ($filesToDelete as $fileId)
1143 {
1144 if (is_numeric($fileId))
1145 {
1146 \CFile::Delete($fileId);
1147 }
1148 }
1149 }
1150
1151 protected function saveVariants()
1152 {
1153 $orderPropsVariant = new \CSaleOrderPropsVariant();
1154
1155 if ($this->property['TYPE'] === 'ENUM')
1156 {
1157 $index = 0;
1158 $variantSettings = $this->getVariantSettings();
1159
1160 foreach ($this->property['VARIANTS'] as $key => $row)
1161 {
1162 if (isset($row['DELETE']) && $row['DELETE'] === 'Y')
1163 {
1164 if ($row['ID'])
1165 {
1166 $orderPropsVariant->Delete($row['ID']);
1167 }
1168
1169 unset($this->property['VARIANTS'][$key]);
1170 }
1171 else
1172 {
1173 ++$index;
1174 $variantId = $row['ID'];
1175 $row = array_intersect_key($row, $variantSettings);
1176
1177 if ($variantId)
1178 {
1179 unset($row['ID']);
1180 if (!$orderPropsVariant->Update($variantId, $row))
1181 {
1182 $this->errors[] = Loc::getMessage('ERROR_EDIT_VARIANT')." $index";
1183 }
1184 }
1185 else
1186 {
1187 $row['ORDER_PROPS_ID'] = $this->property['ID'];
1188
1189 if ($variantId = $orderPropsVariant->Add($row))
1190 {
1191 $variants[$key]['ID'] = $variantId;
1192 }
1193 else
1194 {
1195 $this->errors[] = Loc::getMessage('ERROR_ADD_VARIANT')." $index";
1196 }
1197 }
1198 }
1199 }
1200 }
1201 // cleanup variants
1202 elseif (!empty($this->dbProperty) && $this->dbProperty['TYPE'] === 'ENUM')
1203 {
1204 \CSaleOrderPropsVariant::DeleteAll($this->dbProperty['ID']);
1205 }
1206 }
1207
1208 static public function prepareFields(array $fields)
1209 {
1210 $fields['TYPE'] = mb_strtoupper($fields['TYPE']);
1211
1212 return $fields;
1213 }
1214
1215 protected function checkPermissionEntity($name)
1216 {
1217 if($name == 'getfieldssettingsbytype'
1218 || $name == 'getfieldsbytype'
1219 ){
1220 $r = $this->checkReadPermissionEntity();
1221 }
1222 else
1223 {
1224 $r = parent::checkPermissionEntity($name);
1225 }
1226 return $r;
1227 }
1228}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
updateProperty($propertiesToSave)
updateAction($id, array $fields)
Definition property.php:57
static prepareFields(array $fields)
addProperty($propertiesToSave)
modifyRelationsDataDependedByType($property)
modifyInputSettingsByType(&$propertySettings)
Definition property.php:753