Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
sectionmanager.php
1
<?php
2
3
namespace
Bitrix\Calendar\Sync\Office365
;
4
5
use
Bitrix\Calendar\Core\Base\BaseException
;
6
use
Bitrix\Calendar\Core\Section\Section
;
7
use
Bitrix\Calendar\Sync
;
8
use
Bitrix\Calendar\Sync\Builders\BuilderConnectionFromDM
;
9
use
Bitrix\Calendar\Sync\Connection\SectionConnection
;
10
use
Bitrix\Calendar\Sync\Entities\SyncSection
;
11
use
Bitrix\Calendar\Sync\Exceptions\ApiException
;
12
use
Bitrix\Calendar\Sync\Exceptions\AuthException
;
13
use
Bitrix\Calendar\Sync\Exceptions\ConflictException
;
14
use
Bitrix\Calendar\Sync\Exceptions\NotFoundException
;
15
use
Bitrix\Calendar\Sync\Exceptions\RemoteAccountException
;
16
use Bitrix\Calendar\Sync\Internals\HasContextTrait;
17
use
Bitrix\Calendar\Sync\Managers\IncomingManager
;
18
use
Bitrix\Calendar\Sync\Managers\SectionManagerInterface
;
19
use
Bitrix\Calendar\Sync\Office365\Converter\ColorConverter
;
20
use
Bitrix\Calendar\Sync\Office365\Dto\SectionDto
;
21
use
Bitrix\Calendar\Sync\Push\Push
;
22
use
Bitrix\Calendar\Sync\Util\Result
;
23
use
Bitrix\Calendar\Sync\Util\SectionContext
;
24
use Bitrix\Dav\Internals\DavConnectionTable;
25
use
Bitrix\Main\ArgumentException
;
26
use
Bitrix\Main\ArgumentNullException
;
27
use
Bitrix\Main\Error
;
28
use
Bitrix\Main\Loader
;
29
use
Bitrix\Main\LoaderException
;
30
use
Bitrix\Main\Localization\Loc
;
31
use
Bitrix\Main\SystemException
;
32
use
Bitrix\Main\Type\DateTime
;
33
use Exception;
34
use Throwable;
35
36
class
SectionManager
extends
AbstractManager
implements
SectionManagerInterface
37
{
38
use HasContextTrait;
39
40
private
const
IMPORT_SECTIONS_LIMIT = 10;
41
42
public
function
__construct
(
Office365Context
$context)
43
{
44
$this->context = $context;
45
parent::__construct($context->
getConnection
());
46
}
47
64
public
function
create
(
Section
$section,
SectionContext
$context):
Result
65
{
66
$result =
new
Result
();
67
68
$dto =
new
SectionDto
([
69
'name'
=> $this->getSectionName($section),
70
'color'
=>
ColorConverter::toOffice
($section->
getColor
()),
71
]);
72
$sectionDto = $this->context->getVendorSyncService()->createSection($dto);
73
if
(!empty($sectionDto->id) && !empty($sectionDto->changeKey))
74
{
75
$sectionConnection = (
new
SectionConnection
())
76
->setSection($section)
77
->setConnection($this->connection)
78
->setVendorSectionId($sectionDto->id)
79
->setVersionId($sectionDto->changeKey)
80
->setLastSyncStatus(Sync\
Dictionary::SYNC_STATUS
[
'success'
])
81
->setOwner($section->
getOwner
())
82
->setActive(
true
)
83
;
84
$syncSection = (
new
SyncSection
())
85
->setSection($section)
86
->setSectionConnection($sectionConnection)
87
->setVendorName($section->
getExternalType
())
88
->setAction(Sync\
Dictionary::SYNC_STATUS
[
'success'
])
89
;
90
$result->setData([
91
$sectionDto->id => $syncSection,
92
'id'
=> $sectionDto->id,
93
'version'
=> $sectionDto->changeKey,
94
'syncSection'
=> $syncSection,
95
]);
96
}
97
else
98
{
99
$result->addError(
new
Error
(
'Error of create section into Office365'
));
100
}
101
102
return
$result;
103
}
104
110
private
function
getSectionName(
Section
$section): string
111
{
112
if
($section->
getExternalType
() === Section::LOCAL_EXTERNAL_TYPE)
113
{
114
IncludeModuleLangFile($_SERVER[
'DOCUMENT_ROOT'
] . BX_ROOT .
'/modules/calendar/classes/general/calendar.php'
);
115
116
return
Loc::getMessage
(
'EC_CALENDAR_BITRIX24_NAME'
)
117
.
' '
118
. ($section->
getName
() ?: $section->
getId
());
119
}
120
121
return
$section->
getName
() ?: $section->
getId
();
122
}
123
127
public
function
update
(
Section
$section,
SectionContext
$context):
Result
128
{
129
$result =
new
Result
();
130
$sectionLink = $context->
getSectionConnection
();
131
132
$dto =
new
SectionDto
([
133
'name'
=> $this->getSectionName($sectionLink->getSection()),
134
'id'
=> $context->
getSectionConnection
()->getVendorSectionId(),
135
// 'color' => ColorConverter::toOffice(
136
// $sectionLink->getSection()->getColor()
137
// )
138
]);
139
try
140
{
141
if
($sectionLink->isPrimary())
142
{
143
$dto->name =
null
;
144
}
145
$sectionDto = $this->context->getVendorSyncService()->updateSection($dto);
146
$result->setData([
147
'id'
=> $sectionDto->id,
148
'version'
=> $sectionDto->changeKey,
149
'sectionConnection'
=> $sectionLink,
150
]);
151
}
152
catch
(
NotFoundException
$e)
153
{
154
throw
$e;
155
}
156
catch
(Exception $e)
157
{
158
$result->addError(
new
Error
($e->getMessage()));
159
}
160
161
return
$result;
162
}
163
164
public
function
delete
(
Section
$section,
SectionContext
$context):
Result
165
{
166
$result =
new
Result
();
167
$sectionLink = $context->getSectionConnection();
168
169
$dto =
new
SectionDto
([
170
'id'
=> $sectionLink->getVendorSectionId(),
171
]);
172
try
173
{
174
$this->context->getVendorSyncService()->deleteSection($dto);
175
$result->setData([
176
'sectionConnection'
=> $sectionLink,
177
]);
178
}
179
catch
(Exception $e)
180
{
181
$result->addError(
new
Error
($e->getMessage()));
182
}
183
184
return
$result;
185
}
186
203
public
function
getSections
(
$connection
): array
204
{
205
$result = array();
206
$converter = $this->context->getConverter();
207
$sections = $this->context->getVendorSyncService()->getSections();
208
foreach
($sections as $sectionDto)
209
{
210
if
($sectionDto->canShare)
211
{
212
$result[] = [
213
'section'
=> $converter->convertSection($sectionDto),
214
'id'
=> $sectionDto->id,
215
'version'
=> $sectionDto->changeKey,
216
'is_primary'
=> $sectionDto->isDefaultCalendar,
217
];
218
}
219
}
220
221
return
$result;
222
}
223
232
public
function
subscribe(
SectionConnection
$link):
Result
233
{
234
$makeDateTime =
static
function
(
string
$date)
235
{
236
$phpDateTime = new \DateTime($date);
237
return
DateTime::createFromPhp
($phpDateTime);
238
};
239
240
$result =
new
Result
();
263
$data = $this->context->getVendorSyncService()->subscribeSection($link);
264
265
if
($data)
266
{
267
$result->setData([
268
'CHANNEL_ID'
=> $data[
'channelId'
],
269
'RESOURCE_ID'
=> $data[
'id'
],
270
'EXPIRES'
=> $makeDateTime($data[
'expirationDateTime'
]),
271
]);
272
}
273
else
274
{
275
$result->addError(
new
Error
(
'Error of create subscription.'
));
276
}
277
278
return
$result;
279
}
280
289
public
function
resubscribe
(
Push
$push):
Result
290
{
291
$result =
new
Result
();
292
$data = $this->context->getVendorSyncService()->resubscribe($push->
getResourceId
());
293
294
$result->setData([
295
'EXPIRES'
=>
DateTime::createFromPhp
(
new
\
DateTime
($data[
'expirationDateTime'
])),
296
]);
297
return
$result;
298
}
299
303
public
static
function
updateSectionsAgent
(): string
304
{
305
$agentName = __METHOD__ .
'();'
;
306
307
try
308
{
309
if
(!Loader::includeModule(
'dav'
) || !Loader::includeModule(
'calendar'
))
310
{
311
throw
new
SystemException
(
'Module not found'
);
312
}
313
$connectionsEO = DavConnectionTable::query()
314
->setSelect([
'*'
])
315
->addFilter(
'=ACCOUNT_TYPE'
, [
Helper::ACCOUNT_TYPE
])
316
->addFilter(
'=IS_DELETED'
,
'N'
)
317
->addOrder(
'SYNCHRONIZED'
)
318
->setLimit(self::IMPORT_SECTIONS_LIMIT)
319
->exec();
320
321
while
($connectionEO = $connectionsEO->fetchObject())
322
{
323
try
324
{
325
$connection
= (
new
BuilderConnectionFromDM
($connectionEO))->build();
326
$manager =
new
IncomingManager
(
$connection
);
327
$result = $manager->importSections();
328
if
($result->isSuccess())
329
{
330
DavConnectionTable::update($connectionEO->getId(), [
331
'SYNCHRONIZED'
=>
new
DateTime
(),
332
'LAST_RESULT'
=>
'[200] OK'
,
333
]);
334
}
335
else
336
{
337
DavConnectionTable::update($connectionEO->getId(), [
338
'SYNCHRONIZED'
=>
new
DateTime
(),
339
'LAST_RESULT'
=>
'[400] Error.'
,
340
]);
341
}
342
}
343
catch
(Exception $e)
344
{
345
DavConnectionTable::update($connectionEO->getId(), [
346
'SYNCHRONIZED'
=>
new
DateTime
(),
347
'LAST_RESULT'
=>
'[400] Error.'
,
348
]);
349
}
350
351
}
352
}
catch
(
BaseException
|Throwable $e) {
353
// TODO: write into log
354
}
355
356
return
$agentName;
357
}
358
359
public
function
getAvailableExternalType
(): array
360
{
361
return
[
Helper::ACCOUNT_TYPE
];
362
}
363
}
Bitrix\Bizproc\Error
Definition
error.php:8
Bitrix\Calendar\Core\Base\BaseException
Definition
baseexception.php:8
Bitrix\Calendar\Core\Section\Section
Definition
section.php:11
Bitrix\Calendar\Core\Section\Section\getId
getId()
Definition
section.php:317
Bitrix\Calendar\Core\Section\Section\getName
getName()
Definition
section.php:450
Bitrix\Calendar\Core\Section\Section\getExternalType
getExternalType()
Definition
section.php:437
Bitrix\Calendar\Core\Section\Section\getColor
getColor()
Definition
section.php:405
Bitrix\Calendar\Core\Section\Section\getOwner
getOwner()
Definition
section.php:365
Bitrix\Calendar\Sync\Builders\BuilderConnectionFromDM
Definition
builderconnectionfromdm.php:19
Bitrix\Calendar\Sync\Connection\SectionConnection
Definition
sectionconnection.php:16
Bitrix\Calendar\Sync\Dictionary\SYNC_STATUS
const SYNC_STATUS
Definition
dictionary.php:15
Bitrix\Calendar\Sync\Entities\SyncSection
Definition
syncsection.php:8
Bitrix\Calendar\Sync\Exceptions\ApiException
Definition
apiexception.php:6
Bitrix\Calendar\Sync\Exceptions\AuthException
Definition
authexception.php:6
Bitrix\Calendar\Sync\Exceptions\ConflictException
Definition
conflictexception.php:6
Bitrix\Calendar\Sync\Exceptions\NotFoundException
Definition
notfoundexception.php:6
Bitrix\Calendar\Sync\Exceptions\RemoteAccountException
Definition
remoteaccountexception.php:6
Bitrix\Calendar\Sync\Icloud\IncomingManager
Definition
incomingmanager.php:11
Bitrix\Calendar\Sync\Managers\IncomingManager
Definition
incomingmanager.php:37
Bitrix\Calendar\Sync\Managers\ServiceBase\$connection
Connection $connection
Definition
servicebase.php:13
Bitrix\Calendar\Sync\Office365\AbstractManager
Definition
abstractmanager.php:9
Bitrix\Calendar\Sync\Office365\Converter\ColorConverter
Definition
colorconverter.php:6
Bitrix\Calendar\Sync\Office365\Converter\ColorConverter\toOffice
static toOffice(?string $color=null)
Definition
colorconverter.php:88
Bitrix\Calendar\Sync\Office365\Dto\SectionDto
Definition
sectiondto.php:6
Bitrix\Calendar\Sync\Office365\Helper\ACCOUNT_TYPE
const ACCOUNT_TYPE
Definition
helper.php:10
Bitrix\Calendar\Sync\Office365\Office365Context
Definition
office365context.php:28
Bitrix\Calendar\Sync\Office365\Office365Context\getConnection
getConnection()
Definition
office365context.php:171
Bitrix\Calendar\Sync\Office365\SectionManager
Definition
sectionmanager.php:37
Bitrix\Calendar\Sync\Office365\SectionManager\resubscribe
resubscribe(Push $push)
Definition
sectionmanager.php:289
Bitrix\Calendar\Sync\Office365\SectionManager\updateSectionsAgent
static updateSectionsAgent()
Definition
sectionmanager.php:303
Bitrix\Calendar\Sync\Office365\SectionManager\getAvailableExternalType
getAvailableExternalType()
Definition
sectionmanager.php:359
Bitrix\Calendar\Sync\Office365\SectionManager\__construct
__construct(Office365Context $context)
Definition
sectionmanager.php:42
Bitrix\Calendar\Sync\Office365\SectionManager\create
create(Section $section, SectionContext $context)
Definition
sectionmanager.php:64
Bitrix\Calendar\Sync\Office365\SectionManager\update
update(Section $section, SectionContext $context)
Definition
sectionmanager.php:127
Bitrix\Calendar\Sync\Office365\SectionManager\getSections
getSections($connection)
Definition
sectionmanager.php:203
Bitrix\Calendar\Sync\Push\Push
Definition
push.php:9
Bitrix\Calendar\Sync\Push\Push\getResourceId
getResourceId()
Definition
push.php:96
Bitrix\Calendar\Sync\Util\Result
Definition
result.php:6
Bitrix\Calendar\Sync\Util\SectionContext
Definition
sectioncontext.php:8
Bitrix\Calendar\Sync\Util\SectionContext\getSectionConnection
getSectionConnection()
Definition
sectioncontext.php:28
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\ArgumentNullException
Definition
exception.php:54
Bitrix\Main\Error
Definition
error.php:14
Bitrix\Main\LoaderException
Definition
loader.php:582
Bitrix\Main\Loader
Definition
loader.php:12
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\ORM\Data\Result
Definition
result.php:16
Bitrix\Main\SystemException
Definition
exception.php:8
Bitrix\Main\Type\DateTime
Definition
datetime.php:9
Bitrix\Main\Type\DateTime\createFromPhp
static createFromPhp(\DateTime $datetime)
Definition
datetime.php:232
Bitrix\Calendar\Sync\Managers\SectionManagerInterface
Definition
sectionmanagerinterface.php:12
Bitrix\Calendar\Sync\Office365
Definition
abstractmanager.php:3
Bitrix\Calendar\Sync
modules
calendar
lib
sync
office365
sectionmanager.php
Создано системой
1.10.0