BxBaseCmtsMenuActions.php 8.2 KB

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