PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/vote/lib/uf/manager.php

https://gitlab.com/alexprowars/bitrix
PHP | 329 lines | 194 code | 33 blank | 102 comment | 18 complexity | 1575b4969985f0efde91d915daa28209 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Vote\Uf;
  3. use Bitrix\Vote\Attach;
  4. use Bitrix\Vote\Attachment\Connector;
  5. use Bitrix\Vote\Attachment\DefaultConnector;
  6. use Bitrix\Vote\Attachment\BlogPostConnector;
  7. use Bitrix\Vote\Attachment\ForumMessageConnector;
  8. use Bitrix\Main\ErrorCollection;
  9. use Bitrix\Main\Event;
  10. use Bitrix\Main\EventResult;
  11. use Bitrix\Main\SystemException;
  12. final class Manager
  13. {
  14. /** @var ErrorCollection */
  15. protected $errorCollection;
  16. protected $params;
  17. protected $additionalConnectorList = null;
  18. /** @var Attach[] */
  19. protected $loadedAttachedObjects = array();
  20. protected static $instance = array();
  21. private $int = 0;
  22. /**
  23. * Constructor of UserFieldManager.
  24. * @param array $params
  25. */
  26. public function __construct(array $params)
  27. {
  28. $this->params = $params;
  29. $this->errorCollection = new ErrorCollection();
  30. }
  31. /**
  32. * Returns instance of Manager.
  33. * @param array $params Array (
  34. [ID] => 29
  35. [ENTITY_ID] => BLOG_POST
  36. [FIELD_NAME] => UF_BLOG_POST_VOTE
  37. [USER_TYPE_ID] => vote
  38. [XML_ID] => UF_BLOG_POST_VOTE
  39. [SORT] => 100
  40. [MULTIPLE] => N
  41. [MANDATORY] => N
  42. [SHOW_FILTER] => N
  43. [SHOW_IN_LIST] => Y
  44. [EDIT_IN_LIST] => Y
  45. [IS_SEARCHABLE] => N
  46. [SETTINGS] => Array
  47. (
  48. [CHANNEL_ID] => 1
  49. [UNIQUE] => 8
  50. [UNIQUE_IP_DELAY] => Array
  51. (
  52. [DELAY] =>
  53. [DELAY_TYPE] => S
  54. )
  55. [NOTIFY] => I
  56. )
  57. [EDIT_FORM_LABEL] => UF_BLOG_POST_VOTE
  58. [LIST_COLUMN_LABEL] =>
  59. [LIST_FILTER_LABEL] =>
  60. [ERROR_MESSAGE] =>
  61. [HELP_MESSAGE] =>
  62. [USER_TYPE] => Array
  63. (
  64. [USER_TYPE_ID] => vote
  65. [CLASS_NAME] => Bitrix\Vote\Uf\VoteUserType
  66. [DESCRIPTION] => "Vote"
  67. [BASE_TYPE] => int
  68. )
  69. [VALUE] => 27
  70. [ENTITY_VALUE_ID] => 247)).
  71. * @return Manager
  72. */
  73. public static function getInstance(array $params)
  74. {
  75. $id = md5(serialize($params));
  76. if (!array_key_exists($id, self::$instance))
  77. {
  78. self::$instance[$id] = new static($params);
  79. }
  80. return self::$instance[$id];
  81. }
  82. /**
  83. * Returns attached object by id.
  84. * @param int $id Id of attached object.
  85. * @return Attach
  86. */
  87. public function loadFromAttachId($id)
  88. {
  89. if(!isset($this->loadedAttachedObjects[$id]))
  90. {
  91. $this->loadedAttachedObjects[$id] = \Bitrix\Vote\Attachment\Manager::loadFromAttachId($id);
  92. }
  93. return $this->loadedAttachedObjects[$id];
  94. }
  95. /**
  96. * Returns vote object by id.
  97. * @param int $id Id of vote.
  98. * @return Attach
  99. */
  100. public function loadFromVoteId($id)
  101. {
  102. $id1 = VoteUserType::NEW_VOTE_PREFIX.$id;
  103. if(!isset($this->loadedAttachedObjects[$id1]))
  104. {
  105. [$entityType, $moduleId] = $this->getConnectorDataByEntityType($this->params["ENTITY_ID"]);
  106. $attach = \Bitrix\Vote\Attachment\Manager::loadFromVoteId(array(
  107. "ENTITY_ID" => ($this->params["ENTITY_VALUE_ID"] ?: $this->params["VALUE_ID"]), // http://hg.office.bitrix.ru/repos/modules/rev/b614a075ce64
  108. "ENTITY_TYPE" => $entityType,
  109. "MODULE_ID" => $moduleId), $id);
  110. $this->loadedAttachedObjects[$id1] = $attach;
  111. }
  112. return $this->loadedAttachedObjects[$id1];
  113. }
  114. /**
  115. * @return \Bitrix\Vote\Attach
  116. */
  117. public function loadEmptyObject()
  118. {
  119. [$entityType, $moduleId] = $this->getConnectorDataByEntityType($this->params["ENTITY_ID"]);
  120. return \Bitrix\Vote\Attachment\Manager::loadEmptyAttach(array(
  121. "ENTITY_ID" => ($this->params["ENTITY_VALUE_ID"] ?: $this->params["VALUE_ID"]), // http://hg.office.bitrix.ru/repos/modules/rev/b614a075ce64,
  122. "ENTITY_TYPE" => $entityType,
  123. "MODULE_ID" => $moduleId), array(
  124. "CHANNEL_ID" => $this->params["SETTINGS"]["CHANNEL_ID"]
  125. ));
  126. }
  127. /**
  128. * @return Attach[]
  129. */
  130. public function loadFromEntity()
  131. {
  132. [$entityType, $moduleId] = $this->getConnectorDataByEntityType($this->params["ENTITY_ID"]);
  133. $res = array(
  134. "ENTITY_ID" => ($this->params["ENTITY_VALUE_ID"] ?: $this->params["VALUE_ID"]), // http://hg.office.bitrix.ru/repos/modules/rev/b614a075ce64
  135. "=ENTITY_TYPE" => $entityType,
  136. "=MODULE_ID" => $moduleId);
  137. return \Bitrix\Vote\Attachment\Manager::loadFromEntity($res);
  138. }
  139. /**
  140. * Checks attitude attached object to entity.
  141. * @param Attach $attachedObject Attached object.
  142. * @param string $entityType Entity type (ex. blog_post).
  143. * @param int $entityId Id of entity.
  144. * @return bool
  145. */
  146. public function belongsToEntity(Attach $attachedObject, $entityType, $entityId)
  147. {
  148. [$connectorClass, $moduleId] = $this->getConnectorDataByEntityType($entityType);
  149. return
  150. $attachedObject->getEntityId() == $entityId &&
  151. $attachedObject->getModuleId() == $moduleId &&
  152. $attachedObject->getEntityType() == $connectorClass;
  153. }
  154. /**
  155. * Gets data which describes specific connector by entity type.
  156. * @param string $entityType Entity type (ex. sonet_comment).
  157. * @return array|null Array with two elements: connector class and module.
  158. */
  159. public function getConnectorDataByEntityType($entityType)
  160. {
  161. $defaultConnectors = $this->getDefaultConnectors();
  162. $entityType = mb_strtolower($entityType);
  163. if(isset($defaultConnectors[$entityType]))
  164. return $defaultConnectors[$entityType];
  165. if (($connector = $this->getAdditionalConnector($entityType)) !== null)
  166. return $connector;
  167. return array(DefaultConnector::className(), 'vote');
  168. }
  169. /**
  170. * Returns full list of available connectors for attached objects.
  171. *
  172. * @return array
  173. */
  174. public function getConnectors()
  175. {
  176. return array_merge($this->getDefaultConnectors(), $this->getAdditionalConnectors());
  177. }
  178. private function getDefaultConnectors()
  179. {
  180. return array(
  181. 'blog_post' => array(BlogPostConnector::className(), 'blog'),
  182. 'forum_message' => array(ForumMessageConnector::className(), 'forum')
  183. );
  184. }
  185. private function getAdditionalConnectors()
  186. {
  187. if($this->additionalConnectorList === null)
  188. {
  189. $this->buildAdditionalConnectorList();
  190. }
  191. return $this->additionalConnectorList;
  192. }
  193. private function getAdditionalConnector($entityType)
  194. {
  195. $additionalConnectorList = $this->getAdditionalConnectors();
  196. return isset($additionalConnectorList[$entityType])? $additionalConnectorList[$entityType] : null;
  197. }
  198. private function buildAdditionalConnectorList()
  199. {
  200. $this->additionalConnectorList = array();
  201. $event = new Event("vote", "onBuildAdditionalConnectorList");
  202. $event->send();
  203. foreach($event->getResults() as $evenResult)
  204. {
  205. if($evenResult->getType() != EventResult::SUCCESS)
  206. {
  207. continue;
  208. }
  209. $result = $evenResult->getParameters();
  210. if(!is_array($result))
  211. {
  212. throw new SystemException('Wrong event result by building AdditionalConnectorList. Must be array.');
  213. }
  214. foreach($result as $connector)
  215. {
  216. if(empty($connector['ENTITY_TYPE']))
  217. {
  218. throw new SystemException('Wrong event result by building AdditionalConnectorList. Could not find ENTITY_TYPE.');
  219. }
  220. if(empty($connector['MODULE_ID']))
  221. {
  222. throw new SystemException('Wrong event result by building AdditionalConnectorList. Could not find MODULE_ID.');
  223. }
  224. if(empty($connector['CLASS']))
  225. {
  226. throw new SystemException('Wrong event result by building AdditionalConnectorList. Could not find CLASS.');
  227. }
  228. if(is_string($connector['CLASS']) && class_exists($connector['CLASS']) && is_a($connector['CLASS'], Connector::class, true))
  229. {
  230. $this->additionalConnectorList[mb_strtolower($connector['ENTITY_TYPE'])] = array(
  231. $connector['CLASS'],
  232. $connector['MODULE_ID']
  233. );
  234. }
  235. else
  236. {
  237. throw new SystemException('Wrong event result by building AdditionalConnectorList. Could not find class by CLASS.');
  238. }
  239. }
  240. }
  241. }
  242. /**
  243. * Shows component to edit vote.
  244. * @param array &$params Component parameters.
  245. * @param array &$result Component results.
  246. * @param null $component Component.
  247. * @return void
  248. */
  249. public function showEdit(&$params, &$result, $component = null)
  250. {
  251. global $APPLICATION;
  252. $APPLICATION->IncludeComponent(
  253. "bitrix:voting.uf",
  254. ".default",
  255. array(
  256. 'EDIT' => 'Y',
  257. 'PARAMS' => $params,
  258. 'RESULT' => $result,
  259. ),
  260. $component,
  261. array("HIDE_ICONS" => "Y")
  262. );
  263. }
  264. /**
  265. * Shows component to participate in vote
  266. * @param array &$params Component parameters.
  267. * @param array &$result Component results.
  268. * @param null $component Component.
  269. * @return void
  270. */
  271. public function showView(&$params, &$result, $component = null)
  272. {
  273. global $APPLICATION;
  274. $APPLICATION->IncludeComponent(
  275. "bitrix:voting.uf",
  276. ".default",
  277. array(
  278. "PARAMS" => $params,
  279. "RESULT" => $result
  280. ),
  281. $component,
  282. array("HIDE_ICONS" => "Y")
  283. );
  284. }
  285. /**
  286. * Getting array of errors.
  287. * @return array
  288. */
  289. public function getErrors()
  290. {
  291. return $this->errorCollection->toArray();
  292. }
  293. }