BxBaseCmtsMenuActions.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Menu representation.
  11. * @see BxDolMenu
  12. */
  13. class BxBaseCmtsMenuActions extends BxTemplMenuCustom
  14. {
  15. protected static $_sModeActions = 'actions';
  16. protected static $_sModeCounters = 'counters';
  17. protected $_bIsApi;
  18. protected $_oCmts;
  19. protected $_aCmt;
  20. protected $_aBp;
  21. protected $_aDp;
  22. protected $_sMode;
  23. protected $_bShowTitles;
  24. protected $_bShowCounters;
  25. protected $_bShowCountersEmpty;
  26. protected $_bShowCountersIcons;
  27. public function __construct ($aObject, $oTemplate)
  28. {
  29. parent::__construct ($aObject, $oTemplate);
  30. $this->_bIsApi = bx_is_api();
  31. $this->_aBp = [];
  32. $this->_aDp = [];
  33. $this->_sMode = strpos($this->_sObject, '_actions') !== false ? self::$_sModeActions : self::$_sModeCounters;
  34. $this->_bShowTitles = true;
  35. $this->_bShowCounters = false;
  36. $this->_bShowCountersEmpty = false;
  37. $this->_bShowCountersIcons = true;
  38. }
  39. public function setCmtsData($oCmts, $iCmtId, $aBp = [], $aDp = [])
  40. {
  41. if(empty($oCmts) || empty($iCmtId))
  42. return;
  43. $this->_oCmts = $oCmts;
  44. $this->_aCmt = $oCmts->getCommentRow($iCmtId);
  45. $this->_aBp = $aBp;
  46. $this->_aDp = $aDp;
  47. $sJsObject = $oCmts->getJsObjectName();
  48. $this->addMarkers(array(
  49. 'js_object' => $sJsObject,
  50. 'cmt_system' => $this->_oCmts->getSystemName(),
  51. 'cmt_object_id' => $this->_oCmts->getId(),
  52. 'cmt_id' => $iCmtId,
  53. 'content_id' => $iCmtId,
  54. 'reply_onclick' => $sJsObject . '.toggleReply(this, ' . $iCmtId . ')',
  55. 'quote_onclick' => $sJsObject . '.toggleQuote(this, ' . $iCmtId . ')'
  56. ));
  57. }
  58. public function getMenuItems()
  59. {
  60. if($this->_sMode == self::$_sModeActions && (int)$this->_aCmt['cmt_pinned'] > 0 && !empty($this->_aBp['pinned']))
  61. $this->_aObject['menu_items'] = [
  62. 'item-unpin' => [
  63. 'name' => 'item-unpin',
  64. 'title' => '_sys_menu_item_title_cmts_item_unpin',
  65. 'link' => 'javascript:void(0)',
  66. 'onclick' => 'javascript:{js_object}.cmtPin(this, {content_id}, 0)',
  67. 'icon' => 'thumbtack'
  68. ]
  69. ];
  70. return parent::getMenuItems();
  71. }
  72. protected function _getMenuItemItemVote($aItem)
  73. {
  74. $oVote = $this->_oCmts->getVoteObject($this->_aCmt['cmt_unique_id']);
  75. if(!$oVote)
  76. return false;
  77. $aVotesParams = [
  78. 'dynamic_mode' => $this->_bDynamicMode,
  79. 'show_do_vote_label' => $this->_bShowTitles,
  80. 'show_counter' => $this->_bShowCounters,
  81. 'show_counter_empty' => $this->_bShowCountersEmpty,
  82. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  83. ];
  84. switch($this->_sMode) {
  85. case self::$_sModeActions:
  86. $sVotesMethod = 'getElementInline';
  87. break;
  88. case self::$_sModeCounters:
  89. $sVotesMethod = 'getCounter';
  90. break;
  91. }
  92. if($this->_bIsApi)
  93. return $this->_getMenuItemElementApi($aItem, $oVote->getElementApi(array_merge($aVotesParams, [
  94. 'show_counter' => true,
  95. ])));
  96. return $oVote->$sVotesMethod($aVotesParams);
  97. }
  98. protected function _getMenuItemItemReaction($aItem)
  99. {
  100. $oReaction = $this->_oCmts->getReactionObject($this->_aCmt['cmt_unique_id']);
  101. if(!$oReaction)
  102. return false;
  103. $aReactionsParams = [
  104. 'dynamic_mode' => $this->_bDynamicMode,
  105. 'show_do_vote_label' => $this->_bShowTitles,
  106. 'show_counter' => $this->_bShowCounters,
  107. 'show_counter_empty' => $this->_bShowCountersEmpty,
  108. 'show_counter_style' => 'compound',
  109. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  110. ];
  111. switch($this->_sMode) {
  112. case self::$_sModeActions:
  113. $sReactionsMethod = 'getElementInline';
  114. break;
  115. case self::$_sModeCounters:
  116. $sReactionsMethod = 'getCounter';
  117. break;
  118. }
  119. if($this->_bIsApi)
  120. return $this->_getMenuItemElementApi($aItem, $oReaction->getElementApi(array_merge($aReactionsParams, [
  121. 'show_counter' => true,
  122. ])));
  123. return $oReaction->$sReactionsMethod($aReactionsParams);
  124. }
  125. protected function _getMenuItemItemScore($aItem)
  126. {
  127. $oScore = $this->_oCmts->getScoreObject($this->_aCmt['cmt_unique_id']);
  128. if(!$oScore)
  129. return false;
  130. $aScoresParams = array(
  131. 'dynamic_mode' => $this->_bDynamicMode,
  132. 'show_do_vote_label' => $this->_bShowTitles,
  133. 'show_counter' => $this->_bShowCounters,
  134. 'show_counter_empty' => $this->_bShowCountersEmpty,
  135. 'show_counter_label_icon' => $this->_bShowCountersIcons,
  136. );
  137. switch($this->_sMode) {
  138. case self::$_sModeActions:
  139. $sScoresMethod = 'getElementInline';
  140. break;
  141. case self::$_sModeCounters:
  142. $sScoresMethod = 'getCounter';
  143. break;
  144. }
  145. if($this->_bIsApi)
  146. return $this->_getMenuItemElementApi($aItem, $oScore->getElementApi(array_merge($aScoresParams, [
  147. 'show_counter' => true,
  148. ])));
  149. return $oScore->$sScoresMethod($aScoresParams);
  150. }
  151. protected function _getMenuItemItemReport($aItem)
  152. {
  153. $oReport = $this->_oCmts->getReportObject($this->_aCmt['cmt_unique_id']);
  154. if(!$oReport)
  155. return false;
  156. $aReportParams = [
  157. 'dynamic_mode' => $this->_bDynamicMode,
  158. 'show_do_report_label' => $this->_bShowTitles,
  159. 'show_counter' => $this->_bShowCounters,
  160. ];
  161. if($this->_bIsApi)
  162. return $this->_getMenuItemElementApi($aItem, $oReport->getElementApi(array_merge($aReportParams, [
  163. 'show_counter' => true,
  164. ])));
  165. return $oReport->getElementInline($aReportParams);
  166. }
  167. protected function _getMenuItemElementApi($aItem, $aElement)
  168. {
  169. if(!$this->_bIsApi)
  170. return $aItem;
  171. return [
  172. 'id' => $aItem['id'],
  173. 'name' => $aItem['name'],
  174. 'display_type' => 'element',
  175. 'data' => $aElement
  176. ];
  177. }
  178. /**
  179. * Check if menu items is visible.
  180. * @param $a menu item array
  181. * @return boolean
  182. */
  183. protected function _isVisible ($a)
  184. {
  185. if(!parent::_isVisible($a))
  186. return false;
  187. if((int)$this->_aCmt['cmt_pinned'] > 0 && !empty($this->_aBp['pinned']) && $a['name'] != 'item-unpin')
  188. return false;
  189. $sCheckFuncName = '';
  190. $aCheckFuncParams = array();
  191. switch ($a['name']) {
  192. case 'item-reply':
  193. $sCheckFuncName = 'isReplyAllowed';
  194. if(!empty($this->_aCmt))
  195. $aCheckFuncParams = array($this->_aCmt);
  196. break;
  197. case 'item-quote':
  198. $sCheckFuncName = 'isQuoteAllowed';
  199. if(!empty($this->_aCmt))
  200. $aCheckFuncParams = array($this->_aCmt);
  201. break;
  202. case 'item-vote':
  203. $sCheckFuncName = 'isVoteAllowed';
  204. if(!empty($this->_aCmt))
  205. $aCheckFuncParams = array($this->_aCmt);
  206. break;
  207. case 'item-score':
  208. $sCheckFuncName = 'isScoreAllowed';
  209. if(!empty($this->_aCmt))
  210. $aCheckFuncParams = array($this->_aCmt);
  211. break;
  212. case 'item-report':
  213. $sCheckFuncName = 'isReportAllowed';
  214. if(!empty($this->_aCmt))
  215. $aCheckFuncParams = array($this->_aCmt);
  216. break;
  217. case 'item-pin':
  218. $sCheckFuncName = 'isPinAllowed';
  219. if(!empty($this->_aCmt))
  220. $aCheckFuncParams = array($this->_aCmt);
  221. break;
  222. case 'item-unpin':
  223. $sCheckFuncName = 'isUnpinAllowed';
  224. if(!empty($this->_aCmt))
  225. $aCheckFuncParams = array($this->_aCmt);
  226. break;
  227. case 'item-edit':
  228. $sCheckFuncName = 'isEditAllowed';
  229. if(!empty($this->_aCmt))
  230. $aCheckFuncParams = array($this->_aCmt);
  231. break;
  232. case 'item-delete':
  233. $sCheckFuncName = 'isRemoveAllowed';
  234. if(!empty($this->_aCmt))
  235. $aCheckFuncParams = array($this->_aCmt);
  236. break;
  237. case 'item-more':
  238. $sCheckFuncName = 'isMoreAllowed';
  239. if(!empty($this->_aCmt))
  240. $aCheckFuncParams = array($this->_aCmt);
  241. break;
  242. }
  243. if(!$sCheckFuncName || !method_exists($this->_oCmts, $sCheckFuncName))
  244. return true;
  245. return call_user_func_array(array($this->_oCmts, $sCheckFuncName), $aCheckFuncParams);
  246. }
  247. }
  248. /** @} */