1
0

BxBaseVote.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. $sClass = $this->_sStylePrefix . '-counter';
  81. if(!empty($aParams['class_counter']))
  82. $sClass .= $aParams['class_counter'];
  83. $aTmplVarsAttrs = array(
  84. array('key' => 'class', 'value' => $sClass),
  85. array('key' => 'href', 'value' => 'javascript:void(0)'),
  86. array('key' => 'title', 'value' => bx_html_attribute($this->_getTitleDoBy($aParams))),
  87. array('key' => 'onclick', 'value' => 'javascript:' . $this->getJsClickCounter($aParams))
  88. );
  89. $sHtmlId = isset($aParams['id_counter']) ? $aParams['id_counter'] : $this->_aHtmlIds['counter'];
  90. if(!empty($sHtmlId))
  91. $aTmplVarsAttrs[] = array('key' => 'id', 'value' => $sHtmlId);
  92. $aVote = !empty($aParams['vote']) && is_array($aParams['vote']) ? $aParams['vote'] : $this->_getVote();
  93. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounter(), array(
  94. 'bx_repeat:attrs' => $aTmplVarsAttrs,
  95. 'content' => $bShowEmpty || (int)$aVote['count'] > 0 ? $this->_getLabelCounter($aVote['count'], $aParams) : ''
  96. ));
  97. }
  98. public function getLegend($aParams = array())
  99. {
  100. return '';
  101. }
  102. public function getElementBlock($aParams = array())
  103. {
  104. $aParams['usage'] = BX_DOL_VOTE_USAGE_BLOCK;
  105. return $this->getElement($aParams);
  106. }
  107. public function getElementInline($aParams = array())
  108. {
  109. $aParams['usage'] = BX_DOL_VOTE_USAGE_INLINE;
  110. return $this->getElement($aParams);
  111. }
  112. public function getElement($aParams = array())
  113. {
  114. $sMethod = '_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT);
  115. if(!method_exists($this, $sMethod))
  116. return '';
  117. $aTmplVars = $this->_getTmplVarsElement($aParams);
  118. if(empty($aTmplVars) || !is_array($aTmplVars))
  119. return '';
  120. return $this->_oTemplate->parseHtmlByContent($this->$sMethod(), $aTmplVars);
  121. }
  122. /**
  123. * Internal methods.
  124. *
  125. * This method should be overwritten by subclass.
  126. */
  127. protected function _getTmplVarsElement($aParams = array())
  128. {
  129. $aParams = array_merge($this->_aElementDefaults, $aParams);
  130. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  131. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  132. $iObjectId = $this->getId();
  133. $iAuthorId = $this->_getAuthorId();
  134. $bCount = $this->_isCount();
  135. $isAllowedVote = $this->isAllowedVote();
  136. $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId);
  137. $aParams['track'] = $aParams['is_voted'] ? $this->_getTrack($iObjectId, $iAuthorId) : array();
  138. //--- Do Vote
  139. $bTmplVarsDoVote = $this->_isShowDoVote($aParams, $isAllowedVote, $bCount);
  140. $aTmplVarsDoVote = array();
  141. if($bTmplVarsDoVote)
  142. $aTmplVarsDoVote = array(
  143. 'style_prefix' => $this->_sStylePrefix,
  144. 'do_vote' => $this->_getDoVote($aParams, $isAllowedVote),
  145. );
  146. //--- Counter
  147. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedVote, $bCount);
  148. $aTmplVarsCounter = array();
  149. if($bTmplVarsCounter)
  150. $aTmplVarsCounter = array(
  151. 'style_prefix' => $this->_sStylePrefix,
  152. 'bx_if:show_hidden' => array(
  153. 'condition' => !$bCount && !$bShowCounterEmpty,
  154. 'content' => array()
  155. ),
  156. 'counter' => $this->getCounter($aParams)
  157. );
  158. //--- Legend
  159. $bTmplVarsLegend = $this->_isShowLegend($aParams, $isAllowedVote, $bCount);
  160. $aTmplVarsLegend = array();
  161. if($bTmplVarsLegend)
  162. $aTmplVarsLegend = array(
  163. 'legend' => $this->getLegend($aParams)
  164. );
  165. if(!$bTmplVarsDoVote && !$bTmplVarsCounter && !$bTmplVarsLegend)
  166. return array();
  167. $sClass = $this->_sStylePrefix . '-' . $this->_sType;
  168. if(!empty($aParams['class_element']))
  169. $sClass .= $aParams['class_element'];
  170. return array(
  171. 'style_prefix' => $this->_sStylePrefix,
  172. 'html_id' => $this->_aHtmlIds['main'],
  173. 'class' => $sClass,
  174. 'bx_if:show_vote_data' => array(
  175. 'condition' => false,
  176. 'content' => array()
  177. ),
  178. 'bx_if:show_do_vote' => array(
  179. 'condition' => $bTmplVarsDoVote,
  180. 'content' => $aTmplVarsDoVote
  181. ),
  182. 'bx_if:show_counter' => array(
  183. 'condition' => $bTmplVarsCounter,
  184. 'content' => $aTmplVarsCounter
  185. ),
  186. 'bx_if:show_legend' => array(
  187. 'condition' => $bTmplVarsLegend,
  188. 'content' => $aTmplVarsLegend
  189. ),
  190. 'script' => $this->getJsScript($bDynamicMode)
  191. );
  192. }
  193. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  194. {
  195. return '';
  196. }
  197. protected function _getLabelCounter($iCount, $aParams = array())
  198. {
  199. return _t('_vote_counter', $iCount);
  200. }
  201. protected function _getVotedBy($aParams = array())
  202. {
  203. $aTmplUsers = array();
  204. $aUserIds = $this->_oQuery->getPerformedBy($this->getId());
  205. foreach($aUserIds as $iUserId) {
  206. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  207. $aTmplUsers[] = array(
  208. 'style_prefix' => $this->_sStylePrefix,
  209. 'user_unit' => $sUserUnit
  210. );
  211. }
  212. if(empty($aTmplUsers))
  213. $aTmplUsers = MsgBox(_t('_Empty'));
  214. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  215. 'style_prefix' => $this->_sStylePrefix,
  216. 'bx_repeat:list' => $aTmplUsers
  217. ));
  218. }
  219. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  220. {
  221. return !isset($aParams['show_do_vote']) || $aParams['show_do_vote'] == true;
  222. }
  223. protected function _isShowCounter($aParams, $isAllowedVote, $bCount)
  224. {
  225. return isset($aParams['show_counter']) && $aParams['show_counter'] === true && ($isAllowedVote || $bCount);
  226. }
  227. protected function _isShowLegend($aParams, $isAllowedVote, $bCount)
  228. {
  229. return false;
  230. }
  231. protected function _getTmplContentElementBlock()
  232. {
  233. return self::$_sTmplContentElementBlock;
  234. }
  235. protected function _getTmplContentElementInline()
  236. {
  237. return self::$_sTmplContentElementInline;
  238. }
  239. protected function _getTmplContentCounter()
  240. {
  241. return self::$_sTmplContentCounter;
  242. }
  243. }
  244. /** @} */