BxBaseVote.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolVote');
  10. bx_import('BxDolTemplate');
  11. /**
  12. * @see BxDolVote
  13. */
  14. class BxBaseVote extends BxDolVote
  15. {
  16. protected $_sJsObjName;
  17. protected $_sStylePrefix;
  18. protected $_aHtmlIds;
  19. protected $_aElementDefaults;
  20. protected $_sTmplNameCounter;
  21. protected $_sTmplNameDoVoteLikes;
  22. protected $_sTmplNameDoVoteStars;
  23. public function __construct($sSystem, $iId, $iInit = 1)
  24. {
  25. parent::__construct($sSystem, $iId, $iInit);
  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()
  53. {
  54. $oTemplate = BxDolTemplate::getInstance();
  55. $oTemplate->addJs('BxDolVote.js');
  56. $oTemplate->addCss('vote.css');
  57. }
  58. public function getJsObjectName()
  59. {
  60. return $this->_sJsObjName;
  61. }
  62. public function getJsScript()
  63. {
  64. $aParams = array(
  65. 'sObjName' => $this->_sJsObjName,
  66. 'sSystem' => $this->getSystemName(),
  67. 'iAuthorId' => $this->_getAuthorId(),
  68. 'iObjId' => $this->getId(),
  69. 'iLikeMode' => $this->isLikeMode() ? 1 : 0,
  70. 'sRootUrl' => BX_DOL_URL_ROOT,
  71. 'sStylePrefix' => $this->_sStylePrefix,
  72. 'aHtmlIds' => $this->_aHtmlIds
  73. );
  74. $this->addCssJs();
  75. return BxDolTemplate::getInstance()->_wrapInTagJsCode("var " . $this->_sJsObjName . " = new BxDolVote(" . json_encode($aParams) . ");");
  76. }
  77. public function getJsClick()
  78. {
  79. if(!$this->isLikeMode())
  80. return false;
  81. return $this->getJsObjectName() . '.vote(this, ' . $this->getMaxValue() . ')';
  82. }
  83. public function getCounter($aParams = array())
  84. {
  85. $sJsObject = $this->getJsObjectName();
  86. $aVote = $this->_oQuery->getVote($this->getId());
  87. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameCounter, array(
  88. 'href' => 'javascript:void(0)',
  89. 'title' => _t('_vote_do_like_by'),
  90. 'bx_repeat:attrs' => array(
  91. array('key' => 'id', 'value' => $this->_aHtmlIds['counter']),
  92. array('key' => 'class', 'value' => $this->_sStylePrefix . '-counter'),
  93. array('key' => 'onclick', 'value' => 'javascript:' . $sJsObject . '.toggleByPopup(this)')
  94. ),
  95. 'content' => (int)$aVote['count'] > 0 ? $this->_getLabelCounter($aVote['count']) : ''
  96. ));
  97. }
  98. public function getElementBlock($aParams = array())
  99. {
  100. $aParams['usage'] = BX_DOL_VOTE_USAGE_BLOCK;
  101. return $this->getElement($aParams);
  102. }
  103. public function getElementInline($aParams = array())
  104. {
  105. $aParams['usage'] = BX_DOL_VOTE_USAGE_INLINE;
  106. return $this->getElement($aParams);
  107. }
  108. public function getElement($aParams = array())
  109. {
  110. if(!$this->isAllowedVote())
  111. return '';
  112. $bLike = $this->isLikeMode();
  113. $sType = $bLike ? BX_DOL_VOTE_TYPE_LIKES : BX_DOL_VOTE_TYPE_STARS;
  114. $bShowDoVoteAsButtonSmall = $bLike && isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  115. $bShowDoVoteAsButton = $bLike && !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  116. $sMethodDoVote = '_getDoVote' . ucfirst($sType);
  117. if(!method_exists($this, $sMethodDoVote))
  118. return '';
  119. $iObjectId = $this->getId();
  120. $iAuthorId = $this->_getAuthorId();
  121. $aVote = $this->_oQuery->getVote($iObjectId);
  122. $aParams = array_merge($this->_aElementDefaults[$sType], $aParams);
  123. $aParams['is_voted'] = $this->isUndo() && $this->_oQuery->isVoted($iObjectId, $iAuthorId) ? true : false;
  124. $sTmplName = 'vote_element_' . (!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT) . '.html';
  125. return BxDolTemplate::getInstance()->parseHtmlByName($sTmplName, array(
  126. 'style_prefix' => $this->_sStylePrefix,
  127. 'html_id' => $this->_aHtmlIds['main_' . $sType],
  128. 'class' => $this->_sStylePrefix . '-' . $sType . ($bShowDoVoteAsButton ? '-button' : '') . ($bShowDoVoteAsButtonSmall ? '-button-small' : ''),
  129. 'rate' => $aVote['rate'],
  130. 'count' => $aVote['count'],
  131. 'do_vote' => $this->$sMethodDoVote($aParams),
  132. 'bx_if:show_counter' => array(
  133. 'condition' => isset($aParams['show_counter']) && $aParams['show_counter'] === true,
  134. 'content' => array(
  135. 'style_prefix' => $this->_sStylePrefix,
  136. 'bx_if:show_hidden' => array(
  137. 'condition' => (int)$aVote['count'] == 0,
  138. 'content' => array()
  139. ),
  140. 'counter' => $this->getCounter()
  141. )
  142. ),
  143. 'script' => $this->getJsScript()
  144. ));
  145. }
  146. protected function _getDoVoteStars($aParams = array())
  147. {
  148. $sJsObject = $this->getJsObjectName();
  149. $iMinValue = $this->getMinValue();
  150. $iMaxValue = $this->getMaxValue();
  151. $aTmplVarsButtons = $aTmplVarsStars = array();
  152. for($i = $iMinValue; $i <= $iMaxValue; $i++) {
  153. $aTmplVarsStars[] = array(
  154. 'style_prefix' => $this->_sStylePrefix,
  155. 'value' => $i
  156. );
  157. $aTmplVarsLegend[] = array(
  158. 'style_prefix' => $this->_sStylePrefix,
  159. 'value' => $i
  160. );
  161. $aTmplVarsSlider[] = array(
  162. 'style_prefix' => $this->_sStylePrefix
  163. );
  164. $aTmplVarsButtons[] = array(
  165. 'style_prefix' => $this->_sStylePrefix,
  166. 'js_object' => $sJsObject,
  167. 'value' => $i
  168. );
  169. }
  170. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameDoVoteStars, array(
  171. 'style_prefix' => $this->_sStylePrefix,
  172. 'bx_repeat:stars' => $aTmplVarsStars,
  173. 'bx_if:show_legend' => array(
  174. 'condition' => isset($aParams['show_do_vote_legend']) && $aParams['show_do_vote_legend'] === true,
  175. 'content' => array(
  176. 'style_prefix' => $this->_sStylePrefix,
  177. 'bx_repeat:legend' => $aTmplVarsLegend,
  178. )
  179. ),
  180. 'bx_repeat:slider' => $aTmplVarsSlider,
  181. 'bx_repeat:buttons' => $aTmplVarsButtons,
  182. ));
  183. }
  184. protected function _getDoVoteLikes($aParams = array())
  185. {
  186. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  187. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  188. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameDoVoteLikes, array(
  189. 'style_prefix' => $this->_sStylePrefix,
  190. 'js_object' => $this->getJsObjectName(),
  191. 'class' => ($bShowDoVoteAsButton ? 'bx-btn' : '') . ($bShowDoVoteAsButtonSmall ? 'bx-btn bx-btn-small' : ''),
  192. 'title' => _t('_vote_do_like'),
  193. 'value' => $this->getMinValue(),
  194. 'do_vote' => $this->_getLabelDoLike($aParams),
  195. ));
  196. }
  197. protected function _getLabelCounter($iCount)
  198. {
  199. return $iCount;
  200. }
  201. protected function _getLabelDoLike($aParams = array())
  202. {
  203. $bVoted = isset($aParams['is_voted']) && $aParams['is_voted'] === true;
  204. $oTemplate = BxDolTemplate::getInstance();
  205. $sResult = '';
  206. if(isset($aParams['show_do_vote_icon']) && $aParams['show_do_vote_icon'] == true)
  207. $sResult .= $oTemplate->parseHtmlByName('bx_icon.html', array('name' => $this->_getIconDoLike($bVoted)));
  208. if(isset($aParams['show_do_vote_label']) && $aParams['show_do_vote_label'] == true)
  209. $sResult .= ($sResult != '' ? ' ' : '') . $oTemplate->parseHtmlByName('bx_span.html', array(
  210. 'bx_repeat:attrs' => array(),
  211. 'content' => _t($this->_getTitleDoLike($bVoted))
  212. ));
  213. return $sResult;
  214. }
  215. protected function _getVotedBy()
  216. {
  217. $aTmplUsers = array();
  218. $aUserIds = $this->_oQuery->getVotedBy($this->getId());
  219. foreach($aUserIds as $iUserId) {
  220. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  221. $aTmplUsers[] = array(
  222. 'style_prefix' => $this->_sStylePrefix,
  223. 'user_unit' => $sUserUnit
  224. );
  225. }
  226. if(empty($aTmplUsers))
  227. $aTmplUsers = MsgBox(_t('_Empty'));
  228. return BxDolTemplate::getInstance()->parseHtmlByName('vote_by_list.html', array(
  229. 'style_prefix' => $this->_sStylePrefix,
  230. 'bx_repeat:list' => $aTmplUsers
  231. ));
  232. }
  233. protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false)
  234. {
  235. header('Content-type: text/html; charset=utf-8');
  236. $s = json_encode($a);
  237. if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
  238. $s = '<textarea>' . $s . '</textarea>'; // http://jquery.malsup.com/form/#file-upload
  239. echo $s;
  240. }
  241. }
  242. /** @} */