BxTimelineMenuItemActions.php 14 KB

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