BxBaseFeature.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 static $_sTmplContentElementBlock;
  15. protected static $_sTmplContentElementInline;
  16. protected static $_sTmplContentDoFeatureLabel;
  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 = 'BxDolFeature';
  28. $this->_sJsObjName = 'oFeature' . bx_gen_method_name($sSystem, array('_' , '-')) . $iId;
  29. $this->_sStylePrefix = 'bx-feature';
  30. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sSystem) . '-' . $iId;
  31. $this->_aHtmlIds = array(
  32. 'main' => 'bx-feature-' . $sHtmlId,
  33. 'do_link' => 'bx-feature-do-link-' . $sHtmlId
  34. );
  35. $this->_aElementDefaults = array(
  36. 'show_do_feature_as_button' => false,
  37. 'show_do_feature_as_button_small' => false,
  38. 'show_do_feature_icon' => true,
  39. 'show_do_feature_label' => false
  40. );
  41. if(empty(self::$_sTmplContentElementBlock))
  42. self::$_sTmplContentElementBlock = $this->_oTemplate->getHtml('feature_element_block.html');
  43. if(empty(self::$_sTmplContentElementInline))
  44. self::$_sTmplContentElementInline = $this->_oTemplate->getHtml('feature_element_inline.html');
  45. if(empty(self::$_sTmplContentDoFeatureLabel))
  46. self::$_sTmplContentDoFeatureLabel = $this->_oTemplate->getHtml('feature_do_feature_label.html');
  47. }
  48. public function addCssJs($bDynamicMode = false)
  49. {
  50. if($bDynamicMode || $this->_bCssJsAdded)
  51. return;
  52. $this->_oTemplate->addJs(array('jquery.anim.js', 'BxDolFeature.js'));
  53. $this->_oTemplate->addCss(array('feature.css'));
  54. $this->_bCssJsAdded = true;
  55. }
  56. public function getJsObjectName()
  57. {
  58. return $this->_sJsObjName;
  59. }
  60. public function getJsScript($bDynamicMode = false)
  61. {
  62. $aParams = array(
  63. 'sObjName' => $this->_sJsObjName,
  64. 'sSystem' => $this->getSystemName(),
  65. 'iAuthorId' => $this->_getAuthorId(),
  66. 'iObjId' => $this->getId(),
  67. 'sRootUrl' => BX_DOL_URL_ROOT,
  68. 'sStylePrefix' => $this->_sStylePrefix,
  69. 'aHtmlIds' => $this->_aHtmlIds
  70. );
  71. $sCode = $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . ");";
  72. if($bDynamicMode) {
  73. $sCode = "var " . $this->_sJsObjName . " = null;
  74. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('BxDolFeature.js'), BX_ESCAPE_STR_APOS) . "', function(data, textStatus, jqxhr) {
  75. bx_get_style('" . bx_js_string($this->_oTemplate->getCssUrl('feature.css'), BX_ESCAPE_STR_APOS) . "');
  76. " . $sCode . "
  77. }); ";
  78. }
  79. else
  80. $sCode = "var " . $sCode;
  81. $this->addCssJs($bDynamicMode);
  82. return $this->_oTemplate->_wrapInTagJsCode($sCode);
  83. }
  84. public function getJsClick()
  85. {
  86. return $this->getJsObjectName() . '.feature(this)';
  87. }
  88. public function getElementBlock($aParams = array())
  89. {
  90. $aParams['usage'] = BX_DOL_FEATURED_USAGE_BLOCK;
  91. return $this->getElement($aParams);
  92. }
  93. public function getElementInline($aParams = array())
  94. {
  95. $aParams['usage'] = BX_DOL_FEATURED_USAGE_INLINE;
  96. return $this->getElement($aParams);
  97. }
  98. public function getElement($aParams = array())
  99. {
  100. $aParams = array_merge($this->_aElementDefaults, $aParams);
  101. $bDynamicMode = isset($aParams['dynamic_mode']) && $aParams['dynamic_mode'] === true;
  102. $bShowDoFeatureAsButtonSmall = isset($aParams['show_do_feature_as_button_small']) && $aParams['show_do_feature_as_button_small'] == true;
  103. $bShowDoFeatureAsButton = !$bShowDoFeatureAsButtonSmall && isset($aParams['show_do_feature_as_button']) && $aParams['show_do_feature_as_button'] == true;
  104. $iObjectId = $this->getId();
  105. $iAuthorId = $this->_getAuthorId();
  106. $aFeature = $this->_oQuery->getFeature($iObjectId);
  107. $bCount = (int)$aFeature['count'] != 0;
  108. $bAllowedFeature = $this->isAllowedFeature();
  109. if(!$bAllowedFeature)
  110. return '';
  111. $aParams['is_featured'] = $this->isPerformed($iObjectId, $iAuthorId) ? true : false;
  112. $sTmplName = self::${'_sTmplContentElement' . bx_gen_method_name(!empty($aParams['usage']) ? $aParams['usage'] : BX_DOL_FEATURED_USAGE_DEFAULT)};
  113. return $this->_oTemplate->parseHtmlByContent($sTmplName, array(
  114. 'style_prefix' => $this->_sStylePrefix,
  115. 'html_id' => $this->_aHtmlIds['main'],
  116. 'class' => $this->_sStylePrefix . ($bShowDoFeatureAsButton ? '-button' : '-link') . ($bShowDoFeatureAsButtonSmall ? '-button-small' : ''),
  117. 'count' => $aFeature['count'],
  118. 'do_feature' => $this->_getDoFeature($aParams, $bAllowedFeature),
  119. 'script' => $this->getJsScript($bDynamicMode)
  120. ));
  121. }
  122. protected function _getDoFeature($aParams = array(), $bAllowedFeature = true)
  123. {
  124. $bFeatured = isset($aParams['is_featured']) && $aParams['is_featured'] === true;
  125. $bShowDoFeatureAsButtonSmall = isset($aParams['show_do_feature_as_button_small']) && $aParams['show_do_feature_as_button_small'] == true;
  126. $bShowDoFeatureAsButton = !$bShowDoFeatureAsButtonSmall && isset($aParams['show_do_feature_as_button']) && $aParams['show_do_feature_as_button'] == true;
  127. $bDisabled = !$bAllowedFeature || ($bFeatured && !$this->isUndo());
  128. $sClass = '';
  129. if($bShowDoFeatureAsButton)
  130. $sClass = 'bx-btn';
  131. else if ($bShowDoFeatureAsButtonSmall)
  132. $sClass = 'bx-btn bx-btn-small';
  133. if($bDisabled)
  134. $sClass .= $bShowDoFeatureAsButton || $bShowDoFeatureAsButtonSmall ? ' bx-btn-disabled' : 'bx-feature-disabled';
  135. return $this->_oTemplate->parseLink('javascript:void(0)', $this->_getLabelDoFeature($aParams), array(
  136. 'id' => $this->_aHtmlIds['do_link'],
  137. 'class' => $this->_sStylePrefix . '-do-feature ' . $sClass,
  138. 'title' => _t($this->_getTitleDoFeature($bFeatured)),
  139. 'onclick' => !$bDisabled ? $this->getJsClick() : ''
  140. ));
  141. }
  142. protected function _getLabelDoFeature($aParams = array())
  143. {
  144. $bFeatured = isset($aParams['is_featured']) && $aParams['is_featured'] === true;
  145. return $this->_oTemplate->parseHtmlByContent(self::$_sTmplContentDoFeatureLabel, array(
  146. 'bx_if:show_icon' => array(
  147. 'condition' => isset($aParams['show_do_feature_icon']) && $aParams['show_do_feature_icon'] == true,
  148. 'content' => array(
  149. 'name' => $this->_getIconDoFeature($bFeatured)
  150. )
  151. ),
  152. 'bx_if:show_text' => array(
  153. 'condition' => isset($aParams['show_do_feature_label']) && $aParams['show_do_feature_label'] == true,
  154. 'content' => array(
  155. 'text' => _t($this->_getTitleDoFeature($bFeatured))
  156. )
  157. )
  158. ));
  159. }
  160. }
  161. /** @} */