BxBaseVote.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * @see BxDolVote
  11. */
  12. class BxBaseVote extends BxDolVote
  13. {
  14. protected $_bCssJsAdded;
  15. protected $_sJsClsName;
  16. protected $_sJsObjName;
  17. protected $_sStylePrefix;
  18. protected $_aHtmlIds;
  19. protected $_sTmplNameLegend;
  20. protected $_sTmplNameByList;
  21. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  22. {
  23. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  24. $this->_aElementDefaults = [];
  25. $this->_aElementDefaultsApi = [];
  26. $this->_aElementParamsApi = ['is_voted'];
  27. $this->_bCssJsAdded = false;
  28. $this->_sStylePrefix = 'bx-vote';
  29. $this->_sJsClsName = 'BxDolVote';
  30. $this->_sJsObjName = 'oVote' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  31. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  32. $this->_aHtmlIds = array(
  33. 'counter' => 'bx-vote-counter-' . $sHtmlId,
  34. 'by_popup' => 'bx-vote-by-popup-' . $sHtmlId
  35. );
  36. $this->_sTmplNameLegend = 'vote_legend.html';
  37. $this->_sTmplNameByList = 'vote_by_list.html';
  38. $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('vote_element_block.html');
  39. $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('vote_element_inline.html');
  40. $this->_sTmplContentCounter = $this->_oTemplate->getHtml('vote_counter.html');
  41. }
  42. public function getJsClassName()
  43. {
  44. return $this->_sJsClsName;
  45. }
  46. public function getJsObjectName()
  47. {
  48. return $this->_sJsObjName;
  49. }
  50. public function getJsScript($aParams = array())
  51. {
  52. $sJsObjName = $this->getJsObjectName();
  53. $sJsObjClass = $this->getJsClassName();
  54. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  55. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new " . $sJsObjClass . "(" . json_encode($this->_prepareParamsData([
  56. 'aRequestParams' => $this->_prepareRequestParamsData($aParams)
  57. ])) . ");";
  58. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  59. }
  60. public function getJsClick($iValue = 0)
  61. {
  62. return $this->getJsObjectName() . '.vote(this, ' . $iValue . ')';
  63. }
  64. public function getJsClickCounter($aParams = array())
  65. {
  66. return $this->getJsObjectName() . '.toggleByPopup(this)';
  67. }
  68. public function getCounter($aParams = [])
  69. {
  70. $aParams = array_merge($this->_aElementDefaults, $aParams);
  71. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  72. $bShowEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  73. $bShowActive = $this->isAllowedVoteViewVoters() && (!isset($aParams['show_counter_active']) || (bool)$aParams['show_counter_active'] === true);
  74. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  75. $sClass = 'sys-action-counter';
  76. if(isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true)
  77. $sClass .= ' sys-ac-only';
  78. $sClass .= ' ' .$this->_sStylePrefix . '-counter ' . $this->_sStylePrefix . '-counter-' . $this->_sType;
  79. if(!empty($aParams['class_counter']))
  80. $sClass .= $aParams['class_counter'];
  81. $aTmplVarsAttrs = array();
  82. if($bShowActive)
  83. $aTmplVarsAttrs = array_merge($aTmplVarsAttrs, array(
  84. array('key' => 'href', 'value' => 'javascript:void(0)'),
  85. array('key' => 'title', 'value' => bx_html_attribute($this->_getTitleDoBy($aParams))),
  86. array('key' => 'onclick', 'value' => 'javascript:' . $this->getJsClickCounter($aParams))
  87. ));
  88. $sHtmlId = isset($aParams['id_counter']) ? $aParams['id_counter'] : $this->_aHtmlIds['counter'];
  89. if(!empty($sHtmlId))
  90. $aTmplVarsAttrs[] = array('key' => 'id', 'value' => $sHtmlId);
  91. $aVote = !empty($aParams['vote']) && is_array($aParams['vote']) ? $aParams['vote'] : $this->_getVote();
  92. $sContent = $bShowEmpty || (int)$aVote['count'] > 0 ? $this->_getCounterLabel($aVote['count'], $aParams) : '';
  93. $oSockets= BxDolSockets::getInstance();
  94. $sJs = '';
  95. if ($oSockets->isEnable()){
  96. $sJs .= $this->_oTemplate->_wrapInTagJsCode($oSockets->getSubscribeJsCode($this->_sSystem . '_' . $this->_sType, $this->getId(), 'voted', $this->getJsObjectName().'.onVoteAs('.$this->getId().',"' . $this->_sType . '-' . str_replace('_', '-', $this->_sSystem) . '", data)'));
  97. }
  98. return $sJs . $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounter(), array(
  99. 'bx_if:show_text' => array(
  100. 'condition' => !$bShowActive,
  101. 'content' => array(
  102. 'class' => $sClass,
  103. 'bx_repeat:attrs' => $aTmplVarsAttrs,
  104. 'content' => $sContent
  105. )
  106. ),
  107. 'bx_if:show_link' => array(
  108. 'condition' => $bShowActive,
  109. 'content' => array(
  110. 'class' => $sClass,
  111. 'bx_repeat:attrs' => $aTmplVarsAttrs,
  112. 'content' => $sContent
  113. )
  114. ),
  115. 'class' => $sClass,
  116. 'bx_repeat:attrs' => $aTmplVarsAttrs,
  117. 'content' => $sContent,
  118. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  119. ));
  120. }
  121. public function getLegend($aParams = array())
  122. {
  123. return '';
  124. }
  125. public function getElementBlock($aParams = array())
  126. {
  127. $aParams['usage'] = BX_DOL_VOTE_USAGE_BLOCK;
  128. return $this->getElement($aParams);
  129. }
  130. public function getElementInline($aParams = array())
  131. {
  132. $aParams['usage'] = BX_DOL_VOTE_USAGE_INLINE;
  133. return $this->getElement($aParams);
  134. }
  135. public function getElement($aParams = array())
  136. {
  137. $sMethod = '_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_VOTE_USAGE_DEFAULT);
  138. if(!method_exists($this, $sMethod))
  139. return '';
  140. $aTmplVars = $this->_getTmplVarsElement($aParams);
  141. if(empty($aTmplVars) || !is_array($aTmplVars))
  142. return '';
  143. return $this->_oTemplate->parseHtmlByContent($this->$sMethod(), $aTmplVars);
  144. }
  145. public function getElementAPI($aParams = [])
  146. {
  147. if(!($this->_bApi = bx_is_api()))
  148. return;
  149. if(!$this->isEnabled())
  150. return bx_api_get_msg('_vote_err_not_enabled');
  151. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  152. $iObjectId = $this->getId();
  153. $iAuthorId = $this->_getAuthorId();
  154. $bCount = $this->_isCount();
  155. $isAllowedVote = $this->isAllowedVote();
  156. $isAllowedVoteView = $this->isAllowedVoteView();
  157. $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId);
  158. $aParams['track'] = $aParams['is_voted'] ? $this->_getTrack($iObjectId, $iAuthorId) : array();
  159. //--- Do Vote
  160. $bDoVote = $this->_isShowDoVote($aParams, $isAllowedVote, $bCount);
  161. $aDoVote = $bDoVote ? $this->_getDoVote($aParams, $isAllowedVote) : [];
  162. //--- Counter
  163. $bCounter = $this->_isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  164. $aCounter = $bCounter ? $this->getCounterAPI(array_merge($aParams, [
  165. 'show_counter_only' => false,
  166. 'show_script' => false
  167. ])) : [];
  168. if(!$bDoVote && !$bCounter)
  169. return bx_api_get_msg('');
  170. return [
  171. 'type' => $this->_sType,
  172. 'system' => $this->_sSystem,
  173. 'object_id' => $this->_iId,
  174. 'params' => array_intersect_key($aParams, array_flip($this->_aElementParamsApi)),
  175. 'action' => $aDoVote,
  176. 'counter' => $aCounter
  177. ];
  178. }
  179. /**
  180. * Internal methods.
  181. */
  182. protected function _prepareParamsData($aParams)
  183. {
  184. return parent::_prepareParamsData(array_merge([
  185. 'sObjName' => $this->getJsObjectName(),
  186. 'sStylePrefix' => $this->_sStylePrefix,
  187. 'aHtmlIds' => $this->_aHtmlIds,
  188. ], $aParams));
  189. }
  190. /*
  191. * This method should be overwritten by subclass.
  192. */
  193. protected function _getTmplVarsElement($aParams = array())
  194. {
  195. $aParams = array_merge($this->_aElementDefaults, $aParams);
  196. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  197. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  198. $iObjectId = $this->getId();
  199. $iAuthorId = $this->_getAuthorId();
  200. $bCount = $this->_isCount();
  201. $isAllowedVote = $this->isAllowedVote();
  202. $isAllowedVoteView = $this->isAllowedVoteView();
  203. $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId);
  204. $aParams['track'] = $aParams['is_voted'] ? $this->_getTrack($iObjectId, $iAuthorId) : array();
  205. //--- Do Vote
  206. $bTmplVarsDoVote = $this->_isShowDoVote($aParams, $isAllowedVote, $bCount);
  207. $aTmplVarsDoVote = array();
  208. if($bTmplVarsDoVote)
  209. $aTmplVarsDoVote = array(
  210. 'style_prefix' => $this->_sStylePrefix,
  211. 'do_vote' => $this->_getDoVote($aParams, $isAllowedVote),
  212. );
  213. //--- Counter
  214. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  215. $aTmplVarsCounter = array();
  216. if($bTmplVarsCounter)
  217. $aTmplVarsCounter = array(
  218. 'style_prefix' => $this->_sStylePrefix,
  219. 'bx_if:show_hidden' => array(
  220. 'condition' => !$bCount && !$bShowCounterEmpty,
  221. 'content' => array()
  222. ),
  223. 'counter' => $this->getCounter(array_merge($aParams, [
  224. 'show_counter_only' => false,
  225. 'show_script' => false
  226. ]))
  227. );
  228. //--- Legend
  229. $bTmplVarsLegend = $this->_isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  230. $aTmplVarsLegend = array();
  231. if($bTmplVarsLegend)
  232. $aTmplVarsLegend = array(
  233. 'legend' => $this->getLegend($aParams)
  234. );
  235. if(!$bTmplVarsDoVote && !$bTmplVarsCounter && !$bTmplVarsLegend)
  236. return array();
  237. $sClass = $this->_sStylePrefix . '-' . $this->_sType;
  238. if(!empty($aParams['class_element']))
  239. $sClass .= $aParams['class_element'];
  240. return array(
  241. 'style_prefix' => $this->_sStylePrefix,
  242. 'html_id' => $this->_aHtmlIds['main'],
  243. 'class' => $sClass . ' ' . $this->_aHtmlIds['main'],
  244. 'bx_if:show_vote_data' => array(
  245. 'condition' => false,
  246. 'content' => array()
  247. ),
  248. 'bx_if:show_do_vote' => array(
  249. 'condition' => $bTmplVarsDoVote,
  250. 'content' => $aTmplVarsDoVote
  251. ),
  252. 'bx_if:show_counter' => array(
  253. 'condition' => $bTmplVarsCounter,
  254. 'content' => $aTmplVarsCounter
  255. ),
  256. 'bx_if:show_legend' => array(
  257. 'condition' => $bTmplVarsLegend,
  258. 'content' => $aTmplVarsLegend
  259. ),
  260. 'script' => $this->getJsScript($aParams)
  261. );
  262. }
  263. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  264. {
  265. return $this->_bApi ? [] : '';
  266. }
  267. protected function _getCounterLabel($iCount, $aParams = array())
  268. {
  269. return _t(isset($aParams['caption']) ? $aParams['caption'] : '_vote_counter', $iCount);
  270. }
  271. protected function _getVotedBy($aParams = array())
  272. {
  273. $aTmplUsers = array();
  274. $aUserIds = $this->_oQuery->getPerformedBy($this->getId());
  275. foreach($aUserIds as $iUserId) {
  276. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($iUserId);
  277. $aTmplUsers[] = array(
  278. 'style_prefix' => $this->_sStylePrefix,
  279. 'user_unit' => $sUserUnit
  280. );
  281. }
  282. if(empty($aTmplUsers))
  283. $aTmplUsers = MsgBox(_t('_Empty'));
  284. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  285. 'style_prefix' => $this->_sStylePrefix,
  286. 'class' => '',
  287. 'bx_repeat:list' => $aTmplUsers
  288. ));
  289. }
  290. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  291. {
  292. return !isset($aParams['show_do_vote']) || (bool)$aParams['show_do_vote'] === true;
  293. }
  294. protected function _isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount)
  295. {
  296. return isset($aParams['show_counter']) && (bool)$aParams['show_counter'] === true && $isAllowedVoteView && ($isAllowedVote || $bCount);
  297. }
  298. protected function _isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount)
  299. {
  300. return false;
  301. }
  302. }
  303. /** @} */