PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/forum/lib/comments/entity.php

https://gitlab.com/alexprowars/bitrix
PHP | 334 lines | 244 code | 28 blank | 62 comment | 25 complexity | 94305e2cbafe247378f24944cfe212f7 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Forum\Comments;
  3. use Bitrix\Forum\ForumTable;
  4. use Bitrix\Main\Config\Option;
  5. use Bitrix\Main\Event;
  6. use Bitrix\Main\SystemException;
  7. class Entity
  8. {
  9. const ENTITY_TYPE = 'default';
  10. const MODULE_ID = 'forum';
  11. const XML_ID_PREFIX = 'TOPIC_';
  12. /** @var array */
  13. protected $entity;
  14. /** @var array */
  15. protected $forum;
  16. /** @var array */
  17. protected static $permissions = array();
  18. /** @var bool */
  19. private $editOwn = false;
  20. protected static $pathToUser = '/company/personal/user/#user_id#/';
  21. protected static $pathToGroup = '/workgroups/group/#group_id#/';
  22. /** @var array */
  23. protected static $entities;
  24. /**
  25. * @param array $entity
  26. * @param array $storage
  27. */
  28. public function __construct(array $entity, array $storage)
  29. {
  30. $this->entity = array(
  31. "type" => $entity["type"],
  32. "id" => $entity["id"],
  33. "xml_id" => $entity["xml_id"]
  34. );
  35. $this->forum = $storage;
  36. $this->editOwn = (\COption::GetOptionString("forum", "USER_EDIT_OWN_POST", "Y") == "Y");
  37. }
  38. public function getId()
  39. {
  40. return $this->entity["id"];
  41. }
  42. public function getType()
  43. {
  44. return $this->entity["type"];
  45. }
  46. public function getXmlId()
  47. {
  48. if (!empty($this->entity["xml_id"]))
  49. return $this->entity["xml_id"];
  50. return mb_strtoupper($this->entity["type"]."_".$this->entity["id"]);
  51. }
  52. /**
  53. * @return array
  54. */
  55. public function getFullId()
  56. {
  57. return $this->entity;
  58. }
  59. public static function className()
  60. {
  61. return get_called_class();
  62. }
  63. public static function getModule()
  64. {
  65. return static::MODULE_ID;
  66. }
  67. public static function getEntityType()
  68. {
  69. return static::ENTITY_TYPE;
  70. }
  71. public static function getXmlIdPrefix()
  72. {
  73. return static::XML_ID_PREFIX;
  74. }
  75. /**
  76. * @param integer $userId User id.
  77. * @return bool
  78. */
  79. public function canRead($userId)
  80. {
  81. return $this->getPermission($userId) >= "E";
  82. }
  83. /**
  84. * @param integer $userId User id.
  85. * @return bool
  86. */
  87. public function canAdd($userId)
  88. {
  89. return $this->getPermission($userId) >= "I";
  90. }
  91. /**
  92. * @param integer $userId User id.
  93. * @return bool
  94. */
  95. public function canEdit($userId)
  96. {
  97. return $this->getPermission($userId) >= "U";
  98. }
  99. /**
  100. * @param integer $userId User id.
  101. * @return bool
  102. */
  103. public function canEditOwn($userId)
  104. {
  105. return $this->canEdit($userId) || $this->getPermission($userId) >= "I" && $this->editOwn;
  106. }
  107. /**
  108. * @param integer $userId User id.
  109. * @return bool
  110. */
  111. public function canModerate($userId)
  112. {
  113. return $this->getPermission($userId) >= "Q";
  114. }
  115. /**
  116. * @param integer $userId User id.
  117. * @param string $permission A < E < I < M < Q < U < Y
  118. // A - NO ACCESS E - READ I - ANSWER
  119. // M - NEW TOPIC Q - MODERATE U - EDIT Y - FULL_ACCESS.
  120. * @return $this
  121. */
  122. public function setPermission($userId, $permission)
  123. {
  124. if (is_string($permission))
  125. {
  126. if (!is_array(self::$permissions[$userId]))
  127. self::$permissions[$userId] = array();
  128. self::$permissions[$userId][$this->forum["ID"]] = $permission;
  129. }
  130. return $this;
  131. }
  132. /**
  133. * @param bool $permission
  134. * @return $this
  135. */
  136. public function setEditOwn($permission)
  137. {
  138. $this->editOwn = $permission;
  139. return $this;
  140. }
  141. /**
  142. * @param integer $userId User id.
  143. * @return $this
  144. */
  145. public function getPermission($userId)
  146. {
  147. if (!array_key_exists($userId, self::$permissions))
  148. {
  149. self::$permissions[$userId] = array();
  150. if (!array_key_exists($this->forum["ID"], self::$permissions[$userId]))
  151. {
  152. if (\CForumUser::IsAdmin($userId))
  153. $result = "Y";
  154. else if ($this->forum["ACTIVE"] != "Y")
  155. $result = "A";
  156. else if (\CForumUser::IsLocked($userId))
  157. $result = \CForumNew::GetPermissionUserDefault($this->forum["ID"]);
  158. else
  159. {
  160. if (in_array($this->getType(), array('PH', 'TR', 'TM', 'IBLOCK')))
  161. {
  162. $result = 'Y';
  163. }
  164. else
  165. {
  166. $res = ForumTable::getList(array(
  167. 'filter' => array(
  168. '=ID' => $this->forum["ID"],
  169. '@XML_ID' => array(
  170. 'USERS_AND_GROUPS'
  171. )
  172. ),
  173. 'select' => array('ID')
  174. ));
  175. if ($forumFields = $res->fetch())
  176. {
  177. $result = 'Y';
  178. }
  179. else
  180. {
  181. $result = \CForumNew::GetUserPermission($this->forum["ID"], $userId);
  182. }
  183. }
  184. }
  185. self::$permissions[$userId][$this->forum["ID"]] = $result;
  186. }
  187. }
  188. return self::$permissions[$userId][$this->forum["ID"]];
  189. }
  190. /**
  191. * @param string $type Type entity.
  192. * @return array|null
  193. */
  194. public static function getEntityByType($type = "")
  195. {
  196. $type = mb_strtolower($type);
  197. $entities = self::getEntities();
  198. return (array_key_exists($type, $entities) ? $entities[$type] : null);
  199. }
  200. /**
  201. * @param string $xmlId Type entity.
  202. * @return array|null
  203. */
  204. public static function getEntityByXmlId($xmlId = "")
  205. {
  206. $xmlId = mb_strtoupper($xmlId);
  207. $entities = self::getEntities();
  208. $result = null;
  209. foreach ($entities as $entity)
  210. {
  211. if (preg_match("/^".$entity["xmlIdPrefix"]."(\\d+)/", $xmlId))
  212. {
  213. $result = $entity;
  214. break;
  215. }
  216. }
  217. return $result;
  218. }
  219. private static function getEntities()
  220. {
  221. if (!is_array(self::$entities))
  222. {
  223. self::$entities = array(
  224. "tk" => array(
  225. "entityType" => "tk",
  226. "className" => TaskEntity::className(),
  227. "moduleId" => "tasks",
  228. "xmlIdPrefix" => TaskEntity::getXmlIdPrefix()),
  229. "wf" => array(
  230. "entityType" => "wf",
  231. "className" => WorkflowEntity::className(),
  232. "moduleId" => "lists",
  233. "xmlIdPrefix" => WorkflowEntity::getXmlIdPrefix()),
  234. "ev" => array(
  235. "entityType" => "ev",
  236. "className" => CalendarEntity::className(),
  237. "moduleId" => "calendar",
  238. "xmlIdPrefix" => CalendarEntity::getXmlIdPrefix()),
  239. "tm" => array(
  240. "entityType" => "tm",
  241. "className" => Entity::className(),
  242. "moduleId" => "timeman",
  243. "xmlIdPrefix" => 'TIMEMAN_ENTRY_'
  244. ),
  245. "tr" => array(
  246. "entityType" => "tr",
  247. "className" => Entity::className(),
  248. "moduleId" => "timeman",
  249. "xmlIdPrefix" => 'TIMEMAN_REPORT_'
  250. ),
  251. "default" => array(
  252. "entityType" => "default",
  253. "className" => Entity::className(),
  254. "moduleId" => "forum",
  255. "xmlIdPrefix" => Entity::getXmlIdPrefix()
  256. )
  257. );
  258. $event = new Event("forum", "onBuildAdditionalEntitiesList");
  259. $event->send();
  260. foreach ($event->getResults() as $evenResult)
  261. {
  262. $result = $evenResult->getParameters();
  263. if (!is_array($result))
  264. {
  265. throw new SystemException('Event onBuildAdditionalEntitiesList: result must be an array.');
  266. }
  267. foreach ($result as $connector)
  268. {
  269. if (empty($connector['ENTITY_TYPE']))
  270. {
  271. throw new SystemException('Event onBuildAdditionalEntitiesList: key ENTITY_TYPE is not found.');
  272. }
  273. if (empty($connector['MODULE_ID']))
  274. {
  275. throw new SystemException('Event onBuildAdditionalEntitiesList: key MODULE_ID is not found.');
  276. }
  277. if (empty($connector['CLASS']))
  278. {
  279. throw new SystemException('Event onBuildAdditionalEntitiesList: key CLASS is not found.');
  280. }
  281. if (is_string($connector['CLASS']) && class_exists($connector['CLASS']))
  282. {
  283. self::$entities[mb_strtolower($connector['ENTITY_TYPE'])] = array(
  284. "id" => mb_strtolower($connector['ENTITY_TYPE']),
  285. "className" => str_replace('\\\\', '\\', $connector['CLASS']),
  286. "moduleId" => $connector['MODULE_ID'],
  287. "xmlIdPrefix" => mb_strtoupper($connector['ENTITY_TYPE'])."_"
  288. );
  289. }
  290. }
  291. }
  292. }
  293. return self::$entities;
  294. }
  295. /**
  296. * Event before indexing message.
  297. * @param integer $id Message ID.
  298. * @param array $message Message data.
  299. * @param array &$index Search index array.
  300. * @return boolean
  301. */
  302. public static function onMessageIsIndexed($id, array $message, array &$index)
  303. {
  304. return (empty($message["PARAM1"]) && empty($message["PARAM2"]));
  305. }
  306. }