Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
section.php
1<?php
2
4
6use Bitrix\Calendar\Core;
7use Bitrix\Calendar\Internals\EO_Section;
17use CCalendarSect;
18use Exception;
19
20class Section extends Mapper implements BaseMapperInterface
21{
22 public const SECTION_TYPE_LOCAL = 'local';
23
24 public const DEFAULT_SORT = 100;
25
31 private function convertToArray(Core\Section\Section $section): array
32 {
33 return [
34 'NAME' => $section->getName(),
35 'ACTIVE' => $section->isActive() ? self::POSITIVE_ANSWER : self::NEGATIVE_ANSWER,
36 'DESCRIPTION' => $section->getDescription(),
37 'COLOR' => $section->getColor(),
38 'TEXT_COLOR' => $section->getTextColor(),
39 'SORT' => $section->getSort() ?? self::DEFAULT_SORT,
40 'CAL_TYPE' => $section->getType(),
41 'OWNER_ID' => !$section->getOwner() ?: $section->getOwner()->getId(),
42 'TIMESTAMP_X' => new DateTime,
43 'XML_ID' => $section->getXmlId(),
44 'EXTERNAL_ID' => $section->getExternalId(),
45 'GAPI_CALENDAR_ID' => $section->getGoogleId(),
46 'EXPORT' => $section->getExport(),
47 'CREATED_BY' => !$section->getCreator() ?: $section->getCreator()->getId(),
48 'PARENT_ID' => $section->getParentId(),
49 'DATE_CREATE' => new DateTime,
50 'DAV_EXCH_CAL' => $section->getDavExchangeCal(),
51 'DAV_EXCH_MOD' => $section->getDavExchangeMod(),
52 'CAL_DAV_CON' => $section->getCalDavConnectionId(),
53 'CAL_DAV_CAL' => $section->getCalDavCal(),
54 'CAL_DAV_MOD' => $section->getCalDavMod(),
55 'IS_EXCHANGE' => $section->isExchange() ? 1 : 0,
56 'SYNC_TOKEN' => $section->getSyncToken(),
57 'EXTERNAL_TYPE' => $section->getExternalType(),
58 'PAGE_TOKEN' => $section->getPageToken(),
59 ];
60 }
61
71 protected function getOneEntityByFilter(array $filter): ?object
72 {
73 if ($sectionData = SectionTable::query()
74 ->setFilter($filter)
75 ->setSelect(['*'])
76 ->fetchObject()
77 ) {
78 return $this->convertToObject($sectionData);
79 }
80
81 return null;
82 }
83
89 protected function convertToObject($objectEO): Core\Base\EntityInterface
90 {
91 return (new SectionBuilderFromDataManager($objectEO))->build();
92 }
93
97 protected function getEntityName(): string
98 {
99 return 'section';
100 }
101
111 protected function createEntity($entity, array $params = []): ?Core\Base\EntityInterface
112 {
113 $arrayEntity = $this->prepareArrayEntityForDB($entity);
114
115 $result = SectionTable::add($arrayEntity);
116
117 if ($result->isSuccess())
118 {
119 $this->sendPushEdit($entity->getOwner()->getId(), true);
120 $entity->setId((int)$result->getId());
121 $entity->setXmlId($this->saveXmlId($result->getId(), $entity->getType()));
122
123 return $entity;
124 }
125
126 throw new Core\Base\BaseException('Error of create section');
127 }
128
138 protected function updateEntity($entity, array $params = []): ?Core\Base\EntityInterface
139 {
140 $arrayEntity = $this->prepareArrayEntityForDB($entity);
141
142 $result = SectionTable::update(
143 $entity->getId(),
144 $arrayEntity
145 );
146
147 if ($result->isSuccess())
148 {
149 $this->sendPushEdit($entity->getOwner()->getId(), false);
150 return $entity->setDateModified(new Core\Base\Date());
151 }
152
153 throw new Core\Base\BaseException('Error of update section');
154 }
155
159 protected function getMapClass(): string
160 {
161 return Core\Section\SectionMap::class;
162 }
163
175 protected function deleteEntity(Core\Base\EntityInterface $entity, array $params = ['softDelete' => true]): ?Core\Base\EntityInterface
176 {
177 if (!empty($params['softDelete']))
178 {
179 $entity->setIsActive(false);
180
181 return $this->updateEntity($entity, $params);
182 }
183
184 // TODO: change it to SectionTable::delete() after implementation all logic
185 if (CCalendarSect::Delete($entity->getId(), false, $params))
186 {
187 return null;
188 }
189
190 throw new Core\Base\BaseException('Error of delete section');
191 }
192
202 protected function getDataManagerResult(array $params): Result
203 {
204 return SectionTable::getList($params);
205 }
206
214 private function saveXmlId(int $id, string $type): string
215 {
216 $xmlId = md5($type. '_'. $id. '_'. Random::getString(8));
217
218 SectionTable::update($id, [
219 'XML_ID' => $xmlId
220 ]);
221
222 return $xmlId;
223 }
224
230 private function sendPushEdit(int $userId, bool $isNewSection): void
231 {
233 'edit_section',
234 $userId,
235 [
236 'newSection' => $isNewSection,
237 ],
238 );
239 }
240
246 private function prepareArrayEntityForDB($entity): array
247 {
248 $arrayEntity = $this->convertToArray($entity);
249 if (!empty($arrayEntity['NAME']))
250 {
251 $arrayEntity['NAME'] = Emoji::encode($arrayEntity['NAME']);
252 }
253
254 return $arrayEntity;
255 }
256
260 protected function getEntityClass(): string
261 {
262 return Core\Section\Section::class;
263 }
264}
updateEntity($entity, array $params=[])
Definition section.php:138
deleteEntity(Core\Base\EntityInterface $entity, array $params=['softDelete'=> true])
Definition section.php:175
createEntity($entity, array $params=[])
Definition section.php:111
static addPullEvent(string $command, int $userId, array $params=[])
Definition util.php:373