BxBaseFavorite.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 BxDolFavorite
  11. */
  12. class BxBaseFavorite extends BxDolFavorite
  13. {
  14. protected $_bCssJsAdded;
  15. protected $_sJsObjClass;
  16. protected $_sJsObjName;
  17. protected $_sStylePrefix;
  18. protected $_aHtmlIds;
  19. protected $_aElementDefaults;
  20. public function __construct($sSystem, $iId, $iInit = 1, $oTemplate = false)
  21. {
  22. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  23. $this->_bCssJsAdded = false;
  24. $this->_sJsObjClass = 'BxDolFavorite';
  25. $this->_sJsObjName = 'oFavorite' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  26. $this->_sStylePrefix = 'bx-favorite';
  27. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  28. $this->_aHtmlIds = array(
  29. 'main' => 'bx-favorite-' . $sHtmlId,
  30. 'counter' => 'bx-favorite-counter-' . $sHtmlId,
  31. 'do_link' => 'bx-favorite-do-link-' . $sHtmlId,
  32. 'by_popup' => 'bx-favorite-by-popup-' . $sHtmlId
  33. );
  34. $this->_aElementDefaults = array(
  35. 'show_do_favorite_as_button' => false,
  36. 'show_do_favorite_as_button_small' => false,
  37. 'show_do_favorite_icon' => true,
  38. 'show_do_favorite_label' => false,
  39. 'show_counter' => true
  40. );
  41. $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('favorite_element_block.html');
  42. $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('favorite_element_inline.html');
  43. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('favorite_do_favorite_label.html');
  44. }
  45. public function getJsObjectName()
  46. {
  47. return $this->_sJsObjName;
  48. }
  49. public function getJsScript($bDynamicMode = false)
  50. {
  51. $aParams = array(
  52. 'sObjName' => $this->_sJsObjName,
  53. 'sSystem' => $this->getSystemName(),
  54. 'iAuthorId' => $this->_getAuthorId(),
  55. 'iObjId' => $this->getId(),
  56. 'sRootUrl' => BX_DOL_URL_ROOT,
  57. 'sStylePrefix' => $this->_sStylePrefix,
  58. 'aHtmlIds' => $this->_aHtmlIds
  59. );
  60. $sCode = "var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . ");";
  61. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  62. }
  63. public function getJsClick()
  64. {
  65. return $this->getJsObjectName() . '.favorite(this)';
  66. }
  67. public function getCounter($aParams = array())
  68. {
  69. $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
  70. $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
  71. $aFavorite = $this->_oQuery->getFavorite($this->getId());
  72. $sClass = $this->_sStylePrefix . '-counter';
  73. if($bShowDoFavoriteAsButtonSmall)
  74. $sClass .= ' bx-btn-small-height';
  75. if($bShowDoFavoriteAsButton)
  76. $sClass .= ' bx-btn-height';
  77. return $this->_oTemplate->parseLink('javascript:void(0)', (int)$aFavorite['count'] > 0 ? $this->_getCounterLabel($aFavorite['count']) : '', array(
  78. 'title' => _t('_favorite_do_favorite_by'),
  79. 'id' => $this->_aHtmlIds['counter'],
  80. 'class' => $sClass,
  81. 'onclick' => 'javascript:' . $this->getJsObjectName() . '.toggleByPopup(this)'
  82. ));
  83. }
  84. public function getElementBlock($aParams = array())
  85. {
  86. $aParams['usage'] = BX_DOL_FAVORITE_USAGE_BLOCK;
  87. return $this->getElement($aParams);
  88. }
  89. public function getElementInline($aParams = array())
  90. {
  91. $aParams['usage'] = BX_DOL_FAVORITE_USAGE_INLINE;
  92. return $this->getElement($aParams);
  93. }
  94. public function getElement($aParams = array())
  95. {
  96. $aParams = array_merge($this->_aElementDefaults, $aParams);
  97. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  98. $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
  99. $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
  100. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true && $this->isAllowedFavoriteView();
  101. $iObjectId = $this->getId();
  102. $iAuthorId = $this->_getAuthorId();
  103. $aFavorite = $this->_oQuery->getFavorite($iObjectId);
  104. $bCount = (int)$aFavorite['count'] != 0;
  105. $bAllowedFavorite = $this->isAllowedFavorite();
  106. if(!$bAllowedFavorite && (!$this->isAllowedFavoriteView() || !$bCount))
  107. return '';
  108. $aParams['is_favorited'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
  109. $sTmplName = $this->{'_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_FAVORITE_USAGE_DEFAULT)}();
  110. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  111. 'style_prefix' => $this->_sStylePrefix,
  112. 'html_id' => $this->_aHtmlIds['main'],
  113. 'class' => $this->_sStylePrefix . ($bShowDoFavoriteAsButton ? '-button' : '-link') . ($bShowDoFavoriteAsButtonSmall ? '-button-small' : ''),
  114. 'count' => $aFavorite['count'],
  115. 'do_favorite' => $this->_getDoFavorite($aParams, $bAllowedFavorite),
  116. 'bx_if:show_counter' => array(
  117. 'condition' => $bShowCounter,
  118. 'content' => array(
  119. 'style_prefix' => $this->_sStylePrefix,
  120. 'bx_if:show_hidden' => array(
  121. 'condition' => !$bCount,
  122. 'content' => array()
  123. ),
  124. 'counter' => $this->getCounter($aParams)
  125. )
  126. ),
  127. 'script' => $this->getJsScript($bDynamicMode)
  128. ));
  129. }
  130. protected function _getDoFavorite($aParams = array(), $bAllowedFavorite = true)
  131. {
  132. $bFavorited = isset($aParams['is_favorited']) && $aParams['is_favorited'] === true;
  133. $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
  134. $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
  135. $bDisabled = !$bAllowedFavorite || ($bFavorited && !$this->isUndo());
  136. $sClass = '';
  137. if($bShowDoFavoriteAsButton)
  138. $sClass = 'bx-btn';
  139. else if ($bShowDoFavoriteAsButtonSmall)
  140. $sClass = 'bx-btn bx-btn-small';
  141. if($bDisabled)
  142. $sClass .= $bShowDoFavoriteAsButton || $bShowDoFavoriteAsButtonSmall ? ' bx-btn-disabled' : 'bx-favorite-disabled';
  143. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDoFavorite($aParams), array(
  144. 'id' => $this->_aHtmlIds['do_link'],
  145. 'class' => $this->_sStylePrefix . '-do-favorite ' . $sClass,
  146. 'title' => _t($this->_getTitleDoFavorite($bFavorited)),
  147. 'onclick' => !$bDisabled ? $this->getJsClick() : ''
  148. ));
  149. }
  150. protected function _getCounterLabel($iCount)
  151. {
  152. return _t('_favorite_counter', $iCount);
  153. }
  154. protected function _getLabelDoFavorite($aParams = array())
  155. {
  156. $bFavorited = isset($aParams['is_favorited']) && $aParams['is_favorited'] === true;
  157. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  158. 'style_prefix' => $this->_sStylePrefix,
  159. 'bx_if:show_icon' => array(
  160. 'condition' => isset($aParams['show_do_favorite_icon']) && $aParams['show_do_favorite_icon'] == true,
  161. 'content' => array(
  162. 'style_prefix' => $this->_sStylePrefix,
  163. 'name' => $this->_getIconDoFavorite($bFavorited)
  164. )
  165. ),
  166. 'bx_if:show_text' => array(
  167. 'condition' => isset($aParams['show_do_favorite_label']) && $aParams['show_do_favorite_label'] == true,
  168. 'content' => array(
  169. 'style_prefix' => $this->_sStylePrefix,
  170. 'text' => _t($this->_getTitleDoFavorite($bFavorited))
  171. )
  172. )
  173. ));
  174. }
  175. protected function _getFavoritedBy()
  176. {
  177. $aTmplFavorites = array();
  178. $aFavorites = $this->_oQuery->getPerformedBy($this->getId());
  179. foreach($aFavorites as $aFavorite) {
  180. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aFavorite['author_id']);
  181. $aTmplFavorites[] = array(
  182. 'style_prefix' => $this->_sStylePrefix,
  183. 'user_unit' => $sUserUnit,
  184. );
  185. }
  186. if(empty($aTmplFavorites))
  187. $aTmplFavorites = MsgBox(_t('_Empty'));
  188. return $this->_oTemplate->parseHtmlByName('favorite_by_list.html', array(
  189. 'style_prefix' => $this->_sStylePrefix,
  190. 'bx_repeat:list' => $aTmplFavorites
  191. ));
  192. }
  193. }
  194. /** @} */