Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sectionconnectionfactory.php
1<?php
2
4
5use Bitrix\Calendar\Core;
9use Bitrix\Calendar\Internals\EO_Section;
12use Bitrix\Calendar\Internals\EO_SectionConnection;
18
20{
22 private $mapperFactory;
23 public function __construct()
24 {
25 $this->mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
26 }
27
38 public function getSectionConnection(array $params): ?SectionConnection
39 {
40 $statement = SectionConnectionTable::query()->setSelect(['*']);
41 if (!empty($params['filter']))
42 {
43 $statement->setFilter($params['filter']);
44 }
45 if ($link = $statement->fetchObject())
46 {
47 try {
48 return $this->prepareLink(
49 $link,
50 $params['connectionObject'] ?? null,
51 $params['sectionObject'] ?? null,
52 );
53 } catch (BaseException $e) {
54 return null;
55 }
56 }
57 else
58 {
59 return null;
60 }
61 }
62
73 public function getFromSectionConnection(Section $section, Connection $connection): ?SectionConnection
74 {
75 $linkData = SectionConnectionTable::query()
76 ->setSelect(['*'])
77 ->addFilter('CONNECTION_ID', $connection->getId())
78 ->addFilter('SECTION_ID', $section->getId())
79 ->exec()->fetchObject()
80 ;
81 if (empty($linkData))
82 {
83 return null;
84 }
85
86 return $this->prepareLink($linkData, $connection, $section);
87 }
88
99 public function getListByConnection(Connection $connection, bool $onlyActive = true): array
100 {
101 $statement = SectionConnectionTable::query();
102 $statement->setSelect(['*']);
103 $statement->addFilter('CONNECTION_ID', $connection->getId());
104 if ($onlyActive)
105 {
106 $statement->addFilter('=ACTIVE', 'Y');
107 }
108
109 $links = $statement->exec();
110 $result = [];
111 while ($link = $links->fetchObject())
112 {
113 $result[] = $this->prepareLink($link, $connection);
114 }
115
116 return $result;
117 }
118
131 private function prepareLink(
132 EO_SectionConnection $link,
133 ?Connection $connection = null,
134 ?Section $section = null
136 {
137 if ($connection === null)
138 {
139 $connection = $this->mapperFactory->getConnection()->getById($link->getConnectionId());
140 }
141 $section = $section
142 ?? ($link->getSection()
143 ? $this->buildSection($link->getSection())
144 : null
145 );
146 if ($section === null)
147 {
148 $section = $this->mapperFactory->getSection()->getById($link->getSectionId());
149
150 if (!$section)
151 {
152 throw new BaseException('Section not found');
153 }
154
155 }
156
157 $item = new SectionConnection();
158 $item
159 ->setId($link->getId())
160 ->setSection($section)
161 ->setConnection($connection)
162 ->setVendorSectionId($link->getVendorSectionId())
163 ->setSyncToken($link->getSyncToken())
164 ->setPageToken($link->getPageToken())
165 ->setActive($link->getActive())
166 ->setLastSyncDate(new Core\Base\Date($link->getLastSyncDate()))
167 ->setLastSyncStatus($link->getLastSyncStatus())
168 ->setVersionId($link->get('VERSION_ID'))
169 ;
170
171 return $item;
172 }
173
179 private function buildSection(EO_Section $EOSection): Section
180 {
181 return (new SectionBuilderFromDataManager($EOSection))->build();
182 }
183}
getListByConnection(Connection $connection, bool $onlyActive=true)
getFromSectionConnection(Section $section, Connection $connection)