BxBaseVote.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 $_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. $bShowDoVoteAsButtonSmall = $this->_bLike && isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  104. $bShowDoVoteAsButton = $this->_bLike && !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  105. $aVote = $this->_oQuery->getVote($this->getId());
  106. $sClass = $this->_sStylePrefix . '-counter';
  107. if($bShowDoVoteAsButtonSmall)
  108. $sClass .= ' bx-btn-small-height';
  109. if($bShowDoVoteAsButton)
  110. $sClass .= ' bx-btn-height';
  111. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameCounter, array(
  112. 'href' => 'javascript:void(0)',
  113. 'title' => _t('_vote_do_like_by'),
  114. 'bx_repeat:attrs' => array(
  115. array('key' => 'id', 'value' => $this->_aHtmlIds['counter']),
  116. array('key' => 'class', 'value' => $sClass),
  117. array('key' => 'onclick', 'value' => 'javascript:' . $sJsObject . '.toggleByPopup(this)')
  118. ),
  119. 'content' => (int)$aVote['count'] > 0 ? $this->_getLabelCounter($aVote['count']) : ''
  120. ));
  121. }
  122. /**
  123. * Note. The Legend is available in Start based mode only.
  124. */
  125. public function getLegend($aParams = array())
  126. {
  127. $sJsObject = $this->getJsObjectName();
  128. $iMinValue = $this->getMinValue();
  129. $iMaxValue = $this->getMaxValue();
  130. $aLegend = $this->_oQuery->getLegend($this->_iId);
  131. $aTmplVarsItems = array();
  132. for($i = $iMaxValue; $i >= $iMinValue; $i--) {
  133. $aTmplVarsStars = $aTmplVarsSlider = array();
  134. for($j = $iMinValue; $j <= $iMaxValue; $j++) {
  135. $aTmplVarsStars[] = array(
  136. 'style_prefix' => $this->_sStylePrefix,
  137. 'value' => $i
  138. );
  139. $aTmplVarsSlider[] = array(
  140. 'style_prefix' => $this->_sStylePrefix
  141. );
  142. }
  143. $aTmplVarsItems[] = array(
  144. 'style_prefix' => $this->_sStylePrefix,
  145. 'value' => $i,
  146. 'bx_repeat:stars' => $aTmplVarsStars,
  147. 'bx_repeat:slider' => $aTmplVarsSlider,
  148. 'label' => isset($aLegend[$i]['count']) ? (int)$aLegend[$i]['count'] : 0
  149. );
  150. }
  151. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameLegend, array(
  152. 'style_prefix' => $this->_sStylePrefix,
  153. 'html_id' => $this->_aHtmlIds['legend_stars'],
  154. 'type' => $this->_sType,
  155. 'bx_repeat:items' => $aTmplVarsItems
  156. ));
  157. }
  158. public function getElementBlock($aParams = array())
  159. {
  160. $aParams['usage'] = BX_DOL_VOTE_USAGE_BLOCK;
  161. return $this->getElement($aParams);
  162. }
  163. public function getElementInline($aParams = array())
  164. {
  165. $aParams['usage'] = BX_DOL_VOTE_USAGE_INLINE;
  166. return $this->getElement($aParams);
  167. }
  168. public function getElement($aParams = array())
  169. {
  170. $aParams = array_merge($this->_aElementDefaults[$this->_sType], $aParams);
  171. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  172. $bShowDoVote = !isset($aParams['show_do_vote']) || $aParams['show_do_vote'] == true;
  173. $bShowDoVoteAsButtonSmall = $this->_bLike && isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  174. $bShowDoVoteAsButton = $this->_bLike && !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  175. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true;
  176. $bShowLegend = !$this->_bLike && isset($aParams['show_legend']) && $aParams['show_legend'] === true;
  177. $sMethodDoVote = '_getDoVote' . ucfirst($this->_sType);
  178. if(!method_exists($this, $sMethodDoVote))
  179. return '';
  180. $iObjectId = $this->getId();
  181. $iAuthorId = $this->_getAuthorId();
  182. $aVote = $this->_oQuery->getVote($iObjectId);
  183. $bCount = (int)$aVote['count'] != 0;
  184. $isAllowedVote = $this->isAllowedVote();
  185. $aParams['is_voted'] = $this->_oQuery->isPerformed($iObjectId, $iAuthorId) ? true : false;
  186. //--- Do Vote
  187. $bTmplVarsDoVote = $bShowDoVote && (!$this->_bLike || $bCount || $isAllowedVote);
  188. $aTmplVarsDoVote = array();
  189. if($bTmplVarsDoVote)
  190. $aTmplVarsDoVote = array(
  191. 'style_prefix' => $this->_sStylePrefix,
  192. 'do_vote' => $this->$sMethodDoVote($aParams, $isAllowedVote),
  193. );
  194. //--- Counter
  195. $bTmplVarsCounter = $bShowCounter && ($bCount || $isAllowedVote);
  196. $aTmplVarsCounter = array();
  197. if($bTmplVarsCounter)
  198. $aTmplVarsCounter = array(
  199. 'style_prefix' => $this->_sStylePrefix,
  200. 'bx_if:show_hidden' => array(
  201. 'condition' => !$bCount,
  202. 'content' => array()
  203. ),
  204. 'counter' => $this->getCounter($aParams)
  205. );
  206. //--- Legend
  207. $bTmplVarsLegend = $bShowLegend;
  208. $aTmplVarsLegend = array();
  209. if($bTmplVarsLegend)
  210. $aTmplVarsLegend = array(
  211. 'legend' => $this->getLegend($aParams)
  212. );
  213. if(!$bTmplVarsDoVote && !$bTmplVarsCounter && !$bTmplVarsLegend)
  214. return '';
  215. $sTmplName = 'vote_element_' . (!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT) . '.html';
  216. return $this->_oTemplate->parseHtmlByName($sTmplName, array(
  217. 'style_prefix' => $this->_sStylePrefix,
  218. 'html_id' => $this->_aHtmlIds['main_' . $this->_sType],
  219. 'class' => $this->_sStylePrefix . '-' . $this->_sType . ($bShowDoVoteAsButton ? '-button' : '') . ($bShowDoVoteAsButtonSmall ? '-button-small' : ''),
  220. 'rate' => $aVote['rate'],
  221. 'count' => $aVote['count'],
  222. 'bx_if:show_do_vote' => array(
  223. 'condition' => $bTmplVarsDoVote,
  224. 'content' => $aTmplVarsDoVote
  225. ),
  226. 'bx_if:show_counter' => array(
  227. 'condition' => $bTmplVarsCounter,
  228. 'content' => $aTmplVarsCounter
  229. ),
  230. 'bx_if:show_legend' => array(
  231. 'condition' => $bTmplVarsLegend,
  232. 'content' => $aTmplVarsLegend
  233. ),
  234. 'script' => $this->getJsScript($bDynamicMode)
  235. ));
  236. }
  237. protected function _getDoVoteStars($aParams = array(), $isAllowedVote = true)
  238. {
  239. $sJsObject = $this->getJsObjectName();
  240. $iMinValue = $this->getMinValue();
  241. $iMaxValue = $this->getMaxValue();
  242. $aTmplVarsStars = $aTmplVarsLegend = $aTmplVarsSlider = $aTmplVarsButtons = array();
  243. for($i = $iMinValue; $i <= $iMaxValue; $i++) {
  244. $aTmplVarsStars[] = array(
  245. 'style_prefix' => $this->_sStylePrefix,
  246. 'value' => $i
  247. );
  248. $aTmplVarsLegend[] = array(
  249. 'style_prefix' => $this->_sStylePrefix,
  250. 'value' => $i
  251. );
  252. $aTmplVarsSlider[] = array(
  253. 'style_prefix' => $this->_sStylePrefix
  254. );
  255. if($isAllowedVote)
  256. $aTmplVarsButtons[] = array(
  257. 'style_prefix' => $this->_sStylePrefix,
  258. 'js_object' => $sJsObject,
  259. 'value' => $i
  260. );
  261. }
  262. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoVoteStars, array(
  263. 'style_prefix' => $this->_sStylePrefix,
  264. 'bx_repeat:stars' => $aTmplVarsStars,
  265. 'bx_if:show_legend' => array(
  266. 'condition' => isset($aParams['show_do_vote_legend']) && $aParams['show_do_vote_legend'] === true,
  267. 'content' => array(
  268. 'style_prefix' => $this->_sStylePrefix,
  269. 'bx_repeat:legend' => $aTmplVarsLegend,
  270. )
  271. ),
  272. 'bx_repeat:slider' => $aTmplVarsSlider,
  273. 'bx_repeat:buttons' => $aTmplVarsButtons,
  274. ));
  275. }
  276. protected function _getDoVoteLikes($aParams = array(), $isAllowedVote = true)
  277. {
  278. $bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
  279. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  280. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  281. $bDisabled = !$isAllowedVote || ($bVoted && !$this->isUndo());
  282. $sClass = '';
  283. if($bShowDoVoteAsButton)
  284. $sClass = 'bx-btn';
  285. else if ($bShowDoVoteAsButtonSmall)
  286. $sClass = 'bx-btn bx-btn-small';
  287. if($bDisabled)
  288. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-vote-disabled';
  289. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoVoteLikes, array(
  290. 'style_prefix' => $this->_sStylePrefix,
  291. 'class' => $sClass,
  292. 'title' => _t($bVoted && $this->isUndo() ? '_vote_do_unlike' : '_vote_do_like'),
  293. 'bx_if:show_onclick' => array(
  294. 'condition' => !$bDisabled,
  295. 'content' => array(
  296. 'js_object' => $this->getJsObjectName(),
  297. 'value' => $this->getMinValue(),
  298. )
  299. ),
  300. 'do_vote' => $this->_getLabelDoLike($aParams),
  301. ));
  302. }
  303. protected function _getLabelCounter($iCount)
  304. {
  305. return _t('_vote_counter', $iCount);
  306. }
  307. protected function _getLabelDoLike($aParams = array())
  308. {
  309. $bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
  310. return $this->_oTemplate->parseHtmlByName('vote_do_vote_likes_label.html', array(
  311. 'bx_if:show_icon' => array(
  312. 'condition' => isset($aParams['show_do_vote_icon']) && $aParams['show_do_vote_icon'] == true,
  313. 'content' => array(
  314. 'name' => $this->_getIconDoLike($bVoted)
  315. )
  316. ),
  317. 'bx_if:show_text' => array(
  318. 'condition' => isset($aParams['show_do_vote_label']) && $aParams['show_do_vote_label'] == true,
  319. 'content' => array(
  320. 'text' => _t($this->_getTitleDoLike($bVoted))
  321. )
  322. )
  323. ));
  324. }
  325. protected function _getVotedBy()
  326. {
  327. $aTmplUsers = array();
  328. $aUserIds = $this->_oQuery->getPerformedBy($this->getId());
  329. foreach($aUserIds as $iUserId) {
  330. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  331. $aTmplUsers[] = array(
  332. 'style_prefix' => $this->_sStylePrefix,
  333. 'user_unit' => $sUserUnit
  334. );
  335. }
  336. if(empty($aTmplUsers))
  337. $aTmplUsers = MsgBox(_t('_Empty'));
  338. return $this->_oTemplate->parseHtmlByName('vote_by_list.html', array(
  339. 'style_prefix' => $this->_sStylePrefix,
  340. 'bx_repeat:list' => $aTmplUsers
  341. ));
  342. }
  343. }
  344. /** @} */