BxBaseFeature.php 6.5 KB

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