class validation
{
public static function formalise($validation)
{
$reasons = array();
if(count($validation) > 0)
foreach($validation AS $validation)
if(is_array($validation['hint']))
foreach($validation['hint'] AS $reason)
if(!empty($reason))
$reasons[] = $reason;
$reasons = array_unique($reasons);
$reason = implode("
",$reasons);
return $reason;
}
public static function initial($context)
{
$input_name = $context['input_name'];
$value = $context['value'];
$name = $context['name'];
$rules = $context['rule'];
$id = $context['id'];
$valid = array(
"input_name" => $input_name,
"hint" => "",
"error" => "",
"err" => ""
);
return $valid;
}
public static function validate($context)
{
$input_name = $context['input_name'];
$value = trim($context['value']);
$name = $context['name'];
$rules = $context['rule'];
$id = $context['id'];
$failed_icon = '
';
$success_icon = '
';
$rules_arr = explode("|",$rules);
$status = true;
$reasons = array();
foreach($rules_arr AS $rule)
{
if(!empty($rule))
{
$rule_check = explode("[",$rule);
if($rule_check[0] == "min_length")
{
$rule_check_arr = explode("]",$rule_check[1]);
if(count($rule_check_arr) != 2)
{
$limit = $rule_check_arr[0];
$reasons[] = "$name is invalid";
}
else
{
$length = $rule_check_arr[0];
$check_reason = self::$rule_check[0]($value,$name,$length);
if(!empty($check_reason))
$reasons[] = $check_reason;
}
}
else if($rule_check[0] == "max_length")
{
$rule_check_arr = explode("]",$rule_check[1]);
if(count($rule_check_arr) != 2)
{
$limit = $rule_check_arr[0];
$reasons[] = "$name is invalid";
}
else
{
$length = $rule_check_arr[0];
$check_reason = self::$rule_check[0]($value,$name,$length);
if(!empty($check_reason))
$reasons[] = $check_reason;
}
}
else if($rule_check[0] == "minimum")
{
$rule_check_arr = explode("]",$rule_check[1]);
if(count($rule_check_arr) != 2)
{
$limit = $rule_check_arr[0];
$reasons[] = "$name is invalid";
}
else
{
$length = $rule_check_arr[0];
$check_reason = self::$rule_check[0]($value,$name,$length);
if(!empty($check_reason))
$reasons[] = $check_reason;
}
}
else if($rule_check[0] == "maximum")
{
$rule_check_arr = explode("]",$rule_check[1]);
if(count($rule_check_arr) != 2)
{
$limit = $rule_check_arr[0];
$reasons[] = "$name is invalid";
}
else
{
$length = $rule_check_arr[0];
$check_reason = self::$rule_check[0]($value,$name,$length);
if(!empty($check_reason))
$reasons[] = $check_reason;
}
}
else if($rule_check[0] == "exact_length")
{
$rule_check_arr = explode("]",$rule_check[1]);
if(count($rule_check_arr) != 2)
{
$limit = $rule_check_arr[0];
$reasons[] = "$name is invalid";
}
else
{
$length = $rule_check_arr[0];
$check_reason = self::$rule_check[0]($value,$name,$length);
if(!empty($check_reason))
$reasons[] = $check_reason;
}
}
else
{
if($rule == "required")
{
$check_reason = self::$rule($value,$name,$id);
if(!empty($check_reason))
$reasons[] = $check_reason;
}
else if($rule == "required_include_zero")
{
$check_reason = self::$rule($value,$name,$id);
if(!empty($check_reason))
$reasons[] = $check_reason;
}
else
{
if((!in_array("required",$rules_arr)) && ($value == ""))
{}
else
{
$check_reason = self::$rule($value,$name,$id);
if(!empty($check_reason))
$reasons[] = $check_reason;
}
}
}
}
}
$error_reason = "";
$error_reason = implode(", ",$reasons);
$error_reason = (!empty($error_reason)) ? ''.$name." must ".$error_reason."!" : "";
$error = (count($reasons) == 0) ? "true" : "false";
$err = ($error == "false") ? $failed_icon : $success_icon;
$reason[$name] = $error_reason;
$valid = array(
"input_name" => $input_name,
"hint" => $reason,
"error" => $error,
"err" => $err
);
return $valid;
}
public function minimum($value,$name,$length)
{
return ($value < $length) ? "be more than $length" : "";
}
public function maximum($value,$name,$length)
{
return ($value > $length) ? "be less than $length" : "";
}
public function min_length($value,$name,$length)
{
return (strlen($value) < $length) ? "contain more than $length characters" : "";
}
public function max_length($value,$name,$length)
{
return (strlen($value) > $length) ? "contain less than $length characters" : "";
}
public function exact_length($value,$name,$length)
{
return (strlen($value) != $length) ? "contain exactly $length characters" : "";
}
public function valid_email($value,$name)
{
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $value)) ? "a valid email address" : "";
}
public function valid_emails($value,$name,$id = "")
{
$emails = explode(",",$value);
$status = true;
foreach($emails AS $email)
{
if(! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $value))
{
$status = false;
break;
}
}
return (!$status) ? "a valid email addresses" : "";
}
public function alpha($value,$name,$id = "")
{
return ( ! preg_match("/^([a-z])+$/i", $value)) ? "contain only alphabetical characters" : "";
}
public function alpha_numeric($value,$name,$id = "")
{
return ( ! preg_match("/^([a-z0-9])+$/i", $value)) ? "contain only alpha-numeric characters" : "";
}
public function alpha_dash($value,$name,$id = "")
{
return ( ! preg_match("/^([-a-z0-9_-])+$/i", $value)) ? "contain only alpha-numeric characters, underscores or dashes" : "";
}
public function numeric($value,$name,$id = "")
{
return (!preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $value)) ? "contain only numeric characters" : "";
}
public function integer($value,$name,$id = "")
{
return (!preg_match( '/^[\-+]?[0-9]+$/', $value)) ? "contain only integers" : "";
}
public function hour($value,$name,$id = "")
{
return (!preg_match( '/^([0-9]|[0-1][0-9]|[2][0-3])$/', $value)) ? "contain only hours from 0-23" : "";
}
public function minute($value,$name,$id = "")
{
return (!preg_match( '/^([0-9]|[0-5][0-9])$/', $value)) ? "contain only minutes from 0-59" : "";
}
public function is_natural($value,$name,$id = "")
{
return (!preg_match( '/^[0-9]+$/', $value)) ? "contain only a natural number: 0, 1, 2, 3, etc." : "";
}
public function is_natural_no_zero($value,$name,$id = "")
{
if ($value == 0)
{
return "contain only a natural number, but not zero: 1, 2, 3, etc.";
}
else
{
if ( ! preg_match( '/^[0-9]+$/', $value))
{
return "contain only a natural number, but not zero: 1, 2, 3, etc.";
}
}
}
public function required($value,$name,$id = "")
{
$value = trim($value);
return ((empty($value) || $value == "")&&$value!=0) ? "Not be empty" : "";
}
public function required_include_zero($value,$name,$id = "")
{
$value = trim($value);
if (strlen($value)==0 || $value == "")
{
return "Not be empty";
}
}
public function not_zero($value,$name,$id = ""){
if ($value==0||$value=="")
{
return "Selected From Inventory";
}
}
public function picture($value,$name,$id = "")
{
if (($_FILES["uploadedfile"]["type"] == "image/gif") ||
($_FILES["uploadedfile"]["type"] == "image/png") ||
($_FILES["uploadedfile"]["type"] == "image/jpeg") ||
($_FILES["uploadedfile"]["type"] == "image/pjpeg"))
{}
else
{
return "Upload Picture - Wrong file Type!";
}
}
public static function job_exist($value,$name,$id = "")
{
global $database;
$status = true;
$reason = "";
if(empty($id))
{
$check_dep = "
SELECT count(id)
FROM jobs
WHERE id = '$value'
LIMIT 1
";
}
else
{
$check_dep = "
SELECT count(id)
FROM jobs
WHERE id = '$value' AND id != '$id'
LIMIT 1
";
}
$exist = $database->getOne($check_dep);
if($exist == 1)
{
$status = false;
$reason = "not already exist";
}
return $reason;
}
public static function company_name($value,$name,$id = "")
{
global $database;
$status = true;
$reason = "";
if(empty($id))
{
$check_dep = "
SELECT count(id)
FROM clients
WHERE company_name = '$value'
LIMIT 1
";
}
else
{
$check_dep = "
SELECT count(id)
FROM clients
WHERE company_name = '$value' AND id != '$id'
LIMIT 1
";
}
$exist = $database->getOne($check_dep);
if($exist == 1)
{
$status = false;
$reason = "not already exist";
}
return $reason;
}
public static function email_exist($value,$name,$id = "")
{
global $database;
$db_name = DB_CLIENT_ACCOUNTS;
$status = true;
$reason = "";
if(empty($id))
{
$check_dep = "
SELECT count(id)
FROM $db_name"."users
WHERE email = '$value'
LIMIT 1
";
}
else
{
$check_dep = "
SELECT count(id)
FROM $db_name"."users
WHERE email = '$value' AND id != '$id'
LIMIT 1
";
}
$exist = $database->getOne($check_dep);
if($exist == 1)
{
$status = false;
$reason = "not already exist";
}
return $reason;
}
public static function username_exist($value,$name,$id = "")
{
global $database;
$db_name = DB_CLIENT_ACCOUNTS;
$status = true;
$reason = "";
if(empty($id))
{
$check_dep = "
SELECT count(id)
FROM $db_name"."users
WHERE username = '$value'
LIMIT 1
";
}
else
{
$check_dep = "
SELECT count(id)
FROM $db_name"."users
WHERE username = '$value' AND id != '$id'
LIMIT 1
";
}
$exist = $database->getOne($check_dep);
if($exist == 1)
{
$status = false;
$reason = "not already exist";
}
return $reason;
}
public static function employee_id_exist($value,$name,$id = "")
{
global $database;
$db_name = DB_CLIENT_ACCOUNTS;
$status = true;
$reason = "";
if(empty($id))
{
$check_dep = "
SELECT count(id)
FROM $db_name"."users
WHERE employee_id = '$value'
LIMIT 1
";
}
else
{
$check_dep = "
SELECT count(id)
FROM $db_name"."users
WHERE employee_id = '$value' AND id != '$id'
LIMIT 1
";
}
$exist = $database->getOne($check_dep);
if($exist == 1)
{
$status = false;
$reason = "not already exist";
}
return $reason;
}
public static function sub_domain($value,$name,$id = "")
{
global $database;
$db_name = DB_GLOBAL_CLIENT;
$status = true;
$reason = "";
if(empty($id))
{
$check_dep = "
SELECT count(id)
FROM $db_name"."account
WHERE domain = '$value'
LIMIT 1
";
}
else
{
$check_dep = "
SELECT count(id)
FROM $db_name"."account
WHERE domain = '$value' AND id != '$id'
LIMIT 1
";
}
$exist = $database->getOne($check_dep);
if($exist == 1)
{
$status = false;
$reason = "not already exist";
}
return $reason;
}
public static function supplier_name($value,$name,$id = "")
{
global $database;
$status = true;
$reason = "";
if(empty($id))
{
$check_dep = "
SELECT count(id)
FROM suppliers
WHERE name = '$value'
LIMIT 1
";
}
else
{
$check_dep = "
SELECT count(id)
FROM suppliers
WHERE name = '$value' AND id != '$id'
LIMIT 1
";
}
$exist = $database->getOne($check_dep);
if($exist == 1)
{
$status = false;
$reason = "not already exist";
}
return $reason;
}
public static function item_number_exist($value,$name,$id = "")
{
global $database;
$db_name = DB_CLIENT;
$status = true;
$reason = "";
if(empty($id))
{
$check_item = "
SELECT count(id)
FROM $db_name"."materials_inventory
WHERE item_number = '$value'
LIMIT 1
";
}
else
{
$check_item = "
SELECT count(id)
FROM $db_name"."materials_inventory
WHERE item_number = '$value' AND id != '$id'
LIMIT 1
";
}
$exist = $database->getOne($check_item);
if($exist == 1)
{
$status = false;
$reason = "not already exist";
}
return $reason;
}
}
?>