BxBaseVote.php 13 KB

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