From cb7650e2f8e23217289e2326cc3fbea2a2295dc8 Mon Sep 17 00:00:00 2001 From: LouisGac Date: Fri, 23 Jun 2017 18:33:41 +0200 Subject: [PATCH] Dev: added methods to retreive the path for a template file (recursive through template inheritance tree) --- application/models/TemplateConfiguration.php | 60 ++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/application/models/TemplateConfiguration.php b/application/models/TemplateConfiguration.php index ccc351a3759..f6f7235625e 100644 --- a/application/models/TemplateConfiguration.php +++ b/application/models/TemplateConfiguration.php @@ -179,6 +179,25 @@ public function getLayoutForScreen($sScreen) return false; } + /** + * Retreives the absolute path for a file to edit (current template, mother template, etc) + * Also perform few checks (permission to edit? etc) + * + * @param string $sfile relative path to the file to edit + */ + public function getFilePathForEdition($sFile, $aAllowedFiles=null) + { + + // Check if the file is allowed for edition ($aAllowedFiles is produced via getValidScreenFiles() ) + if (is_array($aAllowedFiles)){ + if (!in_array($sFile, $aAllowedFiles)){ + return false; + } + } + + return $this->getFilePath($sFile, $this); + } + /** * This function will update the config file of a given template so that it extends another one * @@ -571,4 +590,45 @@ private function getFrameworkPackages($oTemplate, $dir="") return array(); } + private function getTemplateForFile($sFile, $oRTemplate) + { + while (!file_exists($oRTemplate->path.'/'.$sFile) && !file_exists($oRTemplate->viewPath.$sFile)){ + $oMotherTemplate = $oRTemplate->oMotherTemplate; + if(!($oMotherTemplate instanceof TemplateConfiguration)){ + return false; + break; + } + $oRTemplate = $oMotherTemplate; + } + + return $oRTemplate; + } + + /** + * Get the file path for a given template. + * It will check if css/js (relative to path), or view (view path) + * It will search for current template and mother templates + * + * @param string $sFile relative path to the file + * @param string $oTemplate the template where to look for (and its mother templates) + */ + private function getFilePath($sFile, $oTemplate) + { + // Remove relative path + $sFile = trim($sFile, '.'); + $sFile = trim($sFile, '/'); + + // Retreive the correct template for this file (can be a mother template) + $oTemplate = $this->getTemplateForFile($sFile, $oTemplate); + + if($oTemplate instanceof TemplateConfiguration){ + if(file_exists($oTemplate->path.'/'.$sFile)){ + return $oTemplate->path.'/'.$sFile; + }elseif(file_exists($oTemplate->viewPath.$sFile)){ + return $oTemplate->viewPath.$sFile; + } + } + return false; + } + }