Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

ThemeEngine.php

Go to the documentation of this file.
00001 <?php
00002 /* $Id: ThemeEngine.php,v 1.2 2003/10/03 15:27:41 mikko Exp $ */
00003 /*
00004  * ImaComm - a web based photo album software
00005  * Copyright (C) 2003, Mikko Kokkonen
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or (at
00010  * your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00021 class ThemeEngine
00022 {
00023         var $theme;
00024         var $template;
00025 
00026         function ThemeEngine()
00027         {
00028                 global $config;
00029 
00030                 $this->theme = $config->option("theme");
00031         }
00032 
00033         function loadTemplate($template)
00034         {
00035                 $tpl = $this->theme."/templates/$template.php";
00036 
00037                 if (file_exists($tpl))
00038                 {
00039                         require_once "$tpl";
00040                         return $template;
00041                 }
00042                 else
00043                 {
00044                         return deverror("Unable to open template <b>$tpl</b>!");
00045                 }
00046         }
00047 
00048         // {{{ string loadTemplateNew($template)
00059         function loadTemplateNew($template)
00060         {
00061                 $tpl = $this->theme."/templates/$template.tpl";
00062 
00063                 if (file_exists($tpl))
00064                 {
00065                         $file = file($tpl);
00066                         foreach ($file as $ln => $line)
00067                         {
00068                                 $this->template[] = trim($line);
00069                         }
00070                         return null;
00071                 }
00072                 else
00073                 {
00074                         return translate("Template $template doesn't exists!");
00075                 }
00076         }
00077         // }}}
00078 
00079         // {{{ string applyTemplate($tags, $values)
00080         function applyTemplate($items, $tags)
00081         {
00082                 if (!is_array($this->template) || !is_array($items) || !is_array($tags))
00083                 {
00084                         return null;
00085                 }
00086 
00087                 // Let's start to parse...
00088                 $rowloop = false;
00089                 $itemloop = null;
00090                 
00091                 // Load item loop block...
00092                 for ($l = 0; $l < count($this->template); $l++)
00093                 {
00094                         $line = $this->template[$l];
00095 
00096                         if ($line == "<@row_begin>")
00097                         {
00098                                 $rowloop = true;
00099                                 continue;
00100                         }
00101                         if ($line == "<@row_end>")
00102                         {
00103                                 $rowloop = false;
00104                                 // Loop all items...
00105                                 
00106                         }
00107                         if ($rowloop)
00108                         {
00109                                 $itemloop[] = $line;
00110                         }
00111                 }
00112                 
00113                 // Generate item loop block...
00114                 $rowloop = false;
00115                 for ($l = 0; $l < count($this->template); $l++)
00116                 {
00117                         $line = $this->template[$l];
00118                         if ($line == "<@row_begin>")
00119                         {
00120                                 $rowloop = true;
00121                                 continue;
00122                         }
00123                         if ($line == "<@row_end>")
00124                         {
00125                                 $rowloop = false;
00126                                 continue;
00127                         }
00128                         if ($rowloop)
00129                         {
00130                                 if (!$itemsdone)
00131                                 {
00132                                         print "DG: Row loop on line $l<br />";
00133                                         // Generate search block..
00134                                         foreach ($tags as $tag)
00135                                         {
00136                                                 $search[] = "/<@$tag>/";
00137                                         }
00138                                         // Put here all our items...
00139                                         for ($i = 0; $i < count($items); $i++)
00140                                         {
00141                                                 print "DG: Item $i<br />";
00142                                                 print dprint($items[$i]);
00143                                                 $block = null;
00144                                                 // How many item block do we have?
00145                                                 $count = preg_match('/\<@item\>/', implode("\n", $itemloop), $match);
00146                                                 print "$count<br />";
00147                                                 for ($c = 0; $c < $count; $c++)
00148                                                 {
00149                                                         // Generate replace block...
00150                                                         $replace = $items[$l];
00151         
00152                                                         // Substitute...
00153                                                         $block .= preg_replace($search, $replace, $itemloop, 1);
00154                                                         $l++;
00155                                                 }
00156                                                 $buf .= $block;
00157                                         }
00158                                         $itemsdone = true;
00159                                 }
00160                         }
00161                         else
00162                         {
00163                                 $buf .= $line;
00164                         }
00165                 }
00166                 print htmlentities($buf);
00167         }
00168         // }}}
00169 }
00170 ?>

Generated on Sun Oct 19 11:08:01 2003 for ImaComm by doxygen1.3