BxBaseVote.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 $_sTmplContentDoVoteLikesLabel;
  17. protected $_bCssJsAdded;
  18. protected $_sJsObjName;
  19. protected $_sStylePrefix;
  20. protected $_aHtmlIds;
  21. protected $_aElementDefaults;
  22. protected $_sTmplNameLegend;
  23. protected $_sTmplNameByList;
  24. protected $_sTmplNameDoVoteStars;
  25. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  26. {
  27. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  28. $this->_bCssJsAdded = false;
  29. $this->_sJsObjName = 'oVote' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  30. $this->_sStylePrefix = 'bx-vote';
  31. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  32. $this->_aHtmlIds = array(
  33. 'main_stars' => 'bx-vote-stars-' . $sHtmlId,
  34. 'main_likes' => 'bx-vote-likes-' . $sHtmlId,
  35. 'counter' => 'bx-vote-counter-' . $sHtmlId,
  36. 'by_popup' => 'bx-vote-by-popup-' . $sHtmlId,
  37. 'legend_stars' => 'bx-vote-stars-legend-' . $sHtmlId,
  38. );
  39. $this->_aElementDefaults = array(
  40. 'stars' => array(
  41. 'show_do_vote_legend' => false,
  42. 'show_counter' => true,
  43. 'show_counter_empty' => false,
  44. 'show_legend' => false
  45. ),
  46. 'likes' => array(
  47. 'show_do_vote_as_button' => false,
  48. 'show_do_vote_as_button_small' => false,
  49. 'show_do_vote_icon' => true,
  50. 'show_do_vote_label' => false,
  51. 'show_counter' => true,
  52. 'show_counter_empty' => false,
  53. 'show_legend' => false
  54. )
  55. );
  56. $this->_sTmplNameLegend = 'vote_legend.html';
  57. $this->_sTmplNameByList = 'vote_by_list.html';
  58. $this->_sTmplNameDoVoteStars = 'vote_do_vote_stars.html';
  59. if(empty(self::$_sTmplContentElementBlock))
  60. self::$_sTmplContentElementBlock = $this->_oTemplate->getHtml('vote_element_block.html');
  61. if(empty(self::$_sTmplContentElementInline))
  62. self::$_sTmplContentElementInline = $this->_oTemplate->getHtml('vote_element_inline.html');
  63. if(empty(self::$_sTmplContentDoVoteLikesLabel))
  64. self::$_sTmplContentDoVoteLikesLabel = $this->_oTemplate->getHtml('vote_do_vote_likes_label.html');
  65. }
  66. public function getJsObjectName()
  67. {
  68. return $this->_sJsObjName;
  69. }
  70. public function getJsScript($bDynamicMode = false)
  71. {
  72. $aParams = array(
  73. 'sObjName' => $this->_sJsObjName,
  74. 'sSystem' => $this->getSystemName(),
  75. 'iAuthorId' => $this->_getAuthorId(),
  76. 'iObjId' => $this->getId(),
  77. 'iLikeMode' => $this->isLikeMode() ? 1 : 0,
  78. 'sRootUrl' => BX_DOL_URL_ROOT,
  79. 'sStylePrefix' => $this->_sStylePrefix,
  80. 'aHtmlIds' => $this->_aHtmlIds
  81. );
  82. $sCode = "var " . $this->_sJsObjName . " = new BxDolVote(" . json_encode($aParams) . ");";
  83. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  84. }
  85. public function getJsClick()
  86. {
  87. if(!$this->isLikeMode())
  88. return false;
  89. return $this->getJsObjectName() . '.vote(this, ' . $this->getMaxValue() . ')';
  90. }
  91. public function getJsClickCounter()
  92. {
  93. return $this->getJsObjectName() . '.toggleByPopup(this)';
  94. }
  95. public function getCounter($aParams = array())
  96. {
  97. $sJsObject = $this->getJsObjectName();
  98. $bShowEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
  99. $bShowDoVoteAsButtonSmall = $this->_bLike && isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  100. $bShowDoVoteAsButton = $this->_bLike && !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  101. $aVote = $this->_oQuery->getVote($this->getId());
  102. $sClass = $this->_sStylePrefix . '-counter';
  103. if($bShowDoVoteAsButtonSmall)
  104. $sClass .= ' bx-btn-small-height';
  105. if($bShowDoVoteAsButton)
  106. $sClass .= ' bx-btn-height';
  107. return $this->_oTemplate->parseLink('javascript:void(0)', $bShowEmpty || (int)$aVote['count'] > 0 ? $this->_getLabelCounter($aVote['count']) : '', array(
  108. 'id' => $this->_aHtmlIds['counter'],
  109. 'class' => $sClass,
  110. 'title' => _t($this->_getTitleDoBy()),
  111. 'onclick' => 'javascript:' . $this->getJsClickCounter()
  112. ));
  113. }
  114. /**
  115. * Note. The Legend is available in Start based mode only.
  116. */
  117. public function getLegend($aParams = array())
  118. {
  119. $sJsObject = $this->getJsObjectName();
  120. $iMinValue = $this->getMinValue();
  121. $iMaxValue = $this->getMaxValue();
  122. $aLegend = $this->_oQuery->getLegend($this->_iId);
  123. $aTmplVarsItems = array();
  124. for($i = $iMaxValue; $i >= $iMinValue; $i--) {
  125. $aTmplVarsStars = $aTmplVarsSlider = array();
  126. for($j = $iMinValue; $j <= $iMaxValue; $j++) {
  127. $aTmplVarsStars[] = array(
  128. 'style_prefix' => $this->_sStylePrefix,
  129. 'value' => $i
  130. );
  131. $aTmplVarsSlider[] = array(
  132. 'style_prefix' => $this->_sStylePrefix
  133. );
  134. }
  135. $aTmplVarsItems[] = array(
  136. 'style_prefix' => $this->_sStylePrefix,
  137. 'value' => $i,
  138. 'bx_repeat:stars' => $aTmplVarsStars,
  139. 'bx_repeat:slider' => $aTmplVarsSlider,
  140. 'label' => isset($aLegend[$i]['count']) ? (int)$aLegend[$i]['count'] : 0
  141. );
  142. }
  143. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameLegend, array(
  144. 'style_prefix' => $this->_sStylePrefix,
  145. 'html_id' => $this->_aHtmlIds['legend_stars'],
  146. 'type' => $this->_sType,
  147. 'bx_repeat:items' => $aTmplVarsItems
  148. ));
  149. }
  150. public function getElementBlock($aParams = array())
  151. {
  152. $aParams['usage'] = BX_DOL_VOTE_USAGE_BLOCK;
  153. return $this->getElement($aParams);
  154. }
  155. public function getElementInline($aParams = array())
  156. {
  157. $aParams['usage'] = BX_DOL_VOTE_USAGE_INLINE;
  158. return $this->getElement($aParams);
  159. }
  160. public function getElement($aParams = array())
  161. {
  162. $aParams = array_merge($this->_aElementDefaults[$this->_sType], $aParams);
  163. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  164. $bShowDoVoteAsButtonSmall = $this->_bLike && isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  165. $bShowDoVoteAsButton = $this->_bLike && !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  166. $sMethodDoVote = '_getDoVote' . ucfirst($this->_sType);
  167. if(!method_exists($this, $sMethodDoVote))
  168. return '';
  169. $iObjectId = $this->getId();
  170. $iAuthorId = $this->_getAuthorId();
  171. $aVote = $this->_oQuery->getVote($iObjectId);
  172. $bCount = (int)$aVote['count'] != 0;
  173. $isAllowedVote = $this->isAllowedVote();
  174. $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
  175. //--- Do Vote
  176. $bTmplVarsDoVote = $this->_isShowDoVote($aParams, $isAllowedVote, $bCount);
  177. $aTmplVarsDoVote = array();
  178. if($bTmplVarsDoVote)
  179. $aTmplVarsDoVote = array(
  180. 'style_prefix' => $this->_sStylePrefix,
  181. 'do_vote' => $this->$sMethodDoVote($aParams, $isAllowedVote),
  182. );
  183. //--- Counter
  184. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedVote, $bCount);
  185. $aTmplVarsCounter = array();
  186. if($bTmplVarsCounter)
  187. $aTmplVarsCounter = array(
  188. 'style_prefix' => $this->_sStylePrefix,
  189. 'bx_if:show_hidden' => array(
  190. 'condition' => !$bCount,
  191. 'content' => array()
  192. ),
  193. 'counter' => $this->getCounter($aParams)
  194. );
  195. //--- Legend
  196. $bTmplVarsLegend = $this->_isShowLegend($aParams, $isAllowedVote, $bCount);
  197. $aTmplVarsLegend = array();
  198. if($bTmplVarsLegend)
  199. $aTmplVarsLegend = array(
  200. 'legend' => $this->getLegend($aParams)
  201. );
  202. if(!$bTmplVarsDoVote && !$bTmplVarsCounter && !$bTmplVarsLegend)
  203. return '';
  204. $sTmplName = self::${'_sTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT)};
  205. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  206. 'style_prefix' => $this->_sStylePrefix,
  207. 'html_id' => $this->_aHtmlIds['main_' . $this->_sType],
  208. 'class' => $this->_sStylePrefix . '-' . $this->_sType . ($bShowDoVoteAsButton ? '-button' : '') . ($bShowDoVoteAsButtonSmall ? '-button-small' : ''),
  209. 'rate' => $aVote['rate'],
  210. 'count' => $aVote['count'],
  211. 'bx_if:show_do_vote' => array(
  212. 'condition' => $bTmplVarsDoVote,
  213. 'content' => $aTmplVarsDoVote
  214. ),
  215. 'bx_if:show_counter' => array(
  216. 'condition' => $bTmplVarsCounter,
  217. 'content' => $aTmplVarsCounter
  218. ),
  219. 'bx_if:show_legend' => array(
  220. 'condition' => $bTmplVarsLegend,
  221. 'content' => $aTmplVarsLegend
  222. ),
  223. 'script' => $this->getJsScript($bDynamicMode)
  224. ));
  225. }
  226. protected function _getDoVoteStars($aParams = array(), $isAllowedVote = true)
  227. {
  228. $sJsObject = $this->getJsObjectName();
  229. $iMinValue = $this->getMinValue();
  230. $iMaxValue = $this->getMaxValue();
  231. $aTmplVarsStars = $aTmplVarsLegend = $aTmplVarsSlider = $aTmplVarsButtons = array();
  232. for($i = $iMinValue; $i <= $iMaxValue; $i++) {
  233. $aTmplVarsStars[] = array(
  234. 'style_prefix' => $this->_sStylePrefix,
  235. 'value' => $i
  236. );
  237. $aTmplVarsLegend[] = array(
  238. 'style_prefix' => $this->_sStylePrefix,
  239. 'value' => $i
  240. );
  241. $aTmplVarsSlider[] = array(
  242. 'style_prefix' => $this->_sStylePrefix
  243. );
  244. if($isAllowedVote)
  245. $aTmplVarsButtons[] = array(
  246. 'style_prefix' => $this->_sStylePrefix,
  247. 'js_object' => $sJsObject,
  248. 'value' => $i
  249. );
  250. }
  251. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoVoteStars, array(
  252. 'style_prefix' => $this->_sStylePrefix,
  253. 'bx_repeat:stars' => $aTmplVarsStars,
  254. 'bx_if:show_legend' => array(
  255. 'condition' => isset($aParams['show_do_vote_legend']) && $aParams['show_do_vote_legend'] === true,
  256. 'content' => array(
  257. 'style_prefix' => $this->_sStylePrefix,
  258. 'bx_repeat:legend' => $aTmplVarsLegend,
  259. )
  260. ),
  261. 'bx_repeat:slider' => $aTmplVarsSlider,
  262. 'bx_repeat:buttons' => $aTmplVarsButtons,
  263. ));
  264. }
  265. protected function _getDoVoteLikes($aParams = array(), $isAllowedVote = true)
  266. {
  267. $bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
  268. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  269. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  270. $bDisabled = !$isAllowedVote || ($bVoted && !$this->isUndo());
  271. $sClass = '';
  272. if($bShowDoVoteAsButton)
  273. $sClass = 'bx-btn';
  274. else if ($bShowDoVoteAsButtonSmall)
  275. $sClass = 'bx-btn bx-btn-small';
  276. if($bDisabled)
  277. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-vote-disabled';
  278. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDoLike($aParams), array(
  279. 'class' => $this->_sStylePrefix . '-do-vote ' . $sClass,
  280. 'title' => _t($this->_getTitleDoLike($bVoted)),
  281. 'onclick' => !$bDisabled ? $this->getJsClick() : ''
  282. ));
  283. }
  284. protected function _getLabelCounter($iCount)
  285. {
  286. return _t('_vote_counter', $iCount);
  287. }
  288. protected function _getLabelDoLike($aParams = array())
  289. {
  290. $bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
  291. return $this->_oTemplate->parseHtmlByContent(self::$_sTmplContentDoVoteLikesLabel, array(
  292. 'bx_if:show_icon' => array(
  293. 'condition' => isset($aParams['show_do_vote_icon']) && $aParams['show_do_vote_icon'] == true,
  294. 'content' => array(
  295. 'name' => $this->_getIconDoLike($bVoted)
  296. )
  297. ),
  298. 'bx_if:show_text' => array(
  299. 'condition' => isset($aParams['show_do_vote_label']) && $aParams['show_do_vote_label'] == true,
  300. 'content' => array(
  301. 'text' => _t($this->_getTitleDoLike($bVoted))
  302. )
  303. )
  304. ));
  305. }
  306. protected function _getVotedBy()
  307. {
  308. $aTmplUsers = array();
  309. $aUserIds = $this->_oQuery->getPerformedBy($this->getId());
  310. foreach($aUserIds as $iUserId) {
  311. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  312. $aTmplUsers[] = array(
  313. 'style_prefix' => $this->_sStylePrefix,
  314. 'user_unit' => $sUserUnit
  315. );
  316. }
  317. if(empty($aTmplUsers))
  318. $aTmplUsers = MsgBox(_t('_Empty'));
  319. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  320. 'style_prefix' => $this->_sStylePrefix,
  321. 'bx_repeat:list' => $aTmplUsers
  322. ));
  323. }
  324. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  325. {
  326. $bShowDoVote = !isset($aParams['show_do_vote']) || $aParams['show_do_vote'] == true;
  327. return $bShowDoVote && (!$this->_bLike || $isAllowedVote || $bCount);
  328. }
  329. protected function _isShowCounter($aParams, $isAllowedVote, $bCount)
  330. {
  331. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true;
  332. return $bShowCounter && ($isAllowedVote || $bCount);
  333. }
  334. protected function _isShowLegend($aParams, $isAllowedVote, $bCount)
  335. {
  336. return !$this->_bLike && isset($aParams['show_legend']) && $aParams['show_legend'] === true;
  337. }
  338. }
  339. /** @} */