BxBaseFeature.php 7.4 KB

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