3 class mw_env_models extends mw_env{
5 public function get_model($model_name, $default_values = array(), $RETURN_INSERT_ID = false){
6 $model_class_name = "mw_model_".$model_name;
7 if(!class_exists($model_class_name)){
8 if($this->app_file_exists("data/models/".$model_name.".php")){
9 require_once $this->app_file("data/models/".$model_name.".php");
10 if(!class_exists($model_class_name)) $model_class_name = "mw_model";
12 else $model_class_name = "mw_model";
14 $model = new $model_class_name($this->root_inst, $model_name, $default_values, $RETURN_INSERT_ID);
20 // -------------------------------------------------------------------------------------------
29 public $RETURN_INSERT_ID;
31 public function __construct(&$env, $model_name, $default_values = array(), $RETURN_INSERT_ID = false){
33 $this->model_name = $model_name;
34 $this->attributs = array();
35 $this->RETURN_INSERT_ID = $RETURN_INSERT_ID;
36 foreach($default_values as $attribut_name => $attribut_value){
37 $set_function = "set_".$attribut_name;
38 $this->$set_function($attribut_value);
42 public function env(){
46 public function get_values(){
48 foreach($this->attributs as $attribut_name => $attribut){
49 $values[$attribut_name] = isset($attribut["value"]) ? $attribut["value"] : null;
54 public function load($index_name, $index_value){
55 if(!isset($this->model_name)) return false;
56 $data = $this->env->data();
59 $values = $data->data_read(
61 "table_name" => $this->model_name,
62 "index_name" => $index_name,
63 "index_value" => $index_value
68 foreach($values as $key => $value){
69 if(!isset($this->attributs[$key])) $this->attributs[$key] = array();
70 $this->attributs[$key]["value"] = $value;
75 public function insert(){
76 if(!isset($this->model_name)) return false;
77 $data = $this->env->data();
78 return $data->data_insert(
80 "table_name" => $this->model_name,
81 "values" => $this->get_values(),
82 "return_insert_id" => $this->RETURN_INSERT_ID
87 public function update($index_name, $index_value = null){
88 if(!isset($this->model_name)) return false;
89 $data = $this->env->data();
90 if(!isset($index_value)){
91 $get_function = "get_".$index_name;
92 $index_value = $this->$get_function();
94 if(!isset($index_value) || !$index_value) return false;
95 return $data->data_update(
97 "table_name" => $this->model_name,
98 "index_name" => $index_name,
99 "index_value" => $index_value,
100 "values" => $this->get_values()
105 public function delete($index_name, $index_value = null){
106 if(!isset($this->model_name)) return false;
107 $data = $this->env->data();
108 if(!isset($index_value)){
109 $get_function = "get_".$index_name;
110 $index_value = $this->$get_function();
112 if(!isset($index_value) || !$index_value) return false;
113 return $data->data_delete(
115 "table_name" => $this->model_name,
116 "index_name" => $index_name,
117 "index_value" => $index_value
122 public function valid(){
125 "messages" => array()
127 foreach($this->attributs as $attribut_name => $attribut){
128 $get_function = "get_".$attribut_name;
129 $valid_function = "valid_".$attribut_name;
130 if(($r = $this->$valid_function($this->$get_function())) !== true){
131 $res["VALID"] = false;
132 $res["messages"][] = $r;
138 public function __call($method_name, $arguments){
140 if(substr($method_name, 0, 4) == "get_"){
142 ($attribut_name = substr($method_name, 4))
144 if(isset($this->attributs[$attribut_name]["value"])){
145 $r = $this->attributs[$attribut_name]["value"];
150 elseif(substr($method_name, 0, 4) == "set_"){
152 ($attribut_name = substr($method_name, 4))
153 && isset($arguments[0])
155 if(!isset($this->attributs[$attribut_name])) $this->attributs[$attribut_name] = array();
156 $this->attributs[$attribut_name]["value"] = $arguments[0];
160 elseif(substr($method_name, 0, 6) == "valid_"){
162 ($attribut_name = substr($method_name, 6))
163 && isset($arguments[0])