Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
iblock.php
1<?php
3
12
14{
15 const IBLOCK_COPY_ERROR = "IBLOCK_COPIER_ERROR";
16
17 private $targetIblockTypeId = "";
18 private $targetSocnetGroupId = 0;
19
23 protected $cacheManager;
24
25 private $child = [];
26
32 public function setChild(Child $child)
33 {
34 $this->child[] = $child;
35 }
36
37 public function setTargetIblockTypeId($targetIblockTypeId)
38 {
39 $this->targetIblockTypeId = $targetIblockTypeId;
40 }
41
42 public function setTargetSocnetGroupId($targetSocnetGroupId)
43 {
44 $this->targetSocnetGroupId = $targetSocnetGroupId;
45 }
46
50 public function setCacheManager(\CCacheManager $cacheManager): void
51 {
52 $this->cacheManager = $cacheManager;
53 }
54
62 public function add(Container $container, array $fields)
63 {
64 $iblockObject = new \CIBlock;
65 $iblockId = $iblockObject->add($fields);
66
67 if ($iblockId)
68 {
69 $this->cleanCache($iblockId);
70 }
71 else
72 {
73 if ($iblockObject->LAST_ERROR)
74 {
75 $this->result->addError(new Error($iblockObject->LAST_ERROR, self::IBLOCK_COPY_ERROR));
76 }
77 else
78 {
79 $this->result->addError(new Error("Unknown error", self::IBLOCK_COPY_ERROR));
80 }
81 }
82
83 return $iblockId;
84 }
85
93 public function getFields(Container $container, $entityId)
94 {
95 $query = \CIBlock::getList([], ["ID" => $entityId, "CHECK_PERMISSIONS" => "N"], true);
96 $iblock = $query->fetch();
97 if ($iblock)
98 {
99 $iblockMessage = \CIBlock::getMessages($entityId);
100 $iblock = array_merge($iblock, $iblockMessage);
101 }
102
103 if ($this->targetIblockTypeId)
104 {
105 $iblock["IBLOCK_TYPE_ID"] = $this->targetIblockTypeId;
106 }
107
108 if ($this->targetSocnetGroupId)
109 {
110 $iblock["SOCNET_GROUP_ID"] = $this->targetSocnetGroupId;
111 }
112
113 if (!empty($iblock["PICTURE"]))
114 {
115 $iblock["PICTURE"] = \CFile::makeFileArray($iblock["PICTURE"]);
116 }
117
118 $iblock["RIGHTS"] = $this->getRights(
119 $entityId,
120 $iblock["RIGHTS_MODE"],
121 $iblock["SOCNET_GROUP_ID"]
122 );
123
124 return $iblock;
125 }
126
134 public function prepareFieldsToCopy(Container $container, array $fields)
135 {
136 unset($fields["XML_ID"]);
137
138 return $fields;
139 }
140
151 public function copyChildren(Container $container, $entityId, $copiedEntityId)
152 {
153 $results = [];
154 $sectionsRatio = [];
155 $enumRatio = [];
156 foreach ($this->child as $child)
157 {
158 if ($child instanceof ElementChild)
159 {
160 $child->setEnumRatio($enumRatio);
161 $child->setSectionsRatio($sectionsRatio);
162 }
163
164 $results[] = $child->copy($entityId, $copiedEntityId);
165
166 if ($child instanceof FieldChild)
167 {
168 $enumRatio = $child->getEnumRatio();
169 }
170 if ($child instanceof SectionChild)
171 {
172 $sectionsRatio = $child->getSectionsRatio();
173 }
174 }
175
176 return $this->getResult($results);
177 }
178
179 protected function cleanCache(int $iblockId): void
180 {
181 if ($this->cacheManager)
182 {
183 $this->cacheManager->cleanDir("menu");
184 }
185 }
186
187 protected function getSocnetPermission($iblockId, $socnetGroupId): array
188 {
189 return [];
190 }
191
192 private function getRights($iblockId, $rightMode, $socnetGroupId = 0)
193 {
194 $rights = [];
195
196 if ($socnetGroupId)
197 {
198 $rights = $this->getSocnetPermission($iblockId, $socnetGroupId);
199 }
200
201 if ($rightMode == "E")
202 {
203 $rightObject = new \CIBlockRights($iblockId);
204 foreach ($rightObject->getRights() as $right)
205 {
206 if (mb_strpos($right["GROUP_CODE"], "SG") !== 0)
207 {
208 $rights["n".(count($rights))] = [
209 "GROUP_CODE" => $right["GROUP_CODE"],
210 "DO_CLEAN" => "N",
211 "TASK_ID" => $right["TASK_ID"],
212 ];
213 }
214 }
215 }
216 else
217 {
218 $groupPermissions = \CIBlock::getGroupPermissions($iblockId);
219 foreach ($groupPermissions as $groupId => $permission)
220 {
221 if ($permission > "W")
222 {
223 $rights["n".(count($rights))] = [
224 "GROUP_CODE" => "G".$groupId,
225 "IS_INHERITED" => "N",
226 "TASK_ID" => \CIBlockRights::letterToTask($permission),
227 ];
228 }
229 }
230 }
231
232 return $rights;
233 }
234}
getSocnetPermission($iblockId, $socnetGroupId)
Definition iblock.php:187
getFields(Container $container, $entityId)
Definition iblock.php:93
setTargetSocnetGroupId($targetSocnetGroupId)
Definition iblock.php:42
setCacheManager(\CCacheManager $cacheManager)
Definition iblock.php:50
add(Container $container, array $fields)
Definition iblock.php:62
setTargetIblockTypeId($targetIblockTypeId)
Definition iblock.php:37
copyChildren(Container $container, $entityId, $copiedEntityId)
Definition iblock.php:151
prepareFieldsToCopy(Container $container, array $fields)
Definition iblock.php:134