1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
sectionmanager.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sync\Google;
4
5use Bitrix\Calendar\Core;
6use Bitrix\Calendar\Sync;
7use Bitrix\Calendar\Sync\Connection\Server;
8use Bitrix\Calendar\Sync\Exceptions\ConflictException;
9use Bitrix\Calendar\Sync\Managers\SectionManagerInterface;
10use Bitrix\Calendar\Sync\Util\SectionContext;
11use Bitrix\Main\ArgumentException;
12use Bitrix\Main\Error;
13use Bitrix\Main\ObjectException;
14use Bitrix\Main\Web\HttpClient;
15use Bitrix\Main\Web\Json;
16use Bitrix\Calendar\Sync\Util\Result;
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
32 public function create(Core\Section\Section $section, SectionContext $context = null): Result
33 {
34 $result = new Result();
35
36 try
37 {
38 // TODO: Remake it: move this logic to parent::request().
39 // Or, better, in separate class.
40 $this->httpClient->query(
41 HttpClient::HTTP_POST,
42 $this->prepareCreateUrl(),
43 $this->encode((new SectionConverter($section))->convertForEdit())
44 );
45
46 if ($this->isRequestSuccess())
47 {
48 $resultData = $this->prepareResult($this->httpClient->getResult(), $section);
49 $this->updateSectionColor($resultData['syncSection']);
50 $result->setData($resultData);
51 }
52 else
53 {
54 $response = Json::decode($this->httpClient->getResult());
55 if (!empty($response['error']))
56 {
57 $error = $response['error'];
58 switch ($error['code'])
59 {
60 case 409:
61 throw new ConflictException($error['message'], $error['code']);
62 case 401:
63 $this->handleUnauthorize($this->connection);
64 $result->addError(new Error($error['message'], $error['code']));
65 break;
66 default:
67 if (!empty($error['code']))
68 {
69 $result->addError(new Error($error['message'], $error['code']));
70 }
71 else
72 {
73 $result->addError(new Error('Uncknown Google API error', 400));
74 }
75 }
76 }
77 else
78 {
79 $result->addError(new Error('do not create section'));
80 }
81 }
82 }
83 catch (ArgumentException $e)
84 {
85 $result->addError(new Error('failed to create an section in google'));
86 }
87 catch (ObjectException $e)
88 {
89 $result->addError(new Error('failed to convert section'));
90 }
91
92 return $result;
93 }
94
103 {
104 $result = new Result();
105
106 try
107 {
108 // TODO: Remake it: move this logic to parent::request().
109 // Or, better, in separate class.
110 $this->httpClient->query(
111 HttpClient::HTTP_PUT,
112 $this->prepareCalendarUrl($context->getSectionConnection()->getVendorSectionId()),
113 $this->encode((new SectionConverter($section))->convertForEdit())
114 );
115
116 if ($this->isRequestSuccess())
117 {
118 $resultData = $this->prepareResult($this->httpClient->getResult(), $section);
119 $this->updateSectionColor($resultData['syncSection']);
120 $result->setData($resultData);
121 }
122 else
123 {
124 $response = Json::decode($this->httpClient->getResult());
125 if (!empty($response['error']))
126 {
127 $error = $response['error'];
128 switch ($error['code'])
129 {
130 case 401:
131 $this->handleUnauthorize($this->connection);
132 $result->addError(new Error($error['message'], $error['code']));
133 break;
134 default:
135 if (!empty($error['code']))
136 {
137 $result->addError(new Error($error['message'], $error['code']));
138 }
139 else
140 {
141 $result->addError(new Error('Uncknown Google API error', 400));
142 }
143 }
144 }
145 $result->addError(new Error('do not update section'));
146 }
147 }
148 catch (ArgumentException $e)
149 {
150 $result->addError(new Error('failed to update an section in google'));
151 }
152
153 return $result;
154 }
155
163 public function delete(Core\Section\Section $section, SectionContext $context): Result
164 {
165 $result = new Result();
166
167 // TODO: Remake it: move this logic to parent::request().
168 // Or, better, in separate class.
169 $this->httpClient->query(
170 HttpClient::HTTP_DELETE,
171 $this->prepareCalendarUrl($context->getSectionConnection()->getVendorSectionId())
172 );
173 if (!$this->isRequestDeleteSuccess())
174 {
175 $response = Json::decode($this->httpClient->getResult());
176 if (!empty($response['error']))
177 {
178 $error = $response['error'];
179 switch ($error['code'])
180 {
181 case 401:
182 $this->handleUnauthorize($this->connection);
183 $result->addError(new Error($error['message'], $error['code']));
184 break;
185 default:
186 if (!empty($error['code']))
187 {
188 $result->addError(new Error($error['message'], $error['code']));
189 }
190 else
191 {
192 $result->addError(new Error('Uncknown Google API error', 400));
193 }
194 }
195 }
196 else
197 {
198 $result->addError(new Error('failed to delete an section in google'));
199 }
200
201 }
202
203 return $result;
204 }
205
206 private function prepareCreateUrl(): string
207 {
208 return $this->connection->getServer()->getFullPath() . self::CREATE_PATH;
209 }
210
211 private function prepareCalendarUrl(string $vendorSectionId): string
212 {
213 return Server::mapUri(
214 $this->connection->getServer()->getFullPath()
215 . self::CALENDAR_PATH,
216 [
217 '%CALENDAR_ID%' => Server::getEncodePath($vendorSectionId)
218 ]
219 );
220 }
221
228 private function prepareResult(string $result, Core\Section\Section $section): array
229 {
230 $externalSection = Json::decode($result);
231
233 $externalSection,
234 $section,
235 $this->connection)
236 )->build();
237
238 $syncSection = (new Sync\Entities\SyncSection())
239 ->setSection($section)
240 ->setVendorName(Core\Section\Section::LOCAL_EXTERNAL_TYPE)
241 ->setAction(Sync\Dictionary::SYNC_EVENT_ACTION['success'])
242 ->setSectionConnection($sectionConnection)
243 ;
244
245 // TODO: get rid of array structure. It's better to replace with SyncSection object
246 return [
247 'id' => $externalSection['id'],
248 'version' => $externalSection['etag'],
249 'syncSection' => $syncSection,
250 ];
251 }
252
258 private function encode(array $section)
259 {
260 return Json::encode($section, JSON_UNESCAPED_SLASHES);
261 }
262
267 {
268 return array_values(Dictionary::ACCESS_ROLE_TO_EXTERNAL_TYPE);
269 }
270
274 private function updateSectionColor(Sync\Entities\SyncSection $syncSection): void
275 {
276 if (empty($syncSection->getSectionConnection()?->getVendorSectionId()))
277 {
278 return;
279 }
280
281 $this->httpClient->put(
282 $this->createCalendarListUpdateUrl($syncSection->getSectionConnection()?->getVendorSectionId()),
283 $this->prepareUpdateColorParams($syncSection)
284 );
285 }
286
287 private function createCalendarListUpdateUrl(?string $getVendorSectionId): string
288 {
289 return Server::mapUri(
290 $this->connection->getServer()->getFullPath()
291 . self::CALENDAR_LIST_PATH
292 . '?' . preg_replace('/(%3D)/', '=', http_build_query(['colorRgbFormat' => "True"])),
293 [
294 '%CALENDAR_ID%' => Server::getEncodePath($getVendorSectionId),
295 ]
296 );
297 }
298
302 private function prepareUpdateColorParams(Sync\Entities\SyncSection $syncSection)
303 {
304 $parameters = [];
305
306 if ($color = $syncSection->getSection()->getColor())
307 {
308 $parameters['backgroundColor'] = $color;
309 $parameters['foregroundColor'] = '#ffffff';
310 }
311
312 $parameters['selected'] = 'true';
313
314 return Json::encode($parameters, JSON_UNESCAPED_SLASHES);
315 }
316}
const SYNC_EVENT_ACTION
Определения dictionary.php:47
const ACCESS_ROLE_TO_EXTERNAL_TYPE
Определения dictionary.php:8
create(Core\Section\Section $section, SectionContext $context=null)
Определения sectionmanager.php:32
update(Core\Section\Section $section, SectionContext $context)
Определения sectionmanager.php:102
Определения error.php:15
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$context
Определения csv_new_setup.php:223
$response
Определения result.php:21