Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
catalog.php
1<?php
2
4
11
12final class Catalog extends Controller
13{
14 //region Actions
15 public function getFieldsAction(): array
16 {
17 return ['CATALOG' => $this->getViewFields()];
18 }
19
20 public function isOffersAction($id)
21 {
22 $r = $this->exists($id);
23 if($r->isSuccess())
24 {
25 return $this->isOffers($id);
26 }
27 else
28 {
29 $this->addErrors($r->getErrors());
30 return null;
31 }
32 }
33
34 public function listAction($select=[], $filter=[], $order=[], $start=0)
35 {
36 $result = [];
37
38 $catalog = new \CCatalog();
39
40 $select = empty($select)? ['*']:$select;
41 $order = empty($order)? ['ID'=>'ASC']:$order;
42
43 $r = $catalog::GetList($order, $filter, false, self::getNavData($start), $select);
44 while ($l = $r->fetch())
45 {
46 $result[] = $l;
47 }
48
49 return new Page('CATALOGS', $result, function() use ($filter)
50 {
51 return (int)\CCatalog::GetList([], $filter, []);
52 });
53 }
54
55 public function getAction($id)
56 {
57 $r = $this->exists($id);
58 if($r->isSuccess())
59 {
60 return ['CATALOG'=>$this->get($id)];
61 }
62 else
63 {
64 $this->addErrors($r->getErrors());
65 return null;
66 }
67 }
68
69 public function addAction($fields)
70 {
71 $r = new Result();
72
73 $res = $this->exists($fields['IBLOCK_ID']);
74 if($res->isSuccess() == false)
75 {
76 $r = $this->addValidate($fields);
77 if($r->isSuccess())
78 {
79 \CCatalog::add($fields);
80 }
81 }
82 else
83 {
84 $r->addError(new Error('Duplicate entry for key [iblockId]'));
85 }
86
87 if(!$r->isSuccess())
88 {
89 $this->addErrors($r->getErrors());
90 return null;
91 }
92 else
93 {
94 return ['CATALOG'=>$this->get($fields['IBLOCK_ID'])];
95 }
96 }
97
98 public function updateAction($id, array $fields)
99 {
100 $r = $this->exists($id);
101 if($r->isSuccess())
102 {
103 $r = $this->updateValidate($fields+['ID'=>$id]);
104 if($r->isSuccess())
105 {
106 \CCatalog::update($id, $fields);
107 }
108 }
109
110 if($r->isSuccess())
111 {
112 return ['CATALOG'=>$this->get($id)];
113 }
114 else
115 {
116 $this->addErrors($r->getErrors());
117 return null;
118 }
119 }
120
121 public function deleteAction($id)
122 {
123 $r = $this->exists($id);
124 if($r->isSuccess())
125 {
126 $r = $this->deleteValidate($id);
127 if($r->isSuccess())
128 {
129 if (!\CCatalog::Delete($id))
130 {
131 if ($ex = self::getApplication()->GetException())
132 $r->addError(new Error($ex->GetString(), $ex->GetId()));
133 else
134 $r->addError(new Error('delete catalog error'));
135 }
136 }
137 }
138
139 if($r->isSuccess())
140 {
141 return true;
142 }
143 else
144 {
145 $this->addErrors($r->getErrors());
146 return null;
147 }
148 }
149 //endregion
150
151 protected function isOffers($id)
152 {
153 return $this->get($id)["PRODUCT_IBLOCK_ID"] ? true:false;
154 }
155
156 protected function getEntityTable()
157 {
158 return new CatalogIblockTable();
159 }
160
161 protected function exists($id)
162 {
163 $r = new Result();
164
165 if(isset($this->get($id)['ID']) == false)
166 $r->addError(new Error('Catalog is not exists'));
167
168 return $r;
169 }
170
171 protected function get($id)
172 {
173 return \CCatalog::GetByID($id);
174 }
175
176 protected function addValidate(array $fields)
177 {
178 $r = new Result();
179
180 if(!\CCatalog::CheckFields("ADD", $fields, $fields['ID']))
181 {
182 if ($ex = self::getApplication()->GetException())
183 $r->addError(new Error($ex->GetString(), $ex->GetId()));
184 else
185 $r->addError(new Error('Validate catalog error'));
186 }
187
188 return $r;
189 }
190
191 protected function updateValidate(array $fields)
192 {
193 $r = new Result();
194
195 if(!\CCatalog::CheckFields("UPDATE", $fields, $fields['ID']))
196 {
197 if ($ex = self::getApplication()->GetException())
198 $r->addError(new Error($ex->GetString(), $ex->GetId()));
199 else
200 $r->addError(new Error('Validate catalog error'));
201 }
202
203 return $r;
204 }
205
206 protected function deleteValidate($id)
207 {
208 $r = new Result();
209
210 if($this->isOffers($id))
211 $r->addError(new Error('Catalog is offers'));
212
213 return $r;
214 }
215
216 protected function checkPermissionEntity($name, $arguments=[])
217 {
218 $name = mb_strtolower($name); //for ajax mode
219
220 if($name == 'isoffers')
221 {
222 $r = $this->checkReadPermissionEntity();
223 }
224 else
225 {
226 $r = parent::checkPermissionEntity($name);
227 }
228
229 return $r;
230 }
231
232 protected function checkModifyPermissionEntity()
233 {
234 $r = $this->checkReadPermissionEntity();
235 if($r->isSuccess())
236 {
237 if (!$this->accessController->check(ActionDictionary::ACTION_CATALOG_SETTINGS_ACCESS))
238 {
239 $r->addError(new Error('Access Denied', 200040300020));
240 }
241 }
242
243 return $r;
244 }
245
246 protected function checkReadPermissionEntity()
247 {
248 $r = new Result();
249
250 $user = CurrentUser::get();
251 if(!$user->canDoOperation('view_other_settings') && !$user->canDoOperation('edit_other_settings'))
252 {
253 $r->addError(new Error('Access Denied', 200040300010));
254 }
255
256 if (
257 !$this->accessController->check(ActionDictionary::ACTION_CATALOG_READ)
258 && !$this->accessController->check(ActionDictionary::ACTION_CATALOG_SETTINGS_ACCESS)
259 )
260 {
261 $r->addError(new Error('Access Denied', 200040300030));
262 }
263
264 return $r;
265 }
266}
updateAction($id, array $fields)
Definition catalog.php:98
checkPermissionEntity($name, $arguments=[])
Definition catalog.php:216
listAction($select=[], $filter=[], $order=[], $start=0)
Definition catalog.php:34