Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
manager.php
1<?php
2
4
12
17{
19 private Category $category;
21 private $error;
22
23 protected function __construct()
24 {
25 }
26
27 public static function createInstance(Category $category): Manager
28 {
29 return (new self())->setCategory($category);
30 }
31
32 public function setCategory(Category $category): Manager
33 {
34 $this->category = $category;
35
36 return $this;
37 }
38
39 private function addError(Error $error): Manager
40 {
41 $this->error = $error;
42
43 return $this;
44 }
45
46 public function getCategory(): Category
47 {
48 return $this->category;
49 }
50
51 public function getError(): ?Error
52 {
53 return $this->error;
54 }
55
61 public function createCategory(): Manager
62 {
63 if($this->getError())
64 {
65 return $this;
66 }
67
68 $this->category->create();
69
70 if($this->category->getError())
71 {
72 $this->addError($this->category->getError());
73 }
74
75 $createdCategoryId = $this->category->getId();
76 $rooms = $this->category->getRooms();
77 if(!empty($rooms) && $createdCategoryId)
78 {
79 foreach ($rooms as &$room)
80 {
81 $room = (int)$room;
82 }
83 global $DB;
84 $tableName = LocationTable::getTableName();
85 $roomsIds = implode(',', $rooms);
86
87 $sqlStr = "
88 UPDATE $tableName
89 SET CATEGORY_ID = $createdCategoryId
90 WHERE SECTION_ID IN ($roomsIds)
91 ";
92 $result = $DB->Query($sqlStr, true);
93 if(!$result)
94 {
95 $this->category->delete();
96 $this->addError(new Error('An error occurred while saving the category'));
97 }
98 }
99
100 return $this;
101 }
102
108 public function updateCategory(): Manager
109 {
110 if ($this->getError())
111 {
112 return $this;
113 }
114
115 $this->category->update();
116
117 if ($this->category->getError())
118 {
119 $this->addError($this->category->getError());
120 }
121
122 global $DB;
123 $tableName = LocationTable::getTableName();
124 $categoryId = $this->category->getId();
125 $rooms = $this->category->getRooms();
126
127 $result = true;
128
129 if(isset($rooms['toAddCategory']))
130 {
131 foreach ($rooms['toAddCategory'] as &$toAddId)
132 {
133 $toAddId = (int)$toAddId;
134 }
135 $toAddIds = implode(',', $rooms['toAddCategory']);
136 $sqlStr = "
137 UPDATE $tableName
138 SET CATEGORY_ID = $categoryId
139 WHERE SECTION_ID IN ($toAddIds)
140 ";
141 $result = $DB->Query($sqlStr, true);
142 }
143
144 if($result && isset($rooms['toRemoveCategory']))
145 {
146 foreach ($rooms['toRemoveCategory'] as &$toRemoveId)
147 {
148 $toRemoveId = (int)$toRemoveId;
149 }
150 $toRemoveIds = implode(',', $rooms['toRemoveCategory']);
151 $sqlStr = "
152 UPDATE $tableName
153 SET CATEGORY_ID = null
154 WHERE SECTION_ID IN ($toRemoveIds)
155 ";
156 $result = $DB->Query($sqlStr, true);
157 }
158
159 if(!$result)
160 {
161 $this->addError(new Error('An error occurred while saving the category'));
162 }
163
164 return $this;
165 }
166
172 public function deleteCategory(): Manager
173 {
174 if ($this->getError())
175 {
176 return $this;
177 }
178
179 $this->category->delete();
180
181 if ($this->category->getError())
182 {
183 $this->addError($this->category->getError());
184 }
185
186 global $DB;
187 $tableName = LocationTable::getTableName();
188 $categoryId = $this->category->getId();
189
190 $DB->Query("
191 UPDATE $tableName
192 SET CATEGORY_ID = null
193 WHERE CATEGORY_ID = $categoryId
194 ");
195
196 return $this;
197 }
198
199 public static function getCategoryList()
200 {
201 $categories = RoomCategoryTable::getList([
202 'select' => [
203 'ID',
204 'NAME',
205 ]
206 ])
207 ->fetchAll()
208 ;
209
210 foreach ($categories as &$category)
211 {
212 $category['NAME'] = Emoji::decode($category['NAME']);
213 }
214
215 return $categories;
216 }
217
224 public static function checkCategoryName(?string $name): ?string
225 {
226 $name = trim($name);
227
228 if (empty($name))
229 {
230 return '';
231 }
232
233 return $name;
234 }
235
236 public function addPullEvent($event): Manager
237 {
238 if ($this->getError())
239 {
240 return $this;
241 }
242
243 \Bitrix\Calendar\Util::addPullEvent(
244 $event,
245 \CCalendar::GetCurUserId(),
246 [
247 'ID' => $this->category->getId()
248 ],
249 );
250
251 return $this;
252 }
253
254 public function clearCache(): Manager
255 {
256 \Bitrix\Calendar\Rooms\Manager::createInstance()->clearCache();
257
258 return $this;
259 }
260}
static checkCategoryName(?string $name)
Definition manager.php:224
static createInstance(Category $category)
Definition manager.php:27