Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Easily Override any template or module by placing the file in the cus…
…tom directory
  • Loading branch information
soif committed Nov 26, 2012
1 parent 5c79c8a commit 06af707
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
1 change: 1 addition & 0 deletions custom/default_template/ReadMe.txt
@@ -0,0 +1 @@
Any template file(s) placed here will override the SimpleInvoices default template file(s) located in /templates/default/
1 change: 1 addition & 0 deletions custom/modules/ReadMe.txt
@@ -0,0 +1 @@
Any php module file(s) placed here will override the SimpleInvoices default module file(s) located in /modules/
84 changes: 59 additions & 25 deletions index.php
Expand Up @@ -30,6 +30,37 @@

require_once("./include/init.php");


/*
GetCustomPath: override template or module with custom one if it exists, else return default path if it exists
---------------------------------------------
@name: name or dir/name of the module or template (without extension)
@mode: template or module
*/

function GetCustomPath($name,$mode='template'){
$my_custom_path="./custom/";
$use_custom=1;
if($mode=='template'){
if($use_custom and file_exists("{$my_custom_path}default_template/{$name}.tpl")){
$out=".{$my_custom_path}default_template/{$name}.tpl";
}
elseif(file_exists("./templates/default/{$name}.tpl")){
$out="../templates/default/{$name}.tpl";
}
}
if($mode=='module'){
if($use_custom and file_exists("{$my_custom_path}modules/{$name}.php")){
$out="{$my_custom_path}modules/{$name}.php";
}
elseif(file_exists("./modules/{$name}.php")){
$out="./modules/{$name}.php";
}
}
return $out;
}


foreach($config->extension as $extension)
{
/*
Expand Down Expand Up @@ -166,9 +197,11 @@
/*
* If no extension php file for requested file load the normal php file if it exists
*/
if( ($extensionInvoiceTemplateFile == 0) AND (file_exists("./modules/invoices/template.php")) )

if( ($extensionInvoiceTemplateFile == 0) AND ($my_path=GetCustomPath("invoices/template",'module') ) )
{
include_once("./modules/invoices/template.php");
/* (soif) This /modules/invoices/template.php is empty : Should we really keep it? */
include_once($my_path);
}


Expand Down Expand Up @@ -199,9 +232,8 @@
/*
* If no extension php file for requested file load the normal php file if it exists
*/
if($extensionXml == 0)
{
include("./modules/$module/$view.php");
if($extensionXml == 0 and $my_path=GetCustomPath("$module/$view",'module')){
include($my_path);
}

exit(0);
Expand All @@ -210,7 +242,6 @@
* xml or ajax page request - end
*/


$file= "$module/$view";

/*
Expand Down Expand Up @@ -247,7 +278,6 @@
/*
* If extension is enabled load its javascript files - end
*/

/*
* Header - start
*/
Expand All @@ -270,11 +300,12 @@
}
}
/*
* If no extension php file for requested file load the normal php file if it exists
* If no extension php file for requested file load the normal template file if it exists
*/

if($extensionHeader == 0)
{
$smarty -> $smarty_output("../templates/default/header.tpl");
$smarty -> $smarty_output(GetCustomPath('header'));
}

}
Expand Down Expand Up @@ -304,8 +335,6 @@

//echo "Enabled:".$value['name']."<br><br>";
if(file_exists("./extensions/$extension->name/modules/$module/$view.php")) {



include_once("./extensions/$extension->name/modules/$module/$view.php");
$extensionPHPFile++;
Expand All @@ -315,9 +344,8 @@
/*
* If no extension php file for requested file load the normal php file if it exists
*/
if( ($extensionPHPFile == 0) AND (file_exists("./modules/$module/$view.php")) )
{
include_once("./modules/$module/$view.php");
if( ($extensionPHPFile == 0) and $my_path=GetCustomPath("$module/$view",'module') ){
include($my_path);
}

/*
Expand Down Expand Up @@ -391,7 +419,7 @@
*/
if($extensionMenu == "0")
{
$smarty -> $smarty_output("../templates/default/menu.tpl");
$smarty -> $smarty_output(GetCustomPath('menu'));
}
}
/*
Expand Down Expand Up @@ -425,7 +453,7 @@
*/
if($extensionMain == "0")
{
$smarty -> $smarty_output("../templates/default/main.tpl");
$smarty -> $smarty_output(GetCustomPath('main'));
}
}

Expand All @@ -444,6 +472,7 @@
* --if = 0 after checking all extensions then show default
*/
$extensionTemplates = 0;
$my_tpl_path='';
foreach($config->extension as $extension)
{
/*
Expand All @@ -453,8 +482,8 @@
{
if(file_exists("./extensions/$extension->name/templates/default/$module/$view.tpl"))
{
$path = "../extensions/$extension->name/templates/default/$module/";
$tplDirectory = "extensions/$extension->name/";
$path = "../extensions/$extension->name/templates/default/$module/";
$my_tpl_path="../extensions/{$extension->name}/templates/default/$module/$view.tpl";
$extensionTemplates++;
}
}
Expand All @@ -464,15 +493,17 @@
* TODO Note: if more than one extension has got a template for the requested file than thats trouble :(
* - we really need a better extensions system
*/
if( ($extensionTemplates == 0) AND (file_exists("./templates/default/$module/$view.tpl")) )

if( $extensionTemplates == 0 )
{
$path = "../templates/default/$module/";
$tplDirectory = "";
$extensionTemplates++;
if($my_tpl_path=GetCustomPath("$module/$view")){
$path = dirname($my_tpl_path).'/';
$extensionTemplates++;
}
}

$smarty->assign("path",$path);
$smarty -> $smarty_output("../".$tplDirectory."templates/default/$module/$view.tpl");
$smarty -> $smarty_output($my_tpl_path);

// If no smarty template - add message - onyl uncomment for dev - commented out for release
if ($extensionTemplates == 0 )
Expand Down Expand Up @@ -509,12 +540,15 @@
*/
if($extensionFooter == 0)
{
$smarty -> $smarty_output("../templates/default/footer.tpl");
$smarty -> $smarty_output(GetCustomPath('footer'));
}

}


/*
* Footer - end
*/



0 comments on commit 06af707

Please sign in to comment.