BxBaseVoteReactions.php 17 KB

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