BxBaseVoteView.php 8.3 KB

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