From dd547a56fc147a8a8b482c51a4f241d1ccbb62cb Mon Sep 17 00:00:00 2001 From: nicolas Date: Mon, 8 Apr 2013 16:23:53 +0200 Subject: [PATCH] fonctions CRUD par defaut --- mw/app/data/modules/sql/mw_data_crud.php | 94 +++++++++++++++++++++++++++++--- mw/app/data/mw_data.php | 5 ++ mw/app/init/0100_functions.php | 1 + mw/libs/empty_class.php | 28 +++++++--- 4 files changed, 113 insertions(+), 15 deletions(-) diff --git a/mw/app/data/modules/sql/mw_data_crud.php b/mw/app/data/modules/sql/mw_data_crud.php index 9ac527e..13aa8b1 100644 --- a/mw/app/data/modules/sql/mw_data_crud.php +++ b/mw/app/data/modules/sql/mw_data_crud.php @@ -3,6 +3,82 @@ class mw_data_crud extends mw_data{ # ---------------------------------------------------------------------------------------- + # call_crud + # + + function call_data_crud($method_name, $arguments){ + $r = false; + $sgbd = $this->sgbd(); + // add_(array $values) + if( + (substr($method_name, 0, 4) == "add_") + && ($table_name = substr($method_name, 4)) + && ($sgbd->table_exists("#--".$table_name)) + ){ + $r = $this->data_insert( + array( + "values" => $arguments[0], + "table_name" => $table_name + ) + ); + } + // get_(string $index_name, string $index_value) + elseif( + (substr($method_name, 0, 4) == "get_") + && ($table_name = substr($method_name, 4)) + && ($sgbd->table_exists("#--".$table_name)) + ){ + $r = $this->data_read( + array( + "table_name" => $table_name, + "index_name" => $arguments[0], + "index_value" => $arguments[1] + ) + ); + } + // list_(array $params) + elseif( + (substr($method_name, 0, 5) == "list_") + && ($table_name = substr($method_name, 5)) + && ($sgbd->table_exists("#--".$table_name)) + ){ + $params = $arguments[0]; + $params["table_name"] = $table_name; + $r = $this->data_list($params); + } + // set_(string index_name, string index_value, array $values) + elseif( + (substr($method_name, 0, 4) == "set_") + && ($table_name = substr($method_name, 4)) + && ($sgbd->table_exists("#--".$table_name)) + ){ + $r = $this->data_update( + array( + "table_name" => $table_name, + "index_name" => $arguments[0], + "index_value" => $arguments[1], + "values" => $arguments[2] + ) + ); + } + // del_(string $index_name, string $index_value) + elseif( + (substr($method_name, 0, 4) == "del_") + && ($table_name = substr($method_name, 4)) + && ($sgbd->table_exists("#--".$table_name)) + ){ + $r = $this->data_delete( + array( + "table_name" => $table_name, + "index_name" => $arguments[0], + "index_value" => $arguments[1] + ) + ); + } + return $r; + } + + # ---------------------------------------------------------------------------------------- # insert # @@ -20,7 +96,7 @@ $attributs_values .= ($attributs_values ? ", " : "").$this->eq($attribut_value); } $attributs_names = $attributs_names ? "(".$attributs_names.")" : ""; - $attributs_values = $attributs_values ? " VALUES (".$attributs_names.")" : ""; + $attributs_values = $attributs_values ? " VALUES (".$attributs_values.")" : ""; try{ $sgbd->query("INSERT INTO `#--".$table_name."`".$attributs_names.$attributs_values); $res = $return_insert_id ? $sgbd->insert_id() : true; @@ -36,8 +112,8 @@ function data_read($params = array()){ $sgbd = $this->sgbd(); $table_name = isset($params["table_name"]) ? $params["table_name"] : ""; - $index_name = isset($params[""]) ? $params["index_name"] : ""; - $index_value = isset($params[""]) ? $params["index_value"] : ""; + $index_name = isset($params["index_name"]) ? $params["index_name"] : ""; + $index_value = isset($params["index_value"]) ? $params["index_value"] : ""; if(!$table_name || !$index_name || !$index_value) return false; $item = array(); try{ @@ -58,7 +134,7 @@ $sgbd = $this->sgbd(); $table_name = isset($params["table_name"]) ? $params["table_name"] : ""; if(!$table_name) return false; - $index_name = isset($params["index_name"]) ? $params["table_index"] : ""; + $index_name = isset($params["index_name"]) ? $params["index_name"] : ""; $select = "SELECT `#--".$table_name."`.*"; $from = " FROM `#--".$table_name."`"; $where = isset($params["where"]) ? " WHERE ".$params["where"] : ""; @@ -96,8 +172,8 @@ $sgbd = $this->sgbd(); $table_name = isset($params["table_name"]) ? $params["table_name"] : false; $values = (isset($params["values"]) && is_array($params["values"])) ? $params["values"] : false; - $index_name = isset($params[""]) ? $params["index_name"] : false; - $index_value = isset($params[""]) ? $params["index_value"] : false; + $index_name = isset($params["index_name"]) ? $params["index_name"] : false; + $index_value = isset($params["index_value"]) ? $params["index_value"] : false; if(!$table_name || !$index_name || !$index_value || !$values) return false; try{ $sql = "UPDATE `#--".$table_name."`"; @@ -107,7 +183,7 @@ } $set = $set ? " SET ".$set : ""; $where = " WHERE `".$index_name."`=".$this->eq($index_value); - $sgbd->query($sql); + $sgbd->query($sql.$set.$where); } catch(Exception $e) { return false; } return true; @@ -119,6 +195,10 @@ function data_delete($params = array()){ $sgbd = $this->sgbd(); + $table_name = isset($params["table_name"]) ? $params["table_name"] : false; + $index_name = isset($params["index_name"]) ? $params["index_name"] : false; + $index_value = isset($params["index_value"]) ? $params["index_value"] : false; + if(!$table_name || !$index_name || !$index_value) return false; try{ $sql = "DELETE FROM `#--".$table_name."` WHERE `".$index_name."`=".$this->eq($index_value); $sgbd->query($sql); diff --git a/mw/app/data/mw_data.php b/mw/app/data/mw_data.php index 76fc2be..1a1c359 100644 --- a/mw/app/data/mw_data.php +++ b/mw/app/data/mw_data.php @@ -1,6 +1,11 @@ call_data_crud($method_name, $arguments); + } + } ?> \ No newline at end of file diff --git a/mw/app/init/0100_functions.php b/mw/app/init/0100_functions.php index 061cef9..b69728d 100644 --- a/mw/app/init/0100_functions.php +++ b/mw/app/init/0100_functions.php @@ -1,6 +1,7 @@ ".htmlentities(print_r($content, true), ENT_QUOTES, "UTF-8").""; diff --git a/mw/libs/empty_class.php b/mw/libs/empty_class.php index 30814be..32894ce 100644 --- a/mw/libs/empty_class.php +++ b/mw/libs/empty_class.php @@ -93,19 +93,31 @@ function empty_class_call($inst, $method_name, $arguments){ $r = false; - $args = ""; - foreach($arguments as $i => $arg) $args .= ($args ? ", " : "")."\$arguments[".$i."]"; - if(isset($inst->modules)) foreach($inst->modules as $module_name => $module){ - if(method_exists($module, $method_name)){ - eval("\$r = \$module->".$method_name."(".$args.");"); + if(($module = $this->get_module_for_method($inst, $method_name)) !== false){ + $args = ""; foreach($arguments as $i => $arg) $args .= ($args ? ", " : "")."\$arguments[".$i."]"; + eval("\$r = \$module->".$method_name."(".$args.");"); + } + else $r = $this->call_default($inst, $method_name, $arguments); + return $r; + } + + function get_module_for_method($inst, $method_name){ + $module = false; + if(isset($inst->modules)) foreach($inst->modules as $module_name => $module_impl){ + if(method_exists($module_impl, $method_name)){ + $module = $module_impl; break; } else{ - $r = $this->empty_class_call($module, $method_name, $arguments); - if($r !== false) break; + $module = $this->get_module_for_method($module_impl, $method_name); + if($module !== false) break; } } - return $r; + return $module; + } + + function call_default($inst, $method_name, $arguments){ + return false; } } -- 2.1.4