BxBaseVote.php 15 KB

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