BxBaseVote.php 11 KB

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