Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
outgoingsectionmanager.php
1<?php
2
4
6use Bitrix\Calendar\Core;
9
11{
14
19 public function __construct(Sync\Factories\FactoryBase $factory, Sync\Entities\SyncSectionMap $sectionMap)
20 {
21 $this->factory = $factory;
22 $this->sectionMap = $sectionMap;
23 }
24
28 public function export(): self
29 {
31 foreach ($this->sectionMap as $key => $syncSection)
32 {
33 if ($syncSection->getSectionConnection() === null)
34 {
35 $result = $this->safeCreate($syncSection->getSection());
36 }
37 else
38 {
39 $result = $this->safeUpdate($syncSection);
40 }
41
42 if ($result->isSuccess())
43 {
44 $exportedSyncSection = $result->getData()['syncSection'];
45 if ($exportedSyncSection)
46 {
47 $this->sectionMap->updateItem($exportedSyncSection,
48 $exportedSyncSection->getSectionConnection()->getVendorSectionId());
49 $this->sectionMap->remove($key);
50 }
51 }
52 }
53
54 return $this;
55 }
56
60 public function getSyncSectionMap(): Sync\Entities\SyncSectionMap
61 {
62 return $this->sectionMap;
63 }
64
69 private function safeCreate(Core\Section\Section $section): Sync\Util\Result
70 {
71 $result = new Result();
72 $counter = 0;
73 $originalName = $section->getName();
74 $sectionManager = $this->factory->getSectionManager();
75 do
76 {
77 try
78 {
79 $result = $sectionManager->create($section, new Sync\Util\SectionContext([]));
80 $success = true;
81 }
82 catch (ConflictException $e)
83 {
84 $counter++;
85 $section->setName($originalName . " ($counter)");
86 $success = false;
87 }
88 }
89 while (!$success);
90
91 $section->setName($originalName);
92
93 return $result;
94 }
95
96 private function safeUpdate(Sync\Entities\SyncSection $syncSection): Sync\Util\Result
97 {
98 try
99 {
100 $result = $this->factory->getSectionManager()->update(
101 $syncSection->getSection(),
102 $this->prepareContextForUpdate($syncSection)
103 );
104 }
105 catch (Sync\Exceptions\NotFoundException $e)
106 {
107 $this->clearBrokenSyncSection($syncSection);
108 $result = $this->safeCreate($syncSection->getSection());
109 }
110
111 return $result;
112 }
113
120 private function clearBrokenSyncSection(Sync\Entities\SyncSection $syncSection)
121 {
122 global $DB;
123 $sql = "DELETE link FROM b_calendar_event_connection link
124 inner join b_calendar_event as event ON event.ID=link.EVENT_ID
125 where event.SECTION_ID = '" . $syncSection->getSection()->getId() . "'
126 and link.CONNECTION_ID = '". $syncSection->getSectionConnection()->getConnection()->getId() ."'
127 ";
128 $DB->Query($sql);
129 (new Core\Mappers\SectionConnection())->delete(
130 $syncSection->getSectionConnection(),
131 ['softDelete' => false]
132 );
133 $syncSection->setSectionConnection(null);
134 }
135
141 private function prepareContextForUpdate(Sync\Entities\SyncSection $syncSection)
142 {
143 return (new Sync\Util\SectionContext())->setSectionConnection($syncSection->getSectionConnection());
144 }
145}
__construct(Sync\Factories\FactoryBase $factory, Sync\Entities\SyncSectionMap $sectionMap)