BxBaseVote.php 14 KB

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