1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
vendorsyncmanager.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sync\Icloud;
4
5use Bitrix\Calendar\Core;
6use Bitrix\Calendar\Integration\Pull\PushCommand;
7use Bitrix\Calendar\Sync\Builders\BuilderConnectionFromArray;
8use Bitrix\Calendar\Sync\Builders\BuilderConnectionFromDM;
9use Bitrix\Calendar\Sync\Managers;
10use Bitrix\Calendar\Util;
11use Bitrix\Main\ArgumentException;
12use Bitrix\Main\DI\ServiceLocator;
13use Bitrix\Calendar\Sync\Managers\NotificationManager;
14use Bitrix\Main\ObjectPropertyException;
15use Bitrix\Main\SystemException;
16
18{
19 private const STATUS_ERROR = 'error';
20 private const STATUS_SUCCESS = 'success';
22 protected Helper $helper;
24 protected ?string $error = null;
26 protected ?Context $context = null;
30 private Core\Mappers\Factory $mapperFactory;
31
32 public function __construct()
33 {
34 $this->helper = new Helper();
35 $this->mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
36 }
37
50 public function syncIcloudConnection(int $connectionId): array
51 {
52 $userId = \CCalendar::GetUserId();
53 $connection = $this->mapperFactory->getConnection()->getById($connectionId);
54
55 if (!$connection)
56 {
57 return [
58 'status' => self::STATUS_ERROR,
59 'message' => 'Connection not found',
60 ];
61 }
62
63 if ($connection->getOwner()->getId() !== $userId)
64 {
65 return [
66 'status' => self::STATUS_ERROR,
67 'message' => 'Access Denied',
68 ];
69 }
70
72 if (!$result)
73 {
74 return [
75 'status' => self::STATUS_ERROR,
76 'message' => 'Error while trying to import events',
77 ];
78 }
79
81 PushCommand::ProcessSyncConnection,
82 $userId,
83 [
84 'vendorName' => $this->helper::ACCOUNT_TYPE,
85 'stage' => 'import_finished',
86 'accountName' => $connection->getServer()->getUserName(),
87 ]
88 );
89
91 if (!$result->isSuccess())
92 {
93 return [
94 'status' => self::STATUS_ERROR,
95 'message' => 'Error while trying to export events',
96 ];
97 }
98
100 PushCommand::ProcessSyncConnection,
101 $userId,
102 [
103 'vendorName' => $this->helper::ACCOUNT_TYPE,
104 'stage' => 'export_finished',
105 'accountName' => $connection->getServer()->getUserName(),
106 ]
107 );
108
109 NotificationManager::addFinishedSyncNotificationAgent(
110 $userId,
111 $this->helper::ACCOUNT_TYPE
112 );
113
114 return [
115 'status' => self::STATUS_SUCCESS
116 ];
117 }
118
127 public function initConnection(string $appleId, string $appPassword): ?int
128 {
129 $params = [
130 'ENTITY_ID' => \CCalendar::GetCurUserId(),
131 'ENTITY_TYPE' => Core\Role\User::TYPE,
132 'SERVER_HOST' => $this->helper::SERVER_PATH,
133 'SERVER_USERNAME' => $appleId,
134 'SERVER_PASSWORD' => $appPassword,
135 'NAME' => str_replace('#NAME#', $appleId, $this->helper::CONNECTION_NAME)
136 ];
137
138 $connection = null;
139 $calendarPath = $this->getSyncService()->getCalendarServerPath($params);
140 if (!$calendarPath)
141 {
142 $this->error = 'Error while trying to get calendars path';
143
144 return null;
145 }
146 $owner = Core\Role\Helper::getRole(\CCalendar::GetUserId(), Core\Role\User::TYPE);
147 $connectionManager = new Managers\ConnectionManager();
148 $connections = $connectionManager->getConnectionsData($owner, [Helper::ACCOUNT_TYPE]);
149 $connectionManager->deactivateConnections($connections);
150 foreach ($connections as $con)
151 {
152 $existPath = $con->getServerScheme()
153 . '://'
154 . $con->getServerHost()
155 . ':'
156 . $con->getServerPort()
157 . $con->getServerPath()
158 ;
159 if ($existPath === $calendarPath)
160 {
161 $connection = (new BuilderConnectionFromDM($con))->build();
162 break;
163 }
164 }
165
166 if ($connection)
167 {
168 $connection->setDeleted(false);
169 $connection->getServer()->setPassword($appPassword);
170 $connectionManager->update($connection);
171
172 return $connection->getId();
173 }
174
175 return $this->addConnection($params, $calendarPath);
176 }
177
184 public function addConnection(array $connection, string $calendarPath): ?int
185 {
186 $connection['SERVER_HOST'] = $calendarPath;
187 $fields = [
188 'ENTITY_TYPE' => $connection['ENTITY_TYPE'],
189 'ENTITY_ID' => $connection['ENTITY_ID'],
190 'ACCOUNT_TYPE' => $this->helper::ACCOUNT_TYPE,
191 'NAME' => $connection['NAME'],
192 'SERVER' => $connection['SERVER_HOST'],
193 'SERVER_USERNAME' => $connection['SERVER_USERNAME'],
194 'SERVER_PASSWORD' => $connection['SERVER_PASSWORD']
195 ];
196
197 $connectionId = \CDavConnection::Add($fields);
198
199 if ($connectionId)
200 {
201 return $connectionId;
202 }
203
204 $this->error = 'Error while trying to save connection';
205
206 return null;
207 }
208
209 private function getSyncService(): VendorSyncService
210 {
211 if (!$this->syncService)
212 {
213 $this->syncService = new VendorSyncService();
214 }
215
216 return $this->syncService;
217 }
218
219 public function getError(): string
220 {
221 return $this->error;
222 }
223}
$connection
Определения actionsdefinitions.php:38
$con
Определения admin_tab.php:7
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getRole(int $id, string $type)
Определения helper.php:17
const TYPE
Определения user.php:9
syncIcloudConnection(int $connectionId)
Определения vendorsyncmanager.php:50
initConnection(string $appleId, string $appPassword)
Определения vendorsyncmanager.php:127
addConnection(array $connection, string $calendarPath)
Определения vendorsyncmanager.php:184
static addPullEvent(PushCommand $command, int $userId, array $params=[])
Определения util.php:385
</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
Определения culture.php:9
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$fields
Определения yandex_run.php:501