BxBaseFavorite.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. 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('jquery.anim.js', '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. $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
  89. $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
  90. $aFavorite = $this->_oQuery->getFavorite($this->getId());
  91. $sClass = $this->_sStylePrefix . '-counter';
  92. if($bShowDoFavoriteAsButtonSmall)
  93. $sClass .= ' bx-btn-small-height';
  94. if($bShowDoFavoriteAsButton)
  95. $sClass .= ' bx-btn-height';
  96. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameCounter, array(
  97. 'href' => 'javascript:void(0)',
  98. 'title' => _t('_favorite_do_favorite_by'),
  99. 'bx_repeat:attrs' => array(
  100. array('key' => 'id', 'value' => $this->_aHtmlIds['counter']),
  101. array('key' => 'class', 'value' => $sClass),
  102. array('key' => 'onclick', 'value' => 'javascript:' . $this->getJsObjectName() . '.toggleByPopup(this)')
  103. ),
  104. 'content' => (int)$aFavorite['count'] > 0 ? $this->_getLabelCounter($aFavorite['count']) : ''
  105. ));
  106. }
  107. public function getElementBlock($aParams = array())
  108. {
  109. $aParams['usage'] = BX_DOL_FAVORITE_USAGE_BLOCK;
  110. return $this->getElement($aParams);
  111. }
  112. public function getElementInline($aParams = array())
  113. {
  114. $aParams['usage'] = BX_DOL_FAVORITE_USAGE_INLINE;
  115. return $this->getElement($aParams);
  116. }
  117. public function getElement($aParams = array())
  118. {
  119. $aParams = array_merge($this->_aElementDefaults, $aParams);
  120. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  121. $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
  122. $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
  123. $bShowCounter = isset($aParams['show_counter']) && $aParams['show_counter'] === true && $this->isAllowedFavoriteView();
  124. $iObjectId = $this->getId();
  125. $iAuthorId = $this->_getAuthorId();
  126. $aFavorite = $this->_oQuery->getFavorite($iObjectId);
  127. $bCount = (int)$aFavorite['count'] != 0;
  128. $bAllowedFavorite = $this->isAllowedFavorite();
  129. if(!$bAllowedFavorite && (!$this->isAllowedFavoriteView() || !$bCount))
  130. return '';
  131. $aParams['is_favorited'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
  132. $sTmplName = 'favorite_element_' . (!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_FAVORITE_USAGE_DEFAULT) . '.html';
  133. return $this->_oTemplate->parseHtmlByName($sTmplName, array(
  134. 'style_prefix' => $this->_sStylePrefix,
  135. 'html_id' => $this->_aHtmlIds['main'],
  136. 'class' => $this->_sStylePrefix . ($bShowDoFavoriteAsButton ? '-button' : '-link') . ($bShowDoFavoriteAsButtonSmall ? '-button-small' : ''),
  137. 'count' => $aFavorite['count'],
  138. 'do_favorite' => $this->_getDoFavorite($aParams, $bAllowedFavorite),
  139. 'bx_if:show_counter' => array(
  140. 'condition' => $bShowCounter,
  141. 'content' => array(
  142. 'style_prefix' => $this->_sStylePrefix,
  143. 'bx_if:show_hidden' => array(
  144. 'condition' => !$bCount,
  145. 'content' => array()
  146. ),
  147. 'counter' => $this->getCounter($aParams)
  148. )
  149. ),
  150. 'script' => $this->getJsScript($bDynamicMode)
  151. ));
  152. }
  153. protected function _getDoFavorite($aParams = array(), $bAllowedFavorite = true)
  154. {
  155. $bFavorited = isset($aParams['is_favorited']) && $aParams['is_favorited'] === true;
  156. $bShowDoFavoriteAsButtonSmall = isset($aParams['show_do_favorite_as_button_small']) && $aParams['show_do_favorite_as_button_small'] == true;
  157. $bShowDoFavoriteAsButton = !$bShowDoFavoriteAsButtonSmall && isset($aParams['show_do_favorite_as_button']) && $aParams['show_do_favorite_as_button'] == true;
  158. $bDisabled = !$bAllowedFavorite || ($bFavorited && !$this->isUndo());
  159. $sClass = '';
  160. if($bShowDoFavoriteAsButton)
  161. $sClass = 'bx-btn';
  162. else if ($bShowDoFavoriteAsButtonSmall)
  163. $sClass = 'bx-btn bx-btn-small';
  164. if($bDisabled)
  165. $sClass .= $bShowDoFavoriteAsButton || $bShowDoFavoriteAsButtonSmall ? ' bx-btn-disabled' : 'bx-favorite-disabled';
  166. return $this->_oTemplate->parseHtmlByName($this->_sTmplNameDoFavorite, array(
  167. 'style_prefix' => $this->_sStylePrefix,
  168. 'html_id' => $this->_aHtmlIds['do_link'],
  169. 'class' => $sClass,
  170. 'title' => bx_html_attribute(_t($this->_getTitleDoFavorite($bFavorited))),
  171. 'bx_if:show_onclick' => array(
  172. 'condition' => !$bDisabled,
  173. 'content' => array(
  174. 'onclick' => $this->getJsClick()
  175. )
  176. ),
  177. 'do_favorite' => $this->_getLabelDoFavorite($aParams),
  178. ));
  179. }
  180. protected function _getLabelCounter($iCount)
  181. {
  182. return _t('_favorite_counter', $iCount);
  183. }
  184. protected function _getLabelDoFavorite($aParams = array())
  185. {
  186. $bFavorited = isset($aParams['is_favorited']) && $aParams['is_favorited'] === true;
  187. return $this->_oTemplate->parseHtmlByName('favorite_do_favorite_label.html', array(
  188. 'bx_if:show_icon' => array(
  189. 'condition' => isset($aParams['show_do_favorite_icon']) && $aParams['show_do_favorite_icon'] == true,
  190. 'content' => array(
  191. 'name' => $this->_getIconDoFavorite($bFavorited)
  192. )
  193. ),
  194. 'bx_if:show_text' => array(
  195. 'condition' => isset($aParams['show_do_favorite_label']) && $aParams['show_do_favorite_label'] == true,
  196. 'content' => array(
  197. 'text' => _t($this->_getTitleDoFavorite($bFavorited))
  198. )
  199. )
  200. ));
  201. }
  202. protected function _getFavoritedBy()
  203. {
  204. $aTmplFavorites = array();
  205. $aFavorites = $this->_oQuery->getPerformedBy($this->getId());
  206. foreach($aFavorites as $aFavorite) {
  207. list($sUserName, $sUserUrl, $sUserIcon, $sUserUnit) = $this->_getAuthorInfo($aFavorite['author_id']);
  208. $aTmplFavorites[] = array(
  209. 'style_prefix' => $this->_sStylePrefix,
  210. 'user_unit' => $sUserUnit,
  211. );
  212. }
  213. if(empty($aTmplFavorites))
  214. $aTmplFavorites = MsgBox(_t('_Empty'));
  215. return $this->_oTemplate->parseHtmlByName('favorite_by_list.html', array(
  216. 'style_prefix' => $this->_sStylePrefix,
  217. 'bx_repeat:list' => $aTmplFavorites
  218. ));
  219. }
  220. }
  221. /** @} */