00001 <?php
00024 load_module('vldMapParser', MOD_INTERNAL);
00030 class validationCore extends vldMapParser{
00031 var $rules = array();
00032 var $res = array();
00033 var $err;
00034 var $validated = false;
00035
00051 function addCriteria($field_name, $rules=null, $msg=null){
00052 if(is_null($msg)){
00053
00054 foreach($rules as $rule => $err_msg){
00055 if(!isset($this->rules[$field_name])){$this->rules[$field_name] = array();}
00056 if(!is_array($this->rules[$field_name])){$this->rules[$field_name] = array();}
00057 array_push($this->rules[$field_name], $rule);
00058 $this->err[$field_name][$rule] = $err_msg;
00059 $this->err[$field_name][] = $err_msg;
00060 }
00061
00062 }else{
00064 if(is_string($msg)){
00065 if(!is_string($rules)){
00066 perror('<code>zigmoyd.validation.core.addCriteria()</code><br />expects $rule to be a string as $msg is given as a string');
00067 }else{
00068 if(!isset($this->rules[$field_name])){$this->rules[$field_name] = array();}
00069 if(!is_array($this->rules[$field_name])){$this->rules[$field_name] = array();}
00070 array_push($this->rules[$field_name], $rules);
00071 $this->err[$field_name][$rules] = $msg;
00072 $this->err[$field_name][] = $msg;
00073 }
00074 }else{
00075 if(is_array($msg)){
00076 foreach($rules as $i => $rule){
00077 if(!isset($this->rules[$field_name])){$this->rules[$field_name] = array();}
00078 if(!is_array($this->rules[$field_name])){$this->rules[$field_name] = array();}
00079 array_push($this->rules[$field_name], $rule);
00080 $this->err[$field_name][$rule] = $msg[$i];
00081 $this->err[$field_name][$i] = $msg[$i];
00082 }
00083 }else{
00084 perror('<code>zigmoyd.validation.core.addCriteria()</code><br />Invalid Datatype of $msg');
00085 }
00086 }
00087
00088 }
00089 }
00096 function removeCriteria($field_name, $criteria = null){
00097 if(!is_null($criteria)){
00098 foreach($this->rules[$field_name] as $i => $rule){
00099
00100 if(!is_numeric($criteria)){
00101 if($rule == $criteria){
00102 unset($this->rules[$field_name][$i]);
00103 $this->rules[$field_name] = array_values($this->rules[$field_name]);
00104 break;
00105 }
00106 }else{
00107 if($i == $criteria){
00108 unset($this->rules[$field_name][$i]);
00109 $this->rules[$field_name] = array_values($this->rules[$field_name]);
00110 break;
00111 }
00112 }
00113
00114 }
00115
00116
00117
00118 $err_keys = array_keys($this->err[$field_name]);
00119 foreach($err_keys as $current_indx => $current_criteria){
00120 if(strcmp($current_criteria, $criteria) == 0){
00121 break;
00122 }
00123 }
00124
00125 if(is_numeric($current_criteria)){
00126
00127 $indx = $err_keys[$current_indx-1];
00128
00129 }else{
00130
00131 $indx = $err_keys[$current_indx+1];
00132
00133 }
00134
00135 if(!isset($this->err[$field_name][$indx])){
00136 perror('<code>zigmoyd.validation.core.removeCriteria()</code><br />Zigmoyd Internal debug level Error Plese Inform this to Zigmoyd Developers<br />Alternative Index not found<br />on File '.__FILE__.' Class validation near Line '.__LINE__);
00137 }
00138 unset($this->err[$field_name][$current_criteria]);
00139 unset($this->err[$field_name][$indx]);
00140
00141 }else{
00142 foreach($this->rules[$field_name] as $i => $rule){
00143 unset($this->rules[$field_name][$i]);
00144 }
00145 }
00146 }
00152 function parseCriteria($map_file){
00153 foreach($this->tokenize($map_file) as $field_name => $criteria){
00154 $this->addCriteria($field_name, $criteria);
00155 }
00156 }
00169 function validationStatus(){
00170
00171 if(func_num_args() > 0){
00172 if(func_num_args() > 2){
00173 perror('<code>zigmoyd.validation.core.validationStatus()</code><br /> doesn\'t accepts more than 2 arguments');
00174 }elseif(func_num_args() == 2){
00175
00176 $key = func_get_arg(0);
00177 $mode = func_get_arg(1);
00178 }else{
00179
00180 if(is_string(func_get_arg(0))){
00181 $key = func_get_arg(0);
00182 $mode = Z_VLD_NUMERICAL;
00183 }elseif(is_bool(func_get_arg(0))){
00184 $key = null;
00185 $mode = func_get_arg(0);
00186 }else{
00187 perror('<code>zigmoyd.validation.core.validationStatus()</code><br />Failed understand the arguments given plsese see the manual first');
00188 }
00189 }
00190 }else{
00191 $key = null;
00192 $mode = Z_VLD_NUMERICAL;
00193 }
00194
00195 if($this->validated != $mode){
00196 $this->validate($mode);
00197 }
00198 if(!is_null($key)){
00199 if(!isset($this->res[$key])){
00200 perror('Either No rules Applied on the field `'.$key.'` or '.__CLASS__.'::validationStatus() is being called without calling '.__CLASS__.'::add_criterias() before');
00201 }else{
00202 return $this->res[$key];
00203 }
00204 }else{
00205 foreach($this->res as $key => $val){
00206 if(!is_bool($val)){return $key;}
00207 }
00208 return true;
00209 }
00210 }
00218 function overrideErrorMsg($field_name, $err, $err_txt){
00219 if(!isset($this->rules[$field_name])){
00220 perror("<code>zigmoyd.validation.core.overrideErrorMsg()</code><br />no rule hs been set for The field name $field_name but being used in ");
00221 }else{
00222 $this->err[$field_name][$err] = $err_txt;
00223
00224
00225 $err_keys = array_keys($this->err[$field_name]);
00226 foreach($err_keys as $current_indx => $current_criteria){
00227 if($current_criteria == $err){
00228 break;
00229 }
00230 }
00231
00232 if(is_numeric($err)){
00233
00234 $indx = $err_keys[$current_indx];
00235
00236 }else{
00237
00238 $indx = $err_keys[$current_indx+1];
00239
00240 }
00241
00242 if(!isset($this->err[$field_name][$indx])){
00243 perror('<code>zigmoyd.validation.core.overrideErrorMsg()</code><br />Zigmoyd Internal debug level Error Plese Inform this to Zigmoyd Developers<br />Alternative Index not found<br />on /usr/lib/module/ums/validation.php Class validation near Line '.__LINE__);
00244 }
00245 $this->err[$field_name][$indx] = $err_txt;
00246 }
00247 }
00257 function isValid($key = null){
00258 if($this->validated != Z_VLD_NUMERICAL)$this->validate();
00259 if($key == null){
00260
00261 $ret = true;
00262 foreach($this->res as $v){
00263 if(!is_bool($v)){$ret = false;break;}
00264 }
00265 return $ret;
00266 }else{
00267
00268 if(is_bool($this->validationStatus($key))){return true;}else{return false;}
00269 }
00270 }
00277 function errtext($field_name){
00278
00279
00280
00281
00282
00283 if(!isset($this->err[$field_name])){
00284 perror('<code>zigmoyd.validation.core.errtext()</code><br />No error message is set for Field '.$field_name);
00285 }else{
00286 if(!isset($this->rules[$field_name])){
00287 perror('<code>zigmoyd.validation.core.errtext()</code><br />No rule is set for Field '.$field_name.' but being used in ');
00288 }else{
00289 if(is_bool($this->res[$field_name]) && $this->res[$field_name]){
00290 return true;
00291 }else{
00292 if(!isset($this->err[$field_name][$this->res[$field_name]])){
00293 perror("<code>zigmoyd.validation.core.errtext()</code><br /> No Error Message has been set for Error Index ".$this->res[$field_name]." Error Name ".$this->rules[$this->res[$field_name]]);
00294 }
00295 return $this->err[$field_name][$this->res[$field_name]];
00296 }
00297 }
00298 }
00299 }
00304 function revalidate(){
00305 $this->validate();
00306 $this->isValid = $this->isValid();
00307 foreach($this->rules as $field_name => $rules){
00308
00309 $this->{$field_name} = new zRom_fields($this->items[$field_name], $this->errtext($field_name), $this->isValid($field_name), $this->validationStatus($field_name));
00310
00311 }
00312 }
00313 }
00315 ?>