BxBaseVote.php 15 KB

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