Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
elementrowactionsprovider.php
1<?php
2
4
20
25{
26 private IblockRightsChecker $rights;
27
28 public function __construct(ElementSettings $settings, IblockRightsChecker $rights)
29 {
30 parent::__construct($settings);
31
32 $this->rights = $rights;
33 }
34
35 final protected function getIblockId(): int
36 {
37 return $this->getSettings()->getIblockId();
38 }
39
40 final protected function getIblockRightsChecker(): IblockRightsChecker
41 {
42 return $this->rights;
43 }
44
45 private function getUrlBuilder(): ?BaseBuilder
46 {
47 return $this->getSettings()->getUrlBuilder();
48 }
49
50 public function prepareActions(): array
51 {
52 $result = [
53 new DetailViewItem(),
54 new DeleteSectionItem($this->rights), // check by concrete section
55 ];
56
57 if ($this->getIblockRightsChecker()->canEditElements())
58 {
59 array_push($result, ... [
60 new EditItem(),
61 new CopyElementItem(),
62 new CreateCodeItem($this->getIblockId(), $this->rights),
63 new DeactivateElementItem($this->getIblockId(), $this->rights),
64 new ActivateElementItem($this->getIblockId(), $this->rights),
65 new ClearCounterItem($this->getIblockId(), $this->rights),
66 ]);
67 }
68
69 if ($this->getIblockRightsChecker()->canDeleteElements())
70 {
71 $result[] = new DeleteElementItem($this->rights);
72 }
73
74 return $result;
75 }
76
77 public function prepareControls(array $rawFields): array
78 {
79 $isSection = isset($rawFields['ROW_TYPE']) && $rawFields['ROW_TYPE'] === 'S';
80 if ($isSection)
81 {
82 $items = $this->getSectionItems($rawFields);
83 }
84 else
85 {
86 $items = $this->getElementItems($rawFields);
87 }
88
89 $result = [];
90
91 foreach ($items as $item)
92 {
93 $config = $item->getControl($rawFields);
94 if (isset($config))
95 {
96 $result[] = $config;
97 }
98 }
99
100 return $result;
101 }
102
108 private function getSectionItems(array $rawFields): array
109 {
110 if (empty($rawFields['ID']))
111 {
112 return [];
113 }
114 $sectionId = (int)$rawFields['ID'];
115
116 $result = [];
117
118 if ($this->rights->canEditSection($sectionId))
119 {
120 $detailUrl = $this->getSectionEditUrl($sectionId);
121 if (isset($detailUrl))
122 {
126 $item = $this->getActionById(EditItem::getId());
127 if (isset($item))
128 {
129 $item->setUrl($detailUrl);
130 $result[] = $item;
131 }
132 }
133
134 self::appendIfNotNull(
135 $result,
137 );
138 }
139
140 if ($this->rights->canDeleteSection($sectionId))
141 {
142 self::appendIfNotNull(
143 $result,
145 );
146 }
147
148 return $result;
149 }
150
156 private function getElementItems(array $rawFields): array
157 {
158 if (empty($rawFields['ID']))
159 {
160 return [];
161 }
162 $elementId = (int)$rawFields['ID'];
163
164 $result = [];
165
166 if ($this->rights->canEditElement($elementId))
167 {
168 $detailUrl = $this->getElementDetailViewUrl($elementId);
169 if (isset($detailUrl))
170 {
174 $item = $this->getActionById(DetailViewItem::getId());
175 if (isset($item))
176 {
177 $item->setUrl($detailUrl);
178 $result[] = $item;
179 }
180 }
181
182 if (isset($rawFields['ACTIVE']) && $rawFields['ACTIVE'] === 'Y')
183 {
184 self::appendIfNotNull(
185 $result,
187 );
188 }
189 else
190 {
191 self::appendIfNotNull(
192 $result,
194 );
195 }
196
197 self::appendIfNotNull(
198 $result,
200 );
201
202 if (!empty($result))
203 {
204 $result[] = new SeparatorAction();
205 }
206
207 $item = $this->getActionById(ClearCounterItem::getId());
208 if (isset($item))
209 {
210 $result[] = $item;
211 $result[] = new SeparatorAction();
212 }
213
214 if ($this->rights->canAddElement($elementId))
215 {
216 $copyUrl = $this->getElementCopyUrl($elementId);
217 if (isset($copyUrl))
218 {
222 $item = $this->getActionById(CopyElementItem::getId());
223 if (isset($item))
224 {
225 $item->setUrl($copyUrl);
226 $result[] = $item;
227 }
228 }
229 }
230 }
231
232 if ($this->rights->canDeleteElement($elementId))
233 {
234 $item = $this->getActionById(DeleteElementItem::getId());
235 if (isset($item))
236 {
237 if (!empty($result))
238 {
239 $lastItem = $result[array_key_last($result)];
240 if (($lastItem instanceof SeparatorAction) === false)
241 {
242 $result[] = new SeparatorAction();
243 }
244 }
245
246 $result[] = $item;
247 }
248 }
249
250 return $result;
251 }
252
253 private function getSectionEditUrl(int $sectionId): ?string
254 {
255 $urlBuilder = $this->getUrlBuilder();
256 if (!isset($urlBuilder))
257 {
258 return null;
259 }
260
261 return $urlBuilder->getSectionDetailUrl($sectionId);
262 }
263
264 private function getElementDetailViewUrl(int $elementId): ?string
265 {
266 $urlBuilder = $this->getUrlBuilder();
267 if (!isset($urlBuilder))
268 {
269 return null;
270 }
271
272 return $urlBuilder->getElementDetailUrl($elementId);
273 }
274
275 private function getElementCopyUrl(int $elementId): ?string
276 {
277 $urlBuilder = $this->getUrlBuilder();
278 if (!isset($urlBuilder))
279 {
280 return null;
281 }
282
283 return $urlBuilder->getElementCopyUrl($elementId);
284 }
285
286 #region helpers
287
288 private static function appendIfNotNull(array &$items, $item): void
289 {
290 if (isset($item))
291 {
292 $items[] = $item;
293 }
294 }
295
296 #endregion helpers
297}
__construct(ElementSettings $settings, IblockRightsChecker $rights)