BxBaseFeature.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 BxDolFeature
  11. */
  12. class BxBaseFeature extends BxDolFeature
  13. {
  14. protected $_bCssJsAdded;
  15. protected $_sJsObjClass;
  16. protected $_sJsObjName;
  17. protected $_sStylePrefix;
  18. protected $_aHtmlIds;
  19. public function __construct($sSystem, $iId, $iInit = 1, $oTemplate = false)
  20. {
  21. parent::__construct($sSystem, $iId, $iInit, $oTemplate);
  22. $this->_bCssJsAdded = false;
  23. $this->_sJsObjClass = 'BxDolFeature';
  24. $this->_sJsObjName = 'oFeature' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  25. $this->_sStylePrefix = 'bx-feature';
  26. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  27. $this->_aHtmlIds = array(
  28. 'main' => 'bx-feature-' . $sHtmlId,
  29. 'do_link' => 'bx-feature-do-link-' . $sHtmlId
  30. );
  31. $this->_aElementDefaults = array(
  32. 'show_do_feature_as_button' => false,
  33. 'show_do_feature_as_button_small' => false,
  34. 'show_do_feature_icon' => true,
  35. 'show_do_feature_label' => false
  36. );
  37. $this->_aElementDefaultsApi = array_merge($this->_aElementDefaults, [
  38. 'show_do_feature_as_button' => true,
  39. ]);
  40. $this->_aElementParamsApi = ['is_featured'];
  41. $this->_sTmplContentElementBlock = $this->_oTemplate->getHtml('feature_element_block.html');
  42. $this->_sTmplContentElementInline = $this->_oTemplate->getHtml('feature_element_inline.html');
  43. $this->_sTmplContentDoActionLabel = $this->_oTemplate->getHtml('feature_do_feature_label.html');
  44. }
  45. public function getJsObjectName()
  46. {
  47. return $this->_sJsObjName;
  48. }
  49. public function getJsScript($bDynamicMode = false)
  50. {
  51. $aParams = array(
  52. 'sObjName' => $this->_sJsObjName,
  53. 'sSystem' => $this->getSystemName(),
  54. 'iAuthorId' => $this->_getAuthorId(),
  55. 'iObjId' => $this->getId(),
  56. 'sRootUrl' => BX_DOL_URL_ROOT,
  57. 'sStylePrefix' => $this->_sStylePrefix,
  58. 'aHtmlIds' => $this->_aHtmlIds
  59. );
  60. $sCode = "var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . ");";
  61. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  62. }
  63. public function getJsClick()
  64. {
  65. return $this->getJsObjectName() . '.feature(this)';
  66. }
  67. public function getElementBlock($aParams = array())
  68. {
  69. $aParams['usage'] = BX_DOL_FEATURED_USAGE_BLOCK;
  70. return $this->getElement($aParams);
  71. }
  72. public function getElementInline($aParams = array())
  73. {
  74. $aParams['usage'] = BX_DOL_FEATURED_USAGE_INLINE;
  75. return $this->getElement($aParams);
  76. }
  77. public function getElement($aParams = array())
  78. {
  79. $aParams = array_merge($this->_aElementDefaults, $aParams);
  80. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  81. $bShowDoFeatureAsButtonSmall = isset($aParams['show_do_feature_as_button_small']) && $aParams['show_do_feature_as_button_small'] == true;
  82. $bShowDoFeatureAsButton = !$bShowDoFeatureAsButtonSmall && isset($aParams['show_do_feature_as_button']) && $aParams['show_do_feature_as_button'] == true;
  83. $iObjectId = $this->getId();
  84. $iAuthorId = $this->_getAuthorId();
  85. $aFeature = $this->_oQuery->getFeature($iObjectId);
  86. $bCount = (int)$aFeature['count'] != 0;
  87. $bAllowedFeature = $this->isAllowedFeature();
  88. if(!$bAllowedFeature)
  89. return '';
  90. $aParams['is_featured'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
  91. $sTmplName = $this->{'_getTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_FEATURED_USAGE_DEFAULT)}();
  92. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  93. 'style_prefix' => $this->_sStylePrefix,
  94. 'html_id' => $this->_aHtmlIds['main'],
  95. 'class' => $this->_sStylePrefix . ($bShowDoFeatureAsButton ? '-button' : '-link') . ($bShowDoFeatureAsButtonSmall ? '-button-small' : ''),
  96. 'count' => $aFeature['count'],
  97. 'do_feature' => $this->_getDoFeature($aParams, $bAllowedFeature),
  98. 'script' => $this->getJsScript($bDynamicMode)
  99. ));
  100. }
  101. public function getElementAPI($aParams = [])
  102. {
  103. if(!($this->_bIsApi = bx_is_api()))
  104. return;
  105. if(!$this->isEnabled())
  106. return bx_api_get_msg('_feature_err_not_enabled');
  107. $aParams = array_merge($this->_aElementDefaultsApi, $aParams);
  108. $iObjectId = $this->getId();
  109. $iAuthorId = $this->_getAuthorId();
  110. $aFeature = $this->_oQuery->getFeature($iObjectId);
  111. $bCount = (int)$aFeature['count'] != 0;
  112. $isAllowedFeature = $this->isAllowedFeature();
  113. $aParams['is_featured'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
  114. //--- Do Feature
  115. $bDoFeature = $this->_isShowDoFeature($aParams, $isAllowedFeature, $bCount);
  116. $aDoFeature = $bDoFeature ? $this->_getDoFeature($aParams, $isAllowedFeature) : [];
  117. if(!$bDoFeature)
  118. return bx_api_get_msg('');
  119. return [
  120. 'type' => $this->_sType,
  121. 'system' => $this->_sSystem,
  122. 'object_id' => $this->_iId,
  123. 'ts' => microtime(true),
  124. 'params' => array_intersect_key($aParams, array_flip($this->_aElementParamsApi)),
  125. 'action' => $aDoFeature
  126. ];
  127. }
  128. protected function _getDoFeature($aParams = array(), $bAllowedFeature = true)
  129. {
  130. $bFeatured = isset($aParams['is_featured']) && $aParams['is_featured'] === true;
  131. $bShowDoFeatureAsButtonSmall = isset($aParams['show_do_feature_as_button_small']) && $aParams['show_do_feature_as_button_small'] == true;
  132. $bShowDoFeatureAsButton = !$bShowDoFeatureAsButtonSmall && isset($aParams['show_do_feature_as_button']) && $aParams['show_do_feature_as_button'] == true;
  133. $bDisabled = !$bAllowedFeature || ($bFeatured && !$this->isUndo());
  134. $sClass = '';
  135. if($bShowDoFeatureAsButton)
  136. $sClass = 'bx-btn';
  137. else if ($bShowDoFeatureAsButtonSmall)
  138. $sClass = 'bx-btn bx-btn-small';
  139. if($bDisabled)
  140. $sClass .= $bShowDoFeatureAsButton || $bShowDoFeatureAsButtonSmall ? ' bx-btn-disabled' : 'bx-feature-disabled';
  141. if($this->_bIsApi)
  142. return [
  143. 'is_undo' => $this->isUndo(),
  144. 'is_featured' => $bFeatured,
  145. 'is_disabled' => $bDisabled,
  146. 'title' => _t($this->_getTitleDoFeature($bFeatured)),
  147. ];
  148. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDoFeature($aParams), array(
  149. 'id' => $this->_aHtmlIds['do_link'],
  150. 'class' => $this->_sStylePrefix . '-do-feature ' . $sClass,
  151. 'title' => _t($this->_getTitleDoFeature($bFeatured)),
  152. 'onclick' => !$bDisabled ? $this->getJsClick() : ''
  153. ));
  154. }
  155. protected function _getLabelDoFeature($aParams = array())
  156. {
  157. $bFeatured = isset($aParams['is_featured']) && $aParams['is_featured'] === true;
  158. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentDoActionLabel(), array(
  159. 'style_prefix' => $this->_sStylePrefix,
  160. 'bx_if:show_icon' => array(
  161. 'condition' => isset($aParams['show_do_feature_icon']) && $aParams['show_do_feature_icon'] == true,
  162. 'content' => array(
  163. 'style_prefix' => $this->_sStylePrefix,
  164. 'icon' => $this->_oTemplate->getImageAuto($this->_getIconDoFeature($bFeatured))
  165. )
  166. ),
  167. 'bx_if:show_text' => array(
  168. 'condition' => isset($aParams['show_do_feature_label']) && $aParams['show_do_feature_label'] == true,
  169. 'content' => array(
  170. 'style_prefix' => $this->_sStylePrefix,
  171. 'text' => _t($this->_getTitleDoFeature($bFeatured))
  172. )
  173. )
  174. ));
  175. }
  176. protected function _isShowDoFeature($aParams, $isAllowedFeature, $bCount)
  177. {
  178. return (!isset($aParams['show_do_feature']) || (bool)$aParams['show_do_feature'] === true) && $isAllowedFeature;
  179. }
  180. }
  181. /** @} */