BxBaseVote.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. $bShowDoVote = !isset($aParams['show_do_vote']) || $aParams['show_do_vote'] == true;
  184. $bShowDoVoteAsButtonSmall = $this->_bLike && isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  185. $bShowDoVoteAsButton = $this->_bLike && !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  186. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true;
  187. $bShowLegend = !$this->_bLike && isset($aParams['show_legend']) && $aParams['show_legend'] === true;
  188. $sMethodDoVote = '_getDoVote' . ucfirst($this->_sType);
  189. if(!method_exists($this, $sMethodDoVote))
  190. return '';
  191. $iObjectId = $this->getId();
  192. $iAuthorId = $this->_getAuthorId();
  193. $aVote = $this->_oQuery->getVote($iObjectId);
  194. $bCount = (int)$aVote['count'] != 0;
  195. $isAllowedVote = $this->isAllowedVote();
  196. $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
  197. //--- Do Vote
  198. $bTmplVarsDoVote = $bShowDoVote && (!$this->_bLike || $bCount || $isAllowedVote);
  199. $aTmplVarsDoVote = array();
  200. if($bTmplVarsDoVote)
  201. $aTmplVarsDoVote = array(
  202. 'style_prefix' => $this->_sStylePrefix,
  203. 'do_vote' => $this->$sMethodDoVote($aParams, $isAllowedVote),
  204. );
  205. //--- Counter
  206. $bTmplVarsCounter = $bShowCounter && ($bCount || $isAllowedVote);
  207. $aTmplVarsCounter = array();
  208. if($bTmplVarsCounter)
  209. $aTmplVarsCounter = array(
  210. 'style_prefix' => $this->_sStylePrefix,
  211. 'bx_if:show_hidden' => array(
  212. 'condition' => !$bCount,
  213. 'content' => array()
  214. ),
  215. 'counter' => $this->getCounter($aParams)
  216. );
  217. //--- Legend
  218. $bTmplVarsLegend = $bShowLegend;
  219. $aTmplVarsLegend = array();
  220. if($bTmplVarsLegend)
  221. $aTmplVarsLegend = array(
  222. 'legend' => $this->getLegend($aParams)
  223. );
  224. if(!$bTmplVarsDoVote && !$bTmplVarsCounter && !$bTmplVarsLegend)
  225. return '';
  226. $sTmplName = $this->{'_sTmplNameElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT)};
  227. return $this->_oTemplate->parseHtmlByName($sTmplName, array(
  228. 'style_prefix' => $this->_sStylePrefix,
  229. 'html_id' => $this->_aHtmlIds['main_' . $this->_sType],
  230. 'class' => $this->_sStylePrefix . '-' . $this->_sType . ($bShowDoVoteAsButton ? '-button' : '') . ($bShowDoVoteAsButtonSmall ? '-button-small' : ''),
  231. 'rate' => $aVote['rate'],
  232. 'count' => $aVote['count'],
  233. 'bx_if:show_do_vote' => array(
  234. 'condition' => $bTmplVarsDoVote,
  235. 'content' => $aTmplVarsDoVote
  236. ),
  237. 'bx_if:show_counter' => array(
  238. 'condition' => $bTmplVarsCounter,
  239. 'content' => $aTmplVarsCounter
  240. ),
  241. 'bx_if:show_legend' => array(
  242. 'condition' => $bTmplVarsLegend,
  243. 'content' => $aTmplVarsLegend
  244. ),
  245. 'script' => $this->getJsScript($bDynamicMode)
  246. ));
  247. }
  248. protected function _getDoVoteStars($aParams = array(), $isAllowedVote = true)
  249. {
  250. $sJsObject = $this->getJsObjectName();
  251. $iMinValue = $this->getMinValue();
  252. $iMaxValue = $this->getMaxValue();
  253. $aTmplVarsStars = $aTmplVarsLegend = $aTmplVarsSlider = $aTmplVarsButtons = array();
  254. for($i = $iMinValue; $i <= $iMaxValue; $i++) {
  255. $aTmplVarsStars[] = array(
  256. 'style_prefix' => $this->_sStylePrefix,
  257. 'value' => $i
  258. );
  259. $aTmplVarsLegend[] = array(
  260. 'style_prefix' => $this->_sStylePrefix,
  261. 'value' => $i
  262. );
  263. $aTmplVarsSlider[] = array(
  264. 'style_prefix' => $this->_sStylePrefix
  265. );
  266. if($isAllowedVote)
  267. $aTmplVarsButtons[] = array(
  268. 'style_prefix' => $this->_sStylePrefix,
  269. 'js_object' => $sJsObject,
  270. 'value' => $i
  271. );
  272. }
  273. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoVoteStars, array(
  274. 'style_prefix' => $this->_sStylePrefix,
  275. 'bx_repeat:stars' => $aTmplVarsStars,
  276. 'bx_if:show_legend' => array(
  277. 'condition' => isset($aParams['show_do_vote_legend']) && $aParams['show_do_vote_legend'] === true,
  278. 'content' => array(
  279. 'style_prefix' => $this->_sStylePrefix,
  280. 'bx_repeat:legend' => $aTmplVarsLegend,
  281. )
  282. ),
  283. 'bx_repeat:slider' => $aTmplVarsSlider,
  284. 'bx_repeat:buttons' => $aTmplVarsButtons,
  285. ));
  286. }
  287. protected function _getDoVoteLikes($aParams = array(), $isAllowedVote = true)
  288. {
  289. $bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
  290. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  291. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  292. $bDisabled = !$isAllowedVote || ($bVoted && !$this->isUndo());
  293. $sClass = '';
  294. if($bShowDoVoteAsButton)
  295. $sClass = 'bx-btn';
  296. else if ($bShowDoVoteAsButtonSmall)
  297. $sClass = 'bx-btn bx-btn-small';
  298. if($bDisabled)
  299. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-vote-disabled';
  300. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoVoteLikes, array(
  301. 'style_prefix' => $this->_sStylePrefix,
  302. 'class' => $sClass,
  303. 'title' => bx_html_attribute(_t($this->_getTitleDoLike($bVoted))),
  304. 'bx_if:show_onclick' => array(
  305. 'condition' => !$bDisabled,
  306. 'content' => array(
  307. 'onclick' => $this->getJsClick()
  308. )
  309. ),
  310. 'do_vote' => $this->_getLabelDoLike($aParams),
  311. ));
  312. }
  313. protected function _getLabelCounter($iCount)
  314. {
  315. return _t('_vote_counter', $iCount);
  316. }
  317. protected function _getLabelDoLike($aParams = array())
  318. {
  319. $bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
  320. return $this->_oTemplate->parseHtmlByName('vote_do_vote_likes_label.html', array(
  321. 'bx_if:show_icon' => array(
  322. 'condition' => isset($aParams['show_do_vote_icon']) && $aParams['show_do_vote_icon'] == true,
  323. 'content' => array(
  324. 'name' => $this->_getIconDoLike($bVoted)
  325. )
  326. ),
  327. 'bx_if:show_text' => array(
  328. 'condition' => isset($aParams['show_do_vote_label']) && $aParams['show_do_vote_label'] == true,
  329. 'content' => array(
  330. 'text' => _t($this->_getTitleDoLike($bVoted))
  331. )
  332. )
  333. ));
  334. }
  335. protected function _getVotedBy()
  336. {
  337. $aTmplUsers = array();
  338. $aUserIds = $this->_oQuery->getPerformedBy($this->getId());
  339. foreach($aUserIds as $iUserId) {
  340. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  341. $aTmplUsers[] = array(
  342. 'style_prefix' => $this->_sStylePrefix,
  343. 'user_unit' => $sUserUnit
  344. );
  345. }
  346. if(empty($aTmplUsers))
  347. $aTmplUsers = MsgBox(_t('_Empty'));
  348. return $this->_oTemplate->parseHtmlByName('vote_by_list.html', array(
  349. 'style_prefix' => $this->_sStylePrefix,
  350. 'bx_repeat:list' => $aTmplUsers
  351. ));
  352. }
  353. }
  354. /** @} */