BxBaseFavorite.php 10 KB

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