1
0

BxBaseVoteReactions.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 = array(
  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_empty' => true,
  33. 'show_counter_style' => self::$_sCounterStyleDivided, //--- Alloved styles are 'simple', 'divided' and 'compound'
  34. 'show_legend' => false,
  35. 'show_script' => true
  36. );
  37. $this->_sTmplNameBySummary = 'vote_by_summary_reactions.html';
  38. $this->_sTmplNameByList = 'vote_by_list_reactions.html';
  39. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('vote_do_vote_label_reactions.html');
  40. $this->_sTmplContentCounterLabel = $this->_oTemplate->getHtml('vote_counter_label_reactions.html');
  41. $this->_sTmplContentCounterWrapper = $this->_oTemplate->getHtml('vote_counter_wrapper_reactions.html');
  42. }
  43. public function getJsClick($iValue = 0)
  44. {
  45. if(empty($iValue))
  46. $iValue = $this->getValue();
  47. return $this->getJsObjectName() . '.toggleDoPopup(this, ' . $iValue . ')';
  48. }
  49. public function getJsClickDo($sReaction, $iValue = 0)
  50. {
  51. if(empty($iValue))
  52. $iValue = $this->getValue();
  53. return $this->getJsObjectName() . '.vote(this, ' . $iValue . ', \'' . $sReaction . '\')';
  54. }
  55. public function getJsClickCounter($aParams = array())
  56. {
  57. $sJsObject = $this->getJsObjectName();
  58. $sJsMethod = 'toggleByPopup';
  59. if(isset($aParams['show_counter_style']) && in_array($aParams['show_counter_style'], array(self::$_sCounterStyleCompound, self::$_sCounterStyleSimple)))
  60. return $sJsObject . '.' . $sJsMethod . '(this)';
  61. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  62. return $sJsObject . '.' . $sJsMethod . '(this, \'' . $sReaction . '\')';
  63. }
  64. public function getCounter($aParams = array())
  65. {
  66. $sDefault = $this->_aElementDefaults['show_counter_style'];
  67. $sCounterStyle = !empty($aParams['show_counter_style']) ? $aParams['show_counter_style'] : $sDefault;
  68. $sMethodPrefix = '_getCounter';
  69. $sMethod = $sMethodPrefix . bx_gen_method_name($sCounterStyle);
  70. if(!method_exists($this, $sMethod))
  71. $sMethod = $sMethodPrefix . bx_gen_method_name($sDefault);
  72. return $this->$sMethod($aParams);
  73. }
  74. public function _getCounterSimple($aParams = array())
  75. {
  76. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  77. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  78. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  79. $bVote = $this->_isVote();
  80. $aVote = $this->_getVote();
  81. $aReactions = $this->getReactions();
  82. $sClass = isset($aParams['class_counter']) ? $aParams['class_counter'] : '';
  83. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  84. $sClass .= ' bx-btn-small-height';
  85. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  86. $sClass .= ' bx-btn-height';
  87. $aParams['id_counter'] = '';
  88. $iResultC = $iResultS = 0;
  89. foreach($aReactions as $sName) {
  90. $iResultC += (int)$aVote['count_' . $sName];
  91. $iResultS += (int)$aVote['sum_' . $sName];
  92. }
  93. $aParams = array_merge($aParams, array(
  94. 'show_counter_active' => false,
  95. 'show_counter_label_icon' => false,
  96. 'show_counter_label_text' => true,
  97. 'class_counter' => ' total-count ' . $sClass,
  98. 'reaction' => '',
  99. 'vote' => array(
  100. 'count' => $iResultC,
  101. 'sum' => $iResultS,
  102. 'rate' => $iResultC > 0 ? round($iResultS / $iResultC, 2) : 0,
  103. )
  104. ));
  105. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterWrapper(), array(
  106. 'html_id' => $this->_aHtmlIds['counter'],
  107. 'style_prefix' => $this->_sStylePrefix,
  108. 'class' => $this->_aHtmlIds['counter'] . (!$bVote && !$bShowCounterEmpty ? ' bx-vc-hidden' : ''),
  109. 'type' => $this->_sType,
  110. 'style' => self::$_sCounterStyleSimple,
  111. 'bx_if:show_link' => array(
  112. 'condition' => true,
  113. 'content' => array(
  114. 'href' => 'javascript:void(0)',
  115. 'onclick' => 'javascript:' . $this->getJsClickCounter($aParams),
  116. 'title' => bx_html_attribute($this->_getTitleDoBy($aParams)),
  117. 'counter' => parent::getCounter(array_merge($aParams, array('show_script' => false))),
  118. )
  119. ),
  120. 'bx_if:show_text' => array(
  121. 'condition' => false,
  122. 'content' => array()
  123. ),
  124. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  125. ));
  126. }
  127. public function _getCounterDivided($aParams = array())
  128. {
  129. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  130. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  131. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  132. $bVote = $this->_isVote();
  133. $aVote = $this->_getVote();
  134. $aReactions = $this->getReactions();
  135. $sClass = isset($aParams['class_counter']) ? $aParams['class_counter'] : '';
  136. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  137. $sClass .= ' bx-btn-small-height';
  138. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  139. $sClass .= ' bx-btn-height';
  140. $sClass .= ' bx-def-margin-sec-right';
  141. $aParams['id_counter'] = '';
  142. $sResult = '';
  143. foreach($aReactions as $sName) {
  144. $iCount = (int)$aVote['count_' . $sName];
  145. $aParams['class_counter'] = ($iCount == 0 && (!$bShowCounterEmpty || $sName != $this->_sDefault || $bVote) ? ' bx-vc-hidden' : '') . ' ' . $sName . ' ' . $sClass;
  146. $aParams['reaction'] = $sName;
  147. $aParams['vote'] = array(
  148. 'count' => $iCount,
  149. 'sum' => $aVote['sum_' . $sName],
  150. 'rate' => $aVote['rate_' . $sName],
  151. );
  152. $sResult .= parent::getCounter(array_merge($aParams, array('show_script' => false)));
  153. }
  154. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterWrapper(), array(
  155. 'html_id' => $this->_aHtmlIds['counter'],
  156. 'style_prefix' => $this->_sStylePrefix,
  157. 'class' => $this->_aHtmlIds['counter'] . (!$bVote && !$bShowCounterEmpty ? ' bx-vc-hidden' : ''),
  158. 'type' => $this->_sType,
  159. 'style' => self::$_sCounterStyleDivided,
  160. 'bx_if:show_link' => array(
  161. 'condition' => false,
  162. 'content' => array()
  163. ),
  164. 'bx_if:show_text' => array(
  165. 'condition' => true,
  166. 'content' => array(
  167. 'counter' => $sResult,
  168. )
  169. ),
  170. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  171. ));
  172. }
  173. public function _getCounterCompound($aParams = array())
  174. {
  175. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  176. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  177. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  178. $bVote = $this->_isVote();
  179. $aVote = $this->_getVote();
  180. $aReactions = $this->getReactions();
  181. $sClass = isset($aParams['class_counter']) ? $aParams['class_counter'] : '';
  182. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  183. $sClass .= ' bx-btn-small-height';
  184. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  185. $sClass .= ' bx-btn-height';
  186. $aParams['id_counter'] = '';
  187. $sResult = '';
  188. $iResultC = $iResultS = 0;
  189. foreach($aReactions as $sName) {
  190. $iCount = (int)$aVote['count_' . $sName];
  191. $aParams = array_merge($aParams, array(
  192. 'show_counter_active' => false,
  193. 'show_counter_label_icon' => true,
  194. 'show_counter_label_text' => false,
  195. 'class_counter' => ($iCount == 0 && (!$bShowCounterEmpty || $sName != $this->_sDefault || $bVote) ? ' bx-vc-hidden' : '') . ' ' . $sName . ' ' . $sClass,
  196. 'reaction' => $sName,
  197. 'vote' => array(
  198. 'count' => $iCount,
  199. 'sum' => $aVote['sum_' . $sName],
  200. 'rate' => $aVote['rate_' . $sName],
  201. )
  202. ));
  203. $iResultC += $iCount;
  204. $iResultS += (int)$aVote['sum_' . $sName];
  205. $sResult .= trim(parent::getCounter(array_merge($aParams, array('show_script' => false))));
  206. }
  207. $aParams = array_merge($aParams, array(
  208. 'show_counter_active' => false,
  209. 'show_counter_label_icon' => false,
  210. 'show_counter_label_text' => true,
  211. 'class_counter' => ' total-count ' . $sClass,
  212. 'reaction' => '',
  213. 'vote' => array(
  214. 'count' => $iResultC,
  215. 'sum' => $iResultS,
  216. 'rate' => $iResultC > 0 ? round($iResultS / $iResultC, 2) : 0,
  217. )
  218. ));
  219. $sResult .= parent::getCounter(array_merge($aParams, array('show_script' => false)));
  220. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterWrapper(), array(
  221. 'html_id' => $this->_aHtmlIds['counter'],
  222. 'style_prefix' => $this->_sStylePrefix,
  223. 'class' => $this->_aHtmlIds['counter'] . (!$bVote && !$bShowCounterEmpty ? ' bx-vc-hidden' : ''),
  224. 'type' => $this->_sType,
  225. 'style' => self::$_sCounterStyleCompound,
  226. 'bx_if:show_link' => array(
  227. 'condition' => true,
  228. 'content' => array(
  229. 'href' => 'javascript:void(0)',
  230. 'onclick' => 'javascript:' . $this->getJsClickCounter($aParams),
  231. 'title' => bx_html_attribute($this->_getTitleDoBy($aParams)),
  232. 'counter' => $sResult,
  233. )
  234. ),
  235. 'bx_if:show_text' => array(
  236. 'condition' => false,
  237. 'content' => array()
  238. ),
  239. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  240. ));
  241. }
  242. public function getElement($aParams = array())
  243. {
  244. $sClass = $this->_sStylePrefix . '-' . $this->_sType;
  245. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  246. $sClass .= '-button-small';
  247. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  248. $sClass .= '-button';
  249. $aParams['class_element'] = isset($aParams['class_element']) ? $aParams['class_element'] : '';
  250. $aParams['class_element'] .= ' ' . $sClass;
  251. return parent::getElement($aParams);
  252. }
  253. /**
  254. * Internal methods.
  255. */
  256. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  257. {
  258. $bResult = parent::_isShowDoVote($aParams, $isAllowedVote, $bCount);
  259. if(!$bResult)
  260. return $bResult;
  261. return $isAllowedVote || $bCount;
  262. }
  263. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  264. {
  265. $bUndo = $this->isUndo();
  266. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  267. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  268. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  269. $bDisabled = !$isAllowedVote || ($bVoted && !$bUndo);
  270. $sClass = '';
  271. if($bShowDoVoteAsButton)
  272. $sClass = ' bx-btn';
  273. else if ($bShowDoVoteAsButtonSmall)
  274. $sClass = ' bx-btn bx-btn-small';
  275. $sJsClick = '';
  276. if(!$bDisabled) {
  277. if($bVoted && $bUndo) {
  278. $sClass = ' ' . $this->_sStylePrefix . '-voted' . $sClass;
  279. $sJsClick = $this->getJsClickDo($aParams['track']['reaction']);
  280. }
  281. else
  282. $sJsClick = $this->getJsClick();
  283. }
  284. else
  285. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : ' ' . $this->_sStylePrefix . '-disabled';
  286. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getDoVoteLabel($aParams), array(
  287. 'class' => $this->_sStylePrefix . '-do-vote' . $sClass,
  288. 'title' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track'])),
  289. 'onclick' => $sJsClick,
  290. ));
  291. }
  292. protected function _getDoVoteLabel($aParams = array())
  293. {
  294. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  295. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  296. 'style_prefix' => $this->_sStylePrefix,
  297. 'bx_if:show_icon' => array(
  298. 'condition' => isset($aParams['show_do_vote_icon']) && (bool)$aParams['show_do_vote_icon'] === true,
  299. 'content' => array(
  300. 'style_prefix' => $this->_sStylePrefix,
  301. 'name' => $this->_getIconDoWithTrack($bVoted, $aParams['track'])
  302. )
  303. ),
  304. 'bx_if:show_text' => array(
  305. 'condition' => isset($aParams['show_do_vote_label']) && (bool)$aParams['show_do_vote_label'] === true,
  306. 'content' => array(
  307. 'style_prefix' => $this->_sStylePrefix,
  308. 'text' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track']))
  309. )
  310. )
  311. ));
  312. }
  313. public function _getDoVotePopup($iValue = 0)
  314. {
  315. if(empty($iValue))
  316. $iValue = $this->getValue();
  317. $oMenu = BxTemplMenu::getObjectInstance($this->_sMenuDoVote);
  318. if(!$oMenu)
  319. return '';
  320. $oMenu->setParams(array(
  321. 'object' => &$this,
  322. 'value' => $iValue,
  323. ));
  324. return $oMenu->getCode();
  325. }
  326. protected function _getCounterLabel($iCount, $aParams = array())
  327. {
  328. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  329. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  330. 'style_prefix' => $this->_sStylePrefix,
  331. 'bx_if:show_icon' => array(
  332. 'condition' => !isset($aParams['show_counter_label_icon']) || (bool)$aParams['show_counter_label_icon'] === true,
  333. 'content' => array(
  334. 'style_prefix' => $this->_sStylePrefix,
  335. 'name' => $this->getIcon($sReaction)
  336. )
  337. ),
  338. 'bx_if:show_text' => array(
  339. 'condition' => !isset($aParams['show_counter_label_text']) || (bool)$aParams['show_counter_label_text'] === true,
  340. 'content' => array(
  341. 'style_prefix' => $this->_sStylePrefix,
  342. 'text' => parent::_getCounterLabel($iCount)
  343. )
  344. )
  345. ));
  346. }
  347. protected function _getVotedBy($aParams = array())
  348. {
  349. if(!isset($aParams['reaction']))
  350. return $this->_getVotedBySummary($aParams);
  351. $bSummary = $aParams['reaction'] == 'summary';
  352. $aBrowseParams = array('type' => 'by', 'object_id' => $this->getId());
  353. if(!$bSummary)
  354. $aBrowseParams['reaction'] = $aParams['reaction'];
  355. $aValues = $this->_oQuery->getPerformed($aBrowseParams);
  356. $aTmplUsers = array();
  357. foreach($aValues as $mValue) {
  358. $mValue = is_array($mValue) ? $mValue : array('author_id' => (int)$mValue, 'reaction' => '');
  359. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($mValue['author_id']);
  360. $aTmplUsers[] = array(
  361. 'style_prefix' => $this->_sStylePrefix,
  362. 'user_unit' => $sUserUnit,
  363. 'bx_if:show_reaction' => array(
  364. 'condition' => $bSummary,
  365. 'content' => array(
  366. 'style_prefix' => $this->_sStylePrefix,
  367. 'icon' => $bSummary ? $this->getIcon($mValue['reaction']) : ''
  368. )
  369. )
  370. );
  371. }
  372. if(empty($aTmplUsers))
  373. $aTmplUsers = MsgBox(_t('_Empty'));
  374. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  375. 'style_prefix' => $this->_sStylePrefix,
  376. 'class' => ' ' . $this->_sStylePrefix . '-bl-' . $aParams['reaction'],
  377. 'bx_repeat:list' => $aTmplUsers
  378. ));
  379. }
  380. protected function _getVotedBySummary($aParams = array())
  381. {
  382. $sJsObject = $this->getJsObjectName();
  383. $sTxtSummary = _t('_vote_do_by_summary');
  384. $aReactions = array_merge(array('summary'), $this->getReactions());
  385. $aMenuItems = array();
  386. $aTmplVarsLists = array();
  387. foreach ($aReactions as $sReaction) {
  388. if($sReaction == $this->_sDefault)
  389. continue;
  390. $bSummary = $sReaction == 'summary';
  391. $sName = $this->_sStylePrefix . '-' . $sReaction;
  392. $sTitle = !$bSummary ? $this->_oTemplate->parseIcon($this->getIcon($sReaction)) : $sTxtSummary;
  393. $sTitleAttr = !$bSummary ? _t('_vote_do_by_x_reaction', _t($this->_aDataList[$sReaction]['title'])) : $sTxtSummary;
  394. $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);
  395. $aTmplVarsLists[] = array(
  396. 'content' => $this->_getVotedBy(array('reaction' => $sReaction))
  397. );
  398. }
  399. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sStylePrefix . '-voted-by', 'menu_items' => $aMenuItems));
  400. $oMenu->setSelected('', $this->_sStylePrefix . '-summary');
  401. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameBySummary, array(
  402. 'style_prefix' => $this->_sStylePrefix,
  403. 'title' => '',
  404. 'menu' => $oMenu->getCode(),
  405. 'bx_repeat:lists' => $aTmplVarsLists
  406. ));
  407. }
  408. protected function _getTmplContentCounterWrapper()
  409. {
  410. return $this->_sTmplContentCounterWrapper;
  411. }
  412. }
  413. /** @} */