BxTimelineMenuItemActions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. $this->_setBrowseParams($aBrowseParams);
  49. $iCommentsObject = 0;
  50. $sCommentsSystem = $sCommentsOnclick = '';
  51. if(isset($aEvent['comments']) && is_array($aEvent['comments']) && isset($aEvent['comments']['system'])) {
  52. $sCommentsSystem = $aEvent['comments']['system'];
  53. $iCommentsObject = $aEvent['comments']['object_id'];
  54. $sCommentsOnclick = bx_replace_markers("{js_object_view}.commentItem(this, '" . $sCommentsSystem . "', " . $iCommentsObject . ")", $this->_aMarkers);
  55. }
  56. $bSystem = $this->_oModule->_oConfig->isSystem($this->_aEvent['type'], $this->_aEvent['action']);
  57. $this->addMarkers(array(
  58. 'content_id' => $this->_iEvent,
  59. 'comment_system' => $sCommentsSystem,
  60. 'comment_object' => $iCommentsObject,
  61. 'comment_onclick' => $sCommentsOnclick,
  62. 'delete_title' => _t('_bx_timeline_menu_item_title_item_delete_' . ($bSystem ? 'system' : 'common'))
  63. ));
  64. return true;
  65. }
  66. public function setEventById($iEventId, $aBrowseParams = array())
  67. {
  68. $aEvent = $this->_oModule->_oDb->getEvents(array('browse' => 'id', 'value' => $iEventId));
  69. if(empty($aEvent) || !is_array($aEvent))
  70. return;
  71. $aEventData = $this->_oModule->_oTemplate->getDataCached($aEvent);
  72. if($aEventData === false)
  73. return;
  74. $aEvent['views'] = $aEventData['views'];
  75. $aEvent['votes'] = $aEventData['votes'];
  76. $aEvent['reactions'] = $aEventData['reactions'];
  77. $aEvent['scores'] = $aEventData['scores'];
  78. $aEvent['reports'] = $aEventData['reports'];
  79. $aEvent['comments'] = $aEventData['comments'];
  80. $this->setEvent($aEvent, $aBrowseParams);
  81. }
  82. public function setTemplateNameItem($sName)
  83. {
  84. $this->_sTmplNameItem = $sName;
  85. }
  86. protected function _setBrowseParams($aBrowseParams = array())
  87. {
  88. $this->_sType = !empty($aBrowseParams['type']) ? $aBrowseParams['type'] : BX_TIMELINE_TYPE_DEFAULT;
  89. $this->_sView = !empty($aBrowseParams['view']) ? $aBrowseParams['view'] : BX_TIMELINE_VIEW_DEFAULT;
  90. $aMarkers = array();
  91. foreach($aBrowseParams as $sKey => $mixedValue)
  92. if(!is_array($mixedValue))
  93. $aMarkers[$sKey] = $mixedValue;
  94. $this->addMarkers($aMarkers);
  95. $this->addMarkers(array(
  96. 'js_object_view' => $this->_oModule->_oConfig->getJsObjectView($aBrowseParams)
  97. ));
  98. }
  99. protected function _getMenuItemItemView($aItem)
  100. {
  101. if(!isset($this->_aEvent['views']) || !is_array($this->_aEvent['views']) || !isset($this->_aEvent['views']['system']))
  102. return false;
  103. $sViewsSystem = $this->_aEvent['views']['system'];
  104. $iViewsObject = $this->_aEvent['views']['object_id'];
  105. $aViewsParams = array(
  106. 'show_do_view_label' => $this->_bShowTitles,
  107. 'show_counter' => $this->_bShowCounters,
  108. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  109. 'dynamic_mode' => $this->_bDynamicMode
  110. );
  111. switch($this->_sMode) {
  112. case self::$_sModeActions:
  113. $sViewsMethod = 'getElementInline';
  114. break;
  115. case self::$_sModeCounters:
  116. $sViewsMethod = 'getCounter';
  117. break;
  118. }
  119. return $this->_oModule->getViewObject($sViewsSystem, $iViewsObject)->$sViewsMethod($aViewsParams);
  120. }
  121. protected function _getMenuItemItemVote($aItem)
  122. {
  123. if(!isset($this->_aEvent['votes']) || !is_array($this->_aEvent['votes']) || !isset($this->_aEvent['votes']['system']))
  124. return false;
  125. $sVotesSystem = $this->_aEvent['votes']['system'];
  126. $iVotesObject = $this->_aEvent['votes']['object_id'];
  127. $aVotesParams = array(
  128. 'show_do_vote_label' => $this->_bShowTitles,
  129. 'show_counter' => $this->_bShowCounters,
  130. 'show_counter_empty' => $this->_bShowCountersEmpty,
  131. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  132. 'dynamic_mode' => $this->_bDynamicMode
  133. );
  134. switch($this->_sMode) {
  135. case self::$_sModeActions:
  136. $sVotesMethod = 'getElementInline';
  137. break;
  138. case self::$_sModeCounters:
  139. $sVotesMethod = 'getCounter';
  140. break;
  141. }
  142. return $this->_oModule->getVoteObject($sVotesSystem, $iVotesObject)->$sVotesMethod($aVotesParams);
  143. }
  144. protected function _getMenuItemItemReaction($aItem)
  145. {
  146. if(!isset($this->_aEvent['reactions']) || !is_array($this->_aEvent['reactions']) || !isset($this->_aEvent['reactions']['system']))
  147. return false;
  148. $sReactionsSystem = $this->_aEvent['reactions']['system'];
  149. $iReactionsObject = $this->_aEvent['reactions']['object_id'];
  150. $aReactionsParams = array(
  151. 'show_do_vote_label' => $this->_bShowTitles,
  152. 'show_counter' => $this->_bShowCounters,
  153. 'show_counter_empty' => $this->_bShowCountersEmpty,
  154. 'show_counter_style' => 'compound',
  155. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  156. 'dynamic_mode' => $this->_bDynamicMode
  157. );
  158. switch($this->_sMode) {
  159. case self::$_sModeActions:
  160. $sReactionsMethod = 'getElementInline';
  161. break;
  162. case self::$_sModeCounters:
  163. $sReactionsMethod = 'getCounter';
  164. break;
  165. }
  166. return $this->_oModule->getReactionObject($sReactionsSystem, $iReactionsObject)->$sReactionsMethod($aReactionsParams);
  167. }
  168. protected function _getMenuItemItemScore($aItem)
  169. {
  170. if(!isset($this->_aEvent['scores']) || !is_array($this->_aEvent['scores']) || !isset($this->_aEvent['scores']['system']))
  171. return false;
  172. $sScoresSystem = $this->_aEvent['scores']['system'];
  173. $iScoresObject = $this->_aEvent['scores']['object_id'];
  174. $aScoresParams = array(
  175. 'show_do_vote_label' => $this->_bShowTitles,
  176. 'show_counter' => $this->_bShowCounters,
  177. 'show_counter_empty' => $this->_bShowCountersEmpty,
  178. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  179. 'dynamic_mode' => $this->_bDynamicMode
  180. );
  181. switch($this->_sMode) {
  182. case self::$_sModeActions:
  183. $sScoresMethod = 'getElementInline';
  184. break;
  185. case self::$_sModeCounters:
  186. $sScoresMethod = 'getCounter';
  187. break;
  188. }
  189. return $this->_oModule->getScoreObject($sScoresSystem, $iScoresObject)->$sScoresMethod($aScoresParams);
  190. }
  191. protected function _getMenuItemItemReport($aItem)
  192. {
  193. if(!isset($this->_aEvent['reports']) || !is_array($this->_aEvent['reports']) || !isset($this->_aEvent['reports']['system']))
  194. return false;
  195. $sReportsSystem = $this->_aEvent['reports']['system'];
  196. $iReportsObject = $this->_aEvent['reports']['object_id'];
  197. $aReportsParams = array(
  198. 'show_do_report_label' => $this->_bShowTitles,
  199. 'show_counter' => $this->_bShowCounters,
  200. 'dynamic_mode' => $this->_bDynamicMode
  201. );
  202. switch($this->_sMode) {
  203. case self::$_sModeActions:
  204. $sReportsMethod = 'getElementInline';
  205. break;
  206. case self::$_sModeCounters:
  207. $sReportsMethod = 'getCounter';
  208. break;
  209. }
  210. return $this->_oModule->getReportObject($sReportsSystem, $iReportsObject)->$sReportsMethod($aReportsParams);
  211. }
  212. protected function _getMenuItemDefault ($aItem)
  213. {
  214. if(!isset($aItem['class_link']))
  215. $aItem['class_link'] = '';
  216. $aItem['class_link'] = 'bx-menu-item-link' . (!empty($aItem['class_link']) ? ' ' : '') . $aItem['class_link'];
  217. return parent::_getMenuItemDefault ($aItem);
  218. }
  219. /**
  220. * Check if menu items is visible.
  221. * @param $a menu item array
  222. * @return boolean
  223. */
  224. protected function _isVisible ($a)
  225. {
  226. if(!parent::_isVisible($a) || empty($this->_aEvent) || !is_array($this->_aEvent))
  227. return false;
  228. $sCheckFuncName = '';
  229. $aCheckFuncParams = array($this->_aEvent);
  230. switch ($a['name']) {
  231. case 'item-view':
  232. $sCheckFuncName = 'isAllowedViewCounter';
  233. break;
  234. case 'item-comment':
  235. if($this->_sView == BX_TIMELINE_VIEW_ITEM)
  236. return false;
  237. $sCheckFuncName = 'isAllowedComment';
  238. break;
  239. case 'item-vote':
  240. $sCheckFuncName = 'isAllowedVote';
  241. break;
  242. case 'item-reaction':
  243. $sCheckFuncName = 'isAllowedReaction';
  244. break;
  245. case 'item-score':
  246. $sCheckFuncName = 'isAllowedScore';
  247. break;
  248. case 'item-report':
  249. $sCheckFuncName = 'isAllowedReport';
  250. break;
  251. case 'item-mute':
  252. $sCheckFuncName = 'isAllowedMute';
  253. break;
  254. case 'item-more':
  255. $sCheckFuncName = 'isAllowedMore';
  256. break;
  257. case 'item-pin':
  258. if($this->_sType != BX_BASE_MOD_NTFS_TYPE_OWNER)
  259. return false;
  260. $sCheckFuncName = 'isAllowedPin';
  261. break;
  262. case 'item-unpin':
  263. if($this->_sType != BX_BASE_MOD_NTFS_TYPE_OWNER)
  264. return false;
  265. $sCheckFuncName = 'isAllowedUnpin';
  266. break;
  267. case 'item-stick':
  268. $sCheckFuncName = 'isAllowedStick';
  269. break;
  270. case 'item-unstick':
  271. $sCheckFuncName = 'isAllowedUnstick';
  272. break;
  273. case 'item-promote':
  274. $sCheckFuncName = 'isAllowedPromote';
  275. break;
  276. case 'item-unpromote':
  277. $sCheckFuncName = 'isAllowedUnpromote';
  278. break;
  279. case 'item-edit':
  280. $sCheckFuncName = 'isAllowedEdit';
  281. break;
  282. case 'item-delete':
  283. $sCheckFuncName = 'isAllowedDelete';
  284. break;
  285. }
  286. if(!$sCheckFuncName || !method_exists($this->_oModule, $sCheckFuncName))
  287. return true;
  288. return call_user_func_array(array($this->_oModule, $sCheckFuncName), $aCheckFuncParams) === true;
  289. }
  290. protected function _getTmplContentItem()
  291. {
  292. if(empty($this->_sTmplNameItem))
  293. return parent::_getTmplContentItem();
  294. return $this->_oModule->_oTemplate->getHtml($this->_sTmplNameItem);
  295. }
  296. }
  297. /** @} */