BxBaseVoteReactions.php 16 KB

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