BxBaseVote.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 $_sTmplNameDoVoteLikes;
  21. protected $_sTmplNameDoVoteStars;
  22. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  23. {
  24. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  25. $this->_bCssJsAdded = false;
  26. $this->_sJsObjName = 'oVote' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  27. $this->_sStylePrefix = 'bx-vote';
  28. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  29. $this->_aHtmlIds = array(
  30. 'main_stars' => 'bx-vote-stars-' . $sHtmlId,
  31. 'main_likes' => 'bx-vote-likes-' . $sHtmlId,
  32. 'counter' => 'bx-vote-counter-' . $sHtmlId,
  33. 'by_popup' => 'bx-vote-by-popup-' . $sHtmlId
  34. );
  35. $this->_aElementDefaults = array(
  36. 'stars' => array(
  37. 'show_do_vote_legend' => false,
  38. 'show_counter' => true
  39. ),
  40. 'likes' => array(
  41. 'show_do_vote_as_button' => false,
  42. 'show_do_vote_as_button_small' => false,
  43. 'show_do_vote_icon' => true,
  44. 'show_do_vote_label' => false,
  45. 'show_counter' => true
  46. )
  47. );
  48. $this->_sTmplNameCounter = 'vote_counter.html';
  49. $this->_sTmplNameDoVoteLikes = 'vote_do_vote_likes.html';
  50. $this->_sTmplNameDoVoteStars = 'vote_do_vote_stars.html';
  51. }
  52. public function addCssJs($bDynamicMode = false)
  53. {
  54. if($bDynamicMode || $this->_bCssJsAdded)
  55. return;
  56. $this->_oTemplate->addJs(array('BxDolVote.js'));
  57. $this->_oTemplate->addCss(array('vote.css'));
  58. $this->_bCssJsAdded = true;
  59. }
  60. public function getJsObjectName()
  61. {
  62. return $this->_sJsObjName;
  63. }
  64. public function getJsScript($bDynamicMode = false)
  65. {
  66. $aParams = array(
  67. 'sObjName' => $this->_sJsObjName,
  68. 'sSystem' => $this->getSystemName(),
  69. 'iAuthorId' => $this->_getAuthorId(),
  70. 'iObjId' => $this->getId(),
  71. 'iLikeMode' => $this->isLikeMode() ? 1 : 0,
  72. 'sRootUrl' => BX_DOL_URL_ROOT,
  73. 'sStylePrefix' => $this->_sStylePrefix,
  74. 'aHtmlIds' => $this->_aHtmlIds
  75. );
  76. $sCode = $this->_sJsObjName . " = new BxDolVote(" . json_encode($aParams) . ");";
  77. if($bDynamicMode) {
  78. $sCode = "var " . $this->_sJsObjName . " = null;
  79. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('BxDolVote.js'), BX_ESCAPE_STR_APOS) . "', function(data, textStatus, jqxhr) {
  80. bx_get_style('" . bx_js_string($this->_oTemplate->getCssUrl('vote.css'), BX_ESCAPE_STR_APOS) . "');
  81. " . $sCode . "
  82. }); ";
  83. }
  84. else
  85. $sCode = "var " . $sCode;
  86. $this->addCssJs($bDynamicMode);
  87. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  88. }
  89. public function getJsClick()
  90. {
  91. if(!$this->isLikeMode())
  92. return false;
  93. return $this->getJsObjectName() . '.vote(this, ' . $this->getMaxValue() . ')';
  94. }
  95. public function getCounter($aParams = array())
  96. {
  97. $sJsObject = $this->getJsObjectName();
  98. $aVote = $this->_oQuery->getVote($this->getId());
  99. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameCounter, array(
  100. 'href' => 'javascript:void(0)',
  101. 'title' => _t('_vote_do_like_by'),
  102. 'bx_repeat:attrs' => array(
  103. array('key' => 'id', 'value' => $this->_aHtmlIds['counter']),
  104. array('key' => 'class', 'value' => $this->_sStylePrefix . '-counter'),
  105. array('key' => 'onclick', 'value' => 'javascript:' . $sJsObject . '.toggleByPopup(this)')
  106. ),
  107. 'content' => (int)$aVote['count'] > 0 ? $this->_getLabelCounter($aVote['count']) : ''
  108. ));
  109. }
  110. public function getElementBlock($aParams = array())
  111. {
  112. $aParams['usage'] = BX_DOL_VOTE_USAGE_BLOCK;
  113. return $this->getElement($aParams);
  114. }
  115. public function getElementInline($aParams = array())
  116. {
  117. $aParams['usage'] = BX_DOL_VOTE_USAGE_INLINE;
  118. return $this->getElement($aParams);
  119. }
  120. public function getElement($aParams = array())
  121. {
  122. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  123. $bLike = $this->isLikeMode();
  124. $sType = $bLike ? BX_DOL_VOTE_TYPE_LIKES : BX_DOL_VOTE_TYPE_STARS;
  125. $bShowDoVoteAsButtonSmall = $bLike && isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  126. $bShowDoVoteAsButton = $bLike && !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  127. $sMethodDoVote = '_getDoVote' . ucfirst($sType);
  128. if(!method_exists($this, $sMethodDoVote))
  129. return '';
  130. $iObjectId = $this->getId();
  131. $iAuthorId = $this->_getAuthorId();
  132. $aVote = $this->_oQuery->getVote($iObjectId);
  133. if(!$this->isAllowedVote() && (int)$aVote['count'] == 0)
  134. return '';
  135. $aParams = array_merge($this->_aElementDefaults[$sType], $aParams);
  136. $aParams['is_voted'] = $this->_oQuery->isPerformed($iObjectId, $iAuthorId) ? true : false;
  137. $sTmplName = 'vote_element_' . (!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT) . '.html';
  138. return $this->_oTemplate->parseHtmlByName($sTmplName, array(
  139. 'style_prefix' => $this->_sStylePrefix,
  140. 'html_id' => $this->_aHtmlIds['main_' . $sType],
  141. 'class' => $this->_sStylePrefix . '-' . $sType . ($bShowDoVoteAsButton ? '-button' : '') . ($bShowDoVoteAsButtonSmall ? '-button-small' : ''),
  142. 'rate' => $aVote['rate'],
  143. 'count' => $aVote['count'],
  144. 'do_vote' => $this->$sMethodDoVote($aParams),
  145. 'bx_if:show_counter' => array(
  146. 'condition' => isset($aParams['show_counter']) && $aParams['show_counter'] === true,
  147. 'content' => array(
  148. 'style_prefix' => $this->_sStylePrefix,
  149. 'bx_if:show_hidden' => array(
  150. 'condition' => (int)$aVote['count'] == 0,
  151. 'content' => array()
  152. ),
  153. 'counter' => $this->getCounter()
  154. )
  155. ),
  156. 'script' => $this->getJsScript($bDynamicMode)
  157. ));
  158. }
  159. protected function _getDoVoteStars($aParams = array())
  160. {
  161. //TODO: Add 'disabled' appearance for this type of votes.
  162. if(!$this->isAllowedVote())
  163. return '';
  164. $sJsObject = $this->getJsObjectName();
  165. $iMinValue = $this->getMinValue();
  166. $iMaxValue = $this->getMaxValue();
  167. $aTmplVarsButtons = $aTmplVarsStars = array();
  168. for($i = $iMinValue; $i <= $iMaxValue; $i++) {
  169. $aTmplVarsStars[] = array(
  170. 'style_prefix' => $this->_sStylePrefix,
  171. 'value' => $i
  172. );
  173. $aTmplVarsLegend[] = array(
  174. 'style_prefix' => $this->_sStylePrefix,
  175. 'value' => $i
  176. );
  177. $aTmplVarsSlider[] = array(
  178. 'style_prefix' => $this->_sStylePrefix
  179. );
  180. $aTmplVarsButtons[] = array(
  181. 'style_prefix' => $this->_sStylePrefix,
  182. 'js_object' => $sJsObject,
  183. 'value' => $i
  184. );
  185. }
  186. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoVoteStars, array(
  187. 'style_prefix' => $this->_sStylePrefix,
  188. 'bx_repeat:stars' => $aTmplVarsStars,
  189. 'bx_if:show_legend' => array(
  190. 'condition' => isset($aParams['show_do_vote_legend']) && $aParams['show_do_vote_legend'] === true,
  191. 'content' => array(
  192. 'style_prefix' => $this->_sStylePrefix,
  193. 'bx_repeat:legend' => $aTmplVarsLegend,
  194. )
  195. ),
  196. 'bx_repeat:slider' => $aTmplVarsSlider,
  197. 'bx_repeat:buttons' => $aTmplVarsButtons,
  198. ));
  199. }
  200. protected function _getDoVoteLikes($aParams = array())
  201. {
  202. $bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
  203. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  204. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  205. $bDisabled = !$this->isAllowedVote() || ($bVoted && !$this->isUndo());
  206. $sClass = '';
  207. if($bShowDoVoteAsButton)
  208. $sClass = 'bx-btn';
  209. else if ($bShowDoVoteAsButtonSmall)
  210. $sClass = 'bx-btn bx-btn-small';
  211. if($bDisabled)
  212. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-vote-disabled';
  213. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoVoteLikes, array(
  214. 'style_prefix' => $this->_sStylePrefix,
  215. 'class' => $sClass,
  216. 'title' => _t($bVoted && $this->isUndo() ? '_vote_do_unlike' : '_vote_do_like'),
  217. 'bx_if:show_onclick' => array(
  218. 'condition' => !$bDisabled,
  219. 'content' => array(
  220. 'js_object' => $this->getJsObjectName(),
  221. 'value' => $this->getMinValue(),
  222. )
  223. ),
  224. 'do_vote' => $this->_getLabelDoLike($aParams),
  225. ));
  226. }
  227. protected function _getLabelCounter($iCount)
  228. {
  229. return _t('_vote_counter', $iCount);
  230. }
  231. protected function _getLabelDoLike($aParams = array())
  232. {
  233. $bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
  234. return $this->_oTemplate->parseHtmlByName('vote_do_vote_likes_label.html', array(
  235. 'bx_if:show_icon' => array(
  236. 'condition' => isset($aParams['show_do_vote_icon']) && $aParams['show_do_vote_icon'] == true,
  237. 'content' => array(
  238. 'name' => $this->_getIconDoLike($bVoted)
  239. )
  240. ),
  241. 'bx_if:show_text' => array(
  242. 'condition' => isset($aParams['show_do_vote_label']) && $aParams['show_do_vote_label'] == true,
  243. 'content' => array(
  244. 'text' => _t($this->_getTitleDoLike($bVoted))
  245. )
  246. )
  247. ));
  248. }
  249. protected function _getVotedBy()
  250. {
  251. $aTmplUsers = array();
  252. $aUserIds = $this->_oQuery->getPerformedBy($this->getId());
  253. foreach($aUserIds as $iUserId) {
  254. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  255. $aTmplUsers[] = array(
  256. 'style_prefix' => $this->_sStylePrefix,
  257. 'user_unit' => $sUserUnit
  258. );
  259. }
  260. if(empty($aTmplUsers))
  261. $aTmplUsers = MsgBox(_t('_Empty'));
  262. return $this->_oTemplate->parseHtmlByName('vote_by_list.html', array(
  263. 'style_prefix' => $this->_sStylePrefix,
  264. 'bx_repeat:list' => $aTmplUsers
  265. ));
  266. }
  267. protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false)
  268. {
  269. header('Content-type: text/html; charset=utf-8');
  270. $s = json_encode($a);
  271. if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
  272. $s = '<textarea>' . $s . '</textarea>'; // http://jquery.malsup.com/form/#file-upload
  273. echo $s;
  274. }
  275. }
  276. /** @} */