BxBaseScore.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 BxDolScore
  11. */
  12. class BxBaseScore extends BxDolScore
  13. {
  14. protected $_bCssJsAdded;
  15. protected $_sJsObjName;
  16. protected $_sStylePrefix;
  17. protected $_aHtmlIds;
  18. protected $_sTmplNameLegend;
  19. protected $_sTmplNameByList;
  20. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  21. {
  22. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  23. $this->_bCssJsAdded = false;
  24. $this->_sJsObjName = 'oScore' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  25. $this->_sStylePrefix = 'bx-score';
  26. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  27. $this->_aHtmlIds = array(
  28. 'main' => 'bx-score-' . $sHtmlId,
  29. 'counter' => 'bx-score-counter-' . $sHtmlId,
  30. 'by_popup' => 'bx-score-by-popup-' . $sHtmlId,
  31. 'legend' => 'bx-score-legend-' . $sHtmlId,
  32. );
  33. $this->_aElementDefaults = [
  34. 'show_do_vote_as_button' => false,
  35. 'show_do_vote_as_button_small' => false,
  36. 'show_do_vote_icon' => true,
  37. 'show_do_vote_label' => false,
  38. 'show_counter' => true,
  39. 'show_counter_only' => true,
  40. 'show_counter_empty' => true,
  41. 'show_counter_label_icon' => false,
  42. 'show_counter_label_text' => true,
  43. 'show_legend' => false,
  44. 'show_script' => true
  45. ];
  46. $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
  47. 'show_counter' => true,
  48. ]);
  49. $this->_aElementParamsApi = ['is_voted'];
  50. $this->_sTmplNameLegend = 'score_legend.html';
  51. $this->_sTmplNameByList = 'score_by_list.html';
  52. $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('score_element_block.html');
  53. $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('score_element_inline.html');
  54. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('score_do_vote_label.html');
  55. $this->_sTmplContentCounterLabel = $this->_oTemplate->getHtml('score_counter_label.html');
  56. }
  57. public function getJsObjectName()
  58. {
  59. return $this->_sJsObjName;
  60. }
  61. public function getJsScript($aParams = [])
  62. {
  63. $sJsObjName = $this->getJsObjectName();
  64. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  65. $aParamsJs = array(
  66. 'sObjName' => $sJsObjName,
  67. 'sSystem' => $this->getSystemName(),
  68. 'iAuthorId' => $this->_getAuthorId(),
  69. 'iObjId' => $this->getId(),
  70. 'sRootUrl' => BX_DOL_URL_ROOT,
  71. 'sSocket' => $this->getSocketName(),
  72. 'sStylePrefix' => $this->_sStylePrefix,
  73. 'aHtmlIds' => $this->_aHtmlIds,
  74. 'aRequestParams' => $this->_prepareRequestParamsData($aParams)
  75. );
  76. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new BxDolScore(" . json_encode($aParamsJs) . ");";
  77. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  78. }
  79. public function getJsClick($sType)
  80. {
  81. return $this->getJsObjectName() . '.vote' . ucfirst($sType) . '(this)';
  82. }
  83. public function getJsClickCounter()
  84. {
  85. return $this->getJsObjectName() . '.toggleByPopup(this)';
  86. }
  87. public function getCounter($aParams = [])
  88. {
  89. $aParams = array_merge($this->_aElementDefaults, $aParams);
  90. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  91. $bShowEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  92. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  93. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  94. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  95. $bAllowedViewViewVoters = $this->isAllowedVoteViewVoters();
  96. $aScore = $this->_oQuery->getScore($this->getId());
  97. $iCup = (int)$aScore['count_up'];
  98. $iCdown = (int)(int)$aScore['count_down'];
  99. $bEmpty = $iCup == 0 && $iCdown == 0;
  100. $sClass = 'sys-action-counter';
  101. if(isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true)
  102. $sClass .= ' sys-ac-only';
  103. $sClass .= ' ' . $this->_sStylePrefix . '-counter';
  104. if($bShowDoVoteAsButtonSmall)
  105. $sClass .= ' bx-btn-small-height';
  106. if($bShowDoVoteAsButton)
  107. $sClass .= ' bx-btn-height';
  108. $bLink = $bAllowedViewViewVoters && !$bEmpty;
  109. $sCounter = !$bEmpty || $bShowEmpty ? $this->_getCounterLabel($iCup - $iCdown, $aParams) : '';
  110. $aTmplVarsText = [
  111. 'id' => $this->_aHtmlIds['counter'],
  112. 'class' => $sClass,
  113. 'content' => $sCounter,
  114. 'title' => _t($this->_getTitleDoBy()),
  115. ];
  116. $aTmplVarsLink = array_merge($aTmplVarsText, [
  117. 'onclick' => $bLink ? 'javascript:' . $this->getJsClickCounter() : ''
  118. ]);
  119. return $this->_oTemplate->parseHtmlByName('score_counter.html', array(
  120. 'bx_if:show_link' => array(
  121. 'condition' => $bLink,
  122. 'content' => $aTmplVarsLink
  123. ),
  124. 'bx_if:show_text' => array(
  125. 'condition' => !$bLink,
  126. 'content' => $aTmplVarsText
  127. ),
  128. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  129. ));
  130. }
  131. public function getCounterAPI($aParams = [])
  132. {
  133. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  134. return $this->_getVote();
  135. }
  136. public function getLegend($aParams = array())
  137. {
  138. $aLegend = $this->_oQuery->getLegend($this->_iId);
  139. $aTypes = array(
  140. BX_DOL_SCORE_DO_UP,
  141. BX_DOL_SCORE_DO_DOWN
  142. );
  143. $aTmplVarsItems = array();
  144. foreach($aTypes as $sType)
  145. $aTmplVarsItems[] = array(
  146. 'style_prefix' => $this->_sStylePrefix,
  147. 'value' => $this->_getLabelDo($sType, $aParams),
  148. 'label' => isset($aLegend[$sType]['count']) ? (int)$aLegend[$sType]['count'] : 0
  149. );
  150. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameLegend, array(
  151. 'style_prefix' => $this->_sStylePrefix,
  152. 'html_id' => $this->_aHtmlIds['legend'],
  153. 'bx_repeat:items' => $aTmplVarsItems
  154. ));
  155. }
  156. public function getElementBlock($aParams = array())
  157. {
  158. $aParams['usage'] = BX_DOL_SCORE_USAGE_BLOCK;
  159. return $this->getElement($aParams);
  160. }
  161. public function getElementInline($aParams = array())
  162. {
  163. $aParams['usage'] = BX_DOL_SCORE_USAGE_INLINE;
  164. return $this->getElement($aParams);
  165. }
  166. public function getElement($aParams = array())
  167. {
  168. $aParams = array_merge($this->_aElementDefaults, $aParams);
  169. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  170. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  171. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  172. $iObjectId = $this->getId();
  173. $iAuthorId = $this->_getAuthorId();
  174. $iAuthorIp = $this->_getAuthorIp();
  175. $aScore = $this->_oQuery->getScore($iObjectId);
  176. $bCount = (int)$aScore['count_up'] != 0 || (int)$aScore['count_down'] != 0;
  177. $isAllowedVote = $this->isAllowedVote();
  178. $isAllowedVoteView = $this->isAllowedVoteView();
  179. $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId, $iAuthorIp) ? true : false;
  180. //--- Do Vote
  181. $bTmplVarsDoVote = $this->_isShowDoVote($aParams, $isAllowedVote, $bCount);
  182. $aTmplVarsDoVoteUp = $aTmplVarsDoVoteDown = array();
  183. if($bTmplVarsDoVote)
  184. $aTmplVarsDoVoteUp = array(
  185. 'style_prefix' => $this->_sStylePrefix,
  186. 'do_vote' => $this->_getDoVote(BX_DOL_SCORE_DO_UP, $aParams, $isAllowedVote),
  187. );
  188. if($bTmplVarsDoVote)
  189. $aTmplVarsDoVoteDown = array(
  190. 'style_prefix' => $this->_sStylePrefix,
  191. 'do_vote' => $this->_getDoVote(BX_DOL_SCORE_DO_DOWN, $aParams, $isAllowedVote),
  192. );
  193. //--- Counter
  194. $bTmplVarsCounter = $this->_isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  195. $aTmplVarsCounter = array();
  196. if($bTmplVarsCounter)
  197. $aTmplVarsCounter = array(
  198. 'style_prefix' => $this->_sStylePrefix,
  199. 'bx_if:show_hidden' => array(
  200. 'condition' => !$bShowCounterEmpty && !$bCount,
  201. 'content' => array()
  202. ),
  203. 'counter' => $this->getCounter(array_merge($aParams, [
  204. 'show_counter_only' => false,
  205. 'show_script' => false
  206. ]))
  207. );
  208. //--- Legend
  209. $bTmplVarsLegend = $this->_isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  210. $aTmplVarsLegend = array();
  211. if($bTmplVarsLegend)
  212. $aTmplVarsLegend = array(
  213. 'legend' => $this->getLegend($aParams)
  214. );
  215. if(!$bTmplVarsDoVote && !$bTmplVarsCounter && !$bTmplVarsLegend)
  216. return '';
  217. $sTmplName = $this->{'_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_SCORE_USAGE_DEFAULT)}();
  218. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  219. 'style_prefix' => $this->_sStylePrefix,
  220. 'html_id' => $this->_aHtmlIds['main'],
  221. 'class' => $this->_sStylePrefix . ($bShowDoVoteAsButton ? '-button' : '') . ($bShowDoVoteAsButtonSmall ? '-button-small' : ''),
  222. 'score' => $aScore['score'],
  223. 'cup' => $aScore['count_up'],
  224. 'cdown' => $aScore['count_down'],
  225. 'bx_if:show_do_vote_up' => array(
  226. 'condition' => $bTmplVarsDoVote,
  227. 'content' => $aTmplVarsDoVoteUp
  228. ),
  229. 'bx_if:show_counter' => array(
  230. 'condition' => $bTmplVarsCounter,
  231. 'content' => $aTmplVarsCounter
  232. ),
  233. 'bx_if:show_do_vote_down' => array(
  234. 'condition' => $bTmplVarsDoVote,
  235. 'content' => $aTmplVarsDoVoteDown
  236. ),
  237. 'bx_if:show_legend' => array(
  238. 'condition' => $bTmplVarsLegend,
  239. 'content' => $aTmplVarsLegend
  240. ),
  241. 'script' => $this->getJsScript($aParams)
  242. ));
  243. }
  244. public function getElementAPI($aParams = [])
  245. {
  246. if(!($this->_bApi = bx_is_api()))
  247. return;
  248. if(!$this->isEnabled())
  249. return bx_api_get_msg('_sys_score_err_not_enabled');
  250. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  251. $iObjectId = $this->getId();
  252. $iAuthorId = $this->_getAuthorId();
  253. $bCount = $this->_isCount();
  254. $isAllowedVote = $this->isAllowedVote();
  255. $isAllowedVoteView = $this->isAllowedVoteView();
  256. $aParams['is_voted'] = $this->isPerformed($iObjectId, $iAuthorId);
  257. $aParams['track'] = $aParams['is_voted'] ? $this->_getTrack($iObjectId, $iAuthorId) : [];
  258. //--- Do Vote
  259. $bDoVote = $this->_isShowDoVote($aParams, $isAllowedVote, $bCount);
  260. $aDoVoteUp = $bDoVote ? $this->_getDoVote(BX_DOL_SCORE_DO_UP, $aParams, $isAllowedVote) : [];
  261. $aDoVoteDown = $bDoVote ? $this->_getDoVote(BX_DOL_SCORE_DO_DOWN, $aParams, $isAllowedVote) : [];
  262. //--- Counter
  263. $bCounter = $this->_isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount);
  264. $aCounter = $bCounter ? $this->getCounterAPI(array_merge($aParams, [
  265. 'show_counter_only' => false,
  266. 'show_script' => false
  267. ])) : [];
  268. if(!$bDoVote && !$bCounter)
  269. return bx_api_get_msg('');
  270. return [
  271. 'type' => 'scores',
  272. 'system' => $this->_sSystem,
  273. 'object_id' => $this->_iId,
  274. 'params' => array_intersect_key($aParams, array_flip($this->_aElementParamsApi)),
  275. 'action' => [
  276. BX_DOL_SCORE_DO_UP => $aDoVoteUp,
  277. BX_DOL_SCORE_DO_DOWN => $aDoVoteDown
  278. ],
  279. 'counter' => $aCounter,
  280. ];
  281. }
  282. protected function _getDoVote($sType, $aParams = [], $isAllowedVote = true)
  283. {
  284. $bUndo = $this->isUndo();
  285. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  286. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  287. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  288. $bDisabled = !$isAllowedVote || ($bVoted && !$bUndo);
  289. $sClass = '';
  290. if($bShowDoVoteAsButton)
  291. $sClass = 'bx-btn';
  292. else if ($bShowDoVoteAsButtonSmall)
  293. $sClass = 'bx-btn bx-btn-small';
  294. if($bDisabled)
  295. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : 'bx-score-disabled';
  296. if($this->_bApi)
  297. return [
  298. 'is_undo' => $bUndo,
  299. 'is_voted' => $bVoted,
  300. 'is_disabled' => $bDisabled,
  301. 'title' => _t($this->_getTitleDo($sType)),
  302. ];
  303. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDo($sType, $aParams), array(
  304. 'class' => $this->_sStylePrefix . '-do-vote ' . $this->_sStylePrefix . '-dv-' . $sType . ' ' . $sClass,
  305. 'title' => _t($this->_getTitleDo($sType)),
  306. 'onclick' => !$bDisabled ? $this->getJsClick($sType) : ''
  307. ));
  308. }
  309. protected function _getCounterLabel($iCount, $aParams = array())
  310. {
  311. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  312. 'style_prefix' => $this->_sStylePrefix,
  313. 'bx_if:show_icon' => array(
  314. 'condition' => !isset($aParams['show_counter_label_icon']) || (bool)$aParams['show_counter_label_icon'] === true,
  315. 'content' => array(
  316. 'style_prefix' => $this->_sStylePrefix,
  317. 'icon' => $this->_oTemplate->getImageAuto($this->_getIconDo())
  318. )
  319. ),
  320. 'bx_if:show_text' => array(
  321. 'condition' => !isset($aParams['show_counter_label_text']) || (bool)$aParams['show_counter_label_text'] === true,
  322. 'content' => array(
  323. 'style_prefix' => $this->_sStylePrefix,
  324. 'text' => _t('_sys_score_counter', $iCount)
  325. )
  326. )
  327. ));
  328. }
  329. protected function _getLabelDo($sType, $aParams = array())
  330. {
  331. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  332. 'style_prefix' => $this->_sStylePrefix,
  333. 'bx_if:show_icon' => array(
  334. 'condition' => isset($aParams['show_do_vote_icon']) && (bool)$aParams['show_do_vote_icon'] === true,
  335. 'content' => array(
  336. 'style_prefix' => $this->_sStylePrefix,
  337. 'icon' => $this->_oTemplate->getImageAuto($this->_getIconDo($sType))
  338. )
  339. ),
  340. 'bx_if:show_text' => array(
  341. 'condition' => isset($aParams['show_do_vote_label']) && (bool)$aParams['show_do_vote_label'] === true,
  342. 'content' => array(
  343. 'style_prefix' => $this->_sStylePrefix,
  344. 'text' => _t($this->_getTitleDo($sType))
  345. )
  346. )
  347. ));
  348. }
  349. protected function _getVotedBy($aParams)
  350. {
  351. $aParams = array_merge($this->_aElementDefaults, $aParams, [
  352. 'show_do_vote_icon' => true,
  353. 'show_do_vote_label' => false
  354. ]);
  355. $aTmplUsers = [];
  356. $aUsers = $this->_oQuery->getPerformedBy($this->getId());
  357. foreach($aUsers as $aUser) {
  358. list($sUserName, $sUserLink, $sUserIcon, $sUserUnit, $sUserUnitWoInfo) = $this->_getAuthorInfo($aUser['id']);
  359. $aTmplUsers[] = [
  360. 'style_prefix' => $this->_sStylePrefix,
  361. 'user_name' => $sUserName,
  362. 'user_link' => $sUserLink,
  363. 'user_unit' => $sUserUnitWoInfo,
  364. 'vote' => $this->_getLabelDo($aUser['vote_type'], $aParams),
  365. 'date' => bx_time_js($aUser['vote_date']),
  366. ];
  367. }
  368. if(empty($aTmplUsers))
  369. $aTmplUsers = MsgBox(_t('_Empty'));
  370. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, [
  371. 'style_prefix' => $this->_sStylePrefix,
  372. 'bx_repeat:list' => $aTmplUsers
  373. ]);
  374. }
  375. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  376. {
  377. $bShowDoVote = !isset($aParams['show_do_vote']) || (bool)$aParams['show_do_vote'] === true;
  378. return $bShowDoVote && ($isAllowedVote || $bCount);
  379. }
  380. protected function _isShowCounter($aParams, $isAllowedVote, $isAllowedVoteView, $bCount)
  381. {
  382. return isset($aParams['show_counter']) && (bool)$aParams['show_counter'] === true && $isAllowedVoteView && ($isAllowedVote || $bCount);
  383. }
  384. protected function _isShowLegend($aParams, $isAllowedVote, $isAllowedVoteView, $bCount)
  385. {
  386. return isset($aParams['show_legend']) && (bool)$aParams['show_legend'] === true && $isAllowedVoteView;
  387. }
  388. }
  389. /** @} */