BxDolModuleTemplate.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolModuleTemplate extends BxDolTemplate
  10. {
  11. protected $_oDb;
  12. protected $_oConfig;
  13. protected $_bObStarted = 0;
  14. protected $_oModule;
  15. /*
  16. * Constructor.
  17. */
  18. public function __construct(&$oConfig, &$oDb, $sRootPath = BX_DIRECTORY_PATH_ROOT, $sRootUrl = BX_DOL_URL_ROOT)
  19. {
  20. parent::__construct($sRootPath, $sRootUrl);
  21. $this->_oDb = &$oDb;
  22. $this->_oConfig = &$oConfig;
  23. $sName = $oConfig->getName();
  24. $sHomePath = $oConfig->getHomePath();
  25. $sHomeUrl = $oConfig->getHomeUrl();
  26. if(method_exists($this, 'addLocationBase'))
  27. $this->addLocationBase();
  28. $this->addLocation($sName, $sHomePath, $sHomeUrl);
  29. $this->addLocationJs($sName, $sHomePath . 'js/', $sHomeUrl . 'js/');
  30. }
  31. public function getModule()
  32. {
  33. if (!$this->_oModule) {
  34. $sName = $this->_oConfig->getName();
  35. $this->_oModule = BxDolModule::getInstance($sName);
  36. }
  37. return $this->_oModule;
  38. }
  39. /**
  40. * Initialize module template engine.
  41. * Note. The method is executed with the system, you shouldn't execute it in your subclasses.
  42. */
  43. public function init()
  44. {
  45. $this->loadTemplates();
  46. bx_import('BxTemplFunctions');
  47. $this->_oTemplateFunctions = BxTemplFunctions::getInstance($this);
  48. }
  49. public function addLocationBase() {}
  50. function addCssSystem($mixedFiles, $bDynamic = false)
  51. {
  52. $sResult = '';
  53. $bResult = false;
  54. foreach($this->getLocations() as $sKey => $aLocation) {
  55. $mixedResult = $this->_addFiles(BxDolTemplate::getInstance(), 'addCssSystem', 'isLocation', 'addLocation', 'removeLocation', '', $mixedFiles, $bDynamic, true, [$sKey => $aLocation]);
  56. if($bDynamic)
  57. $sResult .= $mixedResult;
  58. else
  59. $bResult |= $mixedResult;
  60. }
  61. return $bDynamic ? $sResult : $bResult;
  62. }
  63. function addCss($mixedFiles, $bDynamic = false)
  64. {
  65. $sResult = '';
  66. $bResult = false;
  67. foreach($this->getLocations() as $sKey => $aLocation) {
  68. $mixedResult = $this->_addFiles(BxDolTemplate::getInstance(), 'addCss', 'isLocation', 'addLocation', 'removeLocation', '', $mixedFiles, $bDynamic, true, [$sKey => $aLocation]);
  69. if($bDynamic)
  70. $sResult .= $mixedResult;
  71. else
  72. $bResult |= $mixedResult;
  73. }
  74. return $bDynamic ? $sResult : $bResult;
  75. }
  76. function addJsSystem($mixedFiles, $bDynamic = false)
  77. {
  78. return $this->_addFiles(BxDolTemplate::getInstance(), 'addJsSystem', 'isLocationJs', 'addLocationJs', 'removeLocationJs', 'js/', $mixedFiles, $bDynamic, true);
  79. }
  80. function addJs($mixedFiles, $bDynamic = false)
  81. {
  82. return $this->_addFiles(BxDolTemplate::getInstance(), 'addJs', 'isLocationJs', 'addLocationJs', 'removeLocationJs', 'js/', $mixedFiles, $bDynamic, true);
  83. }
  84. function addJsTranslation($mixedKey, $bDynamic = false)
  85. {
  86. return BxDolTemplate::getInstance()->addJsTranslation($mixedKey, $bDynamic);
  87. }
  88. function addStudioCss($mixedFiles, $bDynamic = false, $bSearchInModule = true)
  89. {
  90. return $this->_addFiles(BxDolStudioTemplate::getInstance(), 'addCss', 'isLocation', 'addLocation', 'removeLocation', '', $mixedFiles, $bDynamic, $bSearchInModule);
  91. }
  92. function addStudioCssSystem($mixedFiles, $bDynamic = false, $bSearchInModule = true)
  93. {
  94. return $this->_addFiles(BxDolStudioTemplate::getInstance(), 'addCssSystem', 'isLocation', 'addLocation', 'removeLocation', '', $mixedFiles, $bDynamic, $bSearchInModule);
  95. }
  96. function addStudioJs($mixedFiles, $bDynamic = false, $bSearchInModule = true)
  97. {
  98. return $this->_addFiles(BxDolStudioTemplate::getInstance(), 'addJs', 'isLocationJs', 'addLocationJs', 'removeLocationJs', 'js/', $mixedFiles, $bDynamic, $bSearchInModule);
  99. }
  100. function addStudioJsSystem($mixedFiles, $bDynamic = false, $bSearchInModule = true)
  101. {
  102. return $this->_addFiles(BxDolStudioTemplate::getInstance(), 'addJsSystem', 'isLocationJs', 'addLocationJs', 'removeLocationJs', 'js/', $mixedFiles, $bDynamic, $bSearchInModule);
  103. }
  104. function addStudioJsTranslation($mixedKey)
  105. {
  106. BxDolStudioTemplate::getInstance()->addJsTranslation($mixedKey);
  107. }
  108. function _addFiles($oTemplate, $sFuncAddFiles, $sFuncIsLocation, $sFuncAddLocation, $sFuncRemoveLocation, $sPath, $mixedFiles, $bDynamic = false, $bSearchInModule = true, $aLocations = array())
  109. {
  110. if($bSearchInModule) {
  111. if(empty($aLocations))
  112. $aLocations = [
  113. $this->_oConfig->getName() => [
  114. 'path' => $this->_oConfig->getHomePath() . $sPath,
  115. 'url' => $this->_oConfig->getHomeUrl() . $sPath
  116. ]
  117. ];
  118. foreach($aLocations as $sLocationKey => $aLocation) {
  119. if($oTemplate->$sFuncIsLocation($sLocationKey))
  120. continue;
  121. $oTemplate->$sFuncAddLocation($sLocationKey, $aLocation['path'], $aLocation['url']);
  122. $aLocations[$sLocationKey]['delete'] = true;
  123. }
  124. }
  125. $mixedResult = $oTemplate->$sFuncAddFiles($mixedFiles, $bDynamic);
  126. if($bSearchInModule && !empty($aLocations))
  127. foreach($aLocations as $sLocationKey => $aLocation)
  128. if(isset($aLocation['delete']) && $aLocation['delete'] === true)
  129. $oTemplate->$sFuncRemoveLocation($sLocationKey);
  130. return $mixedResult;
  131. }
  132. function addStudioInjection($sKey, $sType, $sData, $iReplace = 0)
  133. {
  134. BxDolStudioTemplate::getInstance()->aPage['injections']['page_0'][$sKey][] = array(
  135. 'page_index' => 0,
  136. 'key' => $sKey,
  137. 'type' => $sType,
  138. 'data' => $sData,
  139. 'replace' => $iReplace
  140. );
  141. }
  142. function pageStart ()
  143. {
  144. if (0 == $this->_bObStarted) {
  145. ob_start ();
  146. $this->_bObStarted = 1;
  147. }
  148. }
  149. function pageEnd ($isGetContent = true)
  150. {
  151. if (1 == $this->_bObStarted) {
  152. $sRet = '';
  153. if ($isGetContent)
  154. $sRet = ob_get_clean();
  155. else
  156. ob_end_clean();
  157. $this->_bObStarted = 0;
  158. return $sRet;
  159. }
  160. }
  161. public function addCssJs()
  162. {
  163. }
  164. public function isMethodExists($s)
  165. {
  166. return method_exists($this->_oProxifiedObject, $s);
  167. }
  168. }
  169. /** @} */