BxBaseVote.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. $this->addCssJs();
  65. require_once(BX_DIRECTORY_PATH_PLUGINS . 'Services_JSON.php');
  66. $oJson = new Services_JSON();
  67. return BxDolTemplate::getInstance()->_wrapInTagJsCode("var " . $this->_sJsObjName . " = new BxDolVote({
  68. sObjName: '" . $this->_sJsObjName . "',
  69. sSystem: '" . $this->getSystemName() . "',
  70. iAuthorId: '" . $this->_getAuthorId() . "',
  71. iObjId: '" . $this->getId() . "',
  72. iLikeMode: " . ($this->isLikeMode() ? 1 : 0) . ",
  73. sRootUrl: '" . BX_DOL_URL_ROOT . "',
  74. sStylePrefix: '" . $this->_sStylePrefix . "',
  75. aHtmlIds: " . $oJson->encode($this->_aHtmlIds) . ",
  76. });");
  77. }
  78. public function getJsClick()
  79. {
  80. if(!$this->isLikeMode())
  81. return false;
  82. return $this->getJsObjectName() . '.vote(this, ' . $this->getMaxValue() . ')';
  83. }
  84. public function getCounter($aParams = array()) {
  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. $aVote = $this->_oQuery->getVote($this->getId());
  120. $aParams = array_merge($this->_aElementDefaults[$sType], $aParams);
  121. $sTmplName = 'vote_element_' . (!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT) . '.html';
  122. return BxDolTemplate::getInstance()->parseHtmlByName($sTmplName, array(
  123. 'style_prefix' => $this->_sStylePrefix,
  124. 'html_id' => $this->_aHtmlIds['main_' . $sType],
  125. 'class' => $this->_sStylePrefix . '-' . $sType . ($bShowDoVoteAsButton ? '-button' : '') . ($bShowDoVoteAsButtonSmall ? '-button-small' : ''),
  126. 'rate' => $aVote['rate'],
  127. 'count' => $aVote['count'],
  128. 'do_vote' => $this->$sMethodDoVote($aParams),
  129. 'bx_if:show_counter' => array(
  130. 'condition' => isset($aParams['show_counter']) && $aParams['show_counter'] === true,
  131. 'content' => array(
  132. 'style_prefix' => $this->_sStylePrefix,
  133. 'counter' => $this->getCounter()
  134. )
  135. ),
  136. 'script' => $this->getJsScript()
  137. ));
  138. }
  139. protected function _getDoVoteStars($aParams = array())
  140. {
  141. $sJsObject = $this->getJsObjectName();
  142. $iMinValue = $this->getMinValue();
  143. $iMaxValue = $this->getMaxValue();
  144. $aVote = $this->_oQuery->getVote($this->getId());
  145. $aTmplVarsButtons = $aTmplVarsStars = array();
  146. for($i = $iMinValue; $i <= $iMaxValue; $i++) {
  147. $aTmplVarsStars[] = array(
  148. 'style_prefix' => $this->_sStylePrefix,
  149. 'value' => $i
  150. );
  151. $aTmplVarsLegend[] = array(
  152. 'style_prefix' => $this->_sStylePrefix,
  153. 'value' => $i
  154. );
  155. $aTmplVarsSlider[] = array(
  156. 'style_prefix' => $this->_sStylePrefix
  157. );
  158. $aTmplVarsButtons[] = array(
  159. 'style_prefix' => $this->_sStylePrefix,
  160. 'js_object' => $sJsObject,
  161. 'value' => $i
  162. );
  163. }
  164. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameDoVoteStars, array(
  165. 'style_prefix' => $this->_sStylePrefix,
  166. 'bx_repeat:stars' => $aTmplVarsStars,
  167. 'bx_if:show_legend' => array(
  168. 'condition' => isset($aParams['show_do_vote_legend']) && $aParams['show_do_vote_legend'] === true,
  169. 'content' => array(
  170. 'style_prefix' => $this->_sStylePrefix,
  171. 'bx_repeat:legend' => $aTmplVarsLegend,
  172. )
  173. ),
  174. 'bx_repeat:slider' => $aTmplVarsSlider,
  175. 'bx_repeat:buttons' => $aTmplVarsButtons,
  176. ));
  177. }
  178. protected function _getDoVoteLikes($aParams = array())
  179. {
  180. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  181. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  182. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameDoVoteLikes, array(
  183. 'style_prefix' => $this->_sStylePrefix,
  184. 'js_object' => $this->getJsObjectName(),
  185. 'class' => ($bShowDoVoteAsButton ? 'bx-btn' : '') . ($bShowDoVoteAsButtonSmall ? 'bx-btn bx-btn-small' : ''),
  186. 'value' => $this->getMinValue(),
  187. 'do_vote' => $this->_getLabelDoLike($aParams),
  188. ));
  189. }
  190. protected function _getLabelCounter($iCount)
  191. {
  192. return $iCount;
  193. }
  194. protected function _getLabelDoLike($aParams = array())
  195. {
  196. $sResult = '';
  197. if(isset($aParams['show_do_vote_icon']) && $aParams['show_do_vote_icon'] == true)
  198. $sResult .= BxDolTemplate::getInstance()->parseHtmlByName('bx_icon.html', array('name' => 'plus'));
  199. if(isset($aParams['show_do_vote_label']) && $aParams['show_do_vote_label'] == true)
  200. $sResult .= ($sResult != '' ? ' ' : '') . _t('_vote_do_like');
  201. return $sResult;
  202. }
  203. protected function _getVotedBy()
  204. {
  205. $aTmplUsers = array();
  206. $aUserIds = $this->_oQuery->getVotedBy($this->getId());
  207. foreach($aUserIds as $iUserId) {
  208. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  209. $aTmplUsers[] = array(
  210. 'style_prefix' => $this->_sStylePrefix,
  211. 'user_unit' => $sUserUnit
  212. );
  213. }
  214. if(empty($aTmplUsers))
  215. $aTmplUsers = MsgBox(_t('_Empty'));
  216. return BxDolTemplate::getInstance()->parseHtmlByName('vote_by_list.html', array(
  217. 'style_prefix' => $this->_sStylePrefix,
  218. 'bx_repeat:list' => $aTmplUsers
  219. ));
  220. }
  221. protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false)
  222. {
  223. header('Content-type: text/html; charset=utf-8');
  224. require_once(BX_DIRECTORY_PATH_PLUGINS . 'Services_JSON.php');
  225. $oParser = new Services_JSON();
  226. $s = $oParser->encode($a);
  227. if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
  228. $s = '<textarea>' . $s . '</textarea>'; // http://jquery.malsup.com/form/#file-upload
  229. echo $s;
  230. }
  231. }
  232. /** @} */