BxBaseFavorite.php 9.5 KB

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