BxBaseFavorite.php 9.2 KB

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