BxBaseVote.php 13 KB

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