Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
iblock.php
1<?php
2namespace Bitrix\Lists\Entity;
3
10
11class Iblock implements Controllable, Errorable
12{
14
15 const ERROR_ADD_IBLOCK = "ERROR_ADD_IBLOCK";
16 const ERROR_UPDATE_IBLOCK = "ERROR_UPDATE_IBLOCK";
17 const ERROR_IBLOCK_NOT_FOUND = "ERROR_IBLOCK_NOT_FOUND";
18 const ERROR_IBLOCK_ALREADY_EXISTS = "ERROR_IBLOCK_ALREADY_EXISTS";
19
20 private $param;
21 private $params = [];
22 private $fieldList = [];
23 private $messageList = [];
24
25 private $iblockId;
26
27 public function __construct(Param $param)
28 {
29 $this->param = $param;
30 $this->params = $param->getParams();
31
32 $this->fieldList = ["NAME", "ACTIVE", "DESCRIPTION", "SORT", "BIZPROC", "PICTURE"];
33 $this->messageList = ["ELEMENTS_NAME", "ELEMENT_NAME", "ELEMENT_ADD", "ELEMENT_EDIT", "ELEMENT_DELETE",
34 "SECTIONS_NAME", "SECTION_ADD", "SECTION_EDIT", "SECTION_DELETE"];
35
36 $this->iblockId = Utils::getIblockId($this->params);
37
38 $this->errorCollection = new ErrorCollection;
39 }
40
46 public function isExist()
47 {
48 $this->param->checkRequiredInputParams(["IBLOCK_TYPE_ID", "IBLOCK_CODE"]);
49 if ($this->param->hasErrors())
50 {
51 $this->errorCollection->add($this->param->getErrors());
52 return false;
53 }
54
55 $filter = [
56 "ID" => $this->params["IBLOCK_ID"] ? $this->params["IBLOCK_ID"] : "",
57 "CODE" => $this->params["IBLOCK_CODE"] ?? "",
58 "CHECK_PERMISSIONS" => "N",
59 ];
60 $queryObject = \CIBlock::getList([], $filter);
61
62 return (bool) $queryObject->fetch();
63 }
64
71 public function add()
72 {
73 $this->param->checkRequiredInputParams(["IBLOCK_TYPE_ID", "IBLOCK_CODE", ["FIELDS" => ["NAME"]]]);
74 if ($this->param->hasErrors())
75 {
76 $this->errorCollection->add($this->param->getErrors());
77 return false;
78 }
79
80 $iblockObject = new \CIBlock();
81 $result = $iblockObject->add($this->getFields());
82
83 if ($result)
84 {
85 if (!empty($this->params["SOCNET_GROUP_ID"]) && Loader::includeModule("socialnetwork"))
86 {
87 \CSocNetGroup::setLastActivity($this->params["SOCNET_GROUP_ID"]);
88 }
89 return (int)$result;
90 }
91 else
92 {
93 if ($iblockObject->LAST_ERROR)
94 {
95 $this->errorCollection->setError(new Error($iblockObject->LAST_ERROR, self::ERROR_ADD_IBLOCK));
96 }
97 else
98 {
99 $this->errorCollection->setError(new Error("Unknown error", self::ERROR_ADD_IBLOCK));
100 }
101
102 return false;
103 }
104 }
105
113 public function get(array $navData = [])
114 {
115 $this->param->checkRequiredInputParams(["IBLOCK_TYPE_ID"]);
116 if ($this->param->hasErrors())
117 {
118 $this->errorCollection->add($this->param->getErrors());
119 return [];
120 }
121
122 $iblocks = [];
123
124 $filter = $this->getFilter();
125
126 $order = $this->getOrder();
127
128 $queryObject = \CIBlock::getList($order, $filter);
129 $queryObject->NavStart($navData);
130 while ($result = $queryObject->fetch())
131 {
132 $iblocks[] = $result;
133 }
134
135 return [$iblocks, $queryObject];
136 }
137
143 public function update()
144 {
145 $this->param->checkRequiredInputParams(["IBLOCK_TYPE_ID", "IBLOCK_CODE", "IBLOCK_ID"]);
146 if ($this->param->hasErrors())
147 {
148 $this->errorCollection->add($this->param->getErrors());
149 return false;
150 }
151
152 $iblockObject = new \CIBlock;
153 if ($iblockObject->update($this->iblockId, $this->getFields()))
154 {
155 return true;
156 }
157 else
158 {
159 if ($iblockObject->LAST_ERROR)
160 {
161 $this->errorCollection->setError(new Error($iblockObject->LAST_ERROR, self::ERROR_UPDATE_IBLOCK));
162 }
163 else
164 {
165 $this->errorCollection->setError(new Error("Unknown error", self::ERROR_UPDATE_IBLOCK));
166 }
167
168 return false;
169 }
170 }
171
177 public function delete()
178 {
179 $this->param->checkRequiredInputParams(["IBLOCK_TYPE_ID", "IBLOCK_CODE", "IBLOCK_ID"]);
180 if ($this->param->hasErrors())
181 {
182 $this->errorCollection->add($this->param->getErrors());
183 return false;
184 }
185
186 return \CIBlock::delete($this->iblockId);
187 }
188
189 private function getFilter()
190 {
191 $filter = [
192 "TYPE" => $this->params["IBLOCK_TYPE_ID"],
193 "ID" => ($this->params["IBLOCK_ID"] ? $this->params["IBLOCK_ID"] : ""),
194 "CODE" => ($this->params["IBLOCK_CODE"] ? $this->params["IBLOCK_CODE"] : ""),
195 "ACTIVE" => "Y",
196 "CHECK_PERMISSIONS" => ($this->params["SOCNET_GROUP_ID"]) ? "N" : "Y",
197 ];
198 if ($this->params["SOCNET_GROUP_ID"])
199 {
200 $filter["=SOCNET_GROUP_ID"] = $this->params["SOCNET_GROUP_ID"];
201 }
202 else
203 {
204 $filter["SITE_ID"] = SITE_ID;
205 }
206
207 return $filter;
208 }
209
210 private function getOrder()
211 {
212 $order = [];
213 if (is_array($this->params["IBLOCK_ORDER"]))
214 {
215 $fieldList = ["ID", "IBLOCK_TYPE", "NAME", "ACTIVE", "CODE", "SORT", "ELEMENT_CNT", "TIMESTAMP_X"];
216 $orderList = ["asc", "desc"];
217 foreach ($this->params["IBLOCK_ORDER"] as $fieldId => $orderParam)
218 {
219 if (!in_array($orderParam, $orderList) || !in_array($fieldId, $fieldList))
220 {
221 continue;
222 }
223 $order[$fieldId] = $orderParam;
224 }
225 }
226 if (!isset($order['ID']))
227 {
228 $order['ID'] = 'DESC';
229 }
230
231 return $order;
232 }
233
234 private function getFields()
235 {
236 $fields = [
237 "IBLOCK_TYPE_ID" => $this->params["IBLOCK_TYPE_ID"],
238 "WORKFLOW" => "N",
239 "RIGHTS_MODE" => "E",
240 "SITE_ID" => \CSite::getDefSite(),
241 ];
242 if ($this->params["IBLOCK_CODE"])
243 {
244 $fields["CODE"] = $this->params["IBLOCK_CODE"];
245 }
246 if ($this->params["SOCNET_GROUP_ID"])
247 {
248 $fields["SOCNET_GROUP_ID"] = $this->params["SOCNET_GROUP_ID"];
249 }
250
251 foreach ($this->params["FIELDS"] as $fieldId => $fieldValue)
252 {
253 if (!in_array($fieldId, $this->fieldList))
254 {
255 continue;
256 }
257
258 if ($fieldId == "PICTURE")
259 {
260 $fieldValue = \CRestUtil::saveFile($fieldValue);
261 }
262
263 $fields[$fieldId] = $fieldValue;
264 }
265
266 foreach ($this->params["MESSAGES"] as $messageId => $messageValue)
267 {
268 if (!in_array($messageId, $this->messageList))
269 {
270 continue;
271 }
272
273 $fields[$messageId] = $messageValue;
274 }
275
276 $fields["RIGHTS"] = $this->getCurrentRights();
277 foreach ($this->getInputRight() as $rightId => $right)
278 {
279 $fields["RIGHTS"][$rightId] = $right;
280 }
281
282 return $fields;
283 }
284
285 private function getCurrentRights()
286 {
287 $result = [];
288
289 $iblockRights = new \CIBlockRights($this->iblockId);
290 $rights = $iblockRights->getRights();
291 foreach ($rights as $rightId => $right)
292 {
293 $result[$rightId] = $right;
294 }
295
296 return $result;
297 }
298
299 private function getInputRight()
300 {
301 $result = [];
302
303 if (empty($this->params["RIGHTS"]) || !is_array($this->params["RIGHTS"]))
304 {
305 global $USER;
306 $this->params["RIGHTS"] = [];
307 $this->params["RIGHTS"]["U".$USER->getID()] = "X";
308 }
309
310 $count = 0;
311 foreach ($this->params["RIGHTS"] as $rightCode => $access)
312 {
313 $result["n".($count++)] = [
314 "GROUP_CODE" => $rightCode,
315 "TASK_ID" => \CIBlockRights::letterToTask($access),
316 "DO_CLEAN" => "N",
317 ];
318 }
319
320 return $result;
321 }
322}
__construct(Param $param)
Definition iblock.php:27
static getIblockId(array $params)
Definition utils.php:13