BxBaseVoteReactions.php 27 KB

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