Merged back CPHP updates from CVM and Anontune

develop
Sven Slootweg 12 years ago
parent b40139d102
commit 9254b0594b

@ -19,19 +19,27 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
public $verify_query = ""; public $verify_query = "";
public $table_name = ""; public $table_name = "";
public $query_cache = 60; public $query_cache = 60;
public $id_field = "Id";
public $prototype = array(); public $prototype = array();
public $prototype_render = array(); public $prototype_render = array();
public $prototype_export = array();
public $uData = array(); public $uData = array();
public $sId = 0; public $sId = 0;
public function __construct($uDataSource, $uCommunityId = 0) public function __construct($uDataSource)
{ {
$this->ConstructDataset($uDataSource, $uCommunityId); $this->ConstructDataset($uDataSource);
$this->EventConstructed(); $this->EventConstructed();
} }
public function RefreshData()
{
$this->PurgeCache();
$this->ConstructDataset($this->sId);
}
public function ConstructDataset($uDataSource, $uCommunityId = 0) public function ConstructDataset($uDataSource, $uCommunityId = 0)
{ {
$bind_datasets = true; $bind_datasets = true;
@ -92,7 +100,7 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
if($bind_datasets === true) if($bind_datasets === true)
{ {
$this->sId = (is_numeric($uDataSource['Id'])) ? $uDataSource['Id'] : 0; $this->sId = (is_numeric($uDataSource[$this->id_field])) ? $uDataSource[$this->id_field] : 0;
$this->uData = $uDataSource; $this->uData = $uDataSource;
@ -335,7 +343,7 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
} }
$sQueryKeysValues = implode(", ", $sKeyValueList); $sQueryKeysValues = implode(", ", $sKeyValueList);
$query = "UPDATE {$this->table_name} SET {$sQueryKeysValues} WHERE `Id` = '{$this->sId}'"; $query = "UPDATE {$this->table_name} SET {$sQueryKeysValues} WHERE `{$this->id_field}` = '{$this->sId}'";
} }
if($result = mysql_query($query)) if($result = mysql_query($query))
@ -345,7 +353,7 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
$this->sId = mysql_insert_id(); $this->sId = mysql_insert_id();
} }
$this->PurgeCache(); $this->RefreshData();
return $result; return $result;
} }
@ -364,6 +372,8 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
public function RetrieveChildren($type, $field) public function RetrieveChildren($type, $field)
{ {
// Not done yet!
if(!isset($cphp_class_map[$type])) if(!isset($cphp_class_map[$type]))
{ {
$classname = get_class($this); $classname = get_class($this);
@ -398,6 +408,34 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
return $this->DoRenderInternalTemplate(); return $this->DoRenderInternalTemplate();
} }
public function Export()
{
// Exports the object as a nested array. Observes the export prototype.
$export_array = array();
foreach($this->prototype_export as $field)
{
$variable_name = "s{$field}";
if(is_object($this->$variable_name))
{
if(!empty($this->$variable_name->sId))
{
$export_array[$field] = $this->$variable_name->Export();
}
else
{
$export_array[$field] = null;
}
}
else
{
$export_array[$field] = $this->$variable_name;
}
}
return $export_array;
}
// Define events // Define events
protected function EventConstructed() { } protected function EventConstructed() { }

@ -14,6 +14,7 @@
if($_CPHP !== true) { die(); } if($_CPHP !== true) { die(); }
$template_cache = array(); $template_cache = array();
$template_global_vars = array();
class Templater class Templater
{ {
@ -80,103 +81,162 @@ class Templater
public function Compile($strings) public function Compile($strings)
{ {
global $template_global_vars;
if(!is_null($this->tpl)) if(!is_null($this->tpl))
{ {
$this->tpl_rendered = preg_replace_callback("/<%foreach ([a-z0-9_-]+) in ([a-z0-9_-]+)>(.*?)<%\/foreach>/si", function($matches) use($strings) { $strings = array_merge($strings, $template_global_vars);
$variable_name = $matches[1];
$array_name = $matches[2]; $this->tpl_rendered = $this->ParseForEach($this->tpl_rendered, $strings);
$template = $matches[3]; $this->tpl_rendered = $this->ParseIf($this->tpl_rendered, $strings);
$returnvalue = "";
preg_match_all("/<%\?([a-zA-Z0-9_-]+)>/", $this->tpl_rendered, $strlist);
if(isset($strings[$array_name])) foreach($strlist[1] as $str)
{
if(isset($strings[$str]))
{ {
foreach($strings[$array_name] as $item) $this->tpl_rendered = str_replace("<%?{$str}>", $strings[$str], $this->tpl_rendered);
}
}
}
else
{
Throw new Exception("No template loaded.");
}
}
public function ParseForEach($source, $data)
{
$templater = $this;
return preg_replace_callback("/<%foreach ([a-z0-9_-]+) in ([a-z0-9_-]+)>(.*?)<%\/foreach>/si", function($matches) use($data, $templater) {
$variable_name = $matches[1];
$array_name = $matches[2];
$template = $matches[3];
$returnvalue = "";
if(isset($data[$array_name]))
{
foreach($data[$array_name] as $item)
{
$rendered = $template;
$rendered = $templater->ParseIf($rendered, $data, $item, $variable_name);
foreach($item as $key => $value)
{ {
$rendered = $template; $rendered = str_replace("<%?{$variable_name}[{$key}]>", $value, $rendered);
foreach($item as $key => $value)
{
$rendered = str_replace("<%?{$variable_name}[{$key}]>", $value, $rendered);
}
$returnvalue .= $rendered;
} }
return $returnvalue; $returnvalue .= $rendered;
} }
return false; return $returnvalue;
}, $this->tpl_rendered); }
$this->tpl_rendered = preg_replace_callback("/<%if ([a-z0-9_-]+) (=|==|>|<|>=|<=|!=) ([^>]+)>(.*?)<%\/if>/si", function($matches) use($strings) { return false;
$variable_name = $matches[1]; }, $source);
$operator = $matches[2]; }
$value = $matches[3];
$template = $matches[4]; public function ParseIf($source, $data, $context = null, $identifier = "")
{
if(isset($strings[$variable_name])) return preg_replace_callback("/<%if ([][a-z0-9_-]+) (=|==|>|<|>=|<=|!=) ([^>]+)>(.*?)<%\/if>/si", function($matches) use($data, $context, $identifier) {
$variable_name = $matches[1];
$operator = $matches[2];
$value = $matches[3];
$template = $matches[4];
if(!empty($identifier))
{
if(preg_match("/{$identifier}\[([a-z0-9_-]+)\]/i", $variable_name, $submatches))
{ {
$variable = $strings[$variable_name]; // Local variable.
$name = $submatches[1];
if($variable == "true") { $variable = true; }
if($variable == "false") { $variable = false; }
if(is_numeric($variable)) { $variable = (int)$variable; }
if($value == "true") { $value = true; }
if($value == "false") { $value = false; }
if(is_numeric($value)) { $value = (int)$value; }
switch($operator) if(isset($context[$name]))
{ {
case "=": $variable = $context[$name];
case "==":
$display = ($variable == $value);
break;
case ">":
$display = ($variable > $value);
break;
case "<":
$display = ($variable < $value);
break;
case ">=":
$display = ($variable >= $value);
break;
case "<=":
$display = ($variable <= $value);
break;
case "!=":
$display = ($variable != $value);
break;
default:
return false;
break;
} }
else
if($display === true)
{ {
return $template; return false;
}
}
elseif(preg_match("/[a-z0-9_-]+\[[a-z0-9_-]+\]/i", $variable_name))
{
// Not the right scope.
return false;
}
else
{
// Global variable.
if(isset($data[$variable_name]))
{
$variable = $data[$variable_name];
} }
else else
{ {
return ""; return false;
} }
} }
}
return false; else
}, $this->tpl_rendered);
preg_match_all("/<%\?([a-zA-Z0-9_-]+)>/", $this->tpl_rendered, $strlist);
foreach($strlist[1] as $str)
{ {
if(isset($strings[$str])) if(isset($data[$variable_name]))
{ {
$this->tpl_rendered = str_replace("<%?{$str}>", $strings[$str], $this->tpl_rendered); $variable = $data[$variable_name];
}
else
{
return false;
} }
} }
}
else
{ if($variable === "true") { $variable = true; }
Throw new Exception("No template loaded."); if($variable === "false") { $variable = false; }
} if(is_numeric($variable)) { $variable = (int)$variable; }
if($value === "true") { $value = true; }
if($value === "false") { $value = false; }
if(is_numeric($value)) { $value = (int)$value; }
switch($operator)
{
case "=":
case "==":
$display = ($variable == $value);
break;
case ">":
$display = ($variable > $value);
break;
case "<":
$display = ($variable < $value);
break;
case ">=":
$display = ($variable >= $value);
break;
case "<=":
$display = ($variable <= $value);
break;
case "!=":
$display = ($variable != $value);
break;
default:
return false;
break;
}
if($display === true)
{
return $template;
}
else
{
return "";
}
return false;
}, $source);
} }
public function Render() public function Render()

@ -11,13 +11,15 @@
* licensing text. * licensing text.
*/ */
cphp_dependency_provides("cphp_router", "1.0"); cphp_dependency_provides("cphp_router", "1.1");
class CPHPRouter extends CPHPBaseClass class CPHPRouter extends CPHPBaseClass
{ {
public $routes = array(); public $routes = array();
public $parameters = array(); public $parameters = array();
public $custom_query = ""; public $custom_query = "";
public $allow_slash = false;
public $ignore_query = false;
public function RouteRequest() public function RouteRequest()
{ {
@ -39,6 +41,14 @@ class CPHPRouter extends CPHPBaseClass
} }
} }
if($this->ignore_query === true)
{
if(strpos($requestpath, "?") !== false)
{
list($requestpath, $bogus) = explode("?", $requestpath, 2);
}
}
$found = false; // Workaround because a break after an include apparently doesn't work in PHP. $found = false; // Workaround because a break after an include apparently doesn't work in PHP.
foreach($this->routes as $priority) foreach($this->routes as $priority)
@ -47,6 +57,11 @@ class CPHPRouter extends CPHPBaseClass
{ {
if($found === false) if($found === false)
{ {
if($this->allow_slash === true)
{
$route_regex = "{$route_regex}/?";
}
$regex = str_replace("/", "\/", $route_regex); $regex = str_replace("/", "\/", $route_regex);
if(preg_match("/{$regex}/i", $requestpath, $matches)) if(preg_match("/{$regex}/i", $requestpath, $matches))
{ {
@ -59,4 +74,3 @@ class CPHPRouter extends CPHPBaseClass
} }
} }
} }
?>

Loading…
Cancel
Save