Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
section.php
1<?php
2
3
5
6
12
13final class Section extends Controller
14{
15 //region Actions
16 public function getFieldsAction(): array
17 {
18 return ['SECTION' => $this->getViewFields()];
19 }
20
28 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): ?Page
29 {
30 $r = $this->checkPermissionIBlockSectionList($filter['IBLOCK_ID']);
31 if($r->isSuccess())
32 {
33 $result = [];
34
35 $select = empty($select)? ['*']:$select;
36 $order = empty($order)? ['ID'=>'ASC']:$order;
37
38 if (isset($filter['IBLOCK_SECTION_ID']))
39 {
40 $filter['SECTION_ID'] = $filter['IBLOCK_SECTION_ID'];
41 unset($filter['IBLOCK_SECTION_ID']);
42 }
43
44 $r = \CIBlockSection::GetList($order, $filter, false, $select, self::getNavData($pageNavigation->getOffset()));
45 while ($l = $r->fetch())
46 $result[] = $l;
47
48 return new Page('SECTIONS', $result, function() use ($filter)
49 {
50 return \CIBlockSection::GetCount($filter);
51 });
52 }
53 else
54 {
55 $this->addErrors($r->getErrors());
56 return null;
57 }
58
59 }
60
61 public function getAction($id)
62 {
63 $r = $this->checkPermissionIBlockSectionGet($id);
64 if($r->isSuccess())
65 {
66 $r = $this->exists($id);
67 if($r->isSuccess())
68 {
69 return ['SECTION'=>$this->get($id)];
70 }
71 else
72 {
73 $this->addErrors($r->getErrors());
74 return null;
75 }
76 }
77 else
78 {
79 $this->addErrors($r->getErrors());
80 return null;
81 }
82 }
83
84 public function addAction($fields)
85 {
86 $r = $this->checkPermissionIBlockSectionAdd($fields['IBLOCK_ID']);
87 if($r->isSuccess())
88 {
89 if (isset($fields['IBLOCK_SECTION_ID']) && (int)$fields['IBLOCK_SECTION_ID'] > 0)
90 {
91 $r = $this->checkPermissionIBlockSectionSectionBindUpdate($fields['IBLOCK_SECTION_ID']);
92 }
93 }
94
95 if($r->isSuccess())
96 {
97 $id = 0;
98 $section = new \CIBlockSection();
99
100 $r = $this->addValidate($fields);
101 if($r->isSuccess())
102 {
103 $id = $section->Add($fields);
104 if($section->LAST_ERROR<>'')
105 {
106 $r->addError(new Error($section->LAST_ERROR));
107 }
108 }
109 }
110
111 if(!$r->isSuccess())
112 {
113 $this->addErrors($r->getErrors());
114 return null;
115 }
116 else
117 {
118 return ['SECTION'=>$this->get($id)];
119 }
120 }
121
122 public function updateAction($id, array $fields)
123 {
124 $r = $this->checkPermissionIBlockSectionUpdate($id);
125 if($r->isSuccess())
126 {
127 if (isset($fields['IBLOCK_SECTION_ID']) && (int)$fields['IBLOCK_SECTION_ID'] > 0)
128 {
129 $r = $this->checkPermissionIBlockSectionSectionBindUpdate($fields['IBLOCK_SECTION_ID']);
130 }
131 }
132
133 if($r->isSuccess())
134 {
135 $section = new \CIBlockSection();
136
137 $r = $this->exists($id);
138 if($r->isSuccess())
139 {
140 $r = $this->updateValidate($fields+['ID'=>$id]);
141 if($r->isSuccess())
142 {
143 $section->Update($id, $fields);
144 if($section->LAST_ERROR<>'')
145 {
146 $r->addError(new Error($section->LAST_ERROR));
147 }
148 }
149 }
150 }
151
152 if($r->isSuccess())
153 {
154 return ['SECTION'=>$this->get($id)];
155 }
156 else
157 {
158 $this->addErrors($r->getErrors());
159 return null;
160 }
161 }
162
163 public function deleteAction($id)
164 {
165 $r = $this->checkPermissionIBlockSectionDelete($id);
166 if($r->isSuccess())
167 {
168 $r = $this->exists($id);
169 if($r->isSuccess())
170 {
171 if (!\CIBlockSection::Delete($id))
172 {
173 if ($ex = self::getApplication()->GetException())
174 $r->addError(new Error($ex->GetString(), $ex->GetId()));
175 else
176 $r->addError(new Error('delete section error'));
177 }
178 }
179 }
180
181 if($r->isSuccess())
182 {
183 return true;
184 }
185 else
186 {
187 $this->addErrors($r->getErrors());
188 return null;
189 }
190 }
191 //endregion
192
193 protected function exists($id)
194 {
195 $r = new Result();
196 if(isset($this->get($id)['ID']) == false)
197 $r->addError(new Error('Section is not exists'));
198
199 return $r;
200 }
201
202 protected function get($id)
203 {
204 return \CIBlockSection::GetByID($id)->Fetch();
205 }
206
207 protected function addValidate($fields)
208 {
209 return new Result();
210 }
211
212 protected function updateValidate($fields)
213 {
214 return new Result();
215 }
216
217 //region checkPermissionController
218 protected function checkModifyPermissionEntity()
219 {
220 return $this->checkReadPermissionEntity();
221 }
222
223 protected function checkReadPermissionEntity()
224 {
225 $r = new Result();
226
227 if (
228 !$this->accessController->check(ActionDictionary::ACTION_CATALOG_READ)
229 && !$this->accessController->check(ActionDictionary::ACTION_CATALOG_VIEW)
230 )
231 {
232 $r->addError(new Error('Access Denied', 200040300010));
233 }
234 return $r;
235 }
236 //endregion
237
238 //region checkPermissionIBlock
239 protected function checkPermissionIBlockSectionAdd($iblockId)
240 {
241 return $this->checkPermissionIBlockSectionModify($iblockId, 0);
242 }
243
244 protected function checkPermissionIBlockSectionUpdate($sectionId)
245 {
246 $iblockId = $this->getIBlockBySectionId($sectionId);
247 return $this->checkPermissionIBlockSectionModify($iblockId, $sectionId);
248 }
249
250 protected function checkPermissionIBlockSectionModify($iblockId, $sectionId)
251 {
252 $r = new Result();
253
254 $arIBlock = \CIBlock::GetArrayByID($iblockId);
255 if($arIBlock)
256 $bBadBlock = !\CIBlockSectionRights::UserHasRightTo($iblockId, $sectionId, self::IBLOCK_EDIT);
257 else
258 $bBadBlock = true;
259
260 if($bBadBlock)
261 {
262 $r->addError(new Error('Access Denied', 200040300040));
263 }
264 return $r;
265 }
266
267 protected function checkPermissionIBlockSectionSectionBindModify($iblockId, $iblockSectionId)
268 {
269 $r = new Result();
270
271 $arIBlock = \CIBlock::GetArrayByID($iblockId);
272 if($arIBlock)
273 $bBadBlock = !\CIBlockSectionRights::UserHasRightTo($iblockId, $iblockSectionId, self::IBLOCK_SECTION_SECTION_BIND); //access update
274 else
275 $bBadBlock = true;
276
277 if($bBadBlock)
278 {
279 $r->addError(new Error('Access Denied', 200040300050));
280 }
281 return $r;
282 }
283
284 protected function checkPermissionIBlockSectionSectionBindUpdate($iblockSectionId)
285 {
286 $iblockId = $this->getIBlockBySectionId($iblockSectionId);
287 return $this->checkPermissionIBlockSectionModify($iblockId, $iblockSectionId);
288 }
289
290 protected function checkPermissionIBlockSectionDelete($sectionId)
291 {
292 $r = new Result();
293 $iblockId = \CIBlockElement::GetIBlockByID($sectionId);
294 $bBadBlock = !\CIBlockElementRights::UserHasRightTo($iblockId, $sectionId, self::IBLOCK_SECTION_DELETE); //access delete
295
296 if($bBadBlock)
297 {
298 $r->addError(new Error('Access Denied', 200040300050));
299 }
300
301 return $r;
302 }
303
304 protected function checkPermissionIBlockSectionGet($sectionId)
305 {
306 $r = new Result();
307
308 $iblockId = $this->getIBlockBySectionId($sectionId);
309 $arIBlock = \CIBlock::GetArrayByID($iblockId);
310
311 if($arIBlock)
312 $bBadBlock = !\CIBlockSectionRights::UserHasRightTo($iblockId, $sectionId, self::IBLOCK_SECTION_READ);
313 else
314 $bBadBlock = true;
315
316 if($bBadBlock)
317 {
318 $r->addError(new Error('Access Denied', 200040300040));
319 }
320 return $r;
321 }
322
323 protected function checkPermissionIBlockSectionList($iblockId)
324 {
325 $r = new Result();
326
327 $arIBlock = \CIBlock::GetArrayByID($iblockId);
328 if($arIBlock)
329 $bBadBlock = !\CIBlockRights::UserHasRightTo($iblockId, $iblockId, self::IBLOCK_READ);
330 else
331 $bBadBlock = true;
332
333 if($bBadBlock)
334 {
335 $r->addError(new Error('Access Denied', 200040300030));
336 }
337 return $r;
338 }
339
340 protected function getIBlockBySectionId($id)
341 {
342 $iblockId = 0;
343
344 $section = \CIBlockSection::GetByID($id);
345 if ($res = $section->GetNext())
346 {
347 $iblockId = $res["IBLOCK_ID"];
348 }
349
350 return $iblockId;
351 }
352 //endregion
353}
checkPermissionIBlockSectionSectionBindUpdate($iblockSectionId)
Definition section.php:284
checkPermissionIBlockSectionGet($sectionId)
Definition section.php:304
checkPermissionIBlockSectionUpdate($sectionId)
Definition section.php:244
updateAction($id, array $fields)
Definition section.php:122
checkPermissionIBlockSectionList($iblockId)
Definition section.php:323
checkPermissionIBlockSectionDelete($sectionId)
Definition section.php:290
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
Definition section.php:28
checkPermissionIBlockSectionSectionBindModify($iblockId, $iblockSectionId)
Definition section.php:267
checkPermissionIBlockSectionModify($iblockId, $sectionId)
Definition section.php:250