BxBaseVoteReactions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 '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']) && $aParams['show_counter_style'] == self::$_sCounterStyleCompound)
  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 _getCounterDivided($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. $sClass .= ' bx-def-margin-sec-right';
  88. $aParams['id_counter'] = '';
  89. $sResult = '';
  90. foreach($aReactions as $sName) {
  91. $iCount = (int)$aVote['count_' . $sName];
  92. $aParams['class_counter'] = ($iCount == 0 && (!$bShowCounterEmpty || $sName != $this->_sDefault || $bVote) ? ' bx-vc-hidden' : '') . ' ' . $sName . ' ' . $sClass;
  93. $aParams['reaction'] = $sName;
  94. $aParams['vote'] = array(
  95. 'count' => $iCount,
  96. 'sum' => $aVote['sum_' . $sName],
  97. 'rate' => $aVote['rate_' . $sName],
  98. );
  99. $sResult .= parent::getCounter(array_merge($aParams, array('show_script' => false)));
  100. }
  101. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterWrapper(), array(
  102. 'html_id' => $this->_aHtmlIds['counter'],
  103. 'style_prefix' => $this->_sStylePrefix,
  104. 'type' => $this->_sType,
  105. 'style' => self::$_sCounterStyleDivided,
  106. 'bx_if:show_link' => array(
  107. 'condition' => false,
  108. 'content' => array()
  109. ),
  110. 'bx_if:show_text' => array(
  111. 'condition' => true,
  112. 'content' => array(
  113. 'counter' => $sResult,
  114. )
  115. ),
  116. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  117. ));
  118. }
  119. public function _getCounterCompound($aParams = array())
  120. {
  121. $bDynamicMode = isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true;
  122. $bShowCounterEmpty = isset($aParams['show_counter_empty']) && (bool)$aParams['show_counter_empty'] === true;
  123. $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
  124. $bVote = $this->_isVote();
  125. $aVote = $this->_getVote();
  126. $aReactions = $this->getReactions();
  127. $sClass = isset($aParams['class_counter']) ? $aParams['class_counter'] : '';
  128. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  129. $sClass .= ' bx-btn-small-height';
  130. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  131. $sClass .= ' bx-btn-height';
  132. $aParams['id_counter'] = '';
  133. $sResult = '';
  134. $iResultC = $iResultS = 0;
  135. foreach($aReactions as $sName) {
  136. $iCount = (int)$aVote['count_' . $sName];
  137. $aParams = array_merge($aParams, array(
  138. 'show_counter_active' => false,
  139. 'show_counter_label_icon' => true,
  140. 'show_counter_label_text' => false,
  141. 'class_counter' => ($iCount == 0 && (!$bShowCounterEmpty || $sName != $this->_sDefault || $bVote) ? ' bx-vc-hidden' : '') . ' ' . $sName . ' ' . $sClass,
  142. 'reaction' => $sName,
  143. 'vote' => array(
  144. 'count' => $iCount,
  145. 'sum' => $aVote['sum_' . $sName],
  146. 'rate' => $aVote['rate_' . $sName],
  147. )
  148. ));
  149. $iResultC += $iCount;
  150. $iResultS += (int)$aVote['sum_' . $sName];
  151. $sResult .= trim(parent::getCounter(array_merge($aParams, array('show_script' => false))));
  152. }
  153. $aParams = array_merge($aParams, array(
  154. 'show_counter_active' => false,
  155. 'show_counter_label_icon' => false,
  156. 'show_counter_label_text' => true,
  157. 'class_counter' => ' total-count ' . $sClass,
  158. 'reaction' => '',
  159. 'vote' => array(
  160. 'count' => $iResultC,
  161. 'sum' => $iResultS,
  162. 'rate' => $iResultC > 0 ? round($iResultS / $iResultC, 2) : 0,
  163. )
  164. ));
  165. $sResult .= parent::getCounter(array_merge($aParams, array('show_script' => false)));
  166. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterWrapper(), array(
  167. 'html_id' => $this->_aHtmlIds['counter'],
  168. 'style_prefix' => $this->_sStylePrefix,
  169. 'class' => !$bVote && !$bShowCounterEmpty ? 'bx-vc-hidden' : '',
  170. 'type' => $this->_sType,
  171. 'style' => self::$_sCounterStyleCompound,
  172. 'bx_if:show_link' => array(
  173. 'condition' => true,
  174. 'content' => array(
  175. 'href' => 'javascript:void(0)',
  176. 'onclick' => 'javascript:' . $this->getJsClickCounter($aParams),
  177. 'title' => bx_html_attribute($this->_getTitleDoBy($aParams)),
  178. 'counter' => $sResult,
  179. )
  180. ),
  181. 'bx_if:show_text' => array(
  182. 'condition' => false,
  183. 'content' => array()
  184. ),
  185. 'script' => $bShowScript ? $this->getJsScript($aParams) : ''
  186. ));
  187. }
  188. public function getElement($aParams = array())
  189. {
  190. $sClass = $this->_sStylePrefix . '-' . $this->_sType;
  191. if(isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true)
  192. $sClass .= '-button-small';
  193. else if(isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true)
  194. $sClass .= '-button';
  195. $aParams['class_element'] = isset($aParams['class_element']) ? $aParams['class_element'] : '';
  196. $aParams['class_element'] .= ' ' . $sClass;
  197. return parent::getElement($aParams);
  198. }
  199. /**
  200. * Internal methods.
  201. */
  202. protected function _isShowDoVote($aParams, $isAllowedVote, $bCount)
  203. {
  204. $bResult = parent::_isShowDoVote($aParams, $isAllowedVote, $bCount);
  205. if(!$bResult)
  206. return $bResult;
  207. return $isAllowedVote || $bCount;
  208. }
  209. protected function _getDoVote($aParams = array(), $isAllowedVote = true)
  210. {
  211. $bUndo = $this->isUndo();
  212. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  213. $bShowDoVoteAsButtonSmall = isset($aParams['show_do_vote_as_button_small']) && (bool)$aParams['show_do_vote_as_button_small'] === true;
  214. $bShowDoVoteAsButton = !$bShowDoVoteAsButtonSmall && isset($aParams['show_do_vote_as_button']) && (bool)$aParams['show_do_vote_as_button'] === true;
  215. $bDisabled = !$isAllowedVote || ($bVoted && !$bUndo);
  216. $sClass = '';
  217. if($bShowDoVoteAsButton)
  218. $sClass = ' bx-btn';
  219. else if ($bShowDoVoteAsButtonSmall)
  220. $sClass = ' bx-btn bx-btn-small';
  221. $sJsClick = '';
  222. if(!$bDisabled) {
  223. if($bVoted && $bUndo) {
  224. $sClass = ' ' . $this->_sStylePrefix . '-voted' . $sClass;
  225. $sJsClick = $this->getJsClickDo($aParams['track']['reaction']);
  226. }
  227. else
  228. $sJsClick = $this->getJsClick();
  229. }
  230. else
  231. $sClass .= $bShowDoVoteAsButton || $bShowDoVoteAsButtonSmall ? ' bx-btn-disabled' : ' ' . $this->_sStylePrefix . '-disabled';
  232. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getDoVoteLabel($aParams), array(
  233. 'class' => $this->_sStylePrefix . '-do-vote' . $sClass,
  234. 'title' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track'])),
  235. 'onclick' => $sJsClick,
  236. ));
  237. }
  238. protected function _getDoVoteLabel($aParams = array())
  239. {
  240. $bVoted = isset($aParams['is_voted']) && (bool)$aParams['is_voted'] === true;
  241. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  242. 'style_prefix' => $this->_sStylePrefix,
  243. 'bx_if:show_icon' => array(
  244. 'condition' => isset($aParams['show_do_vote_icon']) && (bool)$aParams['show_do_vote_icon'] === true,
  245. 'content' => array(
  246. 'style_prefix' => $this->_sStylePrefix,
  247. 'name' => $this->_getIconDoWithTrack($bVoted, $aParams['track'])
  248. )
  249. ),
  250. 'bx_if:show_text' => array(
  251. 'condition' => isset($aParams['show_do_vote_label']) && (bool)$aParams['show_do_vote_label'] === true,
  252. 'content' => array(
  253. 'style_prefix' => $this->_sStylePrefix,
  254. 'text' => _t($this->_getTitleDoWithTrack($bVoted, $aParams['track']))
  255. )
  256. )
  257. ));
  258. }
  259. public function _getDoVotePopup($iValue = 0)
  260. {
  261. if(empty($iValue))
  262. $iValue = $this->getValue();
  263. $oMenu = BxTemplMenu::getObjectInstance($this->_sMenuDoVote);
  264. if(!$oMenu)
  265. return '';
  266. $oMenu->setParams(array(
  267. 'object' => &$this,
  268. 'value' => $iValue,
  269. ));
  270. return $oMenu->getCode();
  271. }
  272. protected function _getCounterLabel($iCount, $aParams = array())
  273. {
  274. $sReaction = !empty($aParams['reaction']) ? $aParams['reaction'] : $this->_sDefault;
  275. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentCounterLabel(), array(
  276. 'style_prefix' => $this->_sStylePrefix,
  277. 'bx_if:show_icon' => array(
  278. 'condition' => !isset($aParams['show_counter_label_icon']) || (bool)$aParams['show_counter_label_icon'] === true,
  279. 'content' => array(
  280. 'style_prefix' => $this->_sStylePrefix,
  281. 'name' => $this->_aDataList[$sReaction]['icon']
  282. )
  283. ),
  284. 'bx_if:show_text' => array(
  285. 'condition' => !isset($aParams['show_counter_label_text']) || (bool)$aParams['show_counter_label_text'] === true,
  286. 'content' => array(
  287. 'style_prefix' => $this->_sStylePrefix,
  288. 'text' => parent::_getCounterLabel($iCount)
  289. )
  290. )
  291. ));
  292. }
  293. protected function _getVotedBy($aParams = array())
  294. {
  295. if(!isset($aParams['reaction']))
  296. return $this->_getVotedBySummary($aParams);
  297. $bSummary = $aParams['reaction'] == 'summary';
  298. $aBrowseParams = array('type' => 'by', 'object_id' => $this->getId());
  299. if(!$bSummary)
  300. $aBrowseParams['reaction'] = $aParams['reaction'];
  301. $aValues = $this->_oQuery->getPerformed($aBrowseParams);
  302. $aTmplUsers = array();
  303. foreach($aValues as $mValue) {
  304. $mValue = is_array($mValue) ? $mValue : array('author_id' => (int)$mValue, 'reaction' => '');
  305. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($mValue['author_id']);
  306. $aTmplUsers[] = array(
  307. 'style_prefix' => $this->_sStylePrefix,
  308. 'user_unit' => $sUserUnit,
  309. 'bx_if:show_reaction' => array(
  310. 'condition' => $bSummary,
  311. 'content' => array(
  312. 'style_prefix' => $this->_sStylePrefix,
  313. 'icon' => $bSummary ? $this->_aDataList[$mValue['reaction']]['icon'] : ''
  314. )
  315. )
  316. );
  317. }
  318. if(empty($aTmplUsers))
  319. $aTmplUsers = MsgBox(_t('_Empty'));
  320. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameByList, array(
  321. 'style_prefix' => $this->_sStylePrefix,
  322. 'class' => ' ' . $this->_sStylePrefix . '-bl-' . $aParams['reaction'],
  323. 'bx_repeat:list' => $aTmplUsers
  324. ));
  325. }
  326. protected function _getVotedBySummary($aParams = array())
  327. {
  328. $sJsObject = $this->getJsObjectName();
  329. $sTxtSummary = _t('_vote_do_by_summary');
  330. $aReactions = array_merge(array('summary'), $this->getReactions());
  331. $aMenuItems = array();
  332. $aTmplVarsLists = array();
  333. foreach ($aReactions as $sReaction) {
  334. $bSummary = $sReaction == 'summary';
  335. $sName = $this->_sStylePrefix . '-' . $sReaction;
  336. $sTitle = !$bSummary ? $this->_oTemplate->parseIcon($this->_aDataList[$sReaction]['icon']) : $sTxtSummary;
  337. $sTitleAttr = !$bSummary ? _t('_vote_do_by_x_reaction', _t($this->_aDataList[$sReaction]['title'])) : $sTxtSummary;
  338. $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);
  339. $aTmplVarsLists[] = array(
  340. 'content' => $this->_getVotedBy(array('reaction' => $sReaction))
  341. );
  342. }
  343. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> $this->_sStylePrefix . '-voted-by', 'menu_items' => $aMenuItems));
  344. $oMenu->setSelected('', $this->_sStylePrefix . '-summary');
  345. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameBySummary, array(
  346. 'style_prefix' => $this->_sStylePrefix,
  347. 'title' => '',
  348. 'menu' => $oMenu->getCode(),
  349. 'bx_repeat:lists' => $aTmplVarsLists
  350. ));
  351. }
  352. protected function _getTmplContentCounterWrapper()
  353. {
  354. return $this->_sTmplContentCounterWrapper;
  355. }
  356. }
  357. /** @} */