1
0

BxBaseModTemplateConfig.php 5.7 KB

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