Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Department.php
1<?php
2
4
6
8{
10 private $id;
11
12 public function __construct(int $id = 0)
13 {
14 if ($id > 0)
15 {
16 $this->id = $id;
17 }
18 }
19
23 public function setId(int $id): void
24 {
25 $this->id = $id;
26 }
27
32 public function getPathFromHeadToDepartment(): array
33 {
34 if (!Loader::includeModule('iblock'))
35 {
36 return [];
37 }
38
39 $departmentTree = \CIntranetUtils::GetDeparmentsTree(0);
40 $topDepartmentId = self::getTopDepartmentId();
41
42 if (!$topDepartmentId || empty($departmentTree) || !$this->id)
43 {
44 return [];
45 }
46
47 $path[] = $this->id;
48 $departmentId = $this->id;
49
50 while ($departmentId && $departmentId != $topDepartmentId)
51 {
52 $departmentId = $this->getHeadDepartmentId($departmentId, $departmentTree) ?? $topDepartmentId;
53 $path[] = $departmentId;
54 }
55
56 return array_reverse($path);
57 }
58
65 protected function getHeadDepartmentId(int $curId, array $departmentTree): ?int
66 {
67 foreach ($departmentTree as $headDepartmentId => $subDepartments)
68 {
69 foreach ($subDepartments as $subDepartmentId)
70 {
71 if ((int)$subDepartmentId == $curId)
72 {
73 return (int)$headDepartmentId;
74 }
75 }
76 }
77
78 return null;
79 }
80
86 public function getAccessCodes(array $departmentIds): array
87 {
88 $accessCodes = [];
89 foreach ($departmentIds as $departmentId)
90 {
91 if ((int)$departmentId === $this->id)
92 {
93 $accessCodes[] = 'D' . $this->id;
94 $accessCodes[] = 'DR' . $this->id;
95 }
96 else
97 {
98 $accessCodes[] = 'DR' . $departmentId;
99 }
100 }
101 return $accessCodes;
102 }
103
104 public static function getTopDepartmentId()
105 {
106 if (!Loader::includeModule("iblock"))
107 {
108 return false;
109 }
110
111 $departmentId = false;
112 $res = \CIBlock::GetList([], ["CODE" => "departments"]);
113 if ($iblock = $res->Fetch())
114 {
115 $res = \CIBlockSection::GetList(
116 [],
117 [
118 "SECTION_ID" => 0,
119 "IBLOCK_ID" => $iblock["ID"]
120 ]
121 );
122 if ($department = $res->Fetch())
123 {
124 $departmentId = (int)$department['ID'];
125 }
126 }
127
128 return $departmentId;
129 }
130}
getAccessCodes(array $departmentIds)
getHeadDepartmentId(int $curId, array $departmentTree)