nouveau module "models" dans l'environnement
[mtweb] / mw / app / data / modules / sql / mw_data_crud.php
index 9ac527e..f438ef1 100644 (file)
@@ -3,6 +3,21 @@
   class mw_data_crud extends mw_data{
 
     # ----------------------------------------------------------------------------------------
+    #                                                                              description
+    #
+
+    function data_desc($params = array()){
+      $sgbd = $this->sgbd();
+      $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
+      if(!$table_name) return false;
+      try{
+        $desc = $sgbd->desc_table("#--".$table_name);
+      }
+      catch(Exception $e){ $desc = false; }
+      return $desc;
+    }
+
+    # ----------------------------------------------------------------------------------------
     #                                                                                   insert
     #
 
@@ -20,7 +35,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 +51,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{
     }
 
     # ----------------------------------------------------------------------------------------
+    #                                                                                   update
+    #
+
+    function data_update($params = array()){
+      $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["index_name"]) ? $params["index_name"] : false;
+      $index_value = isset($params["index_value"]) && (strlen($params["index_value"]) > 0) ? $params["index_value"] : false;
+      if(!$table_name || !$index_name || !$index_value || !$values) return false;
+      try{
+        $sql = "UPDATE `#--".$table_name."`";
+        $set = "";
+        foreach($values as $attribut_name => $attribut_value){
+          $set .= ($set ? ", " : "")."`".$attribut_name."`=".$this->eq($attribut_value);
+        }
+        $set = $set ? " SET ".$set : "";
+        $where = " WHERE `".$index_name."`=".$this->eq($index_value);
+        $sgbd->query($sql.$set.$where);
+      }
+      catch(Exception $e) { return false; }
+      return true;
+    }
+
+    # ----------------------------------------------------------------------------------------
+    #                                                                                   delete
+    #
+
+    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);
+      }
+      catch(Exception $e) { debug($e->getMessage()); return false; }
+      return true;
+    }
+
+    # ----------------------------------------------------------------------------------------
     #                                                                                     list
     #
 
       $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"] : "";
+      if(isset($params["filters"])){
+        foreach($params["filters"] as $filter){
+          $and_where = "";
+          if(isset($filter[0]) && isset($filter[1])){
+            switch(strtolower($filter[1])){
+              case "eq":
+                if(isset($filter[2])){
+                  $and_where .= ($where ? " AND " : " WHERE ").$filter[0]."=".$this->eq($filter[2]);
+                  break;
+                }
+              case "lt":
+                if(isset($filter[2])){
+                  $and_where .= ($where ? " AND " : " WHERE ").$filter[0]."<".$this->eq($filter[2]);
+                  break;
+                }
+              case "lte":
+                if(isset($filter[2])){
+                  $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">=".$this->eq($filter[2]);
+                  break;
+                }
+              case "gt":
+                if(isset($filter[2])){
+                  $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">".$this->eq($filter[2]);
+                  break;
+                }
+              case "gte":
+                if(isset($filter[2])){
+                  $and_where .= ($where ? " AND " : " WHERE ").$filter[0].">=".$this->eq($filter[2]);
+                  break;
+                }
+            }
+          }
+          $where .= $and_where;
+        }
+      }
       $order = isset($params["order_by"]) ?
         " ORDER BY ".$params["order_by"].(isset($params["order"]) ? " ".$params["order"] : "")
       : "";
       return $list;
     }
 
-    # ----------------------------------------------------------------------------------------
-    #                                                                                   update
-    #
-
-    function data_update($params = array()){
-      $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;
-      if(!$table_name || !$index_name || !$index_value || !$values) return false;
-      try{
-        $sql = "UPDATE `#--".$table_name."`";
-        $set = "";
-        foreach($values as $attribut_name => $attribut_value){
-          $set .= ($set ? ", " : "")."`".$attribut_name."`=".$this->eq($attribut_value);
-        }
-        $set = $set ? " SET ".$set : "";
-        $where = " WHERE `".$index_name."`=".$this->eq($index_value);
-        $sgbd->query($sql);
-      }
-      catch(Exception $e) { return false; }
-      return true;
-    }
-
-    # ----------------------------------------------------------------------------------------
-    #                                                                                   delete
-    #
-
-    function data_delete($params = array()){
-      $sgbd = $this->sgbd();
-      try{
-        $sql = "DELETE FROM `#--".$table_name."` WHERE `".$index_name."`=".$this->eq($index_value);
-        $sgbd->query($sql);
-      }
-      catch(Exception $e) { return false; }
-      return true;
-    }
-
   }
 
 ?>
\ No newline at end of file