1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
measure.php
См. документацию.
1<?php
2
3namespace Bitrix\Catalog\Controller;
4
5use Bitrix\Catalog\Access\ActionDictionary;
6use Bitrix\Catalog\MeasureTable;
7use Bitrix\Main\Error;
8use Bitrix\Main\ORM\Data\DataManager;
9use Bitrix\Main\Result;
10use Bitrix\Rest\Event\EventBindInterface;
11
12final class Measure extends Controller implements EventBindInterface
13{
14 use ListAction; // default listAction realization
15 use GetAction; // default getAction realization
16 use CheckExists; // default implementation of existence check
17
18 //region Actions
22 public function getFieldsAction(): array
23 {
24 return [$this->getServiceItemName() => $this->getViewFields()];
25 }
26
35 public function addAction(array $fields): ?array
36 {
37 $r = $this->existsByFilter([
38 '=CODE' => $fields['CODE'],
39 ]);
40 if ($r->isSuccess() === false)
41 {
42 $r = $this->checkDefaultValue($fields);
43 if ($r->isSuccess())
44 {
45 $r = parent::add($fields);
46 if ($r->isSuccess())
47 {
48 return [$this->getServiceItemName() => $this->get($r->getPrimary())];
49 }
50 }
51 }
52 else
53 {
54 $r->addError($this->getErrorDublicateFieldCode());
55 }
56
57 $this->addErrors($r->getErrors());
58
59 return null;
60 }
61
71 public function updateAction(int $id, array $fields): ?array
72 {
73 $existsResult = $this->exists($id);
74 if (!$existsResult->isSuccess())
75 {
76 $this->addErrors($existsResult->getErrors());
77
78 return null;
79 }
80
81 $r = $this->checkMeasureBeforeUpdate($id, $fields);
82 if ($r->isSuccess())
83 {
84 $r = parent::update($id, $fields);
85 if ($r->isSuccess())
86 {
87 return [$this->getServiceItemName() => $this->get($id)];
88 }
89 }
90
91 $this->addErrors($r->getErrors());
92
93 return null;
94 }
95
104 public function deleteAction(int $id): ?bool
105 {
106 $existsResult = $this->exists($id);
107 if (!$existsResult->isSuccess())
108 {
109 $this->addErrors($existsResult->getErrors());
110
111 return null;
112 }
113
114 $r = parent::delete($id);
115 if ($r->isSuccess())
116 {
117 return true;
118 }
119 else
120 {
121 $this->addErrors($r->getErrors());
122
123 return null;
124 }
125 }
126
131
136 //endregion
137
139 {
140 $r = new Result();
141
147 $fields['IS_DEFAULT'] = $fields['IS_DEFAULT'] ?? 'N';
148
149 if ($fields['IS_DEFAULT'] === 'Y')
150 {
151 $exist = $this->existsByFilter([
152 '=IS_DEFAULT' => $fields['IS_DEFAULT'],
153 ]);
154 if ($exist->isSuccess())
155 {
156 $r->addError(new Error('default value can be set once [isDefault]', 200600000010));
157 }
158 }
159
160 return $r;
161 }
162
163 protected function getEntityTable(): DataManager
164 {
165 return new MeasureTable();
166 }
167
168 protected function checkModifyPermissionEntity()
169 {
170 $r = new Result();
171
172 if (!$this->accessController->check(ActionDictionary::ACTION_STORE_VIEW))
173 {
174 $r->addError($this->getErrorModifyAccessDenied());
175 }
176
177 return $r;
178 }
179
180 protected function checkReadPermissionEntity()
181 {
182 $r = new Result();
183
184 if (
185 !(
186 $this->accessController->check(ActionDictionary::ACTION_CATALOG_READ)
187 || $this->accessController->check(ActionDictionary::ACTION_STORE_VIEW)
188 )
189 )
190 {
191 $r->addError($this->getErrorReadAccessDenied());
192 }
193 return $r;
194 }
195
196 protected function checkMeasureBeforeUpdate(int $id, array $fields): Result
197 {
198 if (isset($fields['CODE']))
199 {
200 $existsResult = $this->existsByFilter([
201 '!=ID' => $id,
202 '=CODE' => $fields['CODE'],
203 ]);
204 if ($existsResult->isSuccess())
205 {
206 $result = new Result();
207 $result->addError($this->getErrorDublicateFieldCode());
208
209 return $result;
210 }
211 }
212
213 return $this->checkDefaultValue($fields);
214 }
215
216 private function getErrorDublicateFieldCode(): Error
217 {
218 return new Error('Duplicate entry for key [code]', 200600000000);
219 }
220
221 protected function getErrorCodeEntityNotExists(): string
222 {
224 }
225}
const MEASURE_ENTITY_NOT_EXISTS
Определения errorcode.php:20
checkModifyPermissionEntity()
Определения measure.php:168
checkReadPermissionEntity()
Определения measure.php:180
getErrorCodeEntityNotExists()
Определения measure.php:221
addAction(array $fields)
Определения measure.php:35
deleteAction(int $id)
Определения measure.php:104
updateAction(int $id, array $fields)
Определения measure.php:71
checkDefaultValue(array $fields)
Определения measure.php:138
checkMeasureBeforeUpdate(int $id, array $fields)
Определения measure.php:196
Определения error.php:15
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
exists($id)
Определения checkexists.php:30
trait CheckExists
Определения checkexists.php:8
trait GetAction
Определения getaction.php:8
trait ListAction
Определения listaction.php:9
$fields
Определения yandex_run.php:501