1
0

BxBaseModTemplateConfig.php 5.7 KB

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