Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
section.php
1<?
2namespace Bitrix\Lists\Entity;
3
9
11{
13
14 const ERROR_ADD_SECTION = "ERROR_ADD_SECTION";
15 const ERROR_UPDATE_SECTION = "ERROR_UPDATE_SECTION";
16 const ERROR_DELETE_SECTION = "ERROR_DELETE_SECTION";
17 const ERROR_SECTION_NOT_FOUND = "ERROR_SECTION_NOT_FOUND";
18
19 private $param;
20 private $params = [];
21 private $fieldList = [];
22 private $filterList = [];
23 private $selectList = [];
24
25 private $iblockId;
26 private $sectionId;
27
28 public function __construct(Param $param)
29 {
30 $this->param = $param;
31 $this->params = $param->getParams();
32
33 $this->fieldList = ["ID", "CODE", "EXTERNAL_ID", "XML_ID", "IBLOCK_ID", "IBLOCK_SECTION_ID", "TIMESTAMP_X",
34 "SORT", "NAME", "ACTIVE", "SORT", "PICTURE", "DESCRIPTION", "DESCRIPTION_TYPE", "MODIFIED_BY",
35 "DATE_CREATE", "CREATED_BY", "DETAIL_PICTURE", "SECTION_PROPERTY"];
36
37 $this->filterList = ["ACTIVE", "GLOBAL_ACTIVE", "NAME", "CODE", "XML_ID", "EXTERNAL_ID", "SECTION_ID",
38 "DEPTH_LEVEL", "LEFT_BORDER", "RIGHT_BORDER", "LEFT_MARGIN", "RIGHT_MARGIN", "IBLOCK_ID", "ID",
39 "IBLOCK_ACTIVE", "IBLOCK_NAME", "IBLOCK_TYPE", "IBLOCK_CODE", "IBLOCK_XML_ID", "IBLOCK_EXTERNAL_ID",
40 "TIMESTAMP_X", "DATE_CREATE", "MODIFIED_BY", "CREATED_BY", "SOCNET_GROUP_ID", "MIN_PERMISSION",
41 "CHECK_PERMISSIONS", "PERMISSIONS_BY", "PROPERTY"];
42
43 $this->selectList = ["ID", "CODE", "EXTERNAL_ID", "XML_ID", "IBLOCK_ID", "IBLOCK_SECTION_ID", "TIMESTAMP_X",
44 "SORT", "NAME", "ACTIVE", "GLOBAL_ACTIVE", "PICTURE", "DESCRIPTION", "DESCRIPTION_TYPE", "LEFT_MARGIN",
45 "RIGHT_MARGIN", "DEPTH_LEVEL", "SEARCHABLE_CONTENT", "SECTION_PAGE_URL", "MODIFIED_BY", "DATE_CREATE",
46 "CREATED_BY", "DETAIL_PICTURE"];
47
48 $this->iblockId = Utils::getIblockId($this->params);
49 $this->sectionId = Utils::getSectionId($this->params);
50
51 $this->errorCollection = new ErrorCollection;
52 }
53
59 public function isExist()
60 {
61 $this->param->checkRequiredInputParams(["IBLOCK_CODE", "IBLOCK_ID", "SECTION_ID", "SECTION_CODE"]);
62 if ($this->param->hasErrors())
63 {
64 $this->errorCollection->add($this->param->getErrors());
65 return false;
66 }
67
68 $filter = [
69 "ID" => $this->sectionId,
70 "IBLOCK_ID" => $this->iblockId,
71 "CHECK_PERMISSIONS" => "N",
72 ];
73 $queryObject = \CIBlockSection::getList([], $filter, false, ["ID"]);
74
75 return (bool) $queryObject->fetch();
76 }
77
83 public function add()
84 {
85 $this->param->checkRequiredInputParams(["IBLOCK_CODE", "IBLOCK_ID", "SECTION_CODE", ["FIELDS" => ["NAME"]]]);
86 if ($this->param->hasErrors())
87 {
88 $this->errorCollection->add($this->param->getErrors());
89 return false;
90 }
91
92 $sectionObject = new \CIBlockSection;
93 $result = $sectionObject->add($this->getFields());
94
95 if ($result)
96 {
97 return (int)$result;
98 }
99 else
100 {
101 if ($sectionObject->LAST_ERROR)
102 {
103 $this->errorCollection->setError(new Error($sectionObject->LAST_ERROR, self::ERROR_ADD_SECTION));
104 }
105 else
106 {
107 $this->errorCollection->setError(new Error("Unknown error", self::ERROR_ADD_SECTION));
108 }
109
110 return false;
111 }
112 }
113
121 public function get(array $navData = [])
122 {
123 $this->param->checkRequiredInputParams(["IBLOCK_TYPE_ID", "IBLOCK_CODE", "IBLOCK_ID"]);
124 if ($this->param->hasErrors())
125 {
126 $this->errorCollection->add($this->param->getErrors());
127 return [];
128 }
129
130 $sections = [];
131
132 $filter = $this->getFilter($this->iblockId);
133
134 $select = $this->getSelectList($this->params["SELECT"]);
135
136 $queryObject = \CIBlockSection::getList([], $filter, false, $select, $navData);
137 while ($section = $queryObject->fetch())
138 {
139 $sections[] = $section;
140 }
141
142 return [$sections, $queryObject];
143 }
144
150 public function update()
151 {
152 $this->param->checkRequiredInputParams(["IBLOCK_CODE", "IBLOCK_ID",
153 "SECTION_ID", "SECTION_CODE", ["FIELDS" => ["NAME"]]]);
154 if ($this->param->hasErrors())
155 {
156 $this->errorCollection->add($this->param->getErrors());
157 return false;
158 }
159
160 $sectionObject = new \CIBlockSection;
161 if ($sectionObject->update($this->sectionId, $this->getFields()))
162 {
163 return true;
164 }
165 else
166 {
167 if ($sectionObject->LAST_ERROR)
168 {
169 $this->errorCollection->setError(new Error($sectionObject->LAST_ERROR, self::ERROR_UPDATE_SECTION));
170 }
171 else
172 {
173 $this->errorCollection->setError(new Error("Unknown error", self::ERROR_UPDATE_SECTION));
174 }
175
176 return false;
177 }
178 }
179
185 public function delete()
186 {
187 $this->param->checkRequiredInputParams(["SECTION_ID", "SECTION_CODE"]);
188 if ($this->param->hasErrors())
189 {
190 $this->errorCollection->add($this->param->getErrors());
191 return false;
192 }
193
194 $sectionObject = new \CIBlockSection;
195 if ($sectionObject->delete($this->sectionId, false))
196 {
197 return true;
198 }
199 else
200 {
201 if ($sectionObject->LAST_ERROR)
202 {
203 $this->errorCollection->setError(new Error($sectionObject->LAST_ERROR, self::ERROR_DELETE_SECTION));
204 }
205 else
206 {
207 $this->errorCollection->setError(new Error("Unknown error", self::ERROR_DELETE_SECTION));
208 }
209
210 return false;
211 }
212 }
213
214 private function getFields()
215 {
216 $fields = [
217 "IBLOCK_ID" => $this->iblockId,
218 "CODE" => $this->params["SECTION_CODE"],
219 "IBLOCK_SECTION_ID" => $this->params["IBLOCK_SECTION_ID"] ? (int)$this->params["IBLOCK_SECTION_ID"] : 0,
220 "CHECK_PERMISSIONS" => "N"
221 ];
222
223 foreach ($this->params["FIELDS"] as $fieldId => $fieldValue)
224 {
225 if (!in_array($fieldId, $this->fieldList))
226 {
227 continue;
228 }
229
230 if ($fieldId == "PICTURE")
231 {
232 $fieldValue = \CRestUtil::saveFile($fieldValue);
233 }
234
235 $fields[$fieldId] = $fieldValue;
236 }
237
238 return $fields;
239 }
240
241 private function getFilter($iblockId)
242 {
243 $filter = [
244 "IBLOCK_ID" => $iblockId,
245 "CHECK_PERMISSIONS" => "N",
246 ];
247
248 if (!is_array($this->params["FILTER"]))
249 {
250 $this->params["FILTER"] = [];
251 }
252
253 foreach ($this->params["FILTER"] as $fieldId => $fieldValue)
254 {
255 if (in_array($fieldId, $this->filterList))
256 {
257 $filter[$fieldId] = $fieldValue;
258 }
259 }
260
261 return $filter;
262 }
263
264 private function getSelectList($inputSelectList)
265 {
266 $selectList = [];
267
268 if (!is_array($inputSelectList))
269 {
270 $inputSelectList = [];
271 }
272
273 foreach ($inputSelectList as $fieldId)
274 {
275 if (in_array($fieldId, $this->selectList))
276 {
277 $selectList[] = $fieldId;
278 }
279 }
280
281 return $selectList;
282 }
283}
__construct(Param $param)
Definition section.php:28
static getSectionId(array $params)
Definition utils.php:70
static getIblockId(array $params)
Definition utils.php:13