Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sectionmanager.php
1<?php
2
4
16use Bitrix\Calendar\Sync\Internals\HasContextTrait;
24use Bitrix\Dav\Internals\DavConnectionTable;
33use Exception;
34use Throwable;
35
37{
38 use HasContextTrait;
39
40 private const IMPORT_SECTIONS_LIMIT = 10;
41
42 public function __construct(Office365Context $context)
43 {
44 $this->context = $context;
45 parent::__construct($context->getConnection());
46 }
47
64 public function create(Section $section, SectionContext $context): Result
65 {
66 $result = new Result();
67
68 $dto = new SectionDto([
69 'name' => $this->getSectionName($section),
70 'color' => ColorConverter::toOffice($section->getColor()),
71 ]);
72 $sectionDto = $this->context->getVendorSyncService()->createSection($dto);
73 if (!empty($sectionDto->id) && !empty($sectionDto->changeKey))
74 {
75 $sectionConnection = (new SectionConnection())
76 ->setSection($section)
77 ->setConnection($this->connection)
78 ->setVendorSectionId($sectionDto->id)
79 ->setVersionId($sectionDto->changeKey)
80 ->setLastSyncStatus(Sync\Dictionary::SYNC_STATUS['success'])
81 ->setOwner($section->getOwner())
82 ->setActive(true)
83 ;
84 $syncSection = (new SyncSection())
85 ->setSection($section)
86 ->setSectionConnection($sectionConnection)
87 ->setVendorName($section->getExternalType())
88 ->setAction(Sync\Dictionary::SYNC_STATUS['success'])
89 ;
90 $result->setData([
91 $sectionDto->id => $syncSection,
92 'id' => $sectionDto->id,
93 'version' => $sectionDto->changeKey,
94 'syncSection' => $syncSection,
95 ]);
96 }
97 else
98 {
99 $result->addError(new Error('Error of create section into Office365'));
100 }
101
102 return $result;
103 }
104
110 private function getSectionName(Section $section): string
111 {
112 if ($section->getExternalType() === Section::LOCAL_EXTERNAL_TYPE)
113 {
114 IncludeModuleLangFile($_SERVER['DOCUMENT_ROOT'] . BX_ROOT . '/modules/calendar/classes/general/calendar.php');
115
116 return Loc::getMessage('EC_CALENDAR_BITRIX24_NAME')
117 . ' '
118 . ($section->getName() ?: $section->getId());
119 }
120
121 return $section->getName() ?: $section->getId();
122 }
123
127 public function update(Section $section, SectionContext $context): Result
128 {
129 $result = new Result();
130 $sectionLink = $context->getSectionConnection();
131
132 $dto = new SectionDto([
133 'name' => $this->getSectionName($sectionLink->getSection()),
134 'id' => $context->getSectionConnection()->getVendorSectionId(),
135 // 'color' => ColorConverter::toOffice(
136 // $sectionLink->getSection()->getColor()
137 // )
138 ]);
139 try
140 {
141 if ($sectionLink->isPrimary())
142 {
143 $dto->name = null;
144 }
145 $sectionDto = $this->context->getVendorSyncService()->updateSection($dto);
146 $result->setData([
147 'id' => $sectionDto->id,
148 'version' => $sectionDto->changeKey,
149 'sectionConnection' => $sectionLink,
150 ]);
151 }
152 catch (NotFoundException $e)
153 {
154 throw $e;
155 }
156 catch (Exception $e)
157 {
158 $result->addError(new Error($e->getMessage()));
159 }
160
161 return $result;
162 }
163
164 public function delete(Section $section, SectionContext $context): Result
165 {
166 $result = new Result();
167 $sectionLink = $context->getSectionConnection();
168
169 $dto = new SectionDto([
170 'id' => $sectionLink->getVendorSectionId(),
171 ]);
172 try
173 {
174 $this->context->getVendorSyncService()->deleteSection($dto);
175 $result->setData([
176 'sectionConnection' => $sectionLink,
177 ]);
178 }
179 catch (Exception $e)
180 {
181 $result->addError(new Error($e->getMessage()));
182 }
183
184 return $result;
185 }
186
203 public function getSections($connection): array
204 {
205 $result = array();
206 $converter = $this->context->getConverter();
207 $sections = $this->context->getVendorSyncService()->getSections();
208 foreach ($sections as $sectionDto)
209 {
210 if ($sectionDto->canShare)
211 {
212 $result[] = [
213 'section' => $converter->convertSection($sectionDto),
214 'id' => $sectionDto->id,
215 'version' => $sectionDto->changeKey,
216 'is_primary' => $sectionDto->isDefaultCalendar,
217 ];
218 }
219 }
220
221 return $result;
222 }
223
232 public function subscribe(SectionConnection $link): Result
233 {
234 $makeDateTime = static function (string $date)
235 {
236 $phpDateTime = new \DateTime($date);
237 return DateTime::createFromPhp($phpDateTime);
238 };
239
240 $result = new Result();
263 $data = $this->context->getVendorSyncService()->subscribeSection($link);
264
265 if ($data)
266 {
267 $result->setData([
268 'CHANNEL_ID' => $data['channelId'],
269 'RESOURCE_ID' => $data['id'],
270 'EXPIRES' => $makeDateTime($data['expirationDateTime']),
271 ]);
272 }
273 else
274 {
275 $result->addError(new Error('Error of create subscription.'));
276 }
277
278 return $result;
279 }
280
289 public function resubscribe(Push $push): Result
290 {
291 $result = new Result();
292 $data = $this->context->getVendorSyncService()->resubscribe($push->getResourceId());
293
294 $result->setData([
295 'EXPIRES' => DateTime::createFromPhp(new \DateTime($data['expirationDateTime'])),
296 ]);
297 return $result;
298 }
299
303 public static function updateSectionsAgent(): string
304 {
305 $agentName = __METHOD__ . '();';
306
307 try
308 {
309 if (!Loader::includeModule('dav') || !Loader::includeModule('calendar'))
310 {
311 throw new SystemException('Module not found');
312 }
313 $connectionsEO = DavConnectionTable::query()
314 ->setSelect(['*'])
315 ->addFilter('=ACCOUNT_TYPE', [Helper::ACCOUNT_TYPE])
316 ->addFilter('=IS_DELETED', 'N')
317 ->addOrder('SYNCHRONIZED')
318 ->setLimit(self::IMPORT_SECTIONS_LIMIT)
319 ->exec();
320
321 while ($connectionEO = $connectionsEO->fetchObject())
322 {
323 try
324 {
325 $connection = (new BuilderConnectionFromDM($connectionEO))->build();
326 $manager = new IncomingManager($connection);
327 $result = $manager->importSections();
328 if ($result->isSuccess())
329 {
330 DavConnectionTable::update($connectionEO->getId(), [
331 'SYNCHRONIZED' => new DateTime(),
332 'LAST_RESULT' => '[200] OK',
333 ]);
334 }
335 else
336 {
337 DavConnectionTable::update($connectionEO->getId(), [
338 'SYNCHRONIZED' => new DateTime(),
339 'LAST_RESULT' => '[400] Error.',
340 ]);
341 }
342 }
343 catch (Exception $e)
344 {
345 DavConnectionTable::update($connectionEO->getId(), [
346 'SYNCHRONIZED' => new DateTime(),
347 'LAST_RESULT' => '[400] Error.',
348 ]);
349 }
350
351 }
352 } catch (BaseException|Throwable $e) {
353 // TODO: write into log
354 }
355
356 return $agentName;
357 }
358
359 public function getAvailableExternalType(): array
360 {
361 return [Helper::ACCOUNT_TYPE];
362 }
363}
create(Section $section, SectionContext $context)
update(Section $section, SectionContext $context)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static createFromPhp(\DateTime $datetime)
Definition datetime.php:232