Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
numeratorrequestmanager.php
1<?php
3
4use Bitrix\Main\Entity\ReferenceField;
12
18{
19 /*** @var \Bitrix\Main\HttpRequest */
20 private $request;
21
26 public function __construct($request)
27 {
28 $this->request = $request;
29 }
30
39 public function saveFromRequest()
40 {
41 $numeratorConfig = $this->request->getPost(Numerator::getType());
42 $id = $numeratorConfig ['id'];
43 if ($id)
44 {
45 $result = $this->updateNextSequentialNumber($id);
46 if ($result->isSuccess())
47 {
48 $result = Numerator::update($id, $this->request->getPostList()->toArray());
49 }
50 }
51 else
52 {
53 $numerator = Numerator::create();
54 $result = $numerator->setConfig($this->request->getPostList()->toArray());
55 if ($result->isSuccess())
56 {
57 $result = $numerator->save();
58 }
59 }
60 return $result;
61 }
62
70 private function updateNextSequentialNumber($id)
71 {
72 $sequenceConfig = $this->request->getPost(SequentNumberGenerator::getType());
73 $result = new Result();
74 if ($sequenceConfig !== null && is_array($sequenceConfig)
75 && array_key_exists('nextNumberForSequence', $sequenceConfig) && $sequenceConfig['nextNumberForSequence'])
76 {
77 $nextNumber = $sequenceConfig['nextNumberForSequence'];
78 if (is_numeric($nextNumber))
79 {
80 $sequence = NumeratorTable::query()
81 ->registerRuntimeField('',
82 new ReferenceField(
83 'ref',
84 NumeratorSequenceTable::class,
85 ['=this.ID' => 'ref.NUMERATOR_ID']
86 )
87 )
88 ->addSelect(('ID'))
89 ->addSelect('ref.NEXT_NUMBER', 'NEXT_NUMBER')
90 ->addSelect('ref.TEXT_KEY', 'TEXT_KEY')
91 ->where('ID', $id)
92 ->exec()
93 ->fetchAll();
94
95 if ($sequence && count($sequence) == 1)
96 {
97 $numerator = Numerator::load($id);
98 if ($numerator)
99 {
100 $res = $numerator->setNextSequentialNumber($nextNumber, $sequence[0]['NEXT_NUMBER'], $sequence[0]['TEXT_KEY']);
101 if (!$res->isSuccess())
102 {
103 $errors = $res->getErrors();
104 return $result->addError($errors[0]);
105 }
106 }
107 }
108 }
109 else
110 {
111 return $result->addError(new Error(Loc::getMessage('MAIN_NUMERATOR_EDIT_ERROR_NUMBER_NOT_NUMERIC')));
112 }
113 }
114 return $result;
115 }
116}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static update($numId, $config)
static load($numeratorId, $source=null)