BxBaseVote.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. * @see BxDolVote
  11. */
  12. class BxBaseVote extends BxDolVote
  13. {
  14. protected $_bCssJsAdded;
  15. protected $_sJsClsName;
  16. protected $_sJsObjName;
  17. protected $_sStylePrefix;
  18. protected $_aHtmlIds;
  19. protected $_sTmplNameLegend;
  20. protected $_sTmplNameByList;
  21. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  22. {
  23. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  24. $this->_aElementDefaults = array();
  25. $this->_bCssJsAdded = false;
  26. $this->_sStylePrefix = 'bx-vote';
  27. $this->_sJsClsName = 'BxDolVote';
  28. $this->_sJsObjName = 'oVote' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  29. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  30. $this->_aHtmlIds = array(
  31. 'counter' => 'bx-vote-counter-' . $sHtmlId,
  32. 'by_popup' => 'bx-vote-by-popup-' . $sHtmlId
  33. );
  34. $this->_sTmplNameLegend = 'vote_legend.html';
  35. $this->_sTmplNameByList = 'vote_by_list.html';
  36. $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('vote_element_block.html');
  37. $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('vote_element_inline.html');
  38. $this->_sTmplContentCounter = $this->_oTemplate->getHtml('vote_counter.html');
  39. }
  40. public function getJsClassName()
  41. {
  42. return $this->_sJsClsName;
  43. }
  44. public function getJsObjectName()
  45. {
  46. return $this->_sJsObjName;
  47. }
  48. public function getJsScript($aParams = array())
  49. {
  50. $sJsObjName = $this->getJsObjectName();
  51. $sJsObjClass = $this->getJsClassName();
  52. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  53. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new " . $sJsObjClass . "(" . json_encode($this->_prepareParamsData([
  54. 'aRequestParams' => $this->_prepareRequestParamsData($aParams)
  55. ])) . ");";
  56. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  57. }
  58. public function getJsClick($iValue = 0)
  59. {
  60. return $this->getJsObjectName() . '.vote(this, ' . $iValue . ')';
  61. }
  62. public function getJsClickCounter($aParams = array())
  63. {
  64. return $this->getJsObjectName() . '.toggleByPopup(this)';
  65. }
  66. public function getCounter($aParams = [])
  67. {
  68. $aParams = array_merge($this->_aElementDefaults, $aParams);
  69. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  70. $bShowEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  71. $bShowActive = $this->isAllowedVoteViewVoters() && (!isset($aParams['show_counter_active']) || (bool)$aParams['show_counter_active'] === true);
  72. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  73. $sClass = 'sys-action-counter';
  74. if(isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true)
  75. $sClass .= ' sys-ac-only';
  76. $sClass .= ' ' .$this->_sStylePrefix . '-counter ' . $this->_sStylePrefix . '-counter-' . $this->_sType;
  77. if(!empty($aParams['class_counter']))
  78. $sClass .= $aParams['class_counter'];
  79. $aTmplVarsAttrs = array();
  80. if($bShowActive)
  81. $aTmplVarsAttrs = array_merge($aTmplVarsAttrs, array(
  82. array('key' => 'href', 'value' => 'javascript:void(0)'),
  83. array('key' => 'title', 'value' => bx_html_attribute($this->_getTitleDoBy($aParams))),
  84. array('key' => 'onclick', 'value' => 'javascript:' . $this->getJsClickCounter($aParams))
  85. ));
  86. $sHtmlId = isset($aParams['id_counter']) ? $aParams['id_counter'] : $this->_aHtmlIds['counter'];
  87. if(!empty($sHtmlId))
  88. $aTmplVarsAttrs[] = array('key' => 'id', 'value' => $sHtmlId);
  89. $aVote = !empty($aParams['vote']) && is_array($aParams['vote']) ? $aParams['vote'] : $this->_getVote();
  90. $sContent = $bShowEmpty || (int)$aVote['count'] > 0 ? $this->_getCounterLabel($aVote['count'], $aParams) : '';
  91. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounter(), array(
  92. 'bx_if:show_text' => array(
  93. 'condition' => !$bShowActive,
  94. 'content' => array(
  95. 'class' => $sClass,
  96. 'bx_repeat:attrs' => $aTmplVarsAttrs,
  97. 'content' => $sContent
  98. )
  99. ),
  100. 'bx_if:show_link' => array(
  101. 'condition' => $bShowActive,
  102. 'content' => array(
  103. 'class' => $sClass,
  104. 'bx_repeat:attrs' => $aTmplVarsAttrs,
  105. 'content' => $sContent
  106. )
  107. ),
  108. 'class' => $sClass,
  109. 'bx_repeat:attrs' => $aTmplVarsAttrs,
  110. 'content' => $sContent,
  111. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  112. ));
  113. }
  114. public function getLegend($aParams = array())
  115. {
  116. return '';
  117. }
  118. public function getElementBlock($aParams = array())
  119. {
  120. $aParams['usage'] = BX_DOL_VOTE_USAGE_BLOCK;
  121. return $this->getElement($aParams);
  122. }
  123. public function getElementInline($aParams = array())
  124. {
  125. $aParams['usage'] = BX_DOL_VOTE_USAGE_INLINE;
  126. return $this->getElement($aParams);
  127. }
  128. public function getElement($aParams = array())
  129. {
  130. $sMethod = '_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT);
  131. if(!method_exists($this, $sMethod))
  132. return '';
  133. $aTmplVars = $this->_getTmplVarsElement($aParams);
  134. if(empty($aTmplVars) || !is_array($aTmplVars))
  135. return '';
  136. return $this->_oTemplate->parseHtmlByContent($this->$sMethod(), $aTmplVars);
  137. }
  138. /**
  139. * Internal methods.
  140. */
  141. protected function _prepareParamsData($aParams)
  142. {
  143. return parent::_prepareParamsData(array_merge([
  144. 'sObjName' => $this->getJsObjectName(),
  145. 'sStylePrefix' => $this->_sStylePrefix,
  146. 'aHtmlIds' => $this->_aHtmlIds,
  147. ], $aParams));
  148. }
  149. /*
  150. * This method should be overwritten by subclass.
  151. */
  152. protected function _getTmplVarsElement($aParams = array())
  153. {
  154. $aParams = array_merge($this->_aElementDefaults, $aParams);
  155. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  156. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  157. $iObjectId = $this->getId();
  158. $iAuthorId = $this->_getAuthorId();
  159. $bCount = $this->_isCount();
  160. $isAllowedVote = $this->isAllowedVote();
  161. $isAllowedVoteView = $this->isAllowedVoteView();
  162. $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId);
  163. $aParams['track'] = $aParams['is_voted'] ? $this->_getTrack($iObjectId, $iAuthorId) : array();
  164. //--- Do Vote
  165. $bTmplVarsDoVote = $this->_isShowDoVote($aParams, $isAllowedVote, $bCount);
  166. $aTmplVarsDoVote = array();
  167. if($bTmplVarsDoVote)
  168. $aTmplVarsDoVote = array(
  169. 'style_prefix' => $this->_sStylePrefix,
  170. 'do_vote' => $this->_getDoVote($aParams, $isAllowedVote),
  171. );
  172. //--- Counter
  173. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  174. $aTmplVarsCounter = array();
  175. if($bTmplVarsCounter)
  176. $aTmplVarsCounter = array(
  177. 'style_prefix' => $this->_sStylePrefix,
  178. 'bx_if:show_hidden' => array(
  179. 'condition' => !$bCount && !$bShowCounterEmpty,
  180. 'content' => array()
  181. ),
  182. 'counter' => $this->getCounter(array_merge($aParams, [
  183. 'show_counter_only' => false,
  184. 'show_script' => false
  185. ]))
  186. );
  187. //--- Legend
  188. $bTmplVarsLegend = $this->_isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  189. $aTmplVarsLegend = array();
  190. if($bTmplVarsLegend)
  191. $aTmplVarsLegend = array(
  192. 'legend' => $this->getLegend($aParams)
  193. );
  194. if(!$bTmplVarsDoVote && !$bTmplVarsCounter && !$bTmplVarsLegend)
  195. return array();
  196. $sClass = $this->_sStylePrefix . '-' . $this->_sType;
  197. if(!empty($aParams['class_element']))
  198. $sClass .= $aParams['class_element'];
  199. return array(
  200. 'style_prefix' => $this->_sStylePrefix,
  201. 'html_id' => $this->_aHtmlIds['main'],
  202. 'class' => $sClass . ' ' . $this->_aHtmlIds['main'],
  203. 'bx_if:show_vote_data' => array(
  204. 'condition' => false,
  205. 'content' => array()
  206. ),
  207. 'bx_if:show_do_vote' => array(
  208. 'condition' => $bTmplVarsDoVote,
  209. 'content' => $aTmplVarsDoVote
  210. ),
  211. 'bx_if:show_counter' => array(
  212. 'condition' => $bTmplVarsCounter,
  213. 'content' => $aTmplVarsCounter
  214. ),
  215. 'bx_if:show_legend' => array(
  216. 'condition' => $bTmplVarsLegend,
  217. 'content' => $aTmplVarsLegend
  218. ),
  219. 'script' => $this->getJsScript($aParams)
  220. );
  221. }
  222. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  223. {
  224. return '';
  225. }
  226. protected function _getCounterLabel($iCount, $aParams = array())
  227. {
  228. return _t(isset($aParams['caption']) ? $aParams['caption'] : '_vote_counter', $iCount);
  229. }
  230. protected function _getVotedBy($aParams = array())
  231. {
  232. $aTmplUsers = array();
  233. $aUserIds = $this->_oQuery->getPerformedBy($this->getId());
  234. foreach($aUserIds as $iUserId) {
  235. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  236. $aTmplUsers[] = array(
  237. 'style_prefix' => $this->_sStylePrefix,
  238. 'user_unit' => $sUserUnit
  239. );
  240. }
  241. if(empty($aTmplUsers))
  242. $aTmplUsers = MsgBox(_t('_Empty'));
  243. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  244. 'style_prefix' => $this->_sStylePrefix,
  245. 'class' => '',
  246. 'bx_repeat:list' => $aTmplUsers
  247. ));
  248. }
  249. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  250. {
  251. return !isset($aParams['show_do_vote']) || (bool)$aParams['show_do_vote'] === true;
  252. }
  253. protected function _isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount)
  254. {
  255. return isset($aParams['show_counter']) && (bool)$aParams['show_counter'] === true && $isAllowedVoteView && ($isAllowedVote || $bCount);
  256. }
  257. protected function _isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount)
  258. {
  259. return false;
  260. }
  261. }
  262. /** @} */