Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
manager.php
1<?php
2
3namespace Bitrix\Vote\Uf;
4
14
15final class Manager
16{
19
20 protected $params;
21
22 protected $additionalConnectorList = null;
24 protected $loadedAttachedObjects = array();
25
26 protected static $instance = array();
27
28 private $int = 0;
29
34 public function __construct(array $params)
35 {
36 $this->params = $params;
37 $this->params['ENTITY_VALUE_ID'] = $this->params['ENTITY_VALUE_ID'] ?? null;
38 $this->params['VALUE_ID'] = $this->params['VALUE_ID'] ?? null;
39 $this->errorCollection = new ErrorCollection();
40 }
41
87 public static function getInstance(array $params)
88 {
89 $id = md5(serialize($params));
90 if (!array_key_exists($id, self::$instance))
91 {
92 self::$instance[$id] = new static($params);
93 }
94 return self::$instance[$id];
95 }
96
102 public function loadFromAttachId($id)
103 {
104 if(!isset($this->loadedAttachedObjects[$id]))
105 {
106 $this->loadedAttachedObjects[$id] = \Bitrix\Vote\Attachment\Manager::loadFromAttachId($id);
107 }
108 return $this->loadedAttachedObjects[$id];
109 }
110
116 public function loadFromVoteId($id)
117 {
119 if(!isset($this->loadedAttachedObjects[$id1]))
120 {
121 [$entityType, $moduleId] = $this->getConnectorDataByEntityType($this->params["ENTITY_ID"]);
122 $attach = \Bitrix\Vote\Attachment\Manager::loadFromVoteId(array(
123 "ENTITY_ID" => ($this->params["ENTITY_VALUE_ID"] ?: $this->params["VALUE_ID"]), // http://hg.office.bitrix.ru/repos/modules/rev/b614a075ce64
124 "ENTITY_TYPE" => $entityType,
125 "MODULE_ID" => $moduleId), $id);
126 $this->loadedAttachedObjects[$id1] = $attach;
127 }
128 return $this->loadedAttachedObjects[$id1];
129 }
130
134 public function loadEmptyObject()
135 {
136 [$entityType, $moduleId] = $this->getConnectorDataByEntityType($this->params["ENTITY_ID"]);
137 return \Bitrix\Vote\Attachment\Manager::loadEmptyAttach(array(
138 "ENTITY_ID" => ($this->params["ENTITY_VALUE_ID"] ?: $this->params["VALUE_ID"]), // http://hg.office.bitrix.ru/repos/modules/rev/b614a075ce64,
139 "ENTITY_TYPE" => $entityType,
140 "MODULE_ID" => $moduleId), array(
141 "CHANNEL_ID" => $this->params["SETTINGS"]["CHANNEL_ID"]
142 ));
143 }
144
148 public function loadFromEntity()
149 {
150 [$entityType, $moduleId] = $this->getConnectorDataByEntityType($this->params["ENTITY_ID"]);
151 $res = array(
152 "ENTITY_ID" => ($this->params["ENTITY_VALUE_ID"] ?: $this->params["VALUE_ID"]), // http://hg.office.bitrix.ru/repos/modules/rev/b614a075ce64
153 "=ENTITY_TYPE" => $entityType,
154 "=MODULE_ID" => $moduleId);
155 return \Bitrix\Vote\Attachment\Manager::loadFromEntity($res);
156 }
164 public function belongsToEntity(Attach $attachedObject, $entityType, $entityId)
165 {
166 [$connectorClass, $moduleId] = $this->getConnectorDataByEntityType($entityType);
167
168 return
169 $attachedObject->getEntityId() == $entityId &&
170 $attachedObject->getModuleId() == $moduleId &&
171 $attachedObject->getEntityType() == $connectorClass;
172 }
178 public function getConnectorDataByEntityType($entityType)
179 {
180 $defaultConnectors = $this->getDefaultConnectors();
181 $entityType = mb_strtolower($entityType);
182
183 if(isset($defaultConnectors[$entityType]))
184 return $defaultConnectors[$entityType];
185 if (($connector = $this->getAdditionalConnector($entityType)) !== null)
186 return $connector;
187 return array(DefaultConnector::className(), 'vote');
188 }
189
195 public function getConnectors()
196 {
197 return array_merge($this->getDefaultConnectors(), $this->getAdditionalConnectors());
198 }
199
200 private function getDefaultConnectors()
201 {
202 return array(
203 'blog_post' => array(BlogPostConnector::className(), 'blog'),
204 'forum_message' => array(ForumMessageConnector::className(), 'forum')
205 );
206 }
207
208
209 private function getAdditionalConnectors()
210 {
211 if($this->additionalConnectorList === null)
212 {
213 $this->buildAdditionalConnectorList();
214 }
215
217 }
218
219 private function getAdditionalConnector($entityType)
220 {
221 $additionalConnectorList = $this->getAdditionalConnectors();
222
223 return isset($additionalConnectorList[$entityType])? $additionalConnectorList[$entityType] : null;
224 }
225
226 private function buildAdditionalConnectorList()
227 {
228 $this->additionalConnectorList = array();
229
230 $event = new Event("vote", "onBuildAdditionalConnectorList");
231 $event->send();
232
233 foreach($event->getResults() as $evenResult)
234 {
235 if($evenResult->getType() != EventResult::SUCCESS)
236 {
237 continue;
238 }
239
240 $result = $evenResult->getParameters();
241 if(!is_array($result))
242 {
243 throw new SystemException('Wrong event result by building AdditionalConnectorList. Must be array.');
244 }
245
246 foreach($result as $connector)
247 {
248 if(empty($connector['ENTITY_TYPE']))
249 {
250 throw new SystemException('Wrong event result by building AdditionalConnectorList. Could not find ENTITY_TYPE.');
251 }
252
253 if(empty($connector['MODULE_ID']))
254 {
255 throw new SystemException('Wrong event result by building AdditionalConnectorList. Could not find MODULE_ID.');
256 }
257
258 if(empty($connector['CLASS']))
259 {
260 throw new SystemException('Wrong event result by building AdditionalConnectorList. Could not find CLASS.');
261 }
262
263 if(is_string($connector['CLASS']) && class_exists($connector['CLASS']) && is_a($connector['CLASS'], Connector::class, true))
264 {
265 $this->additionalConnectorList[mb_strtolower($connector['ENTITY_TYPE'])] = array(
266 $connector['CLASS'],
267 $connector['MODULE_ID']
268 );
269 }
270 else
271 {
272 throw new SystemException('Wrong event result by building AdditionalConnectorList. Could not find class by CLASS.');
273 }
274 }
275 }
276 }
277
285 public function showEdit(&$params, &$result, $component = null)
286 {
287 global $APPLICATION;
288 $APPLICATION->IncludeComponent(
289 "bitrix:voting.uf",
290 ".default",
291 array(
292 'EDIT' => 'Y',
293 'PARAMS' => $params,
294 'RESULT' => $result,
295 ),
296 $component,
297 array("HIDE_ICONS" => "Y")
298 );
299 }
300
308 public function showView(&$params, &$result, $component = null)
309 {
310 global $APPLICATION;
311 $APPLICATION->IncludeComponent(
312 "bitrix:voting.uf",
313 ".default",
314 array(
315 "PARAMS" => $params,
316 "RESULT" => $result
317 ),
318 $component,
319 array("HIDE_ICONS" => "Y")
320 );
321 }
322
327 public function getErrors()
328 {
329 return $this->errorCollection->toArray();
330 }
331}
showView(&$params, &$result, $component=null)
Definition manager.php:308
__construct(array $params)
Definition manager.php:34
belongsToEntity(Attach $attachedObject, $entityType, $entityId)
Definition manager.php:164
static getInstance(array $params)
Definition manager.php:87
showEdit(&$params, &$result, $component=null)
Definition manager.php:285
getConnectorDataByEntityType($entityType)
Definition manager.php:178