1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
section.php
См. документацию.
1<?php
2
3namespace Bitrix\Catalog\Controller;
4
5use Bitrix\Catalog\Access\ActionDictionary;
6use Bitrix\Iblock\SectionTable;
7use Bitrix\Main\Application;
8use Bitrix\Main\DB\SqlQueryException;
9use Bitrix\Main\Engine\Response\DataType\Page;
10use Bitrix\Main\Error;
11use Bitrix\Main\ORM\Data\DataManager;
12use Bitrix\Main\Result;
13use Bitrix\Main\UI\PageNavigation;
14
15final class Section extends Controller
16{
17 //region Actions
18 public function getFieldsAction(): array
19 {
20 return [$this->getServiceItemName() => $this->getViewFields()];
21 }
22
31 public function listAction(
32 PageNavigation $pageNavigation,
33 array $select = [],
34 array $filter = [],
35 array $order = [],
36 bool $__calculateTotalCount = true
37 ): ?Page
38 {
39 $r = $this->checkPermissionIBlockSectionList($filter['IBLOCK_ID']);
40 if ($r->isSuccess())
41 {
42 $result = [];
43
44 $select = empty($select) ? ['*'] : $select;
45 $order = empty($order) ? ['ID'=>'ASC'] : $order;
46
47 if (isset($filter['IBLOCK_SECTION_ID']))
48 {
49 $filter['SECTION_ID'] = $filter['IBLOCK_SECTION_ID'];
50 unset($filter['IBLOCK_SECTION_ID']);
51 }
52
53 $r = \CIBlockSection::GetList(
54 $order,
55 $filter,
56 false,
57 $select,
58 self::getNavData($pageNavigation->getOffset())
59 );
60 while ($l = $r->fetch())
61 {
62 $result[] = $l;
63 }
64 unset($l, $r);
65
66 return new Page(
67 $this->getServiceListName(),
68 $result,
69 $__calculateTotalCount ? $this->getCount($filter) : 0);
70 }
71 else
72 {
73 $this->addErrors($r->getErrors());
74
75 return null;
76 }
77
78 }
79
80 public function getAction($id)
81 {
83 if (!$result->isSuccess())
84 {
85 $this->addErrors($result->getErrors());
86
87 return null;
88 }
89
90 $row = $this->get($id);
91 if (!is_array($row))
92 {
94
95 return null;
96 }
97
98 return [
99 $this->getServiceItemName() => $row,
100 ];
101 }
102
103 public function addAction($fields)
104 {
105 $r = $this->checkPermissionIBlockSectionAdd($fields['IBLOCK_ID']);
106
107 $id = 0;
108 if ($r->isSuccess())
109 {
110 if (isset($fields['IBLOCK_SECTION_ID']) && (int)$fields['IBLOCK_SECTION_ID'] > 0)
111 {
112 $r = $this->checkPermissionIBlockSectionSectionBindUpdate($fields['IBLOCK_SECTION_ID']);
113
114 if (!$r->isSuccess())
115 {
116 $this->addErrors($r->getErrors());
117
118 return null;
119 }
120 }
121
122 $r = $this->addValidate($fields);
123
124 if ($r->isSuccess())
125 {
126 $section = new \CIBlockSection();
127 $connection = Application::getConnection();
128 $connection->startTransaction();
129 try
130 {
131 $id = $section->Add($fields);
132 $error = $section->getLastError();
133 }
134 catch (SqlQueryException)
135 {
136 $error = 'Internal error adding section. Try adding again.';
137 }
138 if ($error !== '')
139 {
140 $connection->rollbackTransaction();
141 $r->addError(new Error($error, 200700300000));
142 }
143 else
144 {
145 $connection->commitTransaction();
146 }
147 unset(
148 $error,
149 $section,
150 );
151 }
152 }
153
154 if ($r->isSuccess())
155 {
156 return [$this->getServiceItemName() => $this->get($id)];
157 }
158 else
159 {
160 $this->addErrors($r->getErrors());
161
162 return null;
163 }
164 }
165
166 public function updateAction($id, array $fields)
167 {
168 $r = $this->exists($id);
169
170 if ($r->isSuccess())
171 {
172 $r = $this->checkPermissionIBlockSectionUpdate($id);
173
174 if (!$r->isSuccess())
175 {
176 $this->addErrors($r->getErrors());
177
178 return null;
179 }
180
181 if (isset($fields['IBLOCK_SECTION_ID']) && (int)$fields['IBLOCK_SECTION_ID'] > 0)
182 {
183 $r = $this->checkPermissionIBlockSectionSectionBindUpdate($fields['IBLOCK_SECTION_ID']);
184
185 if (!$r->isSuccess())
186 {
187 $this->addErrors($r->getErrors());
188
189 return null;
190 }
191 }
192
193 $r = $this->updateValidate($fields + ['ID' => $id]);
194
195 if ($r->isSuccess())
196 {
197 $section = new \CIBlockSection();
198 $connection = Application::getConnection();
199 $connection->startTransaction();
200 try
201 {
202 $section->Update($id, $fields);
203 $error = $section->getLastError();
204 }
205 catch (SqlQueryException)
206 {
207 $error = 'Internal error updating section. Try updating again.';
208 }
209 if ($error !== '')
210 {
211 $connection->rollbackTransaction();
212 $r->addError(new Error($error, 200700300010));
213 }
214 else
215 {
216 $connection->commitTransaction();
217 }
218 unset(
219 $error,
220 $section,
221 );
222 }
223 }
224
225 if ($r->isSuccess())
226 {
227 return [$this->getServiceItemName() => $this->get($id)];
228 }
229 else
230 {
231 $this->addErrors($r->getErrors());
232
233 return null;
234 }
235 }
236
237 public function deleteAction($id)
238 {
239 $r = $this->checkPermissionIBlockSectionDelete($id);
240
241 if ($r->isSuccess())
242 {
243 $r = $this->exists($id);
244
245 if ($r->isSuccess())
246 {
247 $connection = Application::getConnection();
248 $connection->startTransaction();
249 try
250 {
251 if (!\CIBlockSection::Delete($id))
252 {
253 if ($ex = self::getApplication()->GetException())
254 {
255 $r->addError(new Error($ex->GetString(), $ex->GetId()));
256 }
257 else
258 {
259 $r->addError(new Error('delete section error', 200700300020));
260 }
261 }
262 }
263 catch (SqlQueryException)
264 {
265 $r->addError(new Error('Internal error deleting section. Try deleting again.', 200700300020));
266 }
267 if ($r->isSuccess())
268 {
269 $connection->commitTransaction();
270 }
271 else
272 {
273 $connection->rollbackTransaction();
274 }
275 }
276 }
277
278 if ($r->isSuccess())
279 {
280 return true;
281 }
282 else
283 {
284 $this->addErrors($r->getErrors());
285
286 return null;
287 }
288 }
289 //endregion
290
291 protected function exists($id)
292 {
293 $result = new Result();
294
295 $iterator = \CIBlockSection::GetList(
296 [],
297 ['ID' => (int)$id],
298 false,
299 ['ID']
300 );
301 $row = $iterator->Fetch();
302 unset($iterator);
303 if (empty($row))
304 {
305 $result->addError($this->getErrorEntityNotExists());
306 }
307
308 return $result;
309 }
310
311 protected function get($id)
312 {
313 return \CIBlockSection::GetByID($id)->Fetch();
314 }
315
316 protected function getErrorCodeEntityNotExists(): string
317 {
319 }
320
321 protected function addValidate($fields): Result
322 {
323 $r = new Result();
324
325 if (isset($fields['CODE']))
326 {
327 $isCodeUnique = $this->isCodeUnique($fields);
328
329 if (!$isCodeUnique->isSuccess())
330 {
331 $r->addError($isCodeUnique->getErrors()[0]);
332 }
333 }
334
335 return $r;
336 }
337
338 protected function updateValidate($fields): Result
339 {
340 $r = new Result();
341
342 if (isset($fields['CODE']))
343 {
344 $isCodeUnique = $this->isCodeUnique($fields);
345
346 if (!$isCodeUnique->isSuccess())
347 {
348 $r->addError($isCodeUnique->getErrors()[0]);
349 }
350 }
351
352 return $r;
353 }
354
355 private function isCodeUnique(array $fields): Result
356 {
357 $r = new Result();
358
359 $iblock = \CIBlock::GetArrayByID($fields['IBLOCK_ID']);
360
361 if (isset($iblock['FIELDS']['SECTION_CODE']['DEFAULT_VALUE']))
362 {
363 if (
364 $iblock['FIELDS']['SECTION_CODE']['DEFAULT_VALUE']['TRANSLITERATION'] === 'Y'
365 && $iblock['FIELDS']['SECTION_CODE']['DEFAULT_VALUE']['UNIQUE'] === 'Y'
366 )
367 {
368 $filter = [
369 '=IBLOCK_ID' => $fields['IBLOCK_ID'],
370 '=CODE' => $fields['CODE'],
371 ];
372
373 if (isset($fields['ID']))
374 {
375 $filter['!=ID'] = $fields['ID'];
376 }
377
378 $existsResult = $this->existsByFilter($filter);
379
380 if ($existsResult->isSuccess())
381 {
382 $r->addError($this->getErrorDublicateFieldCode());
383 }
384 }
385 }
386
387 return $r;
388 }
389
390 private function getErrorDublicateFieldCode(): Error
391 {
392 return new Error('Duplicate entry for key [code]', 200700300040);
393 }
394
395 protected function getEntityTable(): DataManager
396 {
397 return new SectionTable();
398 }
399
400 private function getCount(array $filter): \Closure
401 {
402 return function() use ($filter)
403 {
404 return \CIBlockSection::GetCount($filter);
405 };
406 }
407
408 //region checkPermissionController
409 protected function checkModifyPermissionEntity()
410 {
411 return $this->checkReadPermissionEntity();
412 }
413
414 protected function checkReadPermissionEntity()
415 {
416 $r = new Result();
417
418 if (
419 !$this->accessController->check(ActionDictionary::ACTION_CATALOG_READ)
420 && !$this->accessController->check(ActionDictionary::ACTION_CATALOG_VIEW)
421 )
422 {
423 $r->addError($this->getErrorReadAccessDenied());
424 }
425
426 return $r;
427 }
428 //endregion
429
430 //region checkPermissionIBlock
432 {
434 }
435
436 protected function checkPermissionIBlockSectionUpdate($sectionId)
437 {
438 $iblockId = $this->getIBlockBySectionId($sectionId);
439
440 return $this->checkPermissionIBlockSectionModify($iblockId, $sectionId);
441 }
442
443 protected function checkPermissionIBlockSectionModify($iblockId, $sectionId)
444 {
445 $r = new Result();
446
447 $iblock = \CIBlock::GetArrayByID($iblockId);
448 $isBadIblock = false;
449
450 if ($iblock)
451 {
452 $isBadIblock = !\CIBlockSectionRights::UserHasRightTo($iblockId, $sectionId, self::IBLOCK_EDIT);
453 }
454 else
455 {
456 $r->addError(new Error('Iblock is not exists', 200700300050));
457 }
458
459 if ($isBadIblock)
460 {
461 $r->addError(new Error('Access Denied', 200040300040));
462 }
463
464 return $r;
465 }
466
467 protected function checkPermissionIBlockSectionSectionBindModify($iblockId, $iblockSectionId)
468 {
469 $r = new Result();
470
471 $iblock = \CIBlock::GetArrayByID($iblockId);
472 $isBadIblock = false;
473
474 if ($iblock)
475 {
476 $isBadIblock = !\CIBlockSectionRights::UserHasRightTo($iblockId, $iblockSectionId, self::IBLOCK_SECTION_SECTION_BIND); //access update
477 }
478 else
479 {
480 $r->addError(new Error('Iblock is not exists', 200700300050));
481 }
482
483 if ($isBadIblock)
484 {
485 $r->addError(new Error('Access Denied', 200040300050));
486 }
487
488 return $r;
489 }
490
491 protected function checkPermissionIBlockSectionSectionBindUpdate($iblockSectionId)
492 {
493 $iblockId = $this->getIBlockBySectionId($iblockSectionId);
494
495 return $this->checkPermissionIBlockSectionModify($iblockId, $iblockSectionId);
496 }
497
498 protected function checkPermissionIBlockSectionDelete($sectionId)
499 {
500 $r = new Result();
501 $iblockId = \CIBlockElement::GetIBlockByID($sectionId);
502 $isBadIblock = !\CIBlockElementRights::UserHasRightTo($iblockId, $sectionId, self::IBLOCK_SECTION_DELETE); //access delete
503
504 if ($isBadIblock)
505 {
506 $r->addError(new Error('Access Denied', 200040300050));
507 }
508
509 return $r;
510 }
511
512 protected function checkPermissionIBlockSectionGet($sectionId)
513 {
514 $r = new Result();
515
516 $iblockId = $this->getIBlockBySectionId($sectionId);
517 $isBadIblock = !\CIBlockSectionRights::UserHasRightTo($iblockId, $sectionId, self::IBLOCK_SECTION_READ);
518
519 if ($isBadIblock)
520 {
521 $r->addError(new Error('Access Denied', 200040300040));
522 }
523
524 return $r;
525 }
526
528 {
529 $r = new Result();
530
531 $iblock = \CIBlock::GetArrayByID($iblockId);
532 $isBadIblock = false;
533
534 if ($iblock)
535 {
536 $isBadIblock = !\CIBlockRights::UserHasRightTo($iblockId, $iblockId, self::IBLOCK_READ);
537 }
538 else
539 {
540 $r->addError(new Error('Iblock is not exists', 200700300050));
541 }
542
543 if ($isBadIblock)
544 {
545 $r->addError(new Error('Access Denied', 200040300030));
546 }
547
548 return $r;
549 }
550
551 protected function getIBlockBySectionId($id)
552 {
553 $iblockId = 0;
554
555 $section = \CIBlockSection::GetByID($id);
556 if ($res = $section->GetNext())
557 {
558 $iblockId = $res["IBLOCK_ID"];
559 }
560
561 return $iblockId;
562 }
563 //endregion
564}
$connection
Определения actionsdefinitions.php:38
const SECTION_ENTITY_NOT_EXISTS
Определения errorcode.php:12
checkModifyPermissionEntity()
Определения section.php:409
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[], bool $__calculateTotalCount=true)
Определения section.php:31
checkPermissionIBlockSectionSectionBindUpdate($iblockSectionId)
Определения section.php:491
checkReadPermissionEntity()
Определения section.php:414
getErrorCodeEntityNotExists()
Определения section.php:316
getIBlockBySectionId($id)
Определения section.php:551
addValidate($fields)
Определения section.php:321
checkPermissionIBlockSectionGet($sectionId)
Определения section.php:512
updateValidate($fields)
Определения section.php:338
checkPermissionIBlockSectionUpdate($sectionId)
Определения section.php:436
updateAction($id, array $fields)
Определения section.php:166
checkPermissionIBlockSectionList($iblockId)
Определения section.php:527
checkPermissionIBlockSectionDelete($sectionId)
Определения section.php:498
checkPermissionIBlockSectionAdd($iblockId)
Определения section.php:431
checkPermissionIBlockSectionSectionBindModify($iblockId, $iblockSectionId)
Определения section.php:467
checkPermissionIBlockSectionModify($iblockId, $sectionId)
Определения section.php:443
addAction($fields)
Определения section.php:103
Определения 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
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$iblockId
Определения iblock_catalog_edit.php:30
if(! $catalogEdit->isSuccess()) $iblock
Определения iblock_catalog_edit.php:38
$select
Определения iblock_catalog_list.php:194
$filter
Определения iblock_catalog_list.php:54
$l
Определения options.php:783
Определения aliases.php:54
trait Error
Определения error.php:11
$order
Определения payment.php:8
$error
Определения subscription_card_product.php:20
$iterator
Определения yandex_run.php:610
$fields
Определения yandex_run.php:501