1
0

BxBaseVote.php 15 KB

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