1
0

BxBaseFavorite.php 9.9 KB

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