<?php
define(‘TEMPLATE_DIRECTORY’, ”);
define(‘THEME_DIRECTORY’, ”);
class Template {
var $_buffer;
var $_regCommands = array();
var $_data;
var $_hideSections = array();
function gs() {
// this method is overloaded.
$arg = func_get_args();
// must have at least one argument. no error, just do nothing.
if (! isset($arg[0])) {
return;
};
// assign by object
if (is_object($arg[0])) {
// assign public properties
foreach (get_object_vars($arg[0]) as $key => $val) {
if ($key[0] != ‘_’) {
$this->$key = $val;
};
};
return;
}
// assign by associative array
if (is_array($arg[0])) {
foreach ($arg[0] as $key => $val) {
if ($key[0] != ‘_’) {
$this->$key = $val;
};
};
return;
}
if (is_string($arg[0]) && func_num_args() == 2 && $arg[0][0] <> ‘_’) {
$this->$arg[0] = $arg[1];
} elseif (is_string($arg[0])) {
if (is_array($this->_data) && array_key_exists($arg[0], $this->_data)) {
return $this->_data[$arg[0]];
} elseif ($arg[0] <> ‘_’ && isset($this->$arg[0])) {
return $this->$arg[0];
} else {
return ”;
}
} else {
return;
};
}
function get($key) {
return $this->gs($key);
}
function set() {
if (func_num_args() == 2) {
$arg1 = func_get_arg(0);
$arg2 = func_get_arg(1);
$this->gs($arg1, $arg2);
} elseif (!is_string(func_get_arg(0))) {
$arg1 = func_get_arg(0);
$this->gs($arg1);
}
// if only 1 param is passed and it is a string type, do nothing
}
function hide($section) {
$this->_hideSections[] = $section;
}
function expand($buffer = ”, $expand = true) {
if (strlen($buffer) == 0) {
$buffer = $this->_buffer;
} else {
$buffer = $buffer;
};
if ($expand) {
preg_match_all(“/<SECTION:(\w+)>.+<\/SECTION:\\1>/si”,$buffer,$matches, PREG_SET_ORDER);
if (count($matches)) {
foreach ($matches as $match) {
$key = $match[1];
if (in_array($key, $this->_hideSections)) {
$buffer = preg_replace(“/<SECTION:”.$key.”>.+<\/SECTION:”.$key.”>/si”, ”, $buffer);
} else {
$buffer = preg_replace(“/<\/?SECTION:$key>/i”, ”, $buffer);
}
}
}
// replace item with data from command
preg_match_all(“/{\@([\w\s\.,:\|\/\-=’ <>&#;]+)}/”,$buffer,$matches);
foreach ($matches[0] as $token) {
$key = substr($token,1,strlen($token)-2);
if (strpos($key, ‘|’) !== false) {
list($key, $style) = explode(‘|’,$key);
} else {
$style = ”;
};
$replace = $this->command(substr($key,1));
if (strlen($style) > 0) {
$replace = $this->style($replace,$style);
};
$buffer = ereg_replace(preg_replace(‘/\|/’, ‘\|’,$token),$replace,$buffer);
};
// replace item with variable value
preg_match_all(“/{([\w\s\.:\/\|]+)}/”,$buffer,$matches);
foreach ($matches[0] as $token) {
$key = substr($token,1,strlen($token)-2);
if (strpos($key, ‘|’) !== false) {
list($key, $style) = explode(‘|’,$key);
} else {
$style = ”;
};
$replace = $this->gs($key);
if (strlen($style) > 0) {
$replace = $this->style($replace,$style);
};
$buffer = ereg_replace(preg_replace(‘/\|/’, ‘\|’,$token),$replace,$buffer);
}
}
preg_match_all(“/<FIELD:((\w+)[\w\s:\[\]~=!\.,\-\’&#;\|\/ ]*)>.+?<\/FIELD:\\2>/si”,$buffer,$matches, PREG_SET_ORDER);
if (count($matches)) {
foreach ($matches as $match) {
$show = false;
if (strpos($match[1], ‘:’) !== false) { // if logic processing is required
$token = $match[1];
list($key, $operator, $value) = explode(‘:’, $match[1], 3);
$kval = $this->gs($key);
switch($operator) {
case ‘=’:
if ($kval == (int)$value) {
$show = true;
}
break;
case ‘==’:
case ‘eq’:
if (strcasecmp($kval, $value) == 0) {
$show = true;
}
break;
case ‘!’:
if ($kval <> (int)$value) {
$show = true;
}
break;
case ‘!=’:
case ‘ne’:
case ‘[]’:
if (!strcasecmp($kval, $value) == 0) {
$show = true;
}
break;
case ‘[[‘:
case ‘lt’:
if ($kval < $value) { $show = true; } break; case ‘]]’: case ‘gt’: if ($kval > $value) {
$show = true;
}
break;
case ‘[=’:
case ‘lte’:
if ($kval <= $value) { $show = true; } break; case ‘]=’: case ‘gte’: if ($kval >= $value) {
$show = true;
}
break;
case ‘=~’:
case ‘pos’:
if (preg_match(“/”.$value.”/i”, $kval)) {
$show = true;
}
break;
case ‘!~’:
case ‘neg’:
if (!preg_match(“/”.$value.”/i”, $kval)) {
$show = true;
}
break;
default:
if (strlen($kval) <> 0) {
$show = true;
}
break;
}
} else { // if no logic processing is required
$key = $match[1];
$token = $key;
if (strlen($this->gs($key)) <> 0) {
$show = true;
}
}
// escape the square brackets as it causes an issue with the regex below in the if statement
$pats = array(‘/\]/’,’/\[/’);
$reps = array(‘\]’, ‘\[‘);
$token = preg_replace($pats, $reps, $token);
if ($show) {
$buffer = $this->expand(preg_replace(“/<FIELD:$token>(.+?)<\/FIELD:$key>/si”, ‘\\1’, $buffer), false);
} else {
$buffer = preg_replace(“/<FIELD:$token>.+?<\/FIELD:$key>/si”, ”, $buffer);
}
}
}
return $buffer;
}
function register() {
foreach (func_get_args() as $arg) {
$this->_regCommands[] = $arg;
};
}
function style($value, $style) {
switch (strtoupper($style)) {
case ‘UPPER’:
return strtoupper($value);
case ‘LOWER’:
return strtolower($value);
case ‘FIRST’:
return ucfirst(strtolower($value));
case ‘WORDS’:
return ucwords(strtolower($value));
case ‘MONEY’:
return ‘\$’.sprintf(“%0.2f”,$value);
default:
return $value;
};
}
function command($original) {
$parameter = explode(‘:’, $original);
$command = strtoupper($parameter[0]);
if (array_key_exists(1, $parameter) && strlen($parameter[1]) > 0) {
$value = $this->get($parameter[1]);
$flagSet = true;
} else {
$value = ”;
$flagSet = false;
}
switch ($command) {
case ‘DATE’:
$format = (array_key_exists(2, $parameter) ? $parameter[2] : ”);
if (strlen($value) > 0 && $value != 0) {
switch($format) {
case ‘WORD’: return date(‘F d, Y’, $value); break;
default: return date($format, $value); break;
}
} else if ($flagSet) {
return ”;
} else {
return date($format);
};
case ‘CHECKED’:
if (!array_key_exists(2, $parameter) && $value) {
return ‘checked’;
} else if (array_key_exists(2, $parameter) && (string)$parameter[2] == (string)$value) {
return ‘checked’;
} else {
return ”;
}
case ‘SELECTED’:
if ($value == $parameter[2]) {
return ‘selected’;
}
return ”;
case ‘MEMORY’:
array_shift($parameter);
$return = ”;
foreach ($parameter as $arg) {
$return = sprintf(“%s\n”,
$return,
$arg,
$arg,
$this->get($arg)
);
}
return $return;
case ‘FLAG’:
switch ($value) {
case ‘Y’:
case 1:
case ‘Yes’:
return ‘1’;
break;
default: return ‘0’; break;
}
case ‘SUBSTR’:
if (array_key_exists(3, $parameter)) {
$return = substr($value, $parameter[2], $parameter[3]);
if (strlen($value) > ($parameter[3] + $parameter[2])){
$return .= ” …”;
}
} else {
$return = substr($value, $parameter[2]);
}
return $return;
case ‘ERROR’:
if (strlen($value)) {
return sprintf(”
“, $value);
} else if (strlen($this->gs(‘ERROR’))) {
return sprintf(”
“, $this->gs(‘ERROR’));
} else {
return ”
“;
}
case ‘MESSAGE’:
if (strlen($value)) {
return sprintf(”
“, $value);
} else if (strlen($this->gs(‘MESSAGE’))) {
return sprintf(”
“, $this->gs(‘MESSAGE’));
} else {
return ”
“;
}
default:
$function = strtolower($command);
if (in_array($function, $this->_regCommands) && method_exists($this, $function)) {
array_shift($parameter);
return $this->$function($parameter);
} else {
return “@$command”; // this way it is noticeable in the template the function doesn’t exist
};
};
}
function repeat($sections) {
$buffer = &$this->_buffer;
// check if there is any data for a repeat. if not check for single repeat
// section and replace it with blank
if (count($sections) == 0 && preg_match(“/(.+)<\/REPEAT>/si”,$buffer)) {
$insert = ”;
$buffer = preg_replace(“/(.+)<\/REPEAT>/si”, $insert, $buffer);
return;
}
// loop through the sections
foreach ($sections as $section => $data) {
$insert = ”;
// if the sections are numeric then it is just a single repeat
if (is_numeric($section)) {
$section = ”;
$flagMultipleRepeat = false;
} else {
$section = ‘_’.$section;
$flagMultipleRepeat = true;
};
// search for the present repeat section
if (preg_match(“/<REPEAT$section>(.+)<\/REPEAT$section>/si”,$buffer,$matches)) {
$repeat = $matches[1];
// if data is available put it in or else clear out the repeat area
if ($flagMultipleRepeat || (!$flagMultipleRepeat && count($sections[0]) > 0)) {
for($i = 0; $i < count(($flagMultipleRepeat ? $data : $sections)); $i ++) { $temp = $repeat; if ($flagMultipleRepeat) { $this->_data = $data[$i];
} else {
$this->_data = $sections[$i];
}
$insert .= $this->expand($temp);
unset($this->_data);
$this->_data = ”;
};
};
// replace the section if the buffer with the content filled version
$buffer = preg_replace(“/<REPEAT$section>(.+)<\/REPEAT$section>/si”, $insert, $buffer);
};
// if it is a single repeat section then there is no need to loop through anything else
if (!$flagMultipleRepeat) {
break;
};
};
}
function load($page, $path = ”) {
if(!preg_match(‘/\./’,$page)) { $page .= ‘.html’; };
if (strlen($path)) {
$file = $path.$page;
} else {
$file = THEME_DIRECTORY.$page;
}
// if the file doesn’t exist in the theme directory try the template directory
if(!file_exists($file)) { $file = TEMPLATE_DIRECTORY.$page; };
if(!file_exists($file)) {
printf(”
Unable to access page ‘%s'”, $file);
exit;
};
// open the file and save the content to a variable
$f = fopen($file,’r’);
$this->_buffer = fread($f,filesize($file));
fclose($f);
}
}
$page = new Template();
// setup defaults
$showForm = true;
$page->gs(‘SpecialInstr’, “\n\nIf your shipment is not palletized or if pallets are larger than 4′ x 4′ please provide dimensions (L x W x H) or the exact trailer footage required. If expedited service is required please advise when you need the shipment delivered.”);
if (array_key_exists(‘quoteSubmit’, $_POST)) {
$page->gs($_POST);
$required = array(
‘Company’,
‘FirstName’,
‘LastName’,
‘Address’,
‘City’,
‘StateProv’,
‘Postal’,
‘Phone’,
‘Email’,
‘OrigCity’,
‘Orig_StateProv’,
‘DestCity’,
‘Dest_StateProv’,
‘NumPieces’,
‘Stackable’,
‘LbsKilos’,
‘TotalWeight’,
‘ServType’,
‘Payment’,
‘Currency’,
‘DeliveryDate’,
‘date1’,
‘Description’,
);
$passed = true;
foreach ($required as $req) {
if (!strlen($page->gs($req))) {
$passed = false;
$page->gs(‘ERROR’, ‘Please fill in all required fields’);
break;
}
}
if ($passed) { // send email
$message = “A quote has been submitted via the website: \n\n”;
foreach (get_object_vars($page) as $key => $value) {
if ($key[0] <> ‘_’ && $key <> ‘quoteSubmit’) {
$message .= “$key: $value\n”;
}
}
mail(‘[email protected]’, ‘Website quote’, $message, “From: [email protected]\n”);
$showForm = false;
}
}
if ($showForm) {
$page->load(‘quote’);
echo $page->expand();
} else {
echo ‘<div class=”fullBoxTop”><div class=”fullBoxBottom”><h2>Online Freight Quote Contact Information</h2><p>Thank you! <p>Your quote has been submitted. We will contact you shortly.</div></div>’;
}
?>