BxBaseVote.php 11 KB

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