Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
syncsectionfactory.php
1<?php
2
4
7use Bitrix\Calendar\Internals\EO_SectionConnection;
11use Bitrix\Dav\Internals\DavConnectionTable;
12use Bitrix\Dav\Internals\EO_DavConnection;
15use Bitrix\Calendar\Core;
17use Bitrix\Main\Entity\ReferenceField;
23
25{
28
29 public function __construct()
30 {
31 Loader::includeModule('dav');
33 $mapperHelper = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
34 $this->sectionConnectionMapper = $mapperHelper->getSectionConnection();
35 $this->sectionMapper = $mapperHelper->getSection();
36 }
37
43 {
44 $syncSectionMap = new SyncSectionMap();
45 $connection = $factory->getConnection();
46 $connectionId = $connection->getId();
47 $ownerId = $connection->getOwner()->getId();
48 $this->getLocalSyncSectionMapByUserId(
49 $ownerId,
50 $connectionId,
51 $syncSectionMap
52 );
53 $this->getExternalSyncSectionMapByUserId(
54 $ownerId,
55 $connectionId,
56 $syncSectionMap,
57 $factory->getSectionManager()->getAvailableExternalType()
58 );
59
60 return $syncSectionMap;
61 }
62
75 public function getLocalSyncSectionMapByUserId(
76 int $userId,
77 int $connectionId,
78 Sync\Entities\SyncSectionMap $syncSectionMap
79 ): void
80 {
81 $sectionDb = SectionTable::query()
82 ->where('OWNER_ID', $userId)
83 ->where('EXTERNAL_TYPE', Core\Mappers\Section::SECTION_TYPE_LOCAL)
84 ->where('CAL_TYPE', Core\Role\User::TYPE)
85 ->registerRuntimeField('SECTION_CONNECTION',
86 new ReferenceField(
87 'SYNC_DATA',
88 SectionConnectionTable::getEntity(),
89 Join::on('ref.SECTION_ID', 'this.ID')->where('ref.CONNECTION_ID', $connectionId),
90 ['join_type' => Join::TYPE_LEFT]
91 )
92 )
93 ->registerRuntimeField('CONNECTION',
94 new ReferenceField(
95 'CONNECTION',
96 DavConnectionTable::getEntity(),
97 Join::on('ref.ID', 'this.SECTION_CONNECTION.CONNECTION_ID'),
98 ['join_type' => Join::TYPE_LEFT]
99 )
100 )
101 ->setSelect([
102 'ID',
103 'NAME',
104 'XML_ID',
105 'ACTIVE',
106 'DESCRIPTION',
107 'COLOR',
108 'CAL_TYPE',
109 'OWNER_ID',
110 'EXTERNAL_TYPE',
111 'CONNECTION.ACCOUNT_TYPE',
112 'SECTION_CONNECTION.*',
113 'SECTION_CONNECTION.SECTION',
114 'SECTION_CONNECTION.CONNECTION',
115 ])
116 ->exec()
117 ;
118
119 while ($sectionDM = $sectionDb->fetchObject())
120 {
121 $sectionId = null;
122 $syncSection = new Sync\Entities\SyncSection();
123
124 $section = (new SectionBuilderFromDataManager($sectionDM))->build();
125 $syncSection
126 ->setSection($section)
127 ->setAction('success')
128 ;
129
131 if ($sectionConnectionDM = $sectionDM->get('SECTION_CONNECTION'))
132 {
133 $sectionConnection = (new Sync\Builders\BuilderSectionConnectionFromDM($sectionConnectionDM))->build();
134
135 $sectionConnection->setSection($section);
136 $syncSection->setSectionConnection($sectionConnection);
137 $sectionId = $sectionConnection->getVendorSectionId();
138 }
139
141 if ($sectionDM->getExternalType() !== Section::SECTION_TYPE_LOCAL && ($connectionDM = $sectionDM->get('CONNECTION')))
142 {
143 $syncSection->setVendorName($connectionDM->getAccountType());
144 }
145 else
146 {
147 $syncSection->setVendorName($sectionDM->getExternalType() ?? Section::SECTION_TYPE_LOCAL);
148 }
149
150 $sectionId = $sectionId ?? (string)$section->getId();
151
152 $syncSectionMap->add($syncSection, $sectionId);
153 }
154 }
155
167 public function getExternalSyncSectionMapByUserId(
168 int $userId,
169 int $connectionId,
170 Sync\Entities\SyncSectionMap $syncSectionMap,
171 array $externalType
172 ): void
173 {
174 $sectionDb = SectionTable::query()
175 ->where('OWNER_ID', $userId)
176 ->whereIn('EXTERNAL_TYPE', $externalType)
177 ->where('CAL_TYPE', Core\Role\User::TYPE)
178 ->registerRuntimeField('SECTION_CONNECTION',
179 new ReferenceField(
180 'SYNC_DATA',
181 SectionConnectionTable::getEntity(),
182 Join::on('ref.SECTION_ID', 'this.ID'),
183 ['join_type' => Join::TYPE_LEFT]
184 )
185 )
186 ->registerRuntimeField('CONNECTION',
187 new ReferenceField(
188 'CONNECTION',
189 DavConnectionTable::getEntity(),
190 Join::on('ref.ID', 'this.SECTION_CONNECTION.CONNECTION_ID'),
191 ['join_type' => Join::TYPE_LEFT]
192 )
193 )
194 ->setSelect([
195 'ID',
196 'NAME',
197 'XML_ID',
198 'ACTIVE',
199 'DESCRIPTION',
200 'COLOR',
201 'CAL_TYPE',
202 'OWNER_ID',
203 'EXTERNAL_TYPE',
204 'CONNECTION.ACCOUNT_TYPE',
205 'SECTION_CONNECTION.*',
206 'SECTION_CONNECTION.SECTION',
207 'SECTION_CONNECTION.CONNECTION',
208 ])
209 ->exec()
210 ;
211
212 while ($sectionDM = $sectionDb->fetchObject())
213 {
214 $sectionId = null;
215 $syncSection = new Sync\Entities\SyncSection();
216
217 $section = (new SectionBuilderFromDataManager($sectionDM))->build();
218 $syncSection
219 ->setSection($section)
220 ->setAction('success')
221 ;
222
224 if ($sectionConnectionDM = $sectionDM->get('SECTION_CONNECTION'))
225 {
226 $sectionConnection = (new Sync\Builders\BuilderSectionConnectionFromDM($sectionConnectionDM))->build();
227 if (
228 ($sectionConnection->getConnection() !== null)
229 && ($sectionConnection->getConnection()->getId() !== $connectionId)
230 )
231 {
232 continue;
233 }
234
235 $sectionConnection->setSection($section);
236 $syncSection->setSectionConnection($sectionConnection);
237 $sectionId = $sectionConnection->getVendorSectionId();
238 }
239 else
240 {
241 continue;
242 }
243
245 if ($sectionDM->getExternalType() !== Section::SECTION_TYPE_LOCAL && ($connectionDM = $sectionDM->get('CONNECTION')))
246 {
247 $syncSection->setVendorName($connectionDM->getAccountType());
248 }
249 else
250 {
251 $syncSection->setVendorName($sectionDM->getExternalType() ?? Section::SECTION_TYPE_LOCAL);
252 }
253
254 $sectionId = $sectionId ?? (string)$section->getId();
255
256 $syncSectionMap->add($syncSection, $sectionId);
257 }
258 }
259}
Core Mappers SectionConnection $sectionConnectionMapper