BxBaseVote.php 15 KB

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