if (!defined('MOD_CORE_TEMPLATE_CLASS'))
{
define('MOD_CORE_TEMPLATE_CLASS', 1);
include "mod-file.php";
include "mod-mysql.php";
define("TEMPLATE_BLOCK_START", "");
define("TEMPLATE_BLOCK_CASE_CHAR", ":");
define("TEMPLATE_BLOCK_HEADER", " start-->");
define("TEMPLATE_BLOCK_FOOTER", " end-->");
define("TEMPLATE_PREFIX_VARIABLE", "%%");
class Template
{
var $template_name;
var $template_stream;
var $variable_attached;
var $db;
var $error = 1;
var $jQuery = 1;
// Funzione costruttore della classe
public function __construct($template_name)
{
$this->template_name = $template_name;
// carica il file template nella propriet della classe template_stream
$this->template_stream = LoadFile($template_name);
// Azzera l'array associativo dei segnaposto nelle sezioni
$this->variable_attached = Array();
// inizializza l'oggetto database
$this->db = new DB_Sql();
}
// Questo metodo una funzione di utilit, cio potrebbe anche essere esterna alla
// classe dato che non ne usa alcun membro.
// Estrae da una stringa ($text) il testo compreso fra due estremi
// se la stringa non viene trovata non stampa nulla
// i suoi parametri di ingresso sono
// $text La stringa da cui si vuole estrarre una sottostringa
// $strinizio Margine iniziale
// $strfine Margine finale
// $compresi Se vero indica che vengono restituiti anche gli estremi
// Esempi :
// echo ExtractFromText("Il mondo bello", "Il ", " bello");
// scrive a video "mondo "
// echo ExtractFromText("Il mondo bello", "Il ", " bello", true);
// scrive a video "Il mondo bello"
// echo ExtractFromText("Il mondo bello", "xx ", " bello", true);
// non scrive nulla perch il margine iniziale non si trova nella stringa data
function ExtractFromText($text, $strinizio, $strfine, $compresi = false)
{
if ($compresi){
$start = @strpos($text , $strinizio ) ;
$stop = @strpos($text , $strfine )+ strlen($strfine);
}
else
{
$start = @strpos($text , $strinizio ) + strlen($strinizio);
if($start < strlen($text)) {
$stop = @strpos($text , $strfine, $start );
}else{
$stop = false;
}
}
if ($start === false || $stop === false) return "";
return rtrim(substr($text , $start , $stop-$start));
}
// Questi sono tutti wrapper della funzione ExtractFromText
// Questa serve per estrarre una sezione dal testo dato
function ExtractSectionFromText($section, $text, $compresi = false)
{
$strinizio = "";
$strfine = "";
return $this->ExtractFromText($text, $strinizio, $strfine, $compresi);
}
// Questa serve a estrarre una sottosezione dall'attributo template_stream
function ExtractBlock($section, $paragraph)
{
return $this->ExtractSectionFromText($paragraph, $this->ExtractSection($section));
}
// Questa serve a estrarre una sezione dall'attributo template_stream
function ExtractSection($section, $compresi = false)
{
if ($this->ExistSection($section))
return $this->ExtractSectionFromText($section, $this->template_stream, $compresi);
else
if ($this->error)
return "Errore impossibile trovare la sezione $section";
}
// cancella i tag di apertura e chiusura ma non il contenuto
function DeleteSectionTitle($section)
{
$this->template_stream = str_replace("", '',$this->template_stream);
$this->template_stream = str_replace("", '',$this->template_stream);
}
// Dato che la funzione ExtractFromText torna nullo se non trova la sezione anche un suo wrapper
// torner nullo se non trova la sezione quindi :
function ExistSection($section)
{
if (!(strcmp($this->ExtractSectionFromText($section, $this->template_stream), "")))
return 0;
else
return 1;
}
// E' uguale a ExtractSection ma con la differenza che mantiene i margini delle sezioni
function ExtractSectionW($section)
{
if ($this->ExistSection($section))
return $this->ExtractSectionFromText($section, $this->template_stream, true);
else
if ($this->error)
return "Errore impossibile trovare la sezione $section";
}
// Questa funzione cancella i possibili segnaposti registrati dal testo di OUTPUT
function RemoveVariable()
{
foreach ($this->variable_attached as $keys => $value) {
$this->template_stream = str_replace($keys, "",$this->template_stream);
}
return $this->template_stream;
}
// Converte una sezione in una sua sottosezione non mantenendo i margini
// si usa quando la profondit delle sezioni 1 ovvero esiste solo una sezione e una sottosezione
function ConvertBlock($section, $paragraph)
{
$strinizio = "";
$strfine = "";
$start = strpos($this->template_stream , $strinizio );
$before = substr($this->template_stream, 0, $start );
$texttofix = $this->ExtractBlock($section, $paragraph);
$after = strstr($this->template_stream, $strfine);
$after = substr($after, strlen($strfine));
$this->template_stream = $before.$texttofix.$after;
return $this->template_stream;
}
// Converte una sezione in una sua sottosezione ma mantenendo i margini
// si usa invece quando la profondit delle sezioni maggiore di 1 ovvero esistono sottosezioni di sottosezioni
function ConvertBlockW($section, $paragraph)
{
$strinizio = "";
$strfine = "";
$start = strpos($this->template_stream , $strinizio );
$before = substr($this->template_stream, 0, $start + strlen($strinizio));
$texttofix = $this->ExtractBlock($section, $paragraph);
$after = strpos($this->template_stream, $strfine);
$after = substr($this->template_stream, $after);
$this->template_stream = $before.$texttofix.$after;
return $this->template_stream;
}
// Cancella il testo entro certi margini
// Anche questa una funzione di utilit
function DeleteText ($text, $strinizio, $strfine)
{
$start = strpos($text , $strinizio );
$before = substr($text, 0, $start );
$after = strstr($text, $strfine);
$after = substr($after, strlen($strfine));
$text = $before.$after;
return $text;
}
// Cancella un blocco / sezione
function DeleteBlock($section)
{
$strinizio = "";
$strfine = "";
$this->template_stream = $this->DeleteText($this->template_stream, $strinizio, $strfine);
return $this->template_stream;
}
// Inserisce un testo al posto di quello della sezione
function InsertBlock($section, $text)
{
$strinizio = "";
$strfine = "";
$before = substr($this->template_stream, 0, strpos($this->template_stream, $strinizio));
$after = strstr($this->template_stream, $strfine);
$after = substr($after, strlen($strfine));
$this->template_stream = $before.$text.$after;
return $this->template_stream;
}
// Registra un segnaposto , si usa per creare del testo che si modifica con un database collegato o con
// l'eval (quindi qualsiasi funzione php) vedere manuale EVAL
// I placeholder hanno questi prefissi :
// ~% identifica una variabile da valutare con funzioni php
// %% identifica una variabile da valutare con i campi di un database
function AttachVariable($placeholder, $fieldname)
{
$this->variable_attached[$placeholder] = $fieldname;
}
// Cancella tutte le variabili registrate
function ResetAttachedVariables()
{
$this->variable_attached = array ();
}
// Questa dovrei cancellarla non la uso mai :D
function CombineQueryWithSingleResult($sql)
{
$this->db->query($sql);
$this->db->next_record();
foreach ($this->variable_attached as $keys => $value) {
if (substr($keys,0,2) == "%%")
$this->template_stream = str_replace($keys, $this->db->f($value),$this->template_stream);
}
return $this->template_stream;
}
// Qui siccome a volte estraggo dei dati dal database che vanno rimaneggiati con l'eval salvo temporaneamente
// il valore preso da un placeholder in una stringa e qui viene riestratto e reso utilizzabile dal php
function ExtractVariableFromSection($section, $variable)
{
$strinizio = "***$variable***";
$strfine = "###$variable###";
$temp = $this->ExtractFromText($this->ExtractSection($section), $strinizio, $strfine);
$temp_section_text = $this->DeleteText($this->ExtractSection($section, true), $strinizio, $strfine);
$this->InsertBlock($section, $temp_section_text);
return $temp;
}
// Questo una delle funzioni principali della classe
// Utilizza i placeholder e copia il contenuto di una sezione n-volte quanti sono le righe del risultato della
// query, al termine del processo la sezione scompare, poich non mantiene i margini della sezione
// i parametri sono :
// $section nome della sezione su cui effettuare la modifica
// $sql query da inviare al database
// $numresult limite alto delle risposte del database, se 0 nessun limite
// $beginresult limite basso delle risposte del database, se 0 nessun limite
// $RemoveSectionIfNull indica se ci sono campi ritenuti opzionali
// quindi se la sql restituisce dei valori 0 NULL etc
// e esistono delle sottosezioni definite con un "optional_NOMEPLACEHOLDER"
// cancella la sottosezione con il placeholder se no va avanti come se nulla fosse
function CombineQueryWithMultipleResult($section, $sql, $numresult = 0, $beginresult = 0, $RemoveSectionIfNull = 0)
{
$block = "";
$i = 0;
if ($numresult != 0)
$sql .= " LIMIT $beginresult, $numresult";
else
$numresult = 256;
$btext = $this->ExtractSection($section);
$this->db->query($sql);
while ($this->db->next_record() && $i<$numresult)
{
$atext = $btext;
foreach ($this->variable_attached as $keys => $value) {
if (substr($keys,0,2) == "%%")
{
if ($RemoveSectionIfNull && (($this->db->f($value) == "") || ($this->db->f($value) == 0)))
{
$SectionName = "optional_".substr($keys,2);
if ($this->ExistSection($SectionName))
{
$strinizio = "";
$strfine = "";
$atext = $this->DeleteText($atext, $strinizio,$strfine);
continue;
}
}
/* if (!strcmp($keys, "%%count_commenti"))
echo "
".$keys . "=>". $this->db->f($value), $atext;*/
$atext = str_replace($keys, $this->db->f($value),$atext);
/* if (!strcmp($keys, "%%count_commenti"))
echo "
".$keys . "=>". $this->db->f($value), $atext;*/
}
}
$atext = str_replace("%%counter%%", $i, $atext);
$i++;
$block .= $atext;
}
$this->InsertBlock($section,$block);
return $i;
}
// Combina da un array
function CombineFromArray($section, $arraydata, $numresult = 0)
{
$block = "";
$i = 0;
if ($numresult == 0)
$numresult = 256;
$btext = $this->ExtractSection($section);
reset($arraydata);
$flag = true;
$recordpos = 0;
foreach ($arraydata as $chiave => $valore)
{
$maxdepth = count($valore);
break;
}
for ($recordpos = 0 ; $recordpos < $maxdepth ; $recordpos++)
{
$record = array();
foreach ($arraydata as $chiave => $valore)
{
$record[$chiave] = $valore[$recordpos];
}
//print_r($record);
$atext = $btext;
foreach ($record as $chiave => $valore)
{
if (substr($chiave,0,2) == "%%")
{
$atext = str_replace($chiave, $valore,$atext);
}
}
$atext = str_replace("%%counter%%", $i, $atext);
$i++;
$block .= $atext;
}
$this->InsertBlock($section,$block);
return $i;
}
// Esegue la funzione di conversione con l'eval su tutto lo stream e non solo su una sezione
function GlobalCombineWithEval()
{
$this->template_stream = $this->TextCombineWithEval($this->template_stream);
return $this->template_stream;
}
// copia una funzione n volte e se trova un placeholder %~counter gli assegna il numero della copia
function RepeatSection($section, $times)
{
$block = "";
$atext = $this->ExtractSection($section);
for ($i = 0; $i < $times ; $i++)
{
$block .= str_replace("%~counter", $i , $atext);
}
$this->InsertBlock($section, $block);
}
// Esegue una sostituzione con eval in una sezione
function SectionCombineWithEval($section)
{
$temp = $this->ExtractSectionW($section);
$temp = $this->TextCombineWithEval($temp);
$this->InsertBlock($section, $temp);
return $temp;
}
// Esegue una sostituzione con eval in un specificato testo
function TextCombineWithEval($text)
{
foreach ($this->variable_attached as $keys => $value)
if (!strcmp(substr($keys,0,2),"%~"))
{
eval("\$temp = ".$value.";");
$text = str_replace($keys, $temp,$text);
}
return $text;
}
/**
* @return int
* @param section Sezione del template su cui effettuare il combine
* @param nomecampo Il campo della sezione che va sostituito
* @param arraybase L'array di partenza
* @param value il valore di default
* @desc Funzione utility per creare l'array associativo utile per combinare un insieme e un valore predefinito in testa
* utile per esempio per inserire le date nelle select con una data gi impostata
*/
function MakeArrayAndCombine($section, $nomecampo, $arraybase, $value)
{
$temp_default = array($value);
$temp = array_merge($temp_default, $arraybase);
$arrayforfunct = array();
$arrayforfunct[$nomecampo] = $temp;
$ret = $this->CombineFromArray($section, $arrayforfunct);
unset($temp);
unset($temp_default);
unset($arrayforfunct);
return $ret;
}
/**
* @return string Testo convertito
* @param html Testo su cui effettuare la conversione
* @param rules_title_tag Definisce il tag che definisce il titolo del tab
* @param css_div_title Definisce il class_tag del titolo
*/
function _jQTabsConvertAccordions ( $html,
$rules_title_tag = 'h3',
$css_div_title = 'accordion')
{
if (stripos($html, '<' . $rules_title_tag ) === false )
$converted = $html;
else
{
$panels = spliti('<'. $rules_title_tag, $html);
$converted = array_shift($panels). '