BxBaseModTemplateConfig.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 $_sKeyLogoAspectRatio;
  23. function __construct($aModule)
  24. {
  25. parent::__construct($aModule);
  26. $this->_iLogoWidth = 0;
  27. $this->_iLogoHeight = 0;
  28. $this->_fLogoAspectRatioDefault = BxDolDesigns::$fLogoAspectRatioDefault;
  29. }
  30. public function init(&$oDb)
  31. {
  32. $this->_oDb = &$oDb;
  33. $sPrefix = $this->getPrefix('option');
  34. $this->_sKeyLogoAspectRatio = $sPrefix . 'site_logo_aspect_ratio';
  35. $this->_iLogo = (int)$this->_oDb->getParam($sPrefix . 'site_logo');
  36. $this->_sLogoAlt = $this->_oDb->getParam($sPrefix . 'site_logo_alt');
  37. $this->_fLogoAspectRatio = (float)$this->_oDb->getParam($this->_sKeyLogoAspectRatio);
  38. }
  39. public function getLogoParams()
  40. {
  41. $sPrefix = $this->getPrefix('option');
  42. return [
  43. $sPrefix . 'site_logo',
  44. $sPrefix . 'site_logo_alt'
  45. ];
  46. }
  47. public function getLogoValues($sUrl, $aInfo)
  48. {
  49. if(empty($this->_iLogoHeight))
  50. $this->_iLogoHeight = $this->_calculateLogoValuesHeight();
  51. if(!$this->_fLogoAspectRatio)
  52. $this->_fLogoAspectRatio = $this->_calculateLogoValuesAspectRatio($sUrl, $aInfo);
  53. $this->_iLogoWidth = (int)ceil($this->_iLogoHeight * $this->_fLogoAspectRatio);
  54. return [
  55. $this->_iLogoWidth,
  56. $this->_iLogoHeight,
  57. $this->_fLogoAspectRatio
  58. ];
  59. }
  60. public function getLogo()
  61. {
  62. return $this->_iLogo;
  63. }
  64. public function getLogoAlt()
  65. {
  66. return $this->_sLogoAlt;
  67. }
  68. public function getLogoWidth()
  69. {
  70. return $this->_iLogoWidth;
  71. }
  72. public function getLogoHeight()
  73. {
  74. return $this->_iLogoHeight;
  75. }
  76. protected function _calculateLogoValuesHeight()
  77. {
  78. $sPrefix = $this->getPrefix('option');
  79. $sHeaderHeight = $this->_oDb->getParam($sPrefix . 'header_height');
  80. if(!$sHeaderHeight)
  81. return 0;
  82. $iHeaderHeight = $this->_str2px($sHeaderHeight);
  83. if(!$iHeaderHeight)
  84. return 0;
  85. $sHeaderPaddings = $this->_oDb->getParam($sPrefix . 'header_content_padding');
  86. if(!$sHeaderPaddings)
  87. return $iHeaderHeight;
  88. $aHeaderPaddings = explode(' ', $sHeaderPaddings);
  89. if(empty($aHeaderPaddings) || !is_array($aHeaderPaddings))
  90. return $iHeaderHeight;
  91. switch(count($aHeaderPaddings)) {
  92. case 1;
  93. case 2;
  94. $iPTop = $iPBottom = $this->_str2px($aHeaderPaddings[0]);
  95. break;
  96. case 3;
  97. case 4;
  98. $iPTop = $this->_str2px($aHeaderPaddings[0]);
  99. $iPBottom = $this->_str2px($aHeaderPaddings[2]);
  100. break;
  101. }
  102. return $iHeaderHeight - $iPTop - $iPBottom;
  103. }
  104. protected function _calculateLogoValuesAspectRatio($sUrl, $aInfo)
  105. {
  106. if(!$sUrl)
  107. return $this->_fLogoAspectRatioDefault;
  108. $iWidth = $iHeight = 0;
  109. if(strpos($sUrl, '.svg') !== false)
  110. list($iWidth, $iHeight) = bx_get_svg_image_size($sUrl);
  111. else if(isset($aInfo['mime_type']) && strncmp($aInfo['mime_type'], 'image/', 6) === 0)
  112. list($iWidth, $iHeight) = getimagesize($sUrl);
  113. if(!$iHeight)
  114. return $this->_fLogoAspectRatioDefault;
  115. $fResult = $iWidth / $iHeight;
  116. $this->_oDb->setParam($this->_sKeyLogoAspectRatio, $fResult);
  117. return $fResult;
  118. }
  119. protected function _str2px($sValue)
  120. {
  121. if(!$sValue)
  122. return 0;
  123. if(($iPosition = strpos($sValue, 'px')) !== false)
  124. return (int)substr($sValue, 0, $iPosition);
  125. if(($iPosition = strpos($sValue, 'rem')) !== false)
  126. return (int)ceil(16 * (float)substr($sValue, 0, $iPosition));
  127. return (int)$sValue;
  128. }
  129. }
  130. /** @} */