app_file_exists("data/models/".$model_name.".php")){ require_once $this->app_file("data/models/".$model_name.".php"); if(!class_exists($model_class_name)) $model_class_name = "mw_model"; } else $model_class_name = "mw_model"; } $model = new $model_class_name($this->root_inst, $model_name, $default_values, $RETURN_INSERT_ID); return $model; } } // ------------------------------------------------------------------------------------------- // class mw_model // class mw_model{ public $env; public $model_name; public $attributs; public $RETURN_INSERT_ID; public function __construct(&$env, $model_name, $default_values = array(), $RETURN_INSERT_ID = false){ $this->env = &$env; $this->model_name = $model_name; $this->attributs = array(); $this->RETURN_INSERT_ID = $RETURN_INSERT_ID; foreach($default_values as $attribut_name => $attribut_value){ $set_function = "set_".$attribut_name; $this->$set_function($attribut_value); } } public function env(){ return $this->env; } public function get_values(){ $values = array(); foreach($this->attributs as $attribut_name => $attribut){ $values[$attribut_name] = isset($attribut["value"]) ? $attribut["value"] : null; } return $values; } public function load($index_name, $index_value){ if(!isset($this->model_name)) return false; $data = $this->env->data(); if( ( $values = $data->data_read( array( "table_name" => $this->model_name, "index_name" => $index_name, "index_value" => $index_value ) ) ) === false ) return false; foreach($values as $key => $value){ if(!isset($this->attributs[$key])) $this->attributs[$key] = array(); $this->attributs[$key]["value"] = $value; } return true; } public function insert(){ if(!isset($this->model_name)) return false; $data = $this->env->data(); return $data->data_insert( array( "table_name" => $this->model_name, "values" => $this->get_values(), "return_insert_id" => $this->RETURN_INSERT_ID ) ); } public function update($index_name, $index_value = null){ if(!isset($this->model_name)) return false; $data = $this->env->data(); if(!isset($index_value)){ $get_function = "get_".$index_name; $index_value = $this->$get_function(); } if(!isset($index_value) || !$index_value) return false; return $data->data_update( array( "table_name" => $this->model_name, "index_name" => $index_name, "index_value" => $index_value, "values" => $this->get_values() ) ); } public function delete($index_name, $index_value = null){ if(!isset($this->model_name)) return false; $data = $this->env->data(); if(!isset($index_value)){ $get_function = "get_".$index_name; $index_value = $this->$get_function(); } if(!isset($index_value) || !$index_value) return false; return $data->data_delete( array( "table_name" => $this->model_name, "index_name" => $index_name, "index_value" => $index_value ) ); } public function valid(){ $res = array( "VALID" => true, "messages" => array() ); foreach($this->attributs as $attribut_name => $attribut){ $get_function = "get_".$attribut_name; $valid_function = "valid_".$attribut_name; if(($r = $this->$valid_function($this->$get_function())) !== true){ $res["VALID"] = false; $res["messages"][] = $r; } } return $res; } public function __call($method_name, $arguments){ $r = false; if(substr($method_name, 0, 4) == "get_"){ if( ($attribut_name = substr($method_name, 4)) ){ if(isset($this->attributs[$attribut_name]["value"])){ $r = $this->attributs[$attribut_name]["value"]; } else $r = null; } } elseif(substr($method_name, 0, 4) == "set_"){ if( ($attribut_name = substr($method_name, 4)) && isset($arguments[0]) ){ if(!isset($this->attributs[$attribut_name])) $this->attributs[$attribut_name] = array(); $this->attributs[$attribut_name]["value"] = $arguments[0]; $r = true; } } elseif(substr($method_name, 0, 6) == "valid_"){ if( ($attribut_name = substr($method_name, 6)) && isset($arguments[0]) ){ $r = true; } } return $r; } }