BxTimelineMenuItemActions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Timeline Timeline
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * 'Item' menu.
  13. */
  14. class BxTimelineMenuItemActions extends BxTemplMenuCustom
  15. {
  16. protected static $_sModeActions = 'actions';
  17. protected static $_sModeCounters = 'counters';
  18. protected $_sModule;
  19. protected $_oModule;
  20. protected $_iEvent;
  21. protected $_aEvent;
  22. protected $_sType;
  23. protected $_sView;
  24. protected $_sMode;
  25. protected $_bShowTitles;
  26. protected $_bShowCounters;
  27. protected $_bShowCountersIcons;
  28. protected $_sTmplNameItem;
  29. public function __construct($aObject, $oTemplate = false)
  30. {
  31. $this->_sModule = 'bx_timeline';
  32. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  33. parent::__construct($aObject, $this->_oModule->_oTemplate);
  34. $this->_setBrowseParams();
  35. $this->_sMode = self::$_sModeActions;
  36. $this->_bShowTitles = true;
  37. $this->_bShowCounters = false;
  38. $this->_bShowCountersEmpty = false;
  39. $this->_bShowCountersIcons = true;
  40. $this->_sTmplNameItem = 'menu_custom_item_hor.html';
  41. }
  42. public function setEvent($aEvent, $aBrowseParams = array())
  43. {
  44. if(empty($aEvent) || !is_array($aEvent))
  45. return false;
  46. $this->_aEvent = $aEvent;
  47. $this->_iEvent = (int)$this->_aEvent['id'];
  48. $sType = $this->_aEvent['type'];
  49. $sAction = $this->_aEvent['action'];
  50. $this->_setBrowseParams($aBrowseParams);
  51. //--- Comment item
  52. $iCommentsObject = 0;
  53. $sCommentsSystem = $sCommentsOnclick = '';
  54. if(isset($aEvent['comments']) && is_array($aEvent['comments']) && isset($aEvent['comments']['system'])) {
  55. $sCommentsSystem = $aEvent['comments']['system'];
  56. $iCommentsObject = $aEvent['comments']['object_id'];
  57. $sCommentsOnclick = bx_replace_markers("{js_object_view}.commentItem(this, '" . $sCommentsSystem . "', " . $iCommentsObject . ")", $this->_aMarkers);
  58. }
  59. //--- Repost item
  60. $iOwnerId = $this->_oModule->getUserId(); //--- in whose timeline the content will be reposted
  61. $iObjectId = $this->_oModule->_oConfig->isSystem($sType, $sAction) ? $this->_aEvent['object_id'] : $this->_aEvent['id'];
  62. $sRepostOnclick = $this->_oModule->serviceGetRepostJsClick($iOwnerId, $sType, $sAction, $iObjectId);
  63. $bSystem = $this->_oModule->_oConfig->isSystem($sType, $sAction);
  64. $this->addMarkers(array(
  65. 'content_id' => $this->_iEvent,
  66. 'comment_system' => $sCommentsSystem,
  67. 'comment_object' => $iCommentsObject,
  68. 'comment_onclick' => $sCommentsOnclick,
  69. 'repost_onclick' => $sRepostOnclick,
  70. 'delete_title' => _t('_bx_timeline_menu_item_title_item_delete_' . ($bSystem ? 'system' : 'common'))
  71. ));
  72. return true;
  73. }
  74. public function setEventById($iEventId, $aBrowseParams = array())
  75. {
  76. $aEvent = $this->_oModule->_oDb->getEvents(array('browse' => 'id', 'value' => $iEventId));
  77. if(empty($aEvent) || !is_array($aEvent))
  78. return;
  79. $aEventData = $this->_oModule->_oTemplate->getDataCached($aEvent);
  80. if($aEventData === false)
  81. return;
  82. $aEvent['views'] = $aEventData['views'];
  83. $aEvent['votes'] = $aEventData['votes'];
  84. $aEvent['reactions'] = $aEventData['reactions'];
  85. $aEvent['scores'] = $aEventData['scores'];
  86. $aEvent['reports'] = $aEventData['reports'];
  87. $aEvent['comments'] = $aEventData['comments'];
  88. $this->setEvent($aEvent, $aBrowseParams);
  89. }
  90. public function setTemplateNameItem($sName)
  91. {
  92. $this->_sTmplNameItem = $sName;
  93. }
  94. protected function _setBrowseParams($aBrowseParams = array())
  95. {
  96. $this->_sType = !empty($aBrowseParams['type']) ? $aBrowseParams['type'] : BX_TIMELINE_TYPE_DEFAULT;
  97. $this->_sView = !empty($aBrowseParams['view']) ? $aBrowseParams['view'] : BX_TIMELINE_VIEW_DEFAULT;
  98. $aMarkers = array();
  99. foreach($aBrowseParams as $sKey => $mixedValue)
  100. if(!is_array($mixedValue))
  101. $aMarkers[$sKey] = $mixedValue;
  102. $this->addMarkers($aMarkers);
  103. $this->addMarkers(array(
  104. 'js_object_view' => $this->_oModule->_oConfig->getJsObjectView($aBrowseParams)
  105. ));
  106. }
  107. protected function _getMenuItemItemView($aItem)
  108. {
  109. if(!isset($this->_aEvent['views']) || !is_array($this->_aEvent['views']) || !isset($this->_aEvent['views']['system']))
  110. return false;
  111. $sViewsSystem = $this->_aEvent['views']['system'];
  112. $iViewsObject = $this->_aEvent['views']['object_id'];
  113. $aViewsParams = array(
  114. 'show_do_view_label' => $this->_bShowTitles,
  115. 'show_counter' => $this->_bShowCounters,
  116. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  117. 'dynamic_mode' => $this->_bDynamicMode
  118. );
  119. switch($this->_sMode) {
  120. case self::$_sModeActions:
  121. $sViewsMethod = 'getElementInline';
  122. break;
  123. case self::$_sModeCounters:
  124. $sViewsMethod = 'getCounter';
  125. break;
  126. }
  127. return $this->_oModule->getViewObject($sViewsSystem, $iViewsObject)->$sViewsMethod($aViewsParams);
  128. }
  129. protected function _getMenuItemItemVote($aItem)
  130. {
  131. if(!isset($this->_aEvent['votes']) || !is_array($this->_aEvent['votes']) || !isset($this->_aEvent['votes']['system']))
  132. return false;
  133. $sVotesSystem = $this->_aEvent['votes']['system'];
  134. $iVotesObject = $this->_aEvent['votes']['object_id'];
  135. $aVotesParams = array(
  136. 'show_do_vote_label' => $this->_bShowTitles,
  137. 'show_counter' => $this->_bShowCounters,
  138. 'show_counter_empty' => $this->_bShowCountersEmpty,
  139. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  140. 'dynamic_mode' => $this->_bDynamicMode
  141. );
  142. switch($this->_sMode) {
  143. case self::$_sModeActions:
  144. $sVotesMethod = 'getElementInline';
  145. break;
  146. case self::$_sModeCounters:
  147. $sVotesMethod = 'getCounter';
  148. break;
  149. }
  150. return $this->_oModule->getVoteObject($sVotesSystem, $iVotesObject)->$sVotesMethod($aVotesParams);
  151. }
  152. protected function _getMenuItemItemReaction($aItem)
  153. {
  154. if(!isset($this->_aEvent['reactions']) || !is_array($this->_aEvent['reactions']) || !isset($this->_aEvent['reactions']['system']))
  155. return false;
  156. $sReactionsSystem = $this->_aEvent['reactions']['system'];
  157. $iReactionsObject = $this->_aEvent['reactions']['object_id'];
  158. $aReactionsParams = array(
  159. 'show_do_vote_label' => $this->_bShowTitles,
  160. 'show_counter' => $this->_bShowCounters,
  161. 'show_counter_empty' => $this->_bShowCountersEmpty,
  162. 'show_counter_style' => 'compound',
  163. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  164. 'dynamic_mode' => $this->_bDynamicMode
  165. );
  166. switch($this->_sMode) {
  167. case self::$_sModeActions:
  168. $sReactionsMethod = 'getElementInline';
  169. break;
  170. case self::$_sModeCounters:
  171. $sReactionsMethod = 'getCounter';
  172. break;
  173. }
  174. return $this->_oModule->getReactionObject($sReactionsSystem, $iReactionsObject)->$sReactionsMethod($aReactionsParams);
  175. }
  176. protected function _getMenuItemItemScore($aItem)
  177. {
  178. if(!isset($this->_aEvent['scores']) || !is_array($this->_aEvent['scores']) || !isset($this->_aEvent['scores']['system']))
  179. return false;
  180. $sScoresSystem = $this->_aEvent['scores']['system'];
  181. $iScoresObject = $this->_aEvent['scores']['object_id'];
  182. $aScoresParams = array(
  183. 'show_do_vote_label' => $this->_bShowTitles,
  184. 'show_counter' => $this->_bShowCounters,
  185. 'show_counter_empty' => $this->_bShowCountersEmpty,
  186. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  187. 'dynamic_mode' => $this->_bDynamicMode
  188. );
  189. switch($this->_sMode) {
  190. case self::$_sModeActions:
  191. $sScoresMethod = 'getElementInline';
  192. break;
  193. case self::$_sModeCounters:
  194. $sScoresMethod = 'getCounter';
  195. break;
  196. }
  197. return $this->_oModule->getScoreObject($sScoresSystem, $iScoresObject)->$sScoresMethod($aScoresParams);
  198. }
  199. protected function _getMenuItemItemReport($aItem)
  200. {
  201. if(!isset($this->_aEvent['reports']) || !is_array($this->_aEvent['reports']) || !isset($this->_aEvent['reports']['system']))
  202. return false;
  203. $sReportsSystem = $this->_aEvent['reports']['system'];
  204. $iReportsObject = $this->_aEvent['reports']['object_id'];
  205. $aReportsParams = array(
  206. 'show_do_report_label' => $this->_bShowTitles,
  207. 'show_counter' => $this->_bShowCounters,
  208. 'dynamic_mode' => $this->_bDynamicMode
  209. );
  210. switch($this->_sMode) {
  211. case self::$_sModeActions:
  212. $sReportsMethod = 'getElementInline';
  213. break;
  214. case self::$_sModeCounters:
  215. $sReportsMethod = 'getCounter';
  216. break;
  217. }
  218. return $this->_oModule->getReportObject($sReportsSystem, $iReportsObject)->$sReportsMethod($aReportsParams);
  219. }
  220. protected function _getMenuItemDefault ($aItem)
  221. {
  222. if(!isset($aItem['class_link']))
  223. $aItem['class_link'] = '';
  224. $aItem['class_link'] = 'bx-menu-item-link' . (!empty($aItem['class_link']) ? ' ' : '') . $aItem['class_link'];
  225. return parent::_getMenuItemDefault ($aItem);
  226. }
  227. /**
  228. * Check if menu items is visible.
  229. * @param $a menu item array
  230. * @return boolean
  231. */
  232. protected function _isVisible ($a)
  233. {
  234. if(!parent::_isVisible($a) || empty($this->_aEvent) || !is_array($this->_aEvent))
  235. return false;
  236. $sCheckFuncName = '';
  237. $aCheckFuncParams = array($this->_aEvent);
  238. switch ($a['name']) {
  239. case 'item-view':
  240. $sCheckFuncName = 'isAllowedViewCounter';
  241. break;
  242. case 'item-comment':
  243. if($this->_sView == BX_TIMELINE_VIEW_ITEM)
  244. return false;
  245. $sCheckFuncName = 'isAllowedComment';
  246. break;
  247. case 'item-vote':
  248. $sCheckFuncName = 'isAllowedVote';
  249. break;
  250. case 'item-reaction':
  251. $sCheckFuncName = 'isAllowedReaction';
  252. break;
  253. case 'item-score':
  254. $sCheckFuncName = 'isAllowedScore';
  255. break;
  256. case 'item-report':
  257. $sCheckFuncName = 'isAllowedReport';
  258. break;
  259. case 'item-mute':
  260. $sCheckFuncName = 'isAllowedMute';
  261. break;
  262. case 'item-more':
  263. $sCheckFuncName = 'isAllowedMore';
  264. break;
  265. case 'item-pin':
  266. if($this->_sType != BX_BASE_MOD_NTFS_TYPE_OWNER)
  267. return false;
  268. $sCheckFuncName = 'isAllowedPin';
  269. break;
  270. case 'item-unpin':
  271. if($this->_sType != BX_BASE_MOD_NTFS_TYPE_OWNER)
  272. return false;
  273. $sCheckFuncName = 'isAllowedUnpin';
  274. break;
  275. case 'item-stick':
  276. $sCheckFuncName = 'isAllowedStick';
  277. break;
  278. case 'item-unstick':
  279. $sCheckFuncName = 'isAllowedUnstick';
  280. break;
  281. case 'item-promote':
  282. $sCheckFuncName = 'isAllowedPromote';
  283. break;
  284. case 'item-unpromote':
  285. $sCheckFuncName = 'isAllowedUnpromote';
  286. break;
  287. case 'item-edit':
  288. $sCheckFuncName = 'isAllowedEdit';
  289. break;
  290. case 'item-delete':
  291. $sCheckFuncName = 'isAllowedDelete';
  292. break;
  293. }
  294. if(!$sCheckFuncName || !method_exists($this->_oModule, $sCheckFuncName))
  295. return true;
  296. return call_user_func_array(array($this->_oModule, $sCheckFuncName), $aCheckFuncParams) === true;
  297. }
  298. protected function _getTmplContentItem()
  299. {
  300. if(empty($this->_sTmplNameItem))
  301. return parent::_getTmplContentItem();
  302. return $this->_oModule->_oTemplate->getHtml($this->_sTmplNameItem);
  303. }
  304. }
  305. /** @} */