BxBaseFavorite.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 bx-btn-height'),
  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. $bCount = (int)$aFavorite['count'] != 0;
  121. $bAllowedFavorite = $this->isAllowedFavorite();
  122. if(!$bAllowedFavorite && (!$this->isAllowedFavoriteView() || !$bCount))
  123. return '';
  124. $aParams['is_favorited'] = $this->_oQuery->isPerformed($iObjectId, $iAuthorId) ? true : false;
  125. $sTmplName = 'favorite_element_' . (!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_FAVORITE_USAGE_DEFAULT) . '.html';
  126. return $this->_oTemplate->parseHtmlByName($sTmplName, array(
  127. 'style_prefix' => $this->_sStylePrefix,
  128. 'html_id' => $this->_aHtmlIds['main'],
  129. 'class' => $this->_sStylePrefix . ($bShowDoFavoriteAsButton ? '-button' : '') . ($bShowDoFavoriteAsButtonSmall ? '-button-small' : ''),
  130. 'count' => $aFavorite['count'],
  131. 'do_favorite' => $this->_getDoFavorite($aParams, $bAllowedFavorite),
  132. 'bx_if:show_counter' => array(
  133. 'condition' => $bShowCounter,
  134. 'content' => array(
  135. 'style_prefix' => $this->_sStylePrefix,
  136. 'bx_if:show_hidden' => array(
  137. 'condition' => !$bCount,
  138. 'content' => array()
  139. ),
  140. 'counter' => $this->getCounter()
  141. )
  142. ),
  143. 'script' => $this->getJsScript($bDynamicMode)
  144. ));
  145. }
  146. protected function _getDoFavorite($aParams = array(), $bAllowedFavorite = true)
  147. {
  148. $bFavorited = isset($aParams['is_favorited']) && $aParams['is_favorited'] === true;
  149. $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
  150. $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
  151. $bDisabled = !$bAllowedFavorite || ($bFavorited && !$this->isUndo());
  152. $sClass = '';
  153. if($bShowDoFavoriteAsButton)
  154. $sClass = 'bx-btn';
  155. else if ($bShowDoFavoriteAsButtonSmall)
  156. $sClass = 'bx-btn bx-btn-small';
  157. if($bDisabled)
  158. $sClass .= $bShowDoFavoriteAsButton || $bShowDoFavoriteAsButtonSmall ? ' bx-btn-disabled' : 'bx-favorite-disabled';
  159. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoFavorite, array(
  160. 'style_prefix' => $this->_sStylePrefix,
  161. 'html_id' => $this->_aHtmlIds['do_link'],
  162. 'class' => $sClass,
  163. 'title' => bx_html_attribute(_t($this->_getTitleDoFavorite($bFavorited))),
  164. 'bx_if:show_onclick' => array(
  165. 'condition' => !$bDisabled,
  166. 'content' => array(
  167. 'js_object' => $this->getJsObjectName()
  168. )
  169. ),
  170. 'do_favorite' => $this->_getLabelDoFavorite($aParams),
  171. ));
  172. }
  173. protected function _getLabelCounter($iCount)
  174. {
  175. return _t('_favorite_counter', $iCount);
  176. }
  177. protected function _getLabelDoFavorite($aParams = array())
  178. {
  179. $bFavorited = isset($aParams['is_favorited']) && $aParams['is_favorited'] === true;
  180. return $this->_oTemplate->parseHtmlByName('favorite_do_favorite_label.html', array(
  181. 'bx_if:show_icon' => array(
  182. 'condition' => isset($aParams['show_do_favorite_icon']) && $aParams['show_do_favorite_icon'] == true,
  183. 'content' => array(
  184. 'name' => $this->_getIconDoFavorite($bFavorited)
  185. )
  186. ),
  187. 'bx_if:show_text' => array(
  188. 'condition' => isset($aParams['show_do_favorite_label']) && $aParams['show_do_favorite_label'] == true,
  189. 'content' => array(
  190. 'text' => _t($this->_getTitleDoFavorite($bFavorited))
  191. )
  192. )
  193. ));
  194. }
  195. protected function _getFavoritedBy()
  196. {
  197. $aTmplFavorites = array();
  198. $aFavorites = $this->_oQuery->getPerformedBy($this->getId());
  199. foreach($aFavorites as $aFavorite) {
  200. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aFavorite['author_id']);
  201. $aTmplFavorites[] = array(
  202. 'style_prefix' => $this->_sStylePrefix,
  203. 'user_unit' => $sUserUnit,
  204. );
  205. }
  206. if(empty($aTmplFavorites))
  207. $aTmplFavorites = MsgBox(_t('_Empty'));
  208. return $this->_oTemplate->parseHtmlByName('favorite_by_list.html', array(
  209. 'style_prefix' => $this->_sStylePrefix,
  210. 'bx_repeat:list' => $aTmplFavorites
  211. ));
  212. }
  213. }
  214. /** @} */