BxBaseVoteView.php 9.0 KB

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