1
0

BxBaseVoteReactions.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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->_aElementParamsApi = array_merge($this->_aElementParamsApi, ['t']);
  43. $this->_sTmplNameBySummary = 'vote_by_summary_reactions.html';
  44. $this->_sTmplNameByList = 'vote_by_list_reactions.html';
  45. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('vote_do_vote_label_reactions.html');
  46. $this->_sTmplContentCounterLabel = $this->_oTemplate->getHtml('vote_counter_label_reactions.html');
  47. $this->_sTmplContentCounterWrapper = $this->_oTemplate->getHtml('vote_counter_wrapper_reactions.html');
  48. }
  49. public function getJsClick($iValue = 0)
  50. {
  51. if(empty($iValue))
  52. $iValue = $this->getValue();
  53. $sResult = '';
  54. if($this->_bQuickMode || $this->_bSingleMode)
  55. $sResult = $this->getJsClickDo($this->_aDataList[$this->_sDefault]['name'], $iValue);
  56. else
  57. $sResult = $this->getJsObjectName() . '.toggleDoPopup(this, ' . $iValue . ')';
  58. return $sResult;
  59. }
  60. public function getJsClickDo($sReaction, $iValue = 0)
  61. {
  62. if(empty($iValue))
  63. $iValue = $this->getValue();
  64. return $this->getJsObjectName() . '.vote(this, ' . $iValue . ', \'' . $sReaction . '\')';
  65. }
  66. public function getJsClickCounter($aParams = array())
  67. {
  68. $sJsObject = $this->getJsObjectName();
  69. $sJsMethod = 'toggleByPopup';
  70. if(isset($aParams['show_counter_style']) && in_array($aParams['show_counter_style'], array(self::$_sCounterStyleCompound, self::$_sCounterStyleSimple)))
  71. return $sJsObject . '.' . $sJsMethod . '(this)';
  72. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  73. return $sJsObject . '.' . $sJsMethod . '(this, \'' . $sReaction . '\')';
  74. }
  75. public function getCounter($aParams = [])
  76. {
  77. $aParams = array_merge($this->_aElementDefaults, $aParams);
  78. $sDefault = $aParams['show_counter_style'];
  79. $sCounterStyle = !empty($aParams['show_counter_style']) ? $aParams['show_counter_style'] : $sDefault;
  80. $sMethodPrefix = '_getCounter';
  81. $sMethod = $sMethodPrefix . bx_gen_method_name($sCounterStyle);
  82. if(!method_exists($this, $sMethod))
  83. $sMethod = $sMethodPrefix . bx_gen_method_name($sDefault);
  84. return $this->$sMethod($aParams);
  85. }
  86. public function getCounterAPI($aParams = [])
  87. {
  88. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  89. $aVote = $this->_getVote();
  90. $aReactions = $this->getReactions();
  91. $aItems = [];
  92. foreach($aReactions as $sName) {
  93. $iCount = (int)$aVote['count_' . $sName];
  94. $aItems[] = [
  95. 'name' => $sName,
  96. 'count' => $iCount,
  97. 'sum' => $aVote['sum_' . $sName],
  98. 'rate' => $aVote['rate_' . $sName]
  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. $aReactions = $this->getReactions();
  292. $aT = [];
  293. foreach($aReactions as $sName)
  294. $aT[$sName] = _t($this->_aDataList[$sName]['title']);
  295. return parent::getElementAPI(array_merge($aParams, [
  296. 't' => $aT
  297. ]));
  298. }
  299. public function getPerformedByAPI($aParams = [])
  300. {
  301. $aReactions = !empty($aParams['reaction']) ? [$aParams['reaction']] : $this->getReactions();
  302. $aResult = [];
  303. foreach($aReactions as $sReaction) {
  304. $aValues = $this->_oQuery->getPerformed(['type' => 'by', 'object_id' => $this->getId(), 'reaction'=> $sReaction]);
  305. $aTmplUsers = [];
  306. foreach($aValues as $mValue) {
  307. $mValue = is_array($mValue) ? $mValue : ['author_id' => (int)$mValue, 'reaction' => ''];
  308. $aTmplUsers[] = BxDolProfile::getData($mValue['author_id']);
  309. }
  310. $aResult[$sReaction] = $aTmplUsers;
  311. }
  312. return [
  313. 'performed_by' => $aResult
  314. ];
  315. }
  316. /**
  317. * Internal methods.
  318. */
  319. protected function _prepareParamsData($aParams)
  320. {
  321. return parent::_prepareParamsData(array_merge([
  322. 'bQuickMode' => $this->_bQuickMode,
  323. 'bSingleMode' => $this->_bSingleMode
  324. ], $aParams));
  325. }
  326. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  327. {
  328. $bResult = parent::_isShowDoVote($aParams, $isAllowedVote, $bCount);
  329. if(!$bResult)
  330. return $bResult;
  331. return $isAllowedVote;
  332. }
  333. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  334. {
  335. $bUndo = $this->isUndo();
  336. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  337. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  338. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  339. $bDisabled = !$isAllowedVote || ($bVoted && !$bUndo);
  340. $sClass = '';
  341. if($bShowDoVoteAsButton)
  342. $sClass = ' bx-btn';
  343. else if ($bShowDoVoteAsButtonSmall)
  344. $sClass = ' bx-btn bx-btn-small';
  345. $iValue = 0;
  346. $sReaction = '';
  347. $sJsClick = '';
  348. if(!$bDisabled) {
  349. if($bVoted && $bUndo) {
  350. $sClass = ' ' . $this->_sStylePrefix . '-voted' . $sClass;
  351. $iValue = $aParams['track']['value'];
  352. $sReaction = $aParams['track']['reaction'];
  353. $sJsClick = $this->getJsClickDo($sReaction, $iValue);
  354. }
  355. else {
  356. $iValue = $this->getValue();
  357. $sReaction = $this->_aDataList[$this->_sDefault]['name'];
  358. $sJsClick = $this->getJsClick($iValue);
  359. }
  360. }
  361. else
  362. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : ' ' . $this->_sStylePrefix . '-disabled';
  363. if($this->_bApi)
  364. return [
  365. 'is_undo' => $bUndo,
  366. 'is_voted' => $bVoted,
  367. 'is_disabled' => $bDisabled,
  368. 'value' => $iValue,
  369. 'reaction' => $bVoted ? $sReaction : $this->_sDefault,
  370. 'title' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track'])),
  371. ];
  372. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getDoVoteLabel($aParams), array(
  373. 'class' => $this->_sStylePrefix . '-do-vote' . $sClass,
  374. 'title' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track'])),
  375. 'onclick' => $sJsClick,
  376. 'bx-vote-reaction' => $sReaction,
  377. 'bx-vote-value' => $iValue,
  378. ));
  379. }
  380. protected function _getDoVoteLabel($aParams = array())
  381. {
  382. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  383. $aTrack = $bVoted ? $aParams['track'] : [];
  384. $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
  385. $aReaction = $this->getReaction($sReaction);
  386. $sUse = !empty($aReaction['use']) ? $aReaction['use'] : 'emoji';
  387. if(!$bVoted && !empty($aReaction['default']))
  388. $sUse = 'icon';
  389. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  390. 'style_prefix' => $this->_sStylePrefix,
  391. 'bx_if:show_icon' => [
  392. 'condition' => isset($aParams['show_do_vote_icon']) && (bool)$aParams['show_do_vote_icon'] === true,
  393. 'content' => [
  394. 'style_prefix' => $this->_sStylePrefix,
  395. 'bx_if:show_as_icon' => [
  396. 'condition' => $sUse == 'icon',
  397. 'content' => [
  398. 'name' => $sUse == 'icon' ? $this->_getIconDoWithTrack($bVoted, $aTrack) : '',
  399. ]
  400. ],
  401. 'bx_if:show_as_emoji' => [
  402. 'condition' => $sUse == 'emoji',
  403. 'content' => [
  404. 'emoji' => $sUse == 'emoji' ? $this->_getEmojiDoWithTrack($bVoted, $aTrack) : '',
  405. ]
  406. ],
  407. 'bx_if:show_as_image' => [
  408. 'condition' => $sUse == 'image',
  409. 'content' => [
  410. 'image' => $sUse == 'image' ? $this->_getImageDoWithTrack($bVoted, $aTrack) : '',
  411. ]
  412. ],
  413. ]
  414. ],
  415. 'bx_if:show_text' => array(
  416. 'condition' => isset($aParams['show_do_vote_label']) && (bool)$aParams['show_do_vote_label'] === true,
  417. 'content' => array(
  418. 'style_prefix' => $this->_sStylePrefix,
  419. 'text' => _t($this->_getTitleDoWithTrack($bVoted, $aTrack))
  420. )
  421. )
  422. ));
  423. }
  424. public function _getDoVoteMenu($iValue = 0)
  425. {
  426. if(empty($iValue))
  427. $iValue = $this->getValue();
  428. $oMenu = BxTemplMenu::getObjectInstance($this->_sMenuDoVote);
  429. if(!$oMenu)
  430. return false;
  431. $oMenu->setParams(array(
  432. 'object' => &$this,
  433. 'value' => $iValue,
  434. ));
  435. return $oMenu;
  436. }
  437. public function _getDoVotePopup($iValue = 0)
  438. {
  439. $oMenu = $this->_getDoVoteMenu($iValue);
  440. if($oMenu === false)
  441. return '';
  442. return $oMenu->getCode();
  443. }
  444. protected function _getCounterLabel($iCount, $aParams = array())
  445. {
  446. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  447. $aReaction = $this->getReaction($sReaction);
  448. $sUse = !empty($aReaction['use']) ? $aReaction['use'] : 'emoji';
  449. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  450. 'style_prefix' => $this->_sStylePrefix,
  451. 'bx_if:show_icon' => array(
  452. 'condition' => !isset($aParams['show_counter_label_icon']) || (bool)$aParams['show_counter_label_icon'] === true,
  453. 'content' => array(
  454. 'style_prefix' => $this->_sStylePrefix,
  455. 'bx_if:show_as_icon' => [
  456. 'condition' => $sUse == 'icon',
  457. 'content' => [
  458. 'name' => $sUse == 'icon' ? $this->getIcon($sReaction) : '',
  459. ]
  460. ],
  461. 'bx_if:show_as_emoji' => [
  462. 'condition' => $sUse == 'emoji',
  463. 'content' => [
  464. 'emoji' => $sUse == 'emoji' ? $this->getEmoji($sReaction) : '',
  465. ]
  466. ],
  467. 'bx_if:show_as_image' => [
  468. 'condition' => $sUse == 'image',
  469. 'content' => [
  470. 'image' => $sUse == 'image' ? $this->getImage($sReaction) : '',
  471. ]
  472. ],
  473. 'title_attr' => bx_html_attribute(_t($this->_aDataList[$sReaction]['title'])),
  474. )
  475. ),
  476. 'bx_if:show_text' => array(
  477. 'condition' => !isset($aParams['show_counter_label_text']) || (bool)$aParams['show_counter_label_text'] === true,
  478. 'content' => array(
  479. 'style_prefix' => $this->_sStylePrefix,
  480. 'text' => parent::_getCounterLabel($iCount)
  481. )
  482. )
  483. ));
  484. }
  485. protected function _getVotedBy($aParams = array())
  486. {
  487. if(!isset($aParams['reaction']))
  488. return $this->_getVotedBySummary($aParams);
  489. $aReactions = $this->getReactions(true);
  490. $bSummary = $aParams['reaction'] == 'summary';
  491. $aBrowseParams = array('type' => 'by', 'object_id' => $this->getId());
  492. if(!$bSummary)
  493. $aBrowseParams['reaction'] = $aParams['reaction'];
  494. $aValues = $this->_oQuery->getPerformed($aBrowseParams);
  495. $aTmplUsers = array();
  496. foreach($aValues as $mValue) {
  497. $mValue = is_array($mValue) ? $mValue : array('author_id' => (int)$mValue, 'reaction' => '');
  498. $aTmplReaction = [];
  499. if($bSummary) {
  500. $sUse = !empty($aReactions[$mValue['reaction']]['use']) ? $aReactions[$mValue['reaction']]['use'] : 'emoji';
  501. $aTmplReaction = [
  502. 'style_prefix' => $this->_sStylePrefix,
  503. 'bx_if:show_as_icon' => [
  504. 'condition' => $sUse == 'icon',
  505. 'content' => [
  506. 'name' => $sUse == 'icon' ? $this->getIcon($mValue['reaction']) : '',
  507. ]
  508. ],
  509. 'bx_if:show_as_emoji' => [
  510. 'condition' => $sUse == 'emoji',
  511. 'content' => [
  512. 'emoji' => $sUse == 'emoji' ? $this->getEmoji($mValue['reaction']) : '',
  513. ]
  514. ],
  515. 'bx_if:show_as_image' => [
  516. 'condition' => $sUse == 'image',
  517. 'content' => [
  518. 'image' => $sUse == 'image' ? $this->getImage($mValue['reaction']) : '',
  519. ]
  520. ],
  521. ];
  522. }
  523. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($mValue['author_id']);
  524. $aTmplUsers[] = array(
  525. 'style_prefix' => $this->_sStylePrefix,
  526. 'user_unit' => $sUserUnit,
  527. 'bx_if:show_reaction' => array(
  528. 'condition' => $bSummary,
  529. 'content' => $aTmplReaction
  530. )
  531. );
  532. }
  533. if(empty($aTmplUsers))
  534. $aTmplUsers = MsgBox(_t('_Empty'));
  535. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  536. 'style_prefix' => $this->_sStylePrefix,
  537. 'class' => ' ' . $this->_sStylePrefix . '-bl-' . $aParams['reaction'],
  538. 'bx_repeat:list' => $aTmplUsers
  539. ));
  540. }
  541. protected function _getVotedBySummary($aParams = array())
  542. {
  543. $sJsObject = $this->getJsObjectName();
  544. $sTxtSummary = _t('_vote_do_by_summary');
  545. $aReactions = array_merge(['summary' => []], $this->getReactions(true));
  546. $aMenuItems = array();
  547. $aTmplVarsLists = array();
  548. foreach ($aReactions as $sReaction => $aReaction) {
  549. if($sReaction == $this->_sDefault)
  550. continue;
  551. $bSummary = $sReaction == 'summary';
  552. $sUse = !empty($aReaction['use']) ? $aReaction['use'] : 'emoji';
  553. $sIcon = '';
  554. switch($sUse) {
  555. case 'icon':
  556. $sIcon = $this->_oTemplate->parseIcon($this->getIcon($sReaction));
  557. break;
  558. case 'emoji':
  559. $sIcon = $this->getEmoji($sReaction);
  560. break;
  561. case 'image':
  562. $sIcon = $this->getImage($sReaction);
  563. break;
  564. }
  565. $sTitle = !$bSummary ? $sIcon : $sTxtSummary;
  566. $sTitleAttr = !$bSummary ? _t('_vote_do_by_x_reaction', _t($this->_aDataList[$sReaction]['title'])) : $sTxtSummary;
  567. $sName = $this->_sStylePrefix . '-' . $sReaction;
  568. $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);
  569. $aTmplVarsLists[] = array(
  570. 'content' => $this->_getVotedBy(array('reaction' => $sReaction))
  571. );
  572. }
  573. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sStylePrefix . '-voted-by', 'menu_items' => $aMenuItems));
  574. $oMenu->setSelected('', $this->_sStylePrefix . '-summary');
  575. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameBySummary, array(
  576. 'style_prefix' => $this->_sStylePrefix,
  577. 'title' => '',
  578. 'menu' => $oMenu->getCode(),
  579. 'bx_repeat:lists' => $aTmplVarsLists
  580. ));
  581. }
  582. protected function _getTmplContentCounterWrapper()
  583. {
  584. return $this->_sTmplContentCounterWrapper;
  585. }
  586. }
  587. /** @} */