BxBaseFeature.php 6.6 KB

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