BxBaseFavorite.php 9.2 KB

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