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