Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
factoriescollection.php
1<?php
2
4
10use Bitrix\Calendar\Internals\EO_SectionConnection;
18use Bitrix\Dav\Internals\DavConnectionTable;
25
27{
34 public static function createBySectionId(Section $section, array $availableService = null): FactoriesCollection
35 {
36 $collection = [];
37
38 if (!Loader::includeModule('dav'))
39 {
40 return new self($collection);
41 }
42 // $links = self::getLinks($section, $availableService);
43 $links = SectionConnectionTable::query()
44 ->setSelect(['*', 'CONNECTION'])
45 ->addFilter('=SECTION_ID', $section->getId())
46 ->addFilter('=CONNECTION.IS_DELETED', 'N')
47 ;
48
49 if ($availableService)
50 {
51 $links->addFilter('ACCOUNT_TYPE', $availableService);
52 }
53
54 $queryResult = $links->exec();
55
56 while ($link = $queryResult->fetchObject())
57 {
58 $connection = (new Sync\Builders\BuilderConnectionFromDM($link->getConnection()))->build();
59 $sectionConnection = (new Sync\Builders\BuilderSectionConnectionFromDM($link))->build();
60
61 $context = self::prepareContextForSection($connection, $sectionConnection, $section);
62
64 $connection->getVendor()->getCode(),
65 $connection,
66 $context
67 );
68 }
69
70 return new self($collection);
71 }
72
79 public static function createByEvent(Event $event, array $availableService = null)
80 {
81 $collection = [];
82 $links = self::getLinks($event->getSection(), $availableService);
83 $eventLinks = self::getEventLinksByConnectionId($event);
84
86 while ($link = $links->fetchObject())
87 {
88 $connection = (new Sync\Builders\BuilderConnectionFromDM($link->getConnection()))->build();
89 $sectionConnection = (new Sync\Builders\BuilderSectionConnectionFromDM($link))->build();
90
91 $context = self::prepareContextForSection($connection, $sectionConnection, $event->getSection());
92 $context->add('sync', 'eventConnections', $eventLinks->getItem($connection->getId()));
93
95 $connection->getVendor()->getCode(),
96 $connection,
97 $context
98 );
99 }
100
101 return new self($collection);
102 }
103
115 public static function createByUserId(int $userId, array $availableService = []): FactoriesCollection
116 {
117 $collection = [];
118 if (!Loader::includeModule('dav'))
119 {
120 return new self($collection);
121 }
122
123 if (!$availableService)
124 {
125 $availableService = [
126 Sync\Google\Factory::SERVICE_NAME,
127 Sync\Icloud\Factory::SERVICE_NAME,
128 Sync\Office365\Factory::SERVICE_NAME,
129 ];
130 }
131
132 $sectionConnection = DavConnectionTable::query()
133 ->setSelect(['*'])
134 ->where('ENTITY_ID', $userId)
135 ->where('ENTITY_TYPE', 'user')
136 ->where('IS_DELETED', 'N')
137 ->whereIn('ACCOUNT_TYPE', $availableService)
138 ->exec()
139 ;
140 $context = new Context([]);
141
142 while ($connectionDM = $sectionConnection->fetchObject())
143 {
144 $connection = (new Sync\Builders\BuilderConnectionFromDM($connectionDM))->build();
145
147 $connection->getVendor()->getCode(),
148 $connection,
149 $context
150 );
151 }
152
153 return new self($collection);
154 }
155
166 public static function createBySection(Section $section): FactoriesCollection
167 {
168 $collection = [];
169
170 if (!Loader::includeModule('dav'))
171 {
172 return new self($collection);
173 }
174
175 $links = SectionConnectionTable::query()
176 ->setSelect(['*', 'CONNECTION'])
177 ->where('SECTION_ID', $section->getId())
178 ->where('CONNECTION.IS_DELETED', 'N')
179 ->whereIn('CONNECTION.ACCOUNT_TYPE', [
180 Sync\Google\Factory::SERVICE_NAME,
181 Sync\Icloud\Factory::SERVICE_NAME,
182 Sync\Office365\Factory::SERVICE_NAME,
183 ])
184 ->exec()
185 ;
186
187 while ($link = $links->fetchObject())
188 {
189 $connection = (new Sync\Builders\BuilderConnectionFromDM($link->getConnection()))->build();
190 $context = new Context([
191 'section_sync_data' => $link,
192 ]);
193
195 $connection->getVendor()->getCode(),
196 $connection,
197 $context
198 );
199 }
200
201 return new self(array_filter($collection));
202 }
203
213 private static function prepareContextForSection(
214 Connection $connection,
215 Sync\Connection\SectionConnection $sectionConnection,
216 Section $section
217 ): Context
218 {
219 $connectionsMap = new Sync\Connection\ConnectionMap();
220 $connectionsMap->add($connection, $connection->getId());
221
222 $sectionConnectionsMap = new Sync\Connection\SectionConnectionMap();
223 $sectionConnectionsMap->add($sectionConnection, $section->getId());
224
225 $sectionsMap = new SectionMap();
226 $sectionsMap->add($section, $section->getId());
227
228 return new Context([
229 'sections' => $sectionsMap,
230 'sectionConnections' => $sectionConnectionsMap,
231 'connections' => $connectionsMap
232 ]);
233 }
234
240 public static function createByConnection(Connection $connection): FactoriesCollection
241 {
242 $context = new Context([
243 'connection_data' => $connection,
244 ]);
245 $factory = FactoryBuilder::create($connection->getVendor()->getCode(), $connection, $context);
246
247 return new self([$factory]);
248 }
249
258 private static function getLinks(
259 Section $section,
260 ?array $availableService
261 ): Query
262 {
263 Loader::includeModule('dav');
264 $links = SectionConnectionTable::query()
265 ->setSelect(['*', 'CONNECTION'])
266 ->addFilter('SECTION_ID', $section->getId())
267 ->addFilter('=CONNECTION.IS_DELETED', 'N')
268 ;
269
270 if ($availableService)
271 {
272 $links->addFilter('ACCOUNT_TYPE', $availableService);
273 }
274
275 return $links;
276 }
277
278 private static function getServer($connection): Server
279 {
280 return new Server($connection);
281 }
282
291 private static function getEventLinksByConnectionId(Event $event): Map
292 {
293 $links = EventConnectionTable::query()
294 ->setSelect(['*'])
295 ->addFilter('EVENT_ID', $event->getId())
296 ->exec()
297 ;
298
299 $map = new Sync\Connection\EventConnectionMap();
300
301 while ($link = $links->fetchObject())
302 {
303 $map->add((new BuilderEventConnectionFromDM($link))->build(), $link->getConnectionId());
304 }
305
306 return $map;
307 }
308}
static createBySectionId(Section $section, array $availableService=null)
static createByUserId(int $userId, array $availableService=[])
static create(string $accountType, Sync\Connection\Connection $connection, Sync\Util\Context $context)