1
0

BxBaseModTemplateConfig.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 BaseTemplate Base classes for template modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolDesigns');
  12. bx_import('BxBaseModGeneralConfig');
  13. class BxBaseModTemplateConfig extends BxBaseModGeneralConfig
  14. {
  15. protected $_oDb;
  16. protected $_iLogo;
  17. protected $_sLogoAlt;
  18. protected $_iLogoWidth;
  19. protected $_iLogoHeight;
  20. protected $_fLogoAspectRatio;
  21. protected $_fLogoAspectRatioDefault;
  22. protected $_iMark;
  23. protected $_iMarkWidth;
  24. protected $_iMarkHeight;
  25. protected $_fMarkAspectRatio;
  26. protected $_fMarkAspectRatioDefault;
  27. protected $_sKeyLogoAspectRatio;
  28. protected $_sKeyMarkAspectRatio;
  29. function __construct($aModule)
  30. {
  31. parent::__construct($aModule);
  32. $this->_iLogoWidth = 0;
  33. $this->_iLogoHeight = 0;
  34. $this->_fLogoAspectRatioDefault = BxDolDesigns::$fLogoAspectRatioDefault;
  35. $this->_iMarkWidth = 0;
  36. $this->_iMarkHeight = 0;
  37. $this->_fMarkAspectRatioDefault = BxDolDesigns::$fMarkAspectRatioDefault;
  38. }
  39. public function init(&$oDb)
  40. {
  41. $this->_oDb = &$oDb;
  42. $sPrefix = $this->getPrefix('option');
  43. $this->_sKeyLogoAspectRatio = $sPrefix . 'site_logo_aspect_ratio';
  44. $this->_sKeyMarkAspectRatio = $sPrefix . 'site_mark_aspect_ratio';
  45. $this->_iLogo = (int)$this->_oDb->getParam($sPrefix . 'site_logo');
  46. $this->_fLogoAspectRatio = (float)$this->_oDb->getParam($this->_sKeyLogoAspectRatio);
  47. $this->_iMark = (int)$this->_oDb->getParam($sPrefix . 'site_mark');
  48. $this->_fMarkAspectRatio = (float)$this->_oDb->getParam($this->_sKeyMarkAspectRatio);
  49. $this->_sLogoAlt = $this->_oDb->getParam($sPrefix . 'site_logo_alt');
  50. }
  51. public function getLogoParams()
  52. {
  53. $sPrefix = $this->getPrefix('option');
  54. return [
  55. $sPrefix . 'site_logo',
  56. $sPrefix . 'site_mark',
  57. $sPrefix . 'site_logo_alt'
  58. ];
  59. }
  60. public function getLogoValues($sUrlLogo, $aInfoLogo, $sUrlMark = '', $aInfoMark = [])
  61. {
  62. $iDefaultHeight = $this->_calculateValuesHeight();
  63. if(empty($this->_iLogoHeight))
  64. $this->_iLogoHeight = $iDefaultHeight;
  65. if(!$this->_fLogoAspectRatio)
  66. $this->_fLogoAspectRatio = $this->_calculateValuesAspectRatio('logo', $sUrlLogo, $aInfoLogo);
  67. $this->_iLogoWidth = (int)ceil($this->_iLogoHeight * $this->_fLogoAspectRatio);
  68. if(!empty($sUrlMark) && !empty($aInfoMark)) {
  69. if(empty($this->_iMarkHeight))
  70. $this->_iMarkHeight = $iDefaultHeight;
  71. if(!$this->_fMarkAspectRatio)
  72. $this->_fMarkAspectRatio = $this->_calculateValuesAspectRatio('mark', $sUrlMark, $aInfoMark);
  73. $this->_iMarkWidth = (int)ceil($this->_iMarkHeight * $this->_fMarkAspectRatio);
  74. }
  75. return [
  76. $this->_iLogoWidth,
  77. $this->_iLogoHeight,
  78. $this->_fLogoAspectRatio,
  79. $this->_iMarkWidth,
  80. $this->_iMarkHeight,
  81. $this->_fMarkAspectRatio
  82. ];
  83. }
  84. public function getLogo()
  85. {
  86. return $this->_iLogo;
  87. }
  88. public function getLogoAlt()
  89. {
  90. return $this->_sLogoAlt;
  91. }
  92. public function getLogoWidth()
  93. {
  94. return $this->_iLogoWidth;
  95. }
  96. public function getLogoHeight()
  97. {
  98. return $this->_iLogoHeight;
  99. }
  100. protected function _calculateValuesHeight()
  101. {
  102. $sPrefix = $this->getPrefix('option');
  103. $sHeaderHeight = $this->_oDb->getParam($sPrefix . 'header_height');
  104. if(!$sHeaderHeight)
  105. return 0;
  106. $iHeaderHeight = $this->_str2px($sHeaderHeight);
  107. if(!$iHeaderHeight)
  108. return 0;
  109. $sHeaderPaddings = $this->_oDb->getParam($sPrefix . 'header_content_padding');
  110. if(!$sHeaderPaddings)
  111. return $iHeaderHeight;
  112. $aHeaderPaddings = explode(' ', $sHeaderPaddings);
  113. if(empty($aHeaderPaddings) || !is_array($aHeaderPaddings))
  114. return $iHeaderHeight;
  115. switch(count($aHeaderPaddings)) {
  116. case 1;
  117. case 2;
  118. $iPTop = $iPBottom = $this->_str2px($aHeaderPaddings[0]);
  119. break;
  120. case 3;
  121. case 4;
  122. $iPTop = $this->_str2px($aHeaderPaddings[0]);
  123. $iPBottom = $this->_str2px($aHeaderPaddings[2]);
  124. break;
  125. }
  126. return $iHeaderHeight - $iPTop - $iPBottom;
  127. }
  128. protected function _calculateValuesAspectRatio($sType, $sUrl, $aInfo)
  129. {
  130. $sTypeUc = ucfirst($sType);
  131. if(!$sUrl)
  132. return $this->{'_f' . $sTypeUc . 'AspectRatioDefault'};
  133. $iWidth = $iHeight = 0;
  134. if(strpos($sUrl, '.svg') !== false)
  135. list($iWidth, $iHeight) = bx_get_svg_image_size($sUrl);
  136. else if(isset($aInfo['mime_type']) && strncmp($aInfo['mime_type'], 'image/', 6) === 0)
  137. list($iWidth, $iHeight) = getimagesize($sUrl);
  138. if(!$iHeight)
  139. return $this->{'_f' . $sTypeUc . 'AspectRatioDefault'};
  140. $fResult = $iWidth / $iHeight;
  141. $this->_oDb->setParam($this->{'_sKey' . $sTypeUc . 'AspectRatio'}, $fResult);
  142. return $fResult;
  143. }
  144. protected function _str2px($sValue)
  145. {
  146. if(!$sValue)
  147. return 0;
  148. if(($iPosition = strpos($sValue, 'px')) !== false)
  149. return (int)substr($sValue, 0, $iPosition);
  150. if(($iPosition = strpos($sValue, 'rem')) !== false)
  151. return (int)ceil(16 * (float)substr($sValue, 0, $iPosition));
  152. return (int)$sValue;
  153. }
  154. }
  155. /** @} */