Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
vendorsyncmanager.php
1<?php
2
4
5use Bitrix\Calendar\Core;
15
17{
18 private const STATUS_ERROR = 'error';
19 private const STATUS_SUCCESS = 'success';
21 protected Helper $helper;
23 protected ?string $error = null;
25 protected ?Context $context = null;
29 private Core\Mappers\Factory $mapperFactory;
30
31 public function __construct()
32 {
33 $this->helper = new Helper();
34 $this->mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
35 }
36
49 public function syncIcloudConnection(int $connectionId): array
50 {
51 $userId = \CCalendar::GetUserId();
52 $connection = $this->mapperFactory->getConnection()->getById($connectionId);
53
54 if (!$connection)
55 {
56 return [
57 'status' => self::STATUS_ERROR,
58 'message' => 'Connection not found',
59 ];
60 }
61
62 if ($connection->getOwner()->getId() !== $userId)
63 {
64 return [
65 'status' => self::STATUS_ERROR,
66 'message' => 'Access Denied',
67 ];
68 }
69
70 $result = Managers\DataSyncManager::createInstance()->dataSync($userId);
71 if (!$result)
72 {
73 return [
74 'status' => self::STATUS_ERROR,
75 'message' => 'Error while trying to import events',
76 ];
77 }
78
80 'process_sync_connection',
81 $userId,
82 [
83 'vendorName' => $this->helper::ACCOUNT_TYPE,
84 'stage' => 'import_finished',
85 'accountName' => $connection->getServer()->getUserName(),
86 ]
87 );
88
89 $result = (new Managers\OutgoingManager($connection))->export();
90 if (!$result->isSuccess())
91 {
92 return [
93 'status' => self::STATUS_ERROR,
94 'message' => 'Error while trying to export events',
95 ];
96 }
97
99 'process_sync_connection',
100 $userId,
101 [
102 'vendorName' => $this->helper::ACCOUNT_TYPE,
103 'stage' => 'export_finished',
104 'accountName' => $connection->getServer()->getUserName(),
105 ]
106 );
107
108 NotificationManager::addFinishedSyncNotificationAgent(
109 $userId,
110 $this->helper::ACCOUNT_TYPE
111 );
112
113 return [
114 'status' => self::STATUS_SUCCESS
115 ];
116 }
117
126 public function initConnection(string $appleId, string $appPassword): ?int
127 {
128 $params = [
129 'ENTITY_ID' => \CCalendar::GetCurUserId(),
130 'ENTITY_TYPE' => Core\Role\User::TYPE,
131 'SERVER_HOST' => $this->helper::SERVER_PATH,
132 'SERVER_USERNAME' => $appleId,
133 'SERVER_PASSWORD' => $appPassword,
134 'NAME' => str_replace('#NAME#', $appleId, $this->helper::CONNECTION_NAME)
135 ];
136
137 $connection = null;
138 $calendarPath = $this->getSyncService()->getCalendarServerPath($params);
139 if (!$calendarPath)
140 {
141 $this->error = 'Error while trying to get calendars path';
142
143 return null;
144 }
145 $owner = Core\Role\Helper::getRole(\CCalendar::GetUserId(), Core\Role\User::TYPE);
146 $connectionManager = new Managers\ConnectionManager();
147 $connections = $connectionManager->getConnectionsData($owner, [Helper::ACCOUNT_TYPE]);
148 foreach ($connections as $con)
149 {
150 $existPath = $con->getServerScheme()
151 . '://'
152 . $con->getServerHost()
153 . ':'
154 . $con->getServerPort()
155 . $con->getServerPath()
156 ;
157 if ($existPath === $calendarPath)
158 {
159 $connection = (new BuilderConnectionFromDM($con))->build();
160 break;
161 }
162 }
163
164 if ($connection)
165 {
166 if ($connection->isDeleted())
167 {
168 $connection->setDeleted(false);
169 $connection->getServer()->setPassword($appPassword);
170 }
171
172 $connectionManager->update($connection);
173 return $connection->getId();
174 }
175
176 return $this->addConnection($params, $calendarPath);
177 }
178
185 public function addConnection(array $connection, string $calendarPath): ?int
186 {
187 $connection['SERVER_HOST'] = $calendarPath;
188 $fields = [
189 'ENTITY_TYPE' => $connection['ENTITY_TYPE'],
190 'ENTITY_ID' => $connection['ENTITY_ID'],
191 'ACCOUNT_TYPE' => $this->helper::ACCOUNT_TYPE,
192 'NAME' => $connection['NAME'],
193 'SERVER' => $connection['SERVER_HOST'],
194 'SERVER_USERNAME' => $connection['SERVER_USERNAME'],
195 'SERVER_PASSWORD' => $connection['SERVER_PASSWORD']
196 ];
197
198 $connectionId = \CDavConnection::Add($fields);
199
200 if ($connectionId)
201 {
202 return $connectionId;
203 }
204
205 $this->error = 'Error while trying to save connection';
206
207 return null;
208 }
209
210 private function getSyncService(): VendorSyncService
211 {
212 if (!$this->syncService)
213 {
214 $this->syncService = new VendorSyncService();
215 }
216
217 return $this->syncService;
218 }
219
220 public function getError(): string
221 {
222 return $this->error;
223 }
224}
initConnection(string $appleId, string $appPassword)
addConnection(array $connection, string $calendarPath)
static addPullEvent(string $command, int $userId, array $params=[])
Definition util.php:373