1
0

BxBaseVote.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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' . str_replace(' ', '', ucwords(str_replace(array('_' , '-'), array(' ', ' '), $sSystem))) . $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. $sJsObject = $this->getJsObjectName();
  85. $aVote = $this->_oQuery->getVote($this->getId());
  86. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameCounter, array(
  87. 'href' => 'javascript:void(0)',
  88. 'title' => _t('_vote_do_like_by'),
  89. 'bx_repeat:attrs' => array(
  90. array('key' => 'id', 'value' => $this->_aHtmlIds['counter']),
  91. array('key' => 'class', 'value' => $this->_sStylePrefix . '-counter'),
  92. array('key' => 'onclick', 'value' => 'javascript:' . $sJsObject . '.toggleByPopup(this)')
  93. ),
  94. 'content' => (int)$aVote['count'] > 0 ? $this->_getLabelCounter($aVote['count']) : ''
  95. ));
  96. }
  97. public function getElementBlock($aParams = array())
  98. {
  99. $aParams['usage'] = BX_DOL_VOTE_USAGE_BLOCK;
  100. return $this->getElement($aParams);
  101. }
  102. public function getElementInline($aParams = array())
  103. {
  104. $aParams['usage'] = BX_DOL_VOTE_USAGE_INLINE;
  105. return $this->getElement($aParams);
  106. }
  107. public function getElement($aParams = array())
  108. {
  109. if(!$this->isAllowedVote())
  110. return '';
  111. $bLike = $this->isLikeMode();
  112. $sType = $bLike ? BX_DOL_VOTE_TYPE_LIKES : BX_DOL_VOTE_TYPE_STARS;
  113. $bShowDoVoteAsButtonSmall = $bLike && isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  114. $bShowDoVoteAsButton = $bLike && !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  115. $sMethodDoVote = '_getDoVote' . ucfirst($sType);
  116. if(!method_exists($this, $sMethodDoVote))
  117. return '';
  118. $aVote = $this->_oQuery->getVote($this->getId());
  119. $aParams = array_merge($this->_aElementDefaults[$sType], $aParams);
  120. $sTmplName = 'vote_element_' . (!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT) . '.html';
  121. return BxDolTemplate::getInstance()->parseHtmlByName($sTmplName, array(
  122. 'style_prefix' => $this->_sStylePrefix,
  123. 'html_id' => $this->_aHtmlIds['main_' . $sType],
  124. 'class' => $this->_sStylePrefix . '-' . $sType . ($bShowDoVoteAsButton ? '-button' : '') . ($bShowDoVoteAsButtonSmall ? '-button-small' : ''),
  125. 'rate' => $aVote['rate'],
  126. 'count' => $aVote['count'],
  127. 'do_vote' => $this->$sMethodDoVote($aParams),
  128. 'bx_if:show_counter' => array(
  129. 'condition' => isset($aParams['show_counter']) && $aParams['show_counter'] === true,
  130. 'content' => array(
  131. 'style_prefix' => $this->_sStylePrefix,
  132. 'counter' => $this->getCounter()
  133. )
  134. ),
  135. 'script' => $this->getJsScript()
  136. ));
  137. }
  138. protected function _getDoVoteStars($aParams = array())
  139. {
  140. $sJsObject = $this->getJsObjectName();
  141. $iMinValue = $this->getMinValue();
  142. $iMaxValue = $this->getMaxValue();
  143. $aVote = $this->_oQuery->getVote($this->getId());
  144. $aTmplVarsButtons = $aTmplVarsStars = array();
  145. for($i = $iMinValue; $i <= $iMaxValue; $i++) {
  146. $aTmplVarsStars[] = array(
  147. 'style_prefix' => $this->_sStylePrefix,
  148. 'value' => $i
  149. );
  150. $aTmplVarsLegend[] = array(
  151. 'style_prefix' => $this->_sStylePrefix,
  152. 'value' => $i
  153. );
  154. $aTmplVarsSlider[] = array(
  155. 'style_prefix' => $this->_sStylePrefix
  156. );
  157. $aTmplVarsButtons[] = array(
  158. 'style_prefix' => $this->_sStylePrefix,
  159. 'js_object' => $sJsObject,
  160. 'value' => $i
  161. );
  162. }
  163. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameDoVoteStars, array(
  164. 'style_prefix' => $this->_sStylePrefix,
  165. 'bx_repeat:stars' => $aTmplVarsStars,
  166. 'bx_if:show_legend' => array(
  167. 'condition' => isset($aParams['show_do_vote_legend']) && $aParams['show_do_vote_legend'] === true,
  168. 'content' => array(
  169. 'style_prefix' => $this->_sStylePrefix,
  170. 'bx_repeat:legend' => $aTmplVarsLegend,
  171. )
  172. ),
  173. 'bx_repeat:slider' => $aTmplVarsSlider,
  174. 'bx_repeat:buttons' => $aTmplVarsButtons,
  175. ));
  176. }
  177. protected function _getDoVoteLikes($aParams = array())
  178. {
  179. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  180. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  181. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameDoVoteLikes, array(
  182. 'style_prefix' => $this->_sStylePrefix,
  183. 'js_object' => $this->getJsObjectName(),
  184. 'class' => ($bShowDoVoteAsButton ? 'bx-btn' : '') . ($bShowDoVoteAsButtonSmall ? 'bx-btn bx-btn-small' : ''),
  185. 'value' => $this->getMinValue(),
  186. 'do_vote' => $this->_getLabelDoLike($aParams),
  187. ));
  188. }
  189. protected function _getLabelCounter($iCount)
  190. {
  191. return $iCount;
  192. }
  193. protected function _getLabelDoLike($aParams = array())
  194. {
  195. $sResult = '';
  196. if(isset($aParams['show_do_vote_icon']) && $aParams['show_do_vote_icon'] == true)
  197. $sResult .= BxDolTemplate::getInstance()->parseHtmlByName('bx_icon.html', array('name' => 'plus'));
  198. if(isset($aParams['show_do_vote_label']) && $aParams['show_do_vote_label'] == true)
  199. $sResult .= ($sResult != '' ? ' ' : '') . _t('_vote_do_like');
  200. return $sResult;
  201. }
  202. protected function _getVotedBy()
  203. {
  204. $aTmplUsers = array();
  205. $aUserIds = $this->_oQuery->getVotedBy($this->getId());
  206. foreach($aUserIds as $iUserId) {
  207. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  208. $aTmplUsers[] = array(
  209. 'style_prefix' => $this->_sStylePrefix,
  210. 'user_unit' => $sUserUnit
  211. );
  212. }
  213. if(empty($aTmplUsers))
  214. $aTmplUsers = MsgBox(_t('_Empty'));
  215. return BxDolTemplate::getInstance()->parseHtmlByName('vote_by_list.html', array(
  216. 'style_prefix' => $this->_sStylePrefix,
  217. 'bx_repeat:list' => $aTmplUsers
  218. ));
  219. }
  220. protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false)
  221. {
  222. header('Content-type: text/html; charset=utf-8');
  223. require_once(BX_DIRECTORY_PATH_PLUGINS . 'Services_JSON.php');
  224. $oParser = new Services_JSON();
  225. $s = $oParser->encode($a);
  226. if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
  227. $s = '<textarea>' . $s . '</textarea>'; // http://jquery.malsup.com/form/#file-upload
  228. echo $s;
  229. }
  230. }
  231. /** @} */