BxBaseFeature.php 6.8 KB

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