BxTimelineMenuItemActions.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 $_bIsApi;
  25. protected $_sMode;
  26. protected $_bShowTitles;
  27. protected $_bShowCounters;
  28. protected $_bShowCountersEmpty;
  29. protected $_bShowCountersIcons;
  30. protected $_sTmplNameItem;
  31. protected $_aItem2CheckFunc;
  32. public function __construct($aObject, $oTemplate = false)
  33. {
  34. $this->_sModule = 'bx_timeline';
  35. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  36. parent::__construct($aObject, $this->_oModule->_oTemplate);
  37. $this->_setBrowseParams();
  38. $this->_bIsApi = bx_is_api();
  39. $this->_sMode = self::$_sModeActions;
  40. $this->_bShowTitles = true;
  41. $this->_bShowCounters = false;
  42. $this->_bShowCountersEmpty = false;
  43. $this->_bShowCountersIcons = true;
  44. $this->_sTmplNameItem = 'menu_custom_item_hor.html';
  45. $this->_aItem2CheckFunc = array(
  46. 'item-view' => 'isAllowedViewCounter',
  47. 'item-comment' => 'isAllowedComment',
  48. 'item-vote' => 'isAllowedVote',
  49. 'item-reaction' => 'isAllowedReaction',
  50. 'item-score' => 'isAllowedScore',
  51. 'item-report' => 'isAllowedReport',
  52. 'item-mute' => 'isAllowedMute',
  53. 'item-more' => 'isAllowedMore',
  54. 'item-pin' => 'isAllowedPin',
  55. 'item-unpin' => 'isAllowedUnpin',
  56. 'item-stick' => 'isAllowedStick',
  57. 'item-unstick' => 'isAllowedUnstick',
  58. 'item-promote' => 'isAllowedPromote',
  59. 'item-unpromote' => 'isAllowedUnpromote',
  60. 'item-edit' => 'isAllowedEdit',
  61. 'item-delete' => 'isAllowedDelete',
  62. 'item-notes' => 'isAllowedNotes',
  63. );
  64. }
  65. public function setEvent($aEvent, $aBrowseParams = array())
  66. {
  67. if(empty($aEvent) || !is_array($aEvent))
  68. return false;
  69. $this->_aEvent = $aEvent;
  70. $this->_iEvent = (int)$this->_aEvent['id'];
  71. $sType = $this->_aEvent['type'];
  72. $sAction = $this->_aEvent['action'];
  73. $this->_setBrowseParams($aBrowseParams);
  74. //--- Comment item
  75. $iCommentsObject = 0;
  76. $sCommentsSystem = $sCommentsOnclick = '';
  77. if(isset($aEvent['comments']) && is_array($aEvent['comments']) && isset($aEvent['comments']['system'])) {
  78. $sCommentsSystem = $aEvent['comments']['system'];
  79. $iCommentsObject = $aEvent['comments']['object_id'];
  80. $sCommentsOnclick = bx_replace_markers("{js_object_view}.commentItem(this, '" . $sCommentsSystem . "', " . $iCommentsObject . ")", $this->_aMarkers);
  81. }
  82. //--- Repost item
  83. $iOwnerId = $this->_oModule->getUserId(); //--- in whose timeline the content will be reposted
  84. $iObjectId = $this->_oModule->_oConfig->isSystem($sType, $sAction) ? $this->_aEvent['object_id'] : $this->_aEvent['id'];
  85. $sRepostOnclick = $this->_oModule->serviceGetRepostJsClick($iOwnerId, $sType, $sAction, $iObjectId);
  86. $bSystem = $this->_oModule->_oConfig->isSystem($sType, $sAction);
  87. $this->addMarkers(array(
  88. 'content_id' => $this->_iEvent,
  89. 'comment_system' => $sCommentsSystem,
  90. 'comment_object' => $iCommentsObject,
  91. 'comment_onclick' => $sCommentsOnclick,
  92. 'repost_onclick' => $sRepostOnclick,
  93. 'delete_title' => _t('_bx_timeline_menu_item_title_item_delete_' . ($bSystem || $this->_oModule->_oConfig->isHideUponDelete() ? 'system' : 'common'))
  94. ));
  95. return true;
  96. }
  97. public function setEventById($iEventId, $aBrowseParams = array())
  98. {
  99. $aEvent = $this->_oModule->_oDb->getEvents(array('browse' => 'id', 'value' => $iEventId));
  100. if(empty($aEvent) || !is_array($aEvent))
  101. return;
  102. $aEventData = $this->_oModule->_oTemplate->getDataCached($aEvent);
  103. if($aEventData === false)
  104. return;
  105. $aEvent['views'] = $aEventData['views'];
  106. $aEvent['votes'] = $aEventData['votes'];
  107. $aEvent['reactions'] = $aEventData['reactions'];
  108. $aEvent['scores'] = $aEventData['scores'];
  109. $aEvent['reports'] = $aEventData['reports'];
  110. $aEvent['comments'] = $aEventData['comments'];
  111. $this->setEvent($aEvent, $aBrowseParams);
  112. }
  113. public function setTemplateNameItem($sName)
  114. {
  115. $this->_sTmplNameItem = $sName;
  116. }
  117. protected function _setBrowseParams($aBrowseParams = array())
  118. {
  119. $this->_sType = !empty($aBrowseParams['type']) ? $aBrowseParams['type'] : BX_TIMELINE_TYPE_DEFAULT;
  120. $this->_sView = !empty($aBrowseParams['view']) ? $aBrowseParams['view'] : BX_TIMELINE_VIEW_DEFAULT;
  121. $aMarkers = array();
  122. foreach($aBrowseParams as $sKey => $mixedValue)
  123. if(!is_array($mixedValue))
  124. $aMarkers[$sKey] = $mixedValue;
  125. $this->addMarkers($aMarkers);
  126. $this->addMarkers(array(
  127. 'js_object_view' => $this->_oModule->_oConfig->getJsObjectView($aBrowseParams)
  128. ));
  129. }
  130. protected function _getMenuItemItemView($aItem)
  131. {
  132. if(!isset($this->_aEvent['views']) || !is_array($this->_aEvent['views']) || !isset($this->_aEvent['views']['system']))
  133. return false;
  134. $sViewsSystem = $this->_aEvent['views']['system'];
  135. $iViewsObject = $this->_aEvent['views']['object_id'];
  136. $aViewsParams = array(
  137. 'show_do_view_label' => $this->_bShowTitles,
  138. 'show_counter' => $this->_bShowCounters,
  139. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  140. 'dynamic_mode' => $this->_bDynamicMode
  141. );
  142. switch($this->_sMode) {
  143. case self::$_sModeActions:
  144. $sViewsMethod = 'getElementInline';
  145. break;
  146. case self::$_sModeCounters:
  147. $sViewsMethod = 'getCounter';
  148. break;
  149. }
  150. $oObject = $this->_oModule->getViewObject($sViewsSystem, $iViewsObject);
  151. if($this->_bIsApi)
  152. return $this->_getMenuItemElementApi($aItem, $oObject->getElementApi(array_merge($aViewsParams, [
  153. 'show_counter' => true,
  154. ])));
  155. return $oObject->$sViewsMethod($aViewsParams);
  156. }
  157. protected function _getMenuItemItemVote($aItem)
  158. {
  159. if(!isset($this->_aEvent['votes']) || !is_array($this->_aEvent['votes']) || !isset($this->_aEvent['votes']['system']))
  160. return false;
  161. $sVotesSystem = $this->_aEvent['votes']['system'];
  162. $iVotesObject = $this->_aEvent['votes']['object_id'];
  163. $aVotesParams = array(
  164. 'show_do_vote_label' => $this->_bShowTitles,
  165. 'show_counter' => $this->_bShowCounters,
  166. 'show_counter_empty' => $this->_bShowCountersEmpty,
  167. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  168. 'dynamic_mode' => $this->_bDynamicMode
  169. );
  170. switch($this->_sMode) {
  171. case self::$_sModeActions:
  172. $sVotesMethod = 'getElementInline';
  173. break;
  174. case self::$_sModeCounters:
  175. $sVotesMethod = 'getCounter';
  176. break;
  177. }
  178. $oObject = $this->_oModule->getVoteObject($sVotesSystem, $iVotesObject);
  179. if($this->_bIsApi)
  180. return $this->_getMenuItemElementApi($aItem, $oObject->getElementApi(array_merge($aVotesParams, [
  181. 'show_counter' => true,
  182. ])));
  183. return $oObject->$sVotesMethod($aVotesParams);
  184. }
  185. protected function _getMenuItemItemReaction($aItem)
  186. {
  187. if(!isset($this->_aEvent['reactions']) || !is_array($this->_aEvent['reactions']) || !isset($this->_aEvent['reactions']['system']))
  188. return false;
  189. $sReactionsSystem = $this->_aEvent['reactions']['system'];
  190. $iReactionsObject = $this->_aEvent['reactions']['object_id'];
  191. $aReactionsParams = array(
  192. 'show_do_vote_label' => $this->_bShowTitles,
  193. 'show_counter' => $this->_bShowCounters,
  194. 'show_counter_empty' => $this->_bShowCountersEmpty,
  195. 'show_counter_style' => 'compound',
  196. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  197. 'dynamic_mode' => $this->_bDynamicMode
  198. );
  199. switch($this->_sMode) {
  200. case self::$_sModeActions:
  201. $sReactionsMethod = 'getElementInline';
  202. break;
  203. case self::$_sModeCounters:
  204. $sReactionsMethod = 'getCounter';
  205. break;
  206. }
  207. $oObject = $this->_oModule->getReactionObject($sReactionsSystem, $iReactionsObject);
  208. if($this->_bIsApi)
  209. return $this->_getMenuItemElementApi($aItem, $oObject->getElementApi(array_merge($aReactionsParams, [
  210. 'show_counter' => true,
  211. ])));
  212. return $oObject->$sReactionsMethod($aReactionsParams);
  213. }
  214. protected function _getMenuItemItemScore($aItem)
  215. {
  216. if(!isset($this->_aEvent['scores']) || !is_array($this->_aEvent['scores']) || !isset($this->_aEvent['scores']['system']))
  217. return false;
  218. $sScoresSystem = $this->_aEvent['scores']['system'];
  219. $iScoresObject = $this->_aEvent['scores']['object_id'];
  220. $aScoresParams = array(
  221. 'show_do_vote_label' => $this->_bShowTitles,
  222. 'show_counter' => $this->_bShowCounters,
  223. 'show_counter_empty' => $this->_bShowCountersEmpty,
  224. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  225. 'dynamic_mode' => $this->_bDynamicMode
  226. );
  227. switch($this->_sMode) {
  228. case self::$_sModeActions:
  229. $sScoresMethod = 'getElementInline';
  230. break;
  231. case self::$_sModeCounters:
  232. $sScoresMethod = 'getCounter';
  233. break;
  234. }
  235. $oObject = $this->_oModule->getScoreObject($sScoresSystem, $iScoresObject);
  236. if($this->_bIsApi)
  237. return $this->_getMenuItemElementApi($aItem, $oObject->getElementApi(array_merge($aScoresParams, [
  238. 'show_counter' => true,
  239. ])));
  240. return $oObject->$sScoresMethod($aScoresParams);
  241. }
  242. protected function _getMenuItemItemReport($aItem)
  243. {
  244. if(!isset($this->_aEvent['reports']) || !is_array($this->_aEvent['reports']) || !isset($this->_aEvent['reports']['system']))
  245. return false;
  246. $sReportsSystem = $this->_aEvent['reports']['system'];
  247. $iReportsObject = $this->_aEvent['reports']['object_id'];
  248. $aReportsParams = array(
  249. 'show_do_report_label' => $this->_bShowTitles,
  250. 'show_counter' => $this->_bShowCounters,
  251. 'dynamic_mode' => $this->_bDynamicMode
  252. );
  253. switch($this->_sMode) {
  254. case self::$_sModeActions:
  255. $sReportsMethod = 'getElementInline';
  256. break;
  257. case self::$_sModeCounters:
  258. $sReportsMethod = 'getCounter';
  259. break;
  260. }
  261. return $this->_oModule->getReportObject($sReportsSystem, $iReportsObject)->$sReportsMethod($aReportsParams);
  262. }
  263. protected function _getMenuItemItemRepost($aItem)
  264. {
  265. if(!isset($this->_aEvent['reposts']))
  266. return false;
  267. $aRepostsParams = array(
  268. 'show_do_repost_text' => $this->_bShowTitles,
  269. 'show_counter' => $this->_bShowCounters,
  270. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  271. 'dynamic_mode' => $this->_bDynamicMode
  272. );
  273. $sType = $this->_aEvent['type'];
  274. $sAction = $this->_aEvent['action'];
  275. $iObjectId = $this->_oModule->_oConfig->isSystem($sType, $sAction) ? $this->_aEvent['object_id'] : $this->_aEvent['id'];
  276. $sMethodName = '';
  277. $aMethodParams = [];
  278. switch($this->_sMode) {
  279. case self::$_sModeActions:
  280. $sMethodName = 'serviceGetRepostElementBlock';
  281. $aMethodParams = [$this->_aEvent['owner_id'], $sType, $sAction, $iObjectId, $aRepostsParams];
  282. break;
  283. case self::$_sModeCounters:
  284. $sMethodName = 'serviceGetRepostCounter';
  285. $aMethodParams = [$sType, $sAction, $iObjectId, $aRepostsParams];
  286. break;
  287. }
  288. return call_user_func_array([$this->_oModule, $sMethodName], $aMethodParams);
  289. }
  290. protected function _getMenuItemDefault ($aItem)
  291. {
  292. if(!isset($aItem['class_link']))
  293. $aItem['class_link'] = '';
  294. $aItem['class_link'] = 'bx-menu-item-link' . (!empty($aItem['class_link']) ? ' ' : '') . $aItem['class_link'];
  295. return parent::_getMenuItemDefault ($aItem);
  296. }
  297. protected function _getMenuItemDefaultApi ($aItem)
  298. {
  299. $sType = $this->_aEvent['type'];
  300. $sAction = $this->_aEvent['action'];
  301. if(!empty($aItem['submenu']) && ($oSubmenu = BxDolMenu::getObjectInstance($aItem['submenu'])) !== false) {
  302. $sMethod = 'setEventById';
  303. if(method_exists($oSubmenu, $sMethod))
  304. $oSubmenu->$sMethod($this->_iEvent);
  305. $aItem = array_merge($aItem, [
  306. 'content_type' => 'submenu',
  307. 'submenu' => $oSubmenu->getCodeAPI()
  308. ]);
  309. }
  310. switch($aItem['name']) {
  311. case 'item-comment':
  312. $aItem['link'] = $this->_oModule->_oConfig->getItemViewUrl($this->_aEvent);
  313. if($this->_oModule->_oConfig->isSystem($sType, $sAction) && !empty($this->_aEvent['content']['url']))
  314. $aItem['link'] = $this->_aEvent['content']['url'];
  315. $aItem['link'] = bx_api_get_relative_url($aItem['link']);
  316. break;
  317. }
  318. return $aItem;
  319. }
  320. protected function _getMenuItemElementApi($aItem, $aElement)
  321. {
  322. if(!$this->_bIsApi)
  323. return $aItem;
  324. return [
  325. 'id' => $aItem['id'],
  326. 'name' => $aItem['name'],
  327. 'display_type' => 'element',
  328. 'data' => $aElement
  329. ];
  330. }
  331. /**
  332. * Check if menu items is visible.
  333. * @param $a menu item array
  334. * @return boolean
  335. */
  336. protected function _isVisible ($a)
  337. {
  338. if(!parent::_isVisible($a) || empty($this->_aEvent) || !is_array($this->_aEvent))
  339. return false;
  340. $sCheckFuncName = '';
  341. $aCheckFuncParams = array($this->_aEvent);
  342. switch ($a['name']) {
  343. case 'item-comment':
  344. if($this->_sView == BX_TIMELINE_VIEW_ITEM)
  345. return false;
  346. $sCheckFuncName = $this->_aItem2CheckFunc[$a['name']];
  347. break;
  348. case 'item-pin':
  349. if($this->_sType != BX_BASE_MOD_NTFS_TYPE_OWNER)
  350. return false;
  351. $sCheckFuncName = $this->_aItem2CheckFunc[$a['name']];
  352. break;
  353. case 'item-unpin':
  354. if($this->_sType != BX_BASE_MOD_NTFS_TYPE_OWNER)
  355. return false;
  356. $sCheckFuncName = $this->_aItem2CheckFunc[$a['name']];
  357. break;
  358. }
  359. if(empty($sCheckFuncName) && isset($this->_aItem2CheckFunc[$a['name']]))
  360. $sCheckFuncName = $this->_aItem2CheckFunc[$a['name']];
  361. if(!$sCheckFuncName || !method_exists($this->_oModule, $sCheckFuncName))
  362. return true;
  363. return call_user_func_array(array($this->_oModule, $sCheckFuncName), $aCheckFuncParams) === true;
  364. }
  365. protected function _getTmplContentItem()
  366. {
  367. if(empty($this->_sTmplNameItem))
  368. return parent::_getTmplContentItem();
  369. return $this->_oModule->_oTemplate->getHtml($this->_sTmplNameItem);
  370. }
  371. }
  372. /** @} */