📄 forms.php
字号:
<?php
/*
*/
define ("_MSG_FORMS_UNCOMPLETE" , "Please fill in all required fields");
define ("_MSG_FORMS_UNIQUE" , " already exists.");
define ("_MSG_FORMS_FILEEXISTS" , " doesn't exists.");
$form_months = array (1 => "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "November" , "October" , "December");
/**
* abstract library class
*
* @library Forms Auto Generations and validations library
*/
class CForm extends CLibrary{
/**
* description
*
* @var type
*
* @access type
*/
var $db;
/**
* description array with all functions to be executed in various posittions in cform
*
* @var type
*
* @access type
*/
var $functions;
/**
* description
*
* @param
*
* @return
*
* @access
*/
function CForm($template , $db , $tables , $form = "") {
parent::CLibrary("CForm");
//adding extra check if the file is a template or is a string
if (!is_object($template)) {
$template = new CTemplate($template);
}
//optionaly added $form
$this->form = $this->Process($form);
$this->templates = $template;
$this->db = $db;
$this->tables = $tables;
}
/**
* description checking if the input is an array or an xml file
*
* @param
*
* @return
*
* @access
*/
function Process($input) {
if (is_array($input)) {
return $input;
} else {
if (file_exists($input)) {
$xml = new CConfig($input);
$xml->vars["form"]["xmlfile"] = $input ;
return $xml->vars["form"];
} else
return null;
}
}
/**
* description
*
* @param
*
* @return
*
* @access
*/
function ProcessLinks($link) {
return CryptLink($link);
}
/**
* description
*
* @param
*
* @return
*
* @access
*/
function Validate($form , $input) {
$form = $this->Process($form);
if (is_array($form)) {
foreach ($form["fields"] as $key => $val)
if ($val["validate"] && $val["required"] && !$val["hidden"])
$_valid_temp[] = strtoupper($val["name"] ? $val["name"] : $key) . ":" . $val["validate"];
$validate = @implode("," , $_valid_temp);
}
//validate the input fields
$result = ValidateVars($input ,$validate);
$vars = array();
if (is_array($result)) {
foreach ($result as $key => $val)
$fields["errors"][strtolower($val)] = 1;
$fields["error"] = _MSG_FORMS_UNCOMPLETE;
$fields["values"] = $input;
} else {
//proceed to complex validation for unique fields
if (is_array($form)) {
foreach ($form["fields"] as $key => $val) {
if ($val["unique"] == "true") {
//check if this is an adding processor or editing one
if ($input[$form["table_uid"]]) {
$old_record = $this->db->QFetchArray("SELECT * FROM `" . $this->tables[$form["table"]] . "` WHERE `" . $form["table_uid"] . "`='" . $input[$form["table_uid"]] . "'" );
}
$name = $val["name"] ? $val["name"] : $key ;
$data = $this->db->QFetchArray("SELECT `$name` FROM `" . $this->tables[$form["table"]] . "` WHERE `" . $name . "` = '" . $input[$name] . "'");
if (((is_array($data) && is_array($old_record)) && ($data[$name] != $old_record[$name])) || (is_array($data) && !is_array($old_record))) {
//preparing the message
$fields["error"] = $val["unique_err"] ? $val["unique_err"] : $val["title"] . _MSG_FORMS_UNIQUE;
$fields["errors"][$name] = 1;
$fields["values"] = $input;
}
}
// search to see if is a vvalid file path
if ($val["fileexists"] == "true") {
$name = $val["name"] ? $val["name"] : $key ;
if (!is_file($input[$name])) {
//preparing the message
$fields["error"] = $val["fileexists_err"] ? $val["fileexists_err"] : $val["title"] . ": \"$input[$name]\"" . _MSG_FORMS_FILEEXISTS;
$fields["errors"][$name] = 1;
$fields["values"] = $input;
}
}
// search to see if is a vvalid file path
if (($val["type"] == "password") && !strstr($key , "_confirm")) {
$name = $val["name"] ? $val["name"] : $key ;
if ($input[$name] != $input[$name . "_confirm"]) {
//preparing the message
$fields["error"] = "Password and confirmation doesnt match.";
$fields["errors"][$name] = 1;
$fields["errors"][$name . "_confirm"] = 1;
$fields["values"] = $input;
}
}
}
}
}
return is_array($fields) ? $fields : true;
}
/**
* description
*
* @param
*
* @return
*
* @access
*/
function SimpleList($form , $items = "", $count = "", $extra = null , $search = false) {
global $_CONF;
//if i got no elements from preloader functions, then i load it manualy
if (!is_array($items)) {
//cheking if is a normal browse or a search method
if ($search) {
$items = $this->db->QuerySelectLimit($this->tables[$form["table"]],"*", "`" . $_GET["what"] . "` " . ( $_GET["type"] == "int" ? "='" . $_GET["search"] . "'" : "LIKE '%" . $_GET["search"] . "%'"),(int) $_GET["page"],$form["items"]);
$count = $this->db->RowCount($this->tables[$form["table"]] , " WHERE `" . $_GET["what"] . "` " . ( $_GET["type"] == "int" ? "='" . $_GET["search"] . "'" : "LIKE '%" . $_GET["search"] . "%'"));
} else {
}
}
if (is_array($form["vars"])) {
foreach ($form["vars"] as $key => $val) {
//echeking if the default must be evaluated
if ($val["action"] == "eval") {
eval("\$val[\"import\"] = " . $val["default"] .";");
}
switch ($val["type"]) {
case "eval":
eval("\$tpl_vars[\"$key\"] = " . $val["import"] . ";");
break;
case "var":
$tpl_vars[$key] = $val["import"];
break;
}
}
}
if (is_array($form["sql"])) {
if (is_array($form["sql"]["vars"])) {
foreach ($form["sql"]["vars"] as $key => $val) {
//echeking if the default must be evaluated
if ($val["action"] == "eval") {
eval("\$val[\"import\"] = " . $val["default"] .";");
}
switch ($val["type"]) {
case "eval":
eval("\$sql_vars[\"$key\"] = " . $val["import"] . ";");
break;
case "var":
$sql_vars[$key] = $val["import"];
break;
case "page":
$sql_vars[$key] = ($_GET[($val["code"] ? $val["code"] : 'page')] -1 )* $form['items'];
break;
case "form":
eval("\$sql_vars[\"$key\"] = " . $form[$val["var"]] . ";");
break;
}
}
foreach ($sql_vars as $key => $val) {
$this->templates->blocks["Temp"]->input = $val;
$sql_vars[$key] = $this->templates->blocks["Temp"]->Replace($sql_vars);
}
//doing a double replace, in case there are unreplaced variable sfom "vars" type
$this->templates->blocks["Temp"]->input = $form["sql"]["query"];
$sql = $this->templates->blocks["Temp"]->Replace($sql_vars);
//do a precheck for [] elements to be replaced with <>
$sql = str_replace("]" , ">" , str_replace("[" , "<" , $sql));
$items = $this->db->QFetchRowArray($sql);
//$items = $this->Query
//processing the counting query
if (is_array($form["sql"]["count"])) {
//if no table is set then i use the default table'
$form["sql"]["count"]["table"] = $form["sql"]["count"]["table"] ? $form["sql"]["count"]["table"] : $form["table"];
foreach ($form["sql"]["count"] as $key => $val) {
$this->templates->blocks["Temp"]->input = $val;
$form["sql"]["count"][$key] = $this->templates->blocks["Temp"]->Replace($sql_vars);
}
$count = $this->db->RowCount($form["sql"]["count"]["table"] , $form["sql"]["count"]["condition"] , $form["sql"]["count"]["select"]);
}
}
} else {
if (!is_array($items)) {
$items = $this->db->QuerySelectLimit($this->tables[$form["table"]],"*","",(int) $_GET["page"],$form["items"]);
$count = $this->db->RowCount($this->tables[$form["table"]]);
}
}
$_GET["page"] = $_GET["page"] ? $_GET["page"] : 1;
//auto index the element
$start = $form["items"] * ($_GET["page"] ? $_GET["page"] - 1 : 0);
if (is_array($items)) {
foreach ($items as $key => $val) {
$items[$key]["_count"] = ++$start;
}
}
$html = new CHtml();
$form = $this->Process($form);
$template = &$this->templates;
//this sux, building the template
if (is_array($form["fields"])) {
foreach ($form["fields"] as $key => $val) {
$data .= $template->blocks["ListCell"]->Replace (
array (
"width" => $val["width"],
"align" => $val["align"],
"value" => "{" . strtoupper($key) . "}"
)
);
//buildint the title header
$titles .= $template->blocks["ListTitle"]->Replace ( array(
"title" => ($val["header"] ? $val["header"] : " "),
"width" => $val["width"]
));
}
//adding one more title col in titles ( the one for buttons )
$titles .= $template->blocks["ListTitle"]->Replace ( array("title" => " " ));
$row = $data . $template->blocks["ButtonsCell"]->output;
$template->blocks["ListElement"]->input = $template->blocks["ListRow"]->Replace(array("ROW"=> $row ));
$titles = $template->blocks["ListRow"]->Replace(array("ROW"=> $titles ));
// i know this is stupid, but now i dont have other idees
//if i see a variable <no heade> then i clear the template
if ($form["header"]["titles"] == "false") {
$titles = "";
}
}
//bulding the header buttons and search box
if (is_array($form["header"]["buttons"])) {
foreach ($form["header"]["buttons"] as $key => $val) {
$val = array_merge($_GET , $val);
$this->templates->blocks["Temp"]->input = $val["location"];
$val["location"] = $this->templates->blocks["Temp"]->Replace($val);
$val["location"] = CryptLink($val["location"]);
$header["buttons"] .= $template->blocks["Button"]->Replace($val);
}
}
// the search options, this is kinda buggy for this version, will be fixed in other
if (is_array($form["header"]["search"])) {
$form_search = $form["header"]["search"];
//bulding the droplist options
if (is_array($form_search["options"])) {
foreach ($form_search["options"] as $key => $val) {
$search_form[$key]["label"] = $val;
$search_form[$key]["checked"] = $_GET[$form_search["variable"]] == $key ? " selected " : "";
}
$search["droplist"] = $html->FormSelect(
"what" ,
$search_form ,
$template,
"Select" ,
$_GET["what"],
$search_form ,
array(
"width" => "" ,
"onchange" => ""
)
);
//building the form and the buttons
//reading all varibals from $_GET excepting the $_GET["page"], and transform them in hidden fields
if (is_array($_GET)) {
$temp = $_GET;
//force the action variable
$temp[$this->forms["uridata"]["action"]] = $this->forms["uridata"]["search"];
foreach ($temp as $key => $val) {
if (!in_array($key , array( "page" , "what" , "search"))) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -