1
0

BxBaseCmtsMenuActions.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. return $oReport->getElementInline($aReportParams);
  162. }
  163. protected function _getMenuItemElementApi($aItem, $aElement)
  164. {
  165. if(!$this->_bIsApi)
  166. return $aItem;
  167. return [
  168. 'id' => $aItem['id'],
  169. 'name' => $aItem['name'],
  170. 'display_type' => 'element',
  171. 'data' => $aElement
  172. ];
  173. }
  174. /**
  175. * Check if menu items is visible.
  176. * @param $a menu item array
  177. * @return boolean
  178. */
  179. protected function _isVisible ($a)
  180. {
  181. if(!parent::_isVisible($a))
  182. return false;
  183. if((int)$this->_aCmt['cmt_pinned'] > 0 && !empty($this->_aBp['pinned']) && $a['name'] != 'item-unpin')
  184. return false;
  185. $sCheckFuncName = '';
  186. $aCheckFuncParams = array();
  187. switch ($a['name']) {
  188. case 'item-reply':
  189. $sCheckFuncName = 'isReplyAllowed';
  190. if(!empty($this->_aCmt))
  191. $aCheckFuncParams = array($this->_aCmt);
  192. break;
  193. case 'item-quote':
  194. $sCheckFuncName = 'isQuoteAllowed';
  195. if(!empty($this->_aCmt))
  196. $aCheckFuncParams = array($this->_aCmt);
  197. break;
  198. case 'item-vote':
  199. $sCheckFuncName = 'isVoteAllowed';
  200. if(!empty($this->_aCmt))
  201. $aCheckFuncParams = array($this->_aCmt);
  202. break;
  203. case 'item-score':
  204. $sCheckFuncName = 'isScoreAllowed';
  205. if(!empty($this->_aCmt))
  206. $aCheckFuncParams = array($this->_aCmt);
  207. break;
  208. case 'item-report':
  209. $sCheckFuncName = 'isReportAllowed';
  210. if(!empty($this->_aCmt))
  211. $aCheckFuncParams = array($this->_aCmt);
  212. break;
  213. case 'item-pin':
  214. $sCheckFuncName = 'isPinAllowed';
  215. if(!empty($this->_aCmt))
  216. $aCheckFuncParams = array($this->_aCmt);
  217. break;
  218. case 'item-unpin':
  219. $sCheckFuncName = 'isUnpinAllowed';
  220. if(!empty($this->_aCmt))
  221. $aCheckFuncParams = array($this->_aCmt);
  222. break;
  223. case 'item-edit':
  224. $sCheckFuncName = 'isEditAllowed';
  225. if(!empty($this->_aCmt))
  226. $aCheckFuncParams = array($this->_aCmt);
  227. break;
  228. case 'item-delete':
  229. $sCheckFuncName = 'isRemoveAllowed';
  230. if(!empty($this->_aCmt))
  231. $aCheckFuncParams = array($this->_aCmt);
  232. break;
  233. case 'item-more':
  234. $sCheckFuncName = 'isMoreAllowed';
  235. if(!empty($this->_aCmt))
  236. $aCheckFuncParams = array($this->_aCmt);
  237. break;
  238. }
  239. if(!$sCheckFuncName || !method_exists($this->_oCmts, $sCheckFuncName))
  240. return true;
  241. return call_user_func_array(array($this->_oCmts, $sCheckFuncName), $aCheckFuncParams);
  242. }
  243. }
  244. /** @} */