BxBaseVote.php 15 KB

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