BxBaseVote.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. bx_import('BxDolVote');
  9. bx_import('BxDolTemplate');
  10. /**
  11. * @see BxDolVote
  12. */
  13. class BxBaseVote extends BxDolVote
  14. {
  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 = 1)
  23. {
  24. parent::__construct($sSystem, $iId, $iInit);
  25. $this->_sJsObjName = 'oVote' . str_replace(' ', '', ucwords(str_replace(array('_' , '-'), array(' ', ' '), $sSystem))) . $iId;
  26. $this->_sStylePrefix = 'bx-vote';
  27. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  28. $this->_aHtmlIds = array(
  29. 'main_stars' => 'bx-vote-stars-' . $sHtmlId,
  30. 'main_likes' => 'bx-vote-likes-' . $sHtmlId,
  31. 'counter' => 'bx-vote-counter-' . $sHtmlId,
  32. 'by_popup' => 'bx-vote-by-popup-' . $sHtmlId
  33. );
  34. $this->_aElementDefaults = array(
  35. 'stars' => array(
  36. 'show_do_vote_legend' => false,
  37. 'show_counter' => true
  38. ),
  39. 'likes' => array(
  40. 'show_do_vote_as_button' => false,
  41. 'show_do_vote_as_button_small' => false,
  42. 'show_do_vote_icon' => true,
  43. 'show_do_vote_label' => false,
  44. 'show_counter' => true
  45. )
  46. );
  47. $this->_sTmplNameCounter = 'vote_counter.html';
  48. $this->_sTmplNameDoVoteLikes = 'vote_do_vote_likes.html';
  49. $this->_sTmplNameDoVoteStars = 'vote_do_vote_stars.html';
  50. }
  51. public function addCssJs()
  52. {
  53. $oTemplate = BxDolTemplate::getInstance();
  54. $oTemplate->addJs('BxDolVote.js');
  55. $oTemplate->addCss('vote.css');
  56. }
  57. public function getJsObjectName()
  58. {
  59. return $this->_sJsObjName;
  60. }
  61. public function getJsScript()
  62. {
  63. $this->addCssJs();
  64. require_once(BX_DIRECTORY_PATH_PLUGINS . 'Services_JSON.php');
  65. $oJson = new Services_JSON();
  66. return BxDolTemplate::getInstance()->_wrapInTagJsCode("var " . $this->_sJsObjName . " = new BxDolVote({
  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: " . $oJson->encode($this->_aHtmlIds) . ",
  75. });");
  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. 'style_prefix' => $this->_sStylePrefix,
  88. 'js_object' => $sJsObject,
  89. 'html_id_counter' => $this->_aHtmlIds['counter'],
  90. 'counter' => (int)$aVote['count'] > 0 ? $this->_getLabelCounter($aVote['count']) : ''
  91. ));
  92. }
  93. public function getElementBlock($aParams = array())
  94. {
  95. $aParams['usage'] = BX_DOL_VOTE_USAGE_BLOCK;
  96. return $this->getElement($aParams);
  97. }
  98. public function getElementInline($aParams = array())
  99. {
  100. $aParams['usage'] = BX_DOL_VOTE_USAGE_INLINE;
  101. return $this->getElement($aParams);
  102. }
  103. public function getElement($aParams = array())
  104. {
  105. if(!$this->isAllowedVote())
  106. return '';
  107. $bLike = $this->isLikeMode();
  108. $sType = $bLike ? BX_DOL_VOTE_TYPE_LIKES : BX_DOL_VOTE_TYPE_STARS;
  109. $bShowDoVoteAsButtonSmall = $bLike && isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  110. $bShowDoVoteAsButton = $bLike && !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  111. $sMethodDoVote = '_getDoVote' . ucfirst($sType);
  112. if(!method_exists($this, $sMethodDoVote))
  113. return '';
  114. $aVote = $this->_oQuery->getVote($this->getId());
  115. $aParams = array_merge($this->_aElementDefaults[$sType], $aParams);
  116. $sTmplName = 'vote_element_' . (!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT) . '.html';
  117. return BxDolTemplate::getInstance()->parseHtmlByName($sTmplName, array(
  118. 'style_prefix' => $this->_sStylePrefix,
  119. 'html_id' => $this->_aHtmlIds['main_' . $sType],
  120. 'class' => $this->_sStylePrefix . '-' . $sType . ($bShowDoVoteAsButton ? '-button' : '') . ($bShowDoVoteAsButtonSmall ? '-button-small' : ''),
  121. 'rate' => $aVote['rate'],
  122. 'count' => $aVote['count'],
  123. 'do_vote' => $this->$sMethodDoVote($aParams),
  124. 'bx_if:show_counter' => array(
  125. 'condition' => isset($aParams['show_counter']) && $aParams['show_counter'] === true,
  126. 'content' => array(
  127. 'style_prefix' => $this->_sStylePrefix,
  128. 'counter' => $this->getCounter()
  129. )
  130. ),
  131. 'script' => $this->getJsScript()
  132. ));
  133. }
  134. protected function _getDoVoteStars($aParams = array())
  135. {
  136. $sJsObject = $this->getJsObjectName();
  137. $iMinValue = $this->getMinValue();
  138. $iMaxValue = $this->getMaxValue();
  139. $aVote = $this->_oQuery->getVote($this->getId());
  140. $aTmplVarsButtons = $aTmplVarsStars = array();
  141. for($i = $iMinValue; $i <= $iMaxValue; $i++) {
  142. $aTmplVarsStars[] = array(
  143. 'style_prefix' => $this->_sStylePrefix,
  144. 'value' => $i
  145. );
  146. $aTmplVarsLegend[] = array(
  147. 'style_prefix' => $this->_sStylePrefix,
  148. 'value' => $i
  149. );
  150. $aTmplVarsSlider[] = array(
  151. 'style_prefix' => $this->_sStylePrefix
  152. );
  153. $aTmplVarsButtons[] = array(
  154. 'style_prefix' => $this->_sStylePrefix,
  155. 'js_object' => $sJsObject,
  156. 'value' => $i
  157. );
  158. }
  159. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameDoVoteStars, array(
  160. 'style_prefix' => $this->_sStylePrefix,
  161. 'bx_repeat:stars' => $aTmplVarsStars,
  162. 'bx_if:show_legend' => array(
  163. 'condition' => isset($aParams['show_do_vote_legend']) && $aParams['show_do_vote_legend'] === true,
  164. 'content' => array(
  165. 'style_prefix' => $this->_sStylePrefix,
  166. 'bx_repeat:legend' => $aTmplVarsLegend,
  167. )
  168. ),
  169. 'bx_repeat:slider' => $aTmplVarsSlider,
  170. 'bx_repeat:buttons' => $aTmplVarsButtons,
  171. ));
  172. }
  173. protected function _getDoVoteLikes($aParams = array())
  174. {
  175. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && $aParams['show_do_vote_as_button_small'] == true;
  176. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && $aParams['show_do_vote_as_button'] == true;
  177. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameDoVoteLikes, array(
  178. 'style_prefix' => $this->_sStylePrefix,
  179. 'js_object' => $this->getJsObjectName(),
  180. 'class' => ($bShowDoVoteAsButton ? 'bx-btn' : '') . ($bShowDoVoteAsButtonSmall ? 'bx-btn bx-btn-small' : ''),
  181. 'value' => $this->getMinValue(),
  182. 'do_vote' => $this->_getLabelDoLike($aParams),
  183. ));
  184. }
  185. protected function _getLabelCounter($iCount)
  186. {
  187. return $iCount;
  188. }
  189. protected function _getLabelDoLike($aParams = array())
  190. {
  191. $sResult = '';
  192. if(isset($aParams['show_do_vote_icon']) && $aParams['show_do_vote_icon'] == true)
  193. $sResult .= BxDolTemplate::getInstance()->parseHtmlByName('bx_icon.html', array('name' => 'plus'));
  194. if(isset($aParams['show_do_vote_label']) && $aParams['show_do_vote_label'] == true)
  195. $sResult .= ($sResult != '' ? ' ' : '') . _t('_vote_do_like');
  196. return $sResult;
  197. }
  198. protected function _getVotedBy()
  199. {
  200. $aTmplUsers = array();
  201. $aUserIds = $this->_oQuery->getVotedBy($this->getId());
  202. foreach($aUserIds as $iUserId) {
  203. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  204. $aTmplUsers[] = array(
  205. 'style_prefix' => $this->_sStylePrefix,
  206. 'user_unit' => $sUserUnit
  207. );
  208. }
  209. if(empty($aTmplUsers))
  210. $aTmplUsers = MsgBox(_t('_Empty'));
  211. return BxDolTemplate::getInstance()->parseHtmlByName('vote_by_list.html', array(
  212. 'style_prefix' => $this->_sStylePrefix,
  213. 'bx_repeat:list' => $aTmplUsers
  214. ));
  215. }
  216. protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false)
  217. {
  218. header('Content-type: text/html; charset=utf-8');
  219. require_once(BX_DIRECTORY_PATH_PLUGINS . 'Services_JSON.php');
  220. $oParser = new Services_JSON();
  221. $s = $oParser->encode($a);
  222. if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
  223. $s = '<textarea>' . $s . '</textarea>'; // http://jquery.malsup.com/form/#file-upload
  224. echo $s;
  225. }
  226. }