/* SiteClass.php Author: Geraud P. Krawezik (gk@lri.fr) */ if(!defined("_SiteClass_php_")) { define ("_SiteClass_php_", true); require("./phptmpl/SectionClass.php"); require("./phptmpl/SubsectionClass.php"); require("./phptmpl/common.php"); require("./phptmpl/error.php"); Class Site { var $title; // The title as displayed in the browser header var $name; // The internal name, used for example in generic structures var $sections; // The sections included inside the site var $url; // The complete URL of the site var $hiddenParameters; // section and subsection passed through post? (or get?) var $templatePage; // The starting template, describing the whole page layout var $templatesDir; // The directory where templates are situated var $lastsection_index; // Only visible ones !!! var $displayedSection; // The section currently being shown to the user var $displayedSubsection;// The subsection currently being shown var $user_variables; // The variables set inside index.php function Site($_name, $_title, $_url, $_hidden_params = true, $_tmpl_page = "{PAGE}.html", $_tmpl_dir = "./templates") { $this->name = $_name; $this->title = $_title; $this->url = $_url; $this->hiddenParameters = $_hidden_params; $this->templatePage = $_tmpl_page; $this->templatesDir = $_tmpl_dir; $this->lastsection_index = 0; $this->displayedSection = SECTION_DISPLAYED; $this->displayedSubsection = SUBSECTION_DISPLAYED; // We verify that $this->templatesDir exists and is accessible if (!is_dir($this->templatesDir)) Error("The templates directory: ".$this->templatesDir." does not exist"); if (!is_readable($this->templatesDir)) Error("The templates directory: ".$this->templatesDir." cannot be read"); // Same thing for $this->tmpl_page if (!is_file($this->templatesDir."/".$this->templatePage)) Error("The page layout template file: ".$this->templatesDir."/".$this->templatePage." does not exist"); if (!is_readable($this->templatesDir."/".$this->templatePage)) Error("The page layout template file: ".$this->templatesDir."/".$this->templatePage." cannot be read"); } function addVariable($_name, $_value) { $this->user_variables .= "\$".$_name."=".$_value.";"; } /* The two following functions are used to add Section's to the Site */ function add($_new_section) { if (isset($this->sections[$_new_section->name])) { Warning("Trying to add an already existing section: ".$_new_section->name." (IGNORED)"); return ; } $_new_section->visible = true; $_new_section->index = ++$this->lastsection_index; $this->sections[$_new_section->name] = $_new_section; } function addInvisible($_new_section) { if (isset($this->sections[$_new_section->name])) { Warning("Trying to add an already existing section: ".$_new_section->name." (IGNORED)"); return ; } $_new_section->visible = false; $_new_section->index = -1; $this->sections[$_new_section->name] = $_new_section; } /* This is the function used to display the webpage currently accessed */ function toString() { // We check if the site is empty or contains an empty section if (!isset($this->sections)) Error("Site ".$this->name." does not contain any sections"); foreach($this->sections as $mySection) if (!isset($mySection->subsections)) Error("Section ".$mySection->name." does not contain any subsections"); // We take care of the arguments of the displayed area if (!isset($this->sections[$this->displayedSection])) { foreach($this->sections as $mySection) { if ($mySection->visible) { $this->displayedSection = $mySection->name; break; } } } if (!isset($this->sections[$this->displayedSection]->subsections[$this->displayedSubsection])) { foreach($this->sections[$this->displayedSection]->subsections as $mySubsection) { if ($mySubsection->visible) { $this->displayedSubsection = $mySubsection->name; break; } } } $page_template_file = $this->templatesDir."/".$this->templatePage; $page_template = fileToString($page_template_file); $s = $this->stringFromTemplate($page_template, 0, $this->displayedSection, $this->displayedSubsection); // If the user has decided to hide the variables section and subsection // After having evaluated all the templates, it' s time to add the
\n"; if (ereg("<[bB][oO][dD][yY][^>]*>", $s, $bodyTag)) { $s = str_replace($bodyTag[0], $bodyTag[0]."\n".$form, $s); } else { Error("<body> found"); } } echo $s; // We display the copyright at the begining fo the generated HTML echo "\n"; // Then we display the Warnings if there are some] if (defined(WARNINGS_LIST)) echo WARNINGS_LIST; } // End of toString() // // Probably the most important function... // function stringFromTemplate($_template, $_start, $currentSection, $currentSubsection) { $s = $_template; // If we can find some more {***} expressions, then time to evaluate them! while (getFirstTmplCall($s, $_start, $pattern)) { if (is_file($this->templatesDir."/".$pattern.".html")) { /* if {PATTERN}.html is a template file, open it and evaluate... */ //$t = fileToString($this->templatesDir."/".$pattern.".html"); $t = programToString($this->templatesDir."/".$pattern.".html",false); $ex = $this->stringFromTemplate($t, 0, $currentSection, $currentSubsection); $re = true; } else { /* If the {PATTERN} is not referring to any template file */ $pat = substr($pattern, 1, strlen($pattern)-2); // Let's start with the SITE level functions if ($pat == "SITE-NAME") { $ex = $this->name; $re = true; } else if ($pat == "SITE-TITLE") { $ex = $this->title; $re = true; } else if ($pat == "SITE-URL") { $ex = $this->url; $re = true; } else if ($pat == "SITE-NBSECTIONS") { $ex = $this->lastsection_index; $re = true; // Now the SECTION level ones } else if ($pat == "SECTION-NAME") { $ex = $currentSection; $re = true; } else if ($pat == "SECTION-TITLE") { $ex = $this->sections[$currentSection]->title; $re = true; } else if ($pat == "SECTION-NBSUBSECTIONS") { $ex = $this->sections[$currentSection]->lastsubsection_index; $re = true; } else if ($pat == "SECTION-INDEX") { if (isset($this->sections[$currentSection]->index)) $ex = $this->sections[$currentSection]->index; else $ex = ""; $re = true; } else if ($pat == "SECTION-DISPLAYED") { $ex = $this->displayedSection; $re = true; } else if (substr($pat,0,18) == "SECTION-SUBSECTION") { if (ereg("\[(.*)\]", substr($pat,18), $argssection_subsection)) { $index = $this->stringFromTemplate($argssection_subsection[1],0,$currentSection,$currentSubsection); $idx = $index + 0; // Yes +0, because we want to evaluate the string inside index if ($idx <= $this->sections[$currentSection]->lastsubsection_index) { $ex = "".$idx; foreach($this->sections[$currentSection]->subsections as $mySubsection) { if ($mySubsection->index == $idx) { $ex = $mySubsection->name; break; } } } else { $ex = "Incorrect index for SECTION-SUBSECTION[INDEX]: ".$idx; } } else $ex = "Bad argument for SECTION-SUBSECTION[INDEX]".substr($pat,18); $re = true; // Now the SUBSECTION level } else if ($pat == "SUBSECTION-NAME") { $ex = $currentSubsection; $re = true; } else if ($pat == "SUBSECTION-TITLE") { $ex = $this->sections[$currentSection]->subsections[$currentSubsection]->title; $re = true; } else if ($pat == "SUBSECTION-INDEX") { if (isset($this->sections[$currentSection]->subsections[$currentSubsection]->index)) $ex = $this->sections[$currentSection]->subsections[$currentSubsection]->index; else $ex = ""; $re = true; } else if ($pat == "SUBSECTION-DISPLAYED") { $ex = $this->displayedSubsection; $re = true; } else if ($pat == "SUBSECTION-CONTENTS") { $ex = $this->stringFromTemplate($this->sections[$currentSection]->subsections[$currentSubsection]->getContents(), 0, $currentSection, $currentSubsection); //$ex = $this->stringFromTemplate($this->sections[$this->displayedSection]->subsections[$this->displayedSubsection]->getContents(), 0, $currentSection, $currentSubsection); $re = true; } else if (substr($pat,0,6) == "FORALL") { // FORALL(TYPE,ACTION) if (getArguments(substr($pat,6), $args)) { // Checks the arguments if ($args[0] == "SECTION") { $ex = ""; foreach($this->sections as $mySection) if ($mySection->visible) $ex .= $this->stringFromTemplate($args[1], 0, $mySection->name, ""); } else if ($args[0] == "SUBSECTION") { $ex = ""; foreach($this->sections[$currentSection]->subsections as $mySubsection) if ($mySubsection->visible) $ex .= $this->stringFromTemplate($args[1], 0, $currentSection, $mySubsection->name); } else $ex = "Illegal first argument for FORALL: ".$args[0]."