Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sectionmanager.php
1<?php
2
4
5use Bitrix\Calendar\Core;
17
19{
20
21 public const CREATE_PATH = '/calendars/';
22 public const CALENDAR_PATH = '/calendars/%CALENDAR_ID%';
23 public const CALENDAR_LIST_PATH = '/users/me/calendarList/%CALENDAR_ID%';
24
33 public function create(Core\Section\Section $section, SectionContext $context = null): Result
34 {
35 $result = new Result();
36
37 try
38 {
39 // TODO: Remake it: move this logic to parent::request().
40 // Or, better, in separate class.
41 $this->httpClient->query(
43 $this->prepareCreateUrl(),
44 $this->encode((new SectionConverter($section))->convertForEdit())
45 );
46
47 if ($this->isRequestSuccess())
48 {
49 $resultData = $this->prepareResult($this->httpClient->getResult(), $section);
50 $this->updateSectionColor($resultData['syncSection']);
51 $result->setData($resultData);
52 }
53 else
54 {
55 $response = Json::decode($this->httpClient->getResult());
56 if (!empty($response['error']))
57 {
58 $error = $response['error'];
59 switch ($error['code'])
60 {
61 case 409:
62 throw new ConflictException($error['message'], $error['code']);
63 case 401:
64 $this->handleUnauthorize($this->connection);
65 $result->addError(new Error($error['message'], $error['code']));
66 break;
67 default:
68 if (!empty($error['code']))
69 {
70 $result->addError(new Error($error['message'], $error['code']));
71 }
72 else
73 {
74 $result->addError(new Error('Uncknown Google API error', 400));
75 }
76 }
77 }
78 else
79 {
80 $result->addError(new Error('do not create section'));
81 }
82 }
83 }
84 catch (ArgumentException $e)
85 {
86 AddMessage2Log($e->getMessage(), 'calendar', 2, true);
87 $result->addError(new Error('failed to create an section in google'));
88 }
89 catch (ObjectException $e)
90 {
91 AddMessage2Log($e->getMessage(), 'calendar', 2, true);
92 $result->addError(new Error('failed to convert section'));
93 }
94
95 return $result;
96 }
97
105 public function update(Core\Section\Section $section, SectionContext $context): Result
106 {
107 $result = new Result();
108
109 try
110 {
111 // TODO: Remake it: move this logic to parent::request().
112 // Or, better, in separate class.
113 $this->httpClient->query(
115 $this->prepareCalendarUrl($context->getSectionConnection()->getVendorSectionId()),
116 $this->encode((new SectionConverter($section))->convertForEdit())
117 );
118
119 if ($this->isRequestSuccess())
120 {
121 $resultData = $this->prepareResult($this->httpClient->getResult(), $section);
122 $this->updateSectionColor($resultData['syncSection']);
123 $result->setData($resultData);
124 }
125 else
126 {
127 $response = Json::decode($this->httpClient->getResult());
128 if (!empty($response['error']))
129 {
130 $error = $response['error'];
131 switch ($error['code'])
132 {
133 case 401:
134 $this->handleUnauthorize($this->connection);
135 $result->addError(new Error($error['message'], $error['code']));
136 break;
137 default:
138 if (!empty($error['code']))
139 {
140 $result->addError(new Error($error['message'], $error['code']));
141 }
142 else
143 {
144 $result->addError(new Error('Uncknown Google API error', 400));
145 }
146 }
147 }
148 $result->addError(new Error('do not update section'));
149 }
150 }
151 catch (ArgumentException $e)
152 {
153 AddMessage2Log($e->getMessage(), 'calendar', 2, true);
154 $result->addError(new Error('failed to update an section in google'));
155 }
156
157 return $result;
158 }
159
167 public function delete(Core\Section\Section $section, SectionContext $context): Result
168 {
169 $result = new Result();
170
171 // TODO: Remake it: move this logic to parent::request().
172 // Or, better, in separate class.
173 $this->httpClient->query(
175 $this->prepareCalendarUrl($context->getSectionConnection()->getVendorSectionId())
176 );
177 if (!$this->isRequestDeleteSuccess())
178 {
179 $response = Json::decode($this->httpClient->getResult());
180 if (!empty($response['error']))
181 {
182 $error = $response['error'];
183 switch ($error['code'])
184 {
185 case 401:
186 $this->handleUnauthorize($this->connection);
187 $result->addError(new Error($error['message'], $error['code']));
188 break;
189 default:
190 if (!empty($error['code']))
191 {
192 $result->addError(new Error($error['message'], $error['code']));
193 }
194 else
195 {
196 $result->addError(new Error('Uncknown Google API error', 400));
197 }
198 }
199 }
200 else
201 {
202 $result->addError(new Error('failed to delete an section in google'));
203 }
204
205 }
206
207 return $result;
208 }
209
210 private function prepareCreateUrl(): string
211 {
212 return $this->connection->getServer()->getFullPath() . self::CREATE_PATH;
213 }
214
215 private function prepareCalendarUrl(string $vendorSectionId): string
216 {
217 return Server::mapUri(
218 $this->connection->getServer()->getFullPath()
219 . self::CALENDAR_PATH,
220 [
221 '%CALENDAR_ID%' => Server::getEncodePath($vendorSectionId)
222 ]
223 );
224 }
225
232 private function prepareResult(string $result, Core\Section\Section $section): array
233 {
234 $externalSection = Json::decode($result);
235
237 $externalSection,
238 $section,
239 $this->connection)
240 )->build();
241
242 $syncSection = (new Sync\Entities\SyncSection())
243 ->setSection($section)
244 ->setVendorName(Core\Section\Section::LOCAL_EXTERNAL_TYPE)
245 ->setAction(Sync\Dictionary::SYNC_EVENT_ACTION['success'])
246 ->setSectionConnection($sectionConnection)
247 ;
248
249 // TODO: get rid of array structure. It's better to replace with SyncSection object
250 return [
251 'id' => $externalSection['id'],
252 'version' => $externalSection['etag'],
253 'syncSection' => $syncSection,
254 ];
255 }
256
262 private function encode(array $section)
263 {
264 return Json::encode($section, JSON_UNESCAPED_SLASHES);
265 }
266
270 public function getAvailableExternalType(): array
271 {
272 return array_values(Dictionary::ACCESS_ROLE_TO_EXTERNAL_TYPE);
273 }
274
278 private function updateSectionColor(Sync\Entities\SyncSection $syncSection): void
279 {
280 $this->httpClient->put(
281 $this->createCalendarListUpdateUrl($syncSection->getSectionConnection()->getVendorSectionId()),
282 $this->prepareUpdateColorParams($syncSection)
283 );
284 }
285
286 private function createCalendarListUpdateUrl(?string $getVendorSectionId): string
287 {
288 return Server::mapUri(
289 $this->connection->getServer()->getFullPath()
290 . self::CALENDAR_LIST_PATH
291 . '?' . preg_replace('/(%3D)/', '=', http_build_query(['colorRgbFormat' => "True"])),
292 [
293 '%CALENDAR_ID%' => Server::getEncodePath($getVendorSectionId),
294 ]
295 );
296 }
297
301 private function prepareUpdateColorParams(Sync\Entities\SyncSection $syncSection)
302 {
303 $parameters = [];
304
305 if ($color = $syncSection->getSection()->getColor())
306 {
307 $parameters['backgroundColor'] = $color;
308 $parameters['foregroundColor'] = '#ffffff';
309 }
310
311 $parameters['selected'] = 'true';
312
313 return Json::encode($parameters, JSON_UNESCAPED_SLASHES);
314 }
315}
create(Core\Section\Section $section, SectionContext $context=null)
update(Core\Section\Section $section, SectionContext $context)