BxBaseVoteReactions.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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)
  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. ], $aParams));
  324. }
  325. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  326. {
  327. $bResult = parent::_isShowDoVote($aParams, $isAllowedVote, $bCount);
  328. if(!$bResult)
  329. return $bResult;
  330. return $isAllowedVote;
  331. }
  332. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  333. {
  334. $bUndo = $this->isUndo();
  335. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  336. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  337. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  338. $bDisabled = !$isAllowedVote || ($bVoted && !$bUndo);
  339. $sClass = '';
  340. if($bShowDoVoteAsButton)
  341. $sClass = ' bx-btn';
  342. else if ($bShowDoVoteAsButtonSmall)
  343. $sClass = ' bx-btn bx-btn-small';
  344. $iValue = 0;
  345. $sReaction = '';
  346. $sJsClick = '';
  347. if(!$bDisabled) {
  348. if($bVoted && $bUndo) {
  349. $sClass = ' ' . $this->_sStylePrefix . '-voted' . $sClass;
  350. $iValue = $aParams['track']['value'];
  351. $sReaction = $aParams['track']['reaction'];
  352. $sJsClick = $this->getJsClickDo($sReaction, $iValue);
  353. }
  354. else {
  355. $iValue = $this->getValue();
  356. $sReaction = $this->_aDataList[$this->_sDefault]['name'];
  357. $sJsClick = $this->getJsClick($iValue);
  358. }
  359. }
  360. else
  361. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : ' ' . $this->_sStylePrefix . '-disabled';
  362. if($this->_bApi)
  363. return [
  364. 'is_undo' => $bUndo,
  365. 'is_voted' => $bVoted,
  366. 'is_disabled' => $bDisabled,
  367. 'value' => $iValue,
  368. 'reaction' => $bVoted ? $sReaction : $this->_sDefault,
  369. 'title' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track'])),
  370. ];
  371. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getDoVoteLabel($aParams), array(
  372. 'class' => $this->_sStylePrefix . '-do-vote' . $sClass,
  373. 'title' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track'])),
  374. 'onclick' => $sJsClick,
  375. 'bx-vote-reaction' => $sReaction,
  376. 'bx-vote-value' => $iValue,
  377. ));
  378. }
  379. protected function _getDoVoteLabel($aParams = array())
  380. {
  381. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  382. $aTrack = $bVoted ? $aParams['track'] : [];
  383. $sReaction = $bVoted ? $aTrack['reaction'] : $this->_sDefault;
  384. $aReaction = $this->getReaction($sReaction);
  385. $sUse = !empty($aReaction['use']) ? $aReaction['use'] : 'emoji';
  386. if(!$bVoted && !empty($aReaction['default']))
  387. $sUse = 'icon';
  388. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  389. 'style_prefix' => $this->_sStylePrefix,
  390. 'bx_if:show_icon' => [
  391. 'condition' => isset($aParams['show_do_vote_icon']) && (bool)$aParams['show_do_vote_icon'] === true,
  392. 'content' => [
  393. 'style_prefix' => $this->_sStylePrefix,
  394. 'bx_if:show_as_icon' => [
  395. 'condition' => $sUse == 'icon',
  396. 'content' => [
  397. 'name' => $sUse == 'icon' ? $this->_getIconDoWithTrack($bVoted, $aTrack) : '',
  398. ]
  399. ],
  400. 'bx_if:show_as_emoji' => [
  401. 'condition' => $sUse == 'emoji',
  402. 'content' => [
  403. 'emoji' => $sUse == 'emoji' ? $this->_getEmojiDoWithTrack($bVoted, $aTrack) : '',
  404. ]
  405. ],
  406. 'bx_if:show_as_image' => [
  407. 'condition' => $sUse == 'image',
  408. 'content' => [
  409. 'image' => $sUse == 'image' ? $this->_getImageDoWithTrack($bVoted, $aTrack) : '',
  410. ]
  411. ],
  412. ]
  413. ],
  414. 'bx_if:show_text' => array(
  415. 'condition' => isset($aParams['show_do_vote_label']) && (bool)$aParams['show_do_vote_label'] === true,
  416. 'content' => array(
  417. 'style_prefix' => $this->_sStylePrefix,
  418. 'text' => _t($this->_getTitleDoWithTrack($bVoted, $aTrack))
  419. )
  420. )
  421. ));
  422. }
  423. public function _getDoVoteMenu($iValue = 0)
  424. {
  425. if(empty($iValue))
  426. $iValue = $this->getValue();
  427. $oMenu = BxTemplMenu::getObjectInstance($this->_sMenuDoVote);
  428. if(!$oMenu)
  429. return false;
  430. $oMenu->setParams(array(
  431. 'object' => &$this,
  432. 'value' => $iValue,
  433. ));
  434. return $oMenu;
  435. }
  436. public function _getDoVotePopup($iValue = 0)
  437. {
  438. $oMenu = $this->_getDoVoteMenu($iValue);
  439. if($oMenu === false)
  440. return '';
  441. return $oMenu->getCode();
  442. }
  443. protected function _getCounterLabel($iCount, $aParams = array())
  444. {
  445. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  446. $aReaction = $this->getReaction($sReaction);
  447. $sUse = !empty($aReaction['use']) ? $aReaction['use'] : 'emoji';
  448. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  449. 'style_prefix' => $this->_sStylePrefix,
  450. 'bx_if:show_icon' => array(
  451. 'condition' => !isset($aParams['show_counter_label_icon']) || (bool)$aParams['show_counter_label_icon'] === true,
  452. 'content' => array(
  453. 'style_prefix' => $this->_sStylePrefix,
  454. 'bx_if:show_as_icon' => [
  455. 'condition' => $sUse == 'icon',
  456. 'content' => [
  457. 'name' => $sUse == 'icon' ? $this->getIcon($sReaction) : '',
  458. ]
  459. ],
  460. 'bx_if:show_as_emoji' => [
  461. 'condition' => $sUse == 'emoji',
  462. 'content' => [
  463. 'emoji' => $sUse == 'emoji' ? $this->getEmoji($sReaction) : '',
  464. ]
  465. ],
  466. 'bx_if:show_as_image' => [
  467. 'condition' => $sUse == 'image',
  468. 'content' => [
  469. 'image' => $sUse == 'image' ? $this->getImage($sReaction) : '',
  470. ]
  471. ],
  472. 'title_attr' => bx_html_attribute(_t($this->_aDataList[$sReaction]['title'])),
  473. )
  474. ),
  475. 'bx_if:show_text' => array(
  476. 'condition' => !isset($aParams['show_counter_label_text']) || (bool)$aParams['show_counter_label_text'] === true,
  477. 'content' => array(
  478. 'style_prefix' => $this->_sStylePrefix,
  479. 'text' => parent::_getCounterLabel($iCount)
  480. )
  481. )
  482. ));
  483. }
  484. protected function _getVotedBy($aParams = array())
  485. {
  486. if(!isset($aParams['reaction']))
  487. return $this->_getVotedBySummary($aParams);
  488. $aReactions = $this->getReactions(true);
  489. $bSummary = $aParams['reaction'] == 'summary';
  490. $aBrowseParams = array('type' => 'by', 'object_id' => $this->getId());
  491. if(!$bSummary)
  492. $aBrowseParams['reaction'] = $aParams['reaction'];
  493. $aValues = $this->_oQuery->getPerformed($aBrowseParams);
  494. $aTmplUsers = array();
  495. foreach($aValues as $mValue) {
  496. $mValue = is_array($mValue) ? $mValue : array('author_id' => (int)$mValue, 'reaction' => '');
  497. $aTmplReaction = [];
  498. if($bSummary) {
  499. $sUse = !empty($aReactions[$mValue['reaction']]['use']) ? $aReactions[$mValue['reaction']]['use'] : 'emoji';
  500. $aTmplReaction = [
  501. 'style_prefix' => $this->_sStylePrefix,
  502. 'bx_if:show_as_icon' => [
  503. 'condition' => $sUse == 'icon',
  504. 'content' => [
  505. 'name' => $sUse == 'icon' ? $this->getIcon($mValue['reaction']) : '',
  506. ]
  507. ],
  508. 'bx_if:show_as_emoji' => [
  509. 'condition' => $sUse == 'emoji',
  510. 'content' => [
  511. 'emoji' => $sUse == 'emoji' ? $this->getEmoji($mValue['reaction']) : '',
  512. ]
  513. ],
  514. 'bx_if:show_as_image' => [
  515. 'condition' => $sUse == 'image',
  516. 'content' => [
  517. 'image' => $sUse == 'image' ? $this->getImage($mValue['reaction']) : '',
  518. ]
  519. ],
  520. ];
  521. }
  522. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($mValue['author_id']);
  523. $aTmplUsers[] = array(
  524. 'style_prefix' => $this->_sStylePrefix,
  525. 'user_unit' => $sUserUnit,
  526. 'bx_if:show_reaction' => array(
  527. 'condition' => $bSummary,
  528. 'content' => $aTmplReaction
  529. )
  530. );
  531. }
  532. if(empty($aTmplUsers))
  533. $aTmplUsers = MsgBox(_t('_Empty'));
  534. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  535. 'style_prefix' => $this->_sStylePrefix,
  536. 'class' => ' ' . $this->_sStylePrefix . '-bl-' . $aParams['reaction'],
  537. 'bx_repeat:list' => $aTmplUsers
  538. ));
  539. }
  540. protected function _getVotedBySummary($aParams = array())
  541. {
  542. $sJsObject = $this->getJsObjectName();
  543. $sTxtSummary = _t('_vote_do_by_summary');
  544. $aReactions = array_merge(['summary' => []], $this->getReactions(true));
  545. $aMenuItems = array();
  546. $aTmplVarsLists = array();
  547. foreach ($aReactions as $sReaction => $aReaction) {
  548. if($sReaction == $this->_sDefault)
  549. continue;
  550. $bSummary = $sReaction == 'summary';
  551. $sUse = !empty($aReaction['use']) ? $aReaction['use'] : 'emoji';
  552. $sIcon = '';
  553. switch($sUse) {
  554. case 'icon':
  555. $sIcon = $this->_oTemplate->parseIcon($this->getIcon($sReaction));
  556. break;
  557. case 'emoji':
  558. $sIcon = $this->getEmoji($sReaction);
  559. break;
  560. case 'image':
  561. $sIcon = $this->getImage($sReaction);
  562. break;
  563. }
  564. $sTitle = !$bSummary ? $sIcon : $sTxtSummary;
  565. $sTitleAttr = !$bSummary ? _t('_vote_do_by_x_reaction', _t($this->_aDataList[$sReaction]['title'])) : $sTxtSummary;
  566. $sName = $this->_sStylePrefix . '-' . $sReaction;
  567. $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);
  568. $aTmplVarsLists[] = array(
  569. 'content' => $this->_getVotedBy(array('reaction' => $sReaction))
  570. );
  571. }
  572. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sStylePrefix . '-voted-by', 'menu_items' => $aMenuItems));
  573. $oMenu->setSelected('', $this->_sStylePrefix . '-summary');
  574. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameBySummary, array(
  575. 'style_prefix' => $this->_sStylePrefix,
  576. 'title' => '',
  577. 'menu' => $oMenu->getCode(),
  578. 'bx_repeat:lists' => $aTmplVarsLists
  579. ));
  580. }
  581. protected function _getTmplContentCounterWrapper()
  582. {
  583. return $this->_sTmplContentCounterWrapper;
  584. }
  585. }
  586. /** @} */