BxTimelineMenuItemActions.php 12 KB

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