1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
connectionmanager.php
См. документацию.
1<?php
2
4
16use Bitrix\Dav\Internals\DavConnectionTable;
17use Bitrix\Dav\Internals\EO_DavConnection_Collection;
27use CDavConnection;
28use Exception;
29
31{
32 public const INIT_STATUS = [
33 'existed' => 'existed',
34 'created' => 'created',
35 'activated' => 'activated',
36 ];
37
39 private $mapperFactory;
40
41 public function __construct()
42 {
43 $this->mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
44 }
45
57 public function getConnection(Role $owner, string $accountType, array $optionalFilter = []): ?Connection
58 {
59 $connection = null;
60 $connectionData = $this->getConnectionsData($owner, [$accountType], $optionalFilter);
61 foreach ($connectionData as $con)
62 {
64 break;
65 }
66
67 return $connection
68 ? (new BuilderConnectionFromDM($connection))->build()
69 : null
70 ;
71 }
72
84 public function getConnectionsData(
85 Role $owner,
87 array $optionalFilter = []
88 ): ?EO_DavConnection_Collection
89 {
90 $statement = DavConnectionTable::query()
91 ->setSelect(['*'])
92 ->addFilter('=ENTITY_TYPE', $owner->getType())
93 ->addFilter('=ENTITY_ID', $owner->getId())
94 ->addFilter('=ACCOUNT_TYPE', $type)
95 ;
96
97 if (!empty($optionalFilter))
98 {
99 foreach ($optionalFilter as $key => $value)
100 {
101 $statement->addFilter($key, $value);
102 }
103 }
104
105 return $statement->fetchCollection() ?: null;
106 }
107
114 {
115 try
116 {
117 $lastModified = new DateTime();
118 $fields = [
119 'ENTITY_TYPE' => $connection->getOwner()->getType(),
120 'ENTITY_ID' => $connection->getOwner()->getId(),
121 'ACCOUNT_TYPE' => $connection->getVendor()->getCode(),
122 'SYNC_TOKEN' => $connection->getToken(),
123 'NAME' => $connection->getName(),
124 'SERVER_SCHEME' => $connection->getServer()->getScheme(),
125 'SERVER_HOST' => $connection->getServer()->getHost(),
126 'SERVER_PORT' => $connection->getServer()->getPort(),
127 'SERVER_USERNAME' => $connection->getServer()->getUserName(),
128 'SERVER_PASSWORD' => $connection->getServer()->getPassword(),
129 'SERVER_PATH' => $connection->getServer()->getBasePath(),
130 'MODIFIED' => $lastModified,
131 'SYNCHRONIZED' => $lastModified,
132 'LAST_RESULT' => $connection->getStatus(),
133 'IS_DELETED' => $connection->isDeleted() ? 'Y' : 'N'
134 ];
135 $data = DavConnectionTable::update($connection->getId(), $fields)->getData();
136 $data['ID'] = $connection->getId();
137
138 return (new Result())->setData($data);
139 }
140 catch (Exception $e)
141 {
142 return (new Result())->addError(new Error($e->getMessage()));
143 }
144 }
145
155 public function initConnection(Role $owner, string $accountType, string $server): Result
156 {
157 $result = new Result();
158 $resultData = [];
159 try {
160 if (!Loader::includeModule('dav'))
161 {
162 throw new LoaderException('Module dav is required');
163 }
164 $accountName = $this->getSocialUserLogin($owner, $accountType);
165 if ($connection = $this->getConnection($owner, $accountType, ['=NAME' => $accountName]))
166 {
167 if ($connection->isDeleted())
168 {
169 $connection->setDeleted(false);
170 $this->update($connection);
171 $resultData['status'] = self::INIT_STATUS['activated'];
172 }
173 else
174 {
175 $resultData['status'] = self::INIT_STATUS['existed'];
176 }
177 }
178 else
179 {
181 $owner,
182 $accountType,
183 $accountName,
184 $server,
185 );
186 $resultData['status'] = self::INIT_STATUS['created'];
187 }
188 $resultData['connection'] = $connection;
189 }
190 catch (Exception $e)
191 {
192 $result->addError(new Error($e->getMessage()));
193 }
194
195 $result->setData($resultData);
196
197 return $result;
198 }
199
213 protected function createConnection(
214 Role $owner,
215 string $type,
216 string $name,
217 string $serverPath
218 ): Connection
219 {
220 $fields = [
221 'ENTITY_TYPE' => $owner->getType(),
222 'ENTITY_ID' => $owner->getId(),
223 'ACCOUNT_TYPE' => $type,
224 'NAME' => $name,
225 'SERVER' => $serverPath,
226 ];
227 if ($connectionId = CDavConnection::Add($fields))
228 {
229 return $this->mapperFactory->getConnection()->getById($connectionId);
230 }
231
232 throw new BaseException('Error of create new Dav connection');
233 }
234
245 protected function getSocialUserLogin(Role $owner, string $accountType): string
246 {
247 $user = UserTable::query()
248 ->addFilter('=USER_ID', $owner->getId())
249 ->addFilter('=EXTERNAL_AUTH_ID', $accountType)
250 ->setSelect(['LOGIN'])
251 ->fetch();
252
253 return $user['LOGIN'] ?? '';
254 }
255
256 public function deactivateConnections(?EO_DavConnection_Collection $connections): void
257 {
258 if ($connections === null)
259 {
260 return;
261 }
262
263 foreach ($connections as $connectionRow)
264 {
265 $connection = (new BuilderConnectionFromDM($connectionRow))->build();
266 if (!$connection->isDeleted())
267 {
269 }
270 }
271 }
272
285 {
286 $result = new Result();
287
288 if (!Loader::includeModule('dav'))
289 {
290 $result->addError(new Error('Module dav required'));
291 }
292
293 $updateResult = DavConnectionTable::update($connection->getId(), [
294 'IS_DELETED' => 'Y',
295 'SYNC_TOKEN' => null,
296 ]);
297 if ($updateResult->isSuccess())
298 {
299 $this->unsubscribeConnection($connection);
300
301 $accountType = $connection->getAccountType() === Sync\Google\Helper::GOOGLE_ACCOUNT_TYPE_API
302 ? 'google'
303 : $connection->getAccountType()
304 ;
305
307 PushCommand::DeleteSyncConnection,
308 $connection->getOwner()->getId(),
309 [
310 'syncInfo' => [
311 $accountType => [
312 'type' => $accountType,
313 ],
314 ],
315 'connectionId' => $connection->getId()
316 ]
317 );
318 }
319 else
320 {
321 $result->addErrors($updateResult->getErrors());
322 }
323
324 return $result;
325 }
326
340 {
341 $links = SectionConnectionTable::query()
342 ->addFilter('CONNECTION_ID', $connection->getId())
343 ->setSelect(['ID'])
344 ->exec()
345 ;
346
347 while ($link = $links->fetchObject())
348 {
349 SectionConnectionTable::update($link->getId(), [
350 'SYNC_TOKEN' => '',
351 'PAGE_TOKEN' => '',
352 ]);
353 PushTable::delete([
354 'ENTITY_TYPE' => 'SECTION_CONNECTION',
355 'ENTITY_ID' => $link->getId(),
356 ]);
357 }
358 }
359
361 {
362 global $DB;
363 $id = $connection->getId();
364 $DB->Query(
365 "UPDATE b_dav_connections as con SET con.IS_DELETED ='Y' WHERE con.ID = $id;",
366 true
367 );
368 $DB->Query(
369 "DELETE FROM b_calendar_section_connection WHERE CONNECTION_ID = $id;",
370 true
371 );
372 $DB->Query(
373 "DELETE FROM b_calendar_event_connection WHERE CONNECTION_ID = $id;",
374 true
375 );
376
377 $DB->Query("DELETE sect FROM b_calendar_section sect
378 LEFT JOIN b_calendar_section_connection link ON sect.ID = link.SECTION_ID
379 WHERE link.ID IS NULL
380 AND sect.EXTERNAL_TYPE = '{$connection->getVendor()->getCode()}'
381 AND sect.OWNER_ID = '{$connection->getOwner()->getId()}'
382 ;",
383 true
384 );
385 $DB->Query("DELETE event FROM b_calendar_event event
386 LEFT JOIN b_calendar_section sec ON event.SECTION_ID = sec.ID
387 WHERE sec.ID IS NULL
388 AND event.OWNER_ID = '{$connection->getOwner()->getId()}'
389 ;",
390 true
391 );
392 $DB->Query("DELETE push FROM b_calendar_push push
393 LEFT JOIN b_calendar_section_connection as sc on push.ENTITY_ID=sc.ID and push.ENTITY_TYPE='SECTION_CONNECTION'
394 WHERE sc.ID IS NULL;",
395 true
396 );
397 }
398}
$connection
Определения actionsdefinitions.php:38
$con
Определения admin_tab.php:7
$type
Определения options.php:106
const GOOGLE_ACCOUNT_TYPE_API
Определения helper.php:16
getConnection(Role $owner, string $accountType, array $optionalFilter=[])
Определения connectionmanager.php:57
unsubscribeConnection(Connection $connection)
Определения connectionmanager.php:339
deactivateConnection(Connection $connection)
Определения connectionmanager.php:284
disableConnection(Connection $connection)
Определения connectionmanager.php:360
update(Connection $connection)
Определения connectionmanager.php:113
initConnection(Role $owner, string $accountType, string $server)
Определения connectionmanager.php:155
getConnectionsData(Role $owner, array $type, array $optionalFilter=[])
Определения connectionmanager.php:84
getSocialUserLogin(Role $owner, string $accountType)
Определения connectionmanager.php:245
createConnection(Role $owner, string $type, string $name, string $serverPath)
Определения connectionmanager.php:213
deactivateConnections(?EO_DavConnection_Collection $connections)
Определения connectionmanager.php:256
Определения util.php:21
static addPullEvent(PushCommand $command, int $userId, array $params=[])
Определения util.php:385
Определения error.php:15
Определения loader.php:13
Определения user.php:48
$data['IS_AVAILABLE']
Определения .description.php:13
</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
global $DB
Определения cron_frame.php:29
$name
Определения menu_edit.php:35
$user
Определения mysql_to_pgsql.php:33
if(empty($signedUserToken)) $key
Определения quickway.php:257
$fields
Определения yandex_run.php:501