BxBaseVoteView.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_icon' => true,
  42. 'show_do_vote_label' => false,
  43. 'show_counter' => true
  44. )
  45. );
  46. $this->_sTmplNameCounter = 'vote_counter.html';
  47. $this->_sTmplNameDoVoteLikes = 'vote_do_vote_likes.html';
  48. $this->_sTmplNameDoVoteStars = 'vote_do_vote_stars.html';
  49. }
  50. public function addCssJs()
  51. {
  52. $oTemplate = BxDolTemplate::getInstance();
  53. $oTemplate->addJs('BxDolVote.js');
  54. $oTemplate->addCss('vote.css');
  55. }
  56. public function getJsObjectName()
  57. {
  58. return $this->_sJsObjName;
  59. }
  60. public function getJsScript()
  61. {
  62. $this->addCssJs();
  63. require_once(BX_DIRECTORY_PATH_PLUGINS . 'Services_JSON.php');
  64. $oJson = new Services_JSON();
  65. return BxDolTemplate::getInstance()->_wrapInTagJsCode("var " . $this->_sJsObjName . " = new BxDolVote({
  66. sObjName: '" . $this->_sJsObjName . "',
  67. sSystem: '" . $this->getSystemName() . "',
  68. iAuthorId: '" . $this->_getAuthorId() . "',
  69. iObjId: '" . $this->getId() . "',
  70. iLikeMode: " . ($this->isLikeMode() ? 1 : 0) . ",
  71. sRootUrl: '" . BX_DOL_URL_ROOT . "',
  72. sStylePrefix: '" . $this->_sStylePrefix . "',
  73. aHtmlIds: " . $oJson->encode($this->_aHtmlIds) . ",
  74. });");
  75. }
  76. public function getJsClick()
  77. {
  78. if(!$this->isLikeMode())
  79. return false;
  80. return $this->getJsObjectName() . '.vote(this, ' . $this->getMaxValue() . ')';
  81. }
  82. public function getCounter($aParams = array()) {
  83. $sJsObject = $this->getJsObjectName();
  84. $aVote = $this->_oQuery->getVote($this->getId());
  85. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameCounter, array(
  86. 'style_prefix' => $this->_sStylePrefix,
  87. 'js_object' => $sJsObject,
  88. 'html_id_counter' => $this->_aHtmlIds['counter'],
  89. 'counter' => (int)$aVote['count'] > 0 ? $this->_getLabelCounter($aVote['count']) : ''
  90. ));
  91. }
  92. public function getElementBlock($aParams = array())
  93. {
  94. $aParams['usage'] = BX_DOL_VOTE_USAGE_BLOCK;
  95. return $this->getElement($aParams);
  96. }
  97. public function getElementInline($aParams = array())
  98. {
  99. $aParams['usage'] = BX_DOL_VOTE_USAGE_INLINE;
  100. return $this->getElement($aParams);
  101. }
  102. public function getElement($aParams = array())
  103. {
  104. $sType = $this->isLikeMode() ? BX_DOL_VOTE_TYPE_LIKES : BX_DOL_VOTE_TYPE_STARS;
  105. $sMethodDoVote = '_getDoVote' . ucfirst($sType);
  106. if(!method_exists($this, $sMethodDoVote))
  107. return '';
  108. $aVote = $this->_oQuery->getVote($this->getId());
  109. $aParams = array_merge($this->_aElementDefaults[$sType], $aParams);
  110. $sTmplName = 'vote_element_' . (!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT) . '.html';
  111. return BxDolTemplate::getInstance()->parseHtmlByName($sTmplName, array(
  112. 'style_prefix' => $this->_sStylePrefix,
  113. 'html_id' => $this->_aHtmlIds['main_' . $sType],
  114. 'type' => $sType,
  115. 'rate' => $aVote['rate'],
  116. 'count' => $aVote['count'],
  117. 'do_vote' => $this->$sMethodDoVote($aParams),
  118. 'bx_if:show_counter' => array(
  119. 'condition' => isset($aParams['show_counter']) && $aParams['show_counter'] === true,
  120. 'content' => array(
  121. 'style_prefix' => $this->_sStylePrefix,
  122. 'counter' => $this->getCounter()
  123. )
  124. ),
  125. 'script' => $this->getJsScript()
  126. ));
  127. }
  128. protected function _getDoVoteStars($aParams = array())
  129. {
  130. $sJsObject = $this->getJsObjectName();
  131. $iMinValue = $this->getMinValue();
  132. $iMaxValue = $this->getMaxValue();
  133. $aVote = $this->_oQuery->getVote($this->getId());
  134. $aTmplVarsButtons = $aTmplVarsStars = array();
  135. for($i = $iMinValue; $i <= $iMaxValue; $i++) {
  136. $aTmplVarsStars[] = array(
  137. 'style_prefix' => $this->_sStylePrefix,
  138. 'value' => $i
  139. );
  140. $aTmplVarsLegend[] = array(
  141. 'style_prefix' => $this->_sStylePrefix,
  142. 'value' => $i
  143. );
  144. $aTmplVarsSlider[] = array(
  145. 'style_prefix' => $this->_sStylePrefix
  146. );
  147. $aTmplVarsButtons[] = array(
  148. 'style_prefix' => $this->_sStylePrefix,
  149. 'js_object' => $sJsObject,
  150. 'value' => $i
  151. );
  152. }
  153. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameDoVoteStars, array(
  154. 'style_prefix' => $this->_sStylePrefix,
  155. 'bx_repeat:stars' => $aTmplVarsStars,
  156. 'bx_if:show_legend' => array(
  157. 'condition' => isset($aParams['show_do_vote_legend']) && $aParams['show_do_vote_legend'] === true,
  158. 'content' => array(
  159. 'style_prefix' => $this->_sStylePrefix,
  160. 'bx_repeat:legend' => $aTmplVarsLegend,
  161. )
  162. ),
  163. 'bx_repeat:slider' => $aTmplVarsSlider,
  164. 'bx_repeat:buttons' => $aTmplVarsButtons,
  165. ));
  166. }
  167. protected function _getDoVoteLikes($aParams = array())
  168. {
  169. return BxDolTemplate::getInstance()->parseHtmlByName($this->_sTmplNameDoVoteLikes, array(
  170. 'style_prefix' => $this->_sStylePrefix,
  171. 'js_object' => $this->getJsObjectName(),
  172. 'bx_if:show_as_button' => array(
  173. 'condition' => isset($aParams['show_as_button']) && $aParams['show_as_button'] === true,
  174. 'content' => array()
  175. ),
  176. 'value' => $this->getMinValue(),
  177. 'do_vote' => $this->_getLabelDoLike($aParams),
  178. ));
  179. }
  180. protected function _getLabelCounter($iCount)
  181. {
  182. return $iCount;
  183. }
  184. protected function _getLabelDoLike($aParams = array())
  185. {
  186. $sResult = '';
  187. if(isset($aParams['show_do_vote_icon']) && $aParams['show_do_vote_icon'] == true)
  188. $sResult .= BxDolTemplate::getInstance()->parseHtmlByName('bx_icon.html', array('name' => 'plus'));
  189. if(isset($aParams['show_do_vote_label']) && $aParams['show_do_vote_label'] == true)
  190. $sResult .= ($sResult != '' ? ' ' : '') . _t('_vote_do_like');
  191. return $sResult;
  192. }
  193. protected function _getVotedBy()
  194. {
  195. $aTmplUsers = array();
  196. $aUserIds = $this->_oQuery->getVotedBy($this->getId());
  197. foreach($aUserIds as $iUserId) {
  198. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  199. $aTmplUsers[] = array(
  200. 'style_prefix' => $this->_sStylePrefix,
  201. 'user_unit' => $sUserUnit
  202. );
  203. }
  204. if(empty($aTmplUsers))
  205. $aTmplUsers = MsgBox(_t('_Empty'));
  206. return BxDolTemplate::getInstance()->parseHtmlByName('vote_by_list.html', array(
  207. 'style_prefix' => $this->_sStylePrefix,
  208. 'bx_repeat:list' => $aTmplUsers
  209. ));
  210. }
  211. protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false)
  212. {
  213. header('Content-type: text/html; charset=utf-8');
  214. require_once(BX_DIRECTORY_PATH_PLUGINS . 'Services_JSON.php');
  215. $oParser = new Services_JSON();
  216. $s = $oParser->encode($a);
  217. if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
  218. $s = '<textarea>' . $s . '</textarea>'; // http://jquery.malsup.com/form/#file-upload
  219. echo $s;
  220. }
  221. }