BxBaseVoteReactions.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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, BxDolVoteLikes
  11. */
  12. class BxBaseVoteReactions extends BxDolVoteReactions
  13. {
  14. protected $_sTmplNameBySummary;
  15. protected $_sTmplContentCounterWrapper = '';
  16. public function __construct($sSystem, $iId, $iInit = true, $oTemplate = false)
  17. {
  18. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  19. $this->_sJsClsName .= 'Reactions';
  20. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  21. $this->_aHtmlIds = array_merge($this->_aHtmlIds, array(
  22. 'main' => 'bx-vr-' . $sHtmlId,
  23. 'do_menu' => 'bx-vr-do-menu-' . $sHtmlId,
  24. 'do_popup' => 'bx-vr-do-popup-' . $sHtmlId
  25. ));
  26. $this->_aElementDefaults = [
  27. 'show_do_vote_as_button' => false,
  28. 'show_do_vote_as_button_small' => false,
  29. 'show_do_vote_icon' => true,
  30. 'show_do_vote_label' => false,
  31. 'show_counter' => false,
  32. 'show_counter_only' => true,
  33. 'show_counter_empty' => true,
  34. 'show_counter_style' => self::$_sCounterStyleDivided, //--- Alloved styles are 'simple', 'divided' and 'compound'
  35. 'show_legend' => false,
  36. 'show_script' => true
  37. ];
  38. $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
  39. 'show_counter' => true,
  40. ]);
  41. $this->_sTmplNameBySummary = 'vote_by_summary_reactions.html';
  42. $this->_sTmplNameByList = 'vote_by_list_reactions.html';
  43. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('vote_do_vote_label_reactions.html');
  44. $this->_sTmplContentCounterLabel = $this->_oTemplate->getHtml('vote_counter_label_reactions.html');
  45. $this->_sTmplContentCounterWrapper = $this->_oTemplate->getHtml('vote_counter_wrapper_reactions.html');
  46. }
  47. public function getJsClick($iValue = 0)
  48. {
  49. if(empty($iValue))
  50. $iValue = $this->getValue();
  51. $sResult = '';
  52. if($this->_bQuickMode)
  53. $sResult = $this->getJsClickDo($this->_aDataList[$this->_sDefault]['name'], $iValue);
  54. else
  55. $sResult = $this->getJsObjectName() . '.toggleDoPopup(this, ' . $iValue . ')';
  56. return $sResult;
  57. }
  58. public function getJsClickDo($sReaction, $iValue = 0)
  59. {
  60. if(empty($iValue))
  61. $iValue = $this->getValue();
  62. return $this->getJsObjectName() . '.vote(this, ' . $iValue . ', \'' . $sReaction . '\')';
  63. }
  64. public function getJsClickCounter($aParams = array())
  65. {
  66. $sJsObject = $this->getJsObjectName();
  67. $sJsMethod = 'toggleByPopup';
  68. if(isset($aParams['show_counter_style']) && in_array($aParams['show_counter_style'], array(self::$_sCounterStyleCompound, self::$_sCounterStyleSimple)))
  69. return $sJsObject . '.' . $sJsMethod . '(this)';
  70. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  71. return $sJsObject . '.' . $sJsMethod . '(this, \'' . $sReaction . '\')';
  72. }
  73. public function getCounter($aParams = [])
  74. {
  75. $aParams = array_merge($this->_aElementDefaults, $aParams);
  76. $sDefault = $aParams['show_counter_style'];
  77. $sCounterStyle = !empty($aParams['show_counter_style']) ? $aParams['show_counter_style'] : $sDefault;
  78. $sMethodPrefix = '_getCounter';
  79. $sMethod = $sMethodPrefix . bx_gen_method_name($sCounterStyle);
  80. if(!method_exists($this, $sMethod))
  81. $sMethod = $sMethodPrefix . bx_gen_method_name($sDefault);
  82. return $this->$sMethod($aParams);
  83. }
  84. public function getCounterAPI($aParams = [])
  85. {
  86. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  87. $aVote = $this->_getVote();
  88. $aReactions = $this->getReactions();
  89. $aItems = [];
  90. foreach($aReactions as $sName) {
  91. $iCount = (int)$aVote['count_' . $sName];
  92. $aItems[] = [
  93. 'name' => $sName,
  94. 'count' => $iCount,
  95. 'sum' => $aVote['sum_' . $sName],
  96. 'rate' => $aVote['rate_' . $sName],
  97. 'icon' => $this->getEmoji($sName),
  98. 'title' => _t($this->_aDataList[$sName]['title']),
  99. ];
  100. }
  101. $sStyle = !empty($aParams['show_counter_style']) ? $aParams['show_counter_style'] : $this->_aElementDefaults['show_counter_style'];
  102. return ['type' => $sStyle, 'items' => $aItems];
  103. }
  104. public function _getCounterSimple($aParams = array())
  105. {
  106. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  107. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  108. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  109. $bVote = $this->_isVote();
  110. $aVote = $this->_getVote();
  111. $aReactions = $this->getReactions();
  112. $sClass = isset($aParams['class_counter']) ? $aParams['class_counter'] : '';
  113. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  114. $sClass .= ' bx-btn-small-height';
  115. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  116. $sClass .= ' bx-btn-height';
  117. $aParams['id_counter'] = '';
  118. $iResultC = $iResultS = 0;
  119. foreach($aReactions as $sName) {
  120. $iResultC += (int)$aVote['count_' . $sName];
  121. $iResultS += (int)$aVote['sum_' . $sName];
  122. }
  123. $aParams = array_merge($aParams, array(
  124. 'show_counter_active' => false,
  125. 'show_counter_label_icon' => false,
  126. 'show_counter_label_text' => true,
  127. 'class_counter' => ' total-count ' . $sClass,
  128. 'reaction' => '',
  129. 'vote' => array(
  130. 'count' => $iResultC,
  131. 'sum' => $iResultS,
  132. 'rate' => $iResultC > 0 ? round($iResultS / $iResultC, 2) : 0,
  133. )
  134. ));
  135. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterWrapper(), array(
  136. 'html_id' => $this->_aHtmlIds['counter'],
  137. 'style_prefix' => $this->_sStylePrefix,
  138. 'class' => $this->_aHtmlIds['counter'] . (!$bVote && !$bShowCounterEmpty ? ' bx-vc-hidden' : ''),
  139. 'type' => $this->_sType,
  140. 'style' => self::$_sCounterStyleSimple,
  141. 'bx_if:show_link' => array(
  142. 'condition' => true,
  143. 'content' => array(
  144. 'href' => 'javascript:void(0)',
  145. 'onclick' => 'javascript:' . $this->getJsClickCounter($aParams),
  146. 'title' => bx_html_attribute($this->_getTitleDoBy($aParams)),
  147. 'counter' => parent::getCounter(array_merge($aParams, array('show_script' => false))),
  148. )
  149. ),
  150. 'bx_if:show_text' => array(
  151. 'condition' => false,
  152. 'content' => array()
  153. ),
  154. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  155. ));
  156. }
  157. public function _getCounterDivided($aParams = array())
  158. {
  159. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  160. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  161. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  162. $bVote = $this->_isVote();
  163. $aVote = $this->_getVote();
  164. $aReactions = $this->getReactions();
  165. $sClass = isset($aParams['class_counter']) ? $aParams['class_counter'] : '';
  166. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  167. $sClass .= ' bx-btn-small-height';
  168. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  169. $sClass .= ' bx-btn-height';
  170. $sClass .= ' bx-def-margin-sec-right';
  171. $aParams['id_counter'] = '';
  172. $sResult = '';
  173. foreach($aReactions as $sName) {
  174. $iCount = (int)$aVote['count_' . $sName];
  175. $aParams['class_counter'] = ($iCount == 0 && (!$bShowCounterEmpty || $sName != $this->_sDefault || $bVote) ? ' bx-vc-hidden' : '') . ' ' . $sName . ' ' . $sClass;
  176. $aParams['reaction'] = $sName;
  177. $aParams['vote'] = array(
  178. 'count' => $iCount,
  179. 'sum' => $aVote['sum_' . $sName],
  180. 'rate' => $aVote['rate_' . $sName],
  181. );
  182. $sResult .= parent::getCounter(array_merge($aParams, array('show_script' => false)));
  183. }
  184. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterWrapper(), array(
  185. 'html_id' => $this->_aHtmlIds['counter'],
  186. 'style_prefix' => $this->_sStylePrefix,
  187. 'class' => $this->_aHtmlIds['counter'] . (!$bVote && !$bShowCounterEmpty ? ' bx-vc-hidden' : ''),
  188. 'type' => $this->_sType,
  189. 'style' => self::$_sCounterStyleDivided,
  190. 'bx_if:show_link' => array(
  191. 'condition' => false,
  192. 'content' => array()
  193. ),
  194. 'bx_if:show_text' => array(
  195. 'condition' => true,
  196. 'content' => array(
  197. 'counter' => $sResult,
  198. )
  199. ),
  200. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  201. ));
  202. }
  203. public function _getCounterCompound($aParams = array())
  204. {
  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. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  208. $bVote = $this->_isVote();
  209. $aVote = $this->_getVote();
  210. $aReactions = $this->getReactions();
  211. $sClass = isset($aParams['class_counter']) ? $aParams['class_counter'] : '';
  212. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  213. $sClass .= ' bx-btn-small-height';
  214. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  215. $sClass .= ' bx-btn-height';
  216. $aParams['id_counter'] = '';
  217. $sResult = '';
  218. $iResultC = $iResultS = 0;
  219. foreach($aReactions as $sName) {
  220. $iCount = (int)$aVote['count_' . $sName];
  221. $aParams = array_merge($aParams, array(
  222. 'show_counter_active' => false,
  223. 'show_counter_label_icon' => true,
  224. 'show_counter_label_text' => false,
  225. 'class_counter' => ($iCount == 0 && (!$bShowCounterEmpty || $sName != $this->_sDefault || $bVote) ? ' bx-vc-hidden' : '') . ' ' . $sName . ' ' . $sClass,
  226. 'reaction' => $sName,
  227. 'vote' => array(
  228. 'count' => $iCount,
  229. 'sum' => $aVote['sum_' . $sName],
  230. 'rate' => $aVote['rate_' . $sName],
  231. )
  232. ));
  233. $iResultC += $iCount;
  234. $iResultS += (int)$aVote['sum_' . $sName];
  235. $sResult .= trim(parent::getCounter(array_merge($aParams, [
  236. 'show_script' => false
  237. ])));
  238. }
  239. $aParams = array_merge($aParams, array(
  240. 'show_counter_active' => false,
  241. 'show_counter_label_icon' => false,
  242. 'show_counter_label_text' => true,
  243. 'class_counter' => ' total-count ' . $sClass,
  244. 'reaction' => '',
  245. 'vote' => array(
  246. 'count' => $iResultC,
  247. 'sum' => $iResultS,
  248. 'rate' => $iResultC > 0 ? round($iResultS / $iResultC, 2) : 0,
  249. )
  250. ));
  251. $sResult .= parent::getCounter(array_merge($aParams, array('show_script' => false)));
  252. $sClassWrapper = 'sys-action-counter';
  253. if(isset($aParams['show_counter_only']) && (bool)$aParams['show_counter_only'] === true)
  254. $sClassWrapper .= ' sys-ac-only';
  255. $sClassWrapper .= ' ' . $this->_aHtmlIds['counter'] . (!$bVote && !$bShowCounterEmpty ? ' bx-vc-hidden' : '');
  256. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterWrapper(), array(
  257. 'html_id' => $this->_aHtmlIds['counter'],
  258. 'style_prefix' => $this->_sStylePrefix,
  259. 'class' => $sClassWrapper,
  260. 'type' => $this->_sType,
  261. 'style' => self::$_sCounterStyleCompound,
  262. 'bx_if:show_link' => array(
  263. 'condition' => true,
  264. 'content' => array(
  265. 'href' => 'javascript:void(0)',
  266. 'onclick' => 'javascript:' . $this->getJsClickCounter($aParams),
  267. 'title' => bx_html_attribute($this->_getTitleDoBy($aParams)),
  268. 'counter' => $sResult,
  269. )
  270. ),
  271. 'bx_if:show_text' => array(
  272. 'condition' => false,
  273. 'content' => array()
  274. ),
  275. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  276. ));
  277. }
  278. public function getElement($aParams = array())
  279. {
  280. $sClass = $this->_sStylePrefix . '-' . $this->_sType;
  281. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  282. $sClass .= '-button-small';
  283. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  284. $sClass .= '-button';
  285. $aParams['class_element'] = isset($aParams['class_element']) ? $aParams['class_element'] : '';
  286. $aParams['class_element'] .= ' ' . $sClass;
  287. return parent::getElement($aParams);
  288. }
  289. public function getElementAPI($aParams = [])
  290. {
  291. $aResult = parent::getElementAPI($aParams);
  292. if(($oDoVoteMenu = $this->_getDoVoteMenu()) !== false)
  293. $aResult['action']['menu'] = $oDoVoteMenu->getCodeAPI();
  294. return $aResult;
  295. }
  296. /**
  297. * Internal methods.
  298. */
  299. protected function _prepareParamsData($aParams)
  300. {
  301. return parent::_prepareParamsData(array_merge([
  302. 'bQuickMode' => $this->_bQuickMode
  303. ], $aParams));
  304. }
  305. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  306. {
  307. $bResult = parent::_isShowDoVote($aParams, $isAllowedVote, $bCount);
  308. if(!$bResult)
  309. return $bResult;
  310. return $isAllowedVote || $bCount;
  311. }
  312. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  313. {
  314. $bUndo = $this->isUndo();
  315. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  316. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  317. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  318. $bDisabled = !$isAllowedVote || ($bVoted && !$bUndo);
  319. $sClass = '';
  320. if($bShowDoVoteAsButton)
  321. $sClass = ' bx-btn';
  322. else if ($bShowDoVoteAsButtonSmall)
  323. $sClass = ' bx-btn bx-btn-small';
  324. $iValue = 0;
  325. $sReaction = '';
  326. $sJsClick = '';
  327. if(!$bDisabled) {
  328. if($bVoted && $bUndo) {
  329. $sClass = ' ' . $this->_sStylePrefix . '-voted' . $sClass;
  330. $iValue = $aParams['track']['value'];
  331. $sReaction = $aParams['track']['reaction'];
  332. $sJsClick = $this->getJsClickDo($sReaction, $iValue);
  333. }
  334. else {
  335. $iValue = $this->getValue();
  336. $sReaction = $this->_aDataList[$this->_sDefault]['name'];
  337. $sJsClick = $this->getJsClick($iValue);
  338. }
  339. }
  340. else
  341. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : ' ' . $this->_sStylePrefix . '-disabled';
  342. if($this->_bApi)
  343. return [
  344. 'is_undo' => $bUndo,
  345. 'is_voted' => $bVoted,
  346. 'is_disabled' => $bDisabled,
  347. 'value' => $iValue,
  348. 'reaction' => $sReaction,
  349. 'icon' => ($sEmoji = $this->getEmoji($sReaction)) != '' ? $sEmoji : '',
  350. 'title' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track'])),
  351. ];
  352. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getDoVoteLabel($aParams), array(
  353. 'class' => $this->_sStylePrefix . '-do-vote' . $sClass,
  354. 'title' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track'])),
  355. 'onclick' => $sJsClick,
  356. 'bx-vote-reaction' => $sReaction,
  357. 'bx-vote-value' => $iValue,
  358. ));
  359. }
  360. protected function _getDoVoteLabel($aParams = array())
  361. {
  362. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  363. $aTrack = $bVoted ? $aParams['track'] : [];
  364. $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
  365. $aReaction = $this->getReaction($sReaction);
  366. $sUse = !empty($aReaction['use']) ? $aReaction['use'] : 'emoji';
  367. if(!$bVoted && !empty($aReaction['default']))
  368. $sUse = 'icon';
  369. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  370. 'style_prefix' => $this->_sStylePrefix,
  371. 'bx_if:show_icon' => [
  372. 'condition' => isset($aParams['show_do_vote_icon']) && (bool)$aParams['show_do_vote_icon'] === true,
  373. 'content' => [
  374. 'style_prefix' => $this->_sStylePrefix,
  375. 'bx_if:show_as_icon' => [
  376. 'condition' => $sUse == 'icon',
  377. 'content' => [
  378. 'name' => $sUse == 'icon' ? $this->_getIconDoWithTrack($bVoted, $aTrack) : '',
  379. ]
  380. ],
  381. 'bx_if:show_as_emoji' => [
  382. 'condition' => $sUse == 'emoji',
  383. 'content' => [
  384. 'emoji' => $sUse == 'emoji' ? $this->_getEmojiDoWithTrack($bVoted, $aTrack) : '',
  385. ]
  386. ],
  387. 'bx_if:show_as_image' => [
  388. 'condition' => $sUse == 'image',
  389. 'content' => [
  390. 'image' => $sUse == 'image' ? $this->_getImageDoWithTrack($bVoted, $aTrack) : '',
  391. ]
  392. ],
  393. ]
  394. ],
  395. 'bx_if:show_text' => array(
  396. 'condition' => isset($aParams['show_do_vote_label']) && (bool)$aParams['show_do_vote_label'] === true,
  397. 'content' => array(
  398. 'style_prefix' => $this->_sStylePrefix,
  399. 'text' => _t($this->_getTitleDoWithTrack($bVoted, $aTrack))
  400. )
  401. )
  402. ));
  403. }
  404. public function _getDoVoteMenu($iValue = 0)
  405. {
  406. if(empty($iValue))
  407. $iValue = $this->getValue();
  408. $oMenu = BxTemplMenu::getObjectInstance($this->_sMenuDoVote);
  409. if(!$oMenu)
  410. return false;
  411. $oMenu->setParams(array(
  412. 'object' => &$this,
  413. 'value' => $iValue,
  414. ));
  415. return $oMenu;
  416. }
  417. public function _getDoVotePopup($iValue = 0)
  418. {
  419. $oMenu = $this->_getDoVoteMenu($iValue);
  420. if($oMenu === false)
  421. return '';
  422. return $oMenu->getCode();
  423. }
  424. protected function _getCounterLabel($iCount, $aParams = array())
  425. {
  426. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  427. $aReaction = $this->getReaction($sReaction);
  428. $sUse = !empty($aReaction['use']) ? $aReaction['use'] : 'emoji';
  429. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  430. 'style_prefix' => $this->_sStylePrefix,
  431. 'bx_if:show_icon' => array(
  432. 'condition' => !isset($aParams['show_counter_label_icon']) || (bool)$aParams['show_counter_label_icon'] === true,
  433. 'content' => array(
  434. 'style_prefix' => $this->_sStylePrefix,
  435. 'bx_if:show_as_icon' => [
  436. 'condition' => $sUse == 'icon',
  437. 'content' => [
  438. 'name' => $sUse == 'icon' ? $this->getIcon($sReaction) : '',
  439. ]
  440. ],
  441. 'bx_if:show_as_emoji' => [
  442. 'condition' => $sUse == 'emoji',
  443. 'content' => [
  444. 'emoji' => $sUse == 'emoji' ? $this->getEmoji($sReaction) : '',
  445. ]
  446. ],
  447. 'bx_if:show_as_image' => [
  448. 'condition' => $sUse == 'image',
  449. 'content' => [
  450. 'image' => $sUse == 'image' ? $this->getImage($sReaction) : '',
  451. ]
  452. ],
  453. 'title_attr' => bx_html_attribute(_t($this->_aDataList[$sReaction]['title'])),
  454. )
  455. ),
  456. 'bx_if:show_text' => array(
  457. 'condition' => !isset($aParams['show_counter_label_text']) || (bool)$aParams['show_counter_label_text'] === true,
  458. 'content' => array(
  459. 'style_prefix' => $this->_sStylePrefix,
  460. 'text' => parent::_getCounterLabel($iCount)
  461. )
  462. )
  463. ));
  464. }
  465. protected function _getVotedBy($aParams = array())
  466. {
  467. if(!isset($aParams['reaction']))
  468. return $this->_getVotedBySummary($aParams);
  469. $aReactions = $this->getReactions(true);
  470. $bSummary = $aParams['reaction'] == 'summary';
  471. $aBrowseParams = array('type' => 'by', 'object_id' => $this->getId());
  472. if(!$bSummary)
  473. $aBrowseParams['reaction'] = $aParams['reaction'];
  474. $aValues = $this->_oQuery->getPerformed($aBrowseParams);
  475. $aTmplUsers = array();
  476. foreach($aValues as $mValue) {
  477. $mValue = is_array($mValue) ? $mValue : array('author_id' => (int)$mValue, 'reaction' => '');
  478. $aTmplReaction = [];
  479. if($bSummary) {
  480. $sUse = !empty($aReactions[$mValue['reaction']]['use']) ? $aReactions[$mValue['reaction']]['use'] : 'emoji';
  481. $aTmplReaction = [
  482. 'style_prefix' => $this->_sStylePrefix,
  483. 'bx_if:show_as_icon' => [
  484. 'condition' => $sUse == 'icon',
  485. 'content' => [
  486. 'name' => $sUse == 'icon' ? $this->getIcon($mValue['reaction']) : '',
  487. ]
  488. ],
  489. 'bx_if:show_as_emoji' => [
  490. 'condition' => $sUse == 'emoji',
  491. 'content' => [
  492. 'emoji' => $sUse == 'emoji' ? $this->getEmoji($mValue['reaction']) : '',
  493. ]
  494. ],
  495. 'bx_if:show_as_image' => [
  496. 'condition' => $sUse == 'image',
  497. 'content' => [
  498. 'image' => $sUse == 'image' ? $this->getImage($mValue['reaction']) : '',
  499. ]
  500. ],
  501. ];
  502. }
  503. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($mValue['author_id']);
  504. $aTmplUsers[] = array(
  505. 'style_prefix' => $this->_sStylePrefix,
  506. 'user_unit' => $sUserUnit,
  507. 'bx_if:show_reaction' => array(
  508. 'condition' => $bSummary,
  509. 'content' => $aTmplReaction
  510. )
  511. );
  512. }
  513. if(empty($aTmplUsers))
  514. $aTmplUsers = MsgBox(_t('_Empty'));
  515. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  516. 'style_prefix' => $this->_sStylePrefix,
  517. 'class' => ' ' . $this->_sStylePrefix . '-bl-' . $aParams['reaction'],
  518. 'bx_repeat:list' => $aTmplUsers
  519. ));
  520. }
  521. protected function _getVotedBySummary($aParams = array())
  522. {
  523. $sJsObject = $this->getJsObjectName();
  524. $sTxtSummary = _t('_vote_do_by_summary');
  525. $aReactions = array_merge(['summary' => []], $this->getReactions(true));
  526. $aMenuItems = array();
  527. $aTmplVarsLists = array();
  528. foreach ($aReactions as $sReaction => $aReaction) {
  529. if($sReaction == $this->_sDefault)
  530. continue;
  531. $bSummary = $sReaction == 'summary';
  532. $sUse = !empty($aReaction['use']) ? $aReaction['use'] : 'emoji';
  533. $sIcon = '';
  534. switch($sUse) {
  535. case 'icon':
  536. $sIcon = $this->_oTemplate->parseIcon($this->getIcon($sReaction));
  537. break;
  538. case 'emoji':
  539. $sIcon = $this->getEmoji($sReaction);
  540. break;
  541. case 'image':
  542. $sIcon = $this->getImage($sReaction);
  543. break;
  544. }
  545. $sTitle = !$bSummary ? $sIcon : $sTxtSummary;
  546. $sTitleAttr = !$bSummary ? _t('_vote_do_by_x_reaction', _t($this->_aDataList[$sReaction]['title'])) : $sTxtSummary;
  547. $sName = $this->_sStylePrefix . '-' . $sReaction;
  548. $aMenuItems[] = array('id' => $sName, 'name' => $sName, 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:' . $sJsObject . '.changeVotedBy(this, \'' . $sReaction . '\')', 'target' => '_self', 'title' => $sTitle, 'title_attr' => $sTitleAttr, 'active' => 1);
  549. $aTmplVarsLists[] = array(
  550. 'content' => $this->_getVotedBy(array('reaction' => $sReaction))
  551. );
  552. }
  553. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sStylePrefix . '-voted-by', 'menu_items' => $aMenuItems));
  554. $oMenu->setSelected('', $this->_sStylePrefix . '-summary');
  555. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameBySummary, array(
  556. 'style_prefix' => $this->_sStylePrefix,
  557. 'title' => '',
  558. 'menu' => $oMenu->getCode(),
  559. 'bx_repeat:lists' => $aTmplVarsLists
  560. ));
  561. }
  562. protected function _getTmplContentCounterWrapper()
  563. {
  564. return $this->_sTmplContentCounterWrapper;
  565. }
  566. }
  567. /** @} */