nouveau module "models" dans l'environnement
[mtweb] / mw / app / data / modules / xml / mw_data_crud.php
diff --git a/mw/app/data/modules/xml/mw_data_crud.php b/mw/app/data/modules/xml/mw_data_crud.php
new file mode 100644 (file)
index 0000000..126f451
--- /dev/null
@@ -0,0 +1,195 @@
+<?php
+
+  class mw_data_crud extends mw_data{
+
+    # ----------------------------------------------------------------------------------------
+    #                                                                                   insert
+    #
+
+    function data_insert($params = array()){
+      $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
+      if(!$table_name) return false;
+      $values = (isset($params["values"]) && is_array($params["values"])) ? $params["values"] : false;
+      if(!$values) return false;
+      $sgbd = $this->sgbd();
+      if(($id = $sgbd->add_data($table_name, $values)) === false) return false;
+      return $id;
+    }
+
+    # ----------------------------------------------------------------------------------------
+    #                                                                                     read
+    #
+
+    function data_read($params = array()){
+      $sgbd = $this->sgbd();
+      $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
+      $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();
+      if($rst = $sgbd->open_data($table_name)){
+        while($v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst)){
+            if(isset($v_rst[$index_name]) && ($v_rst[$index_name] == $index_value)){
+              $item = $v_rst;
+            }
+          }
+          else{
+            $sgbd->close_data($rst);
+            return false;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else return false;
+      return $item;
+    }
+
+    # ----------------------------------------------------------------------------------------
+    #                                                                                   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;
+      if($rst = $sgbd->open_data($table_name)){
+        while($v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst)){
+            if(isset($v_rst["id"]) && isset($v_rst[$index_name]) && ($v_rst[$index_name] == $index_value)){
+              foreach($values as $attribut_name => $attribut_value){
+                $v_rst[$attribut_name] = $attribut_value;
+                if(!$sgbd->set_data($table_name, $v_rst["id"], $v_rst)){
+                  $sgbd->close_data($rst);
+                  return false;
+                }
+              }
+            }
+          }
+          else{
+            $sgbd->close_data($rst);
+            return false;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else 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"]) && (strlen($params["index_value"]) > 0) ? $params["index_value"] : false;
+      if(!$table_name || !$index_name || !$index_value) return false;
+      if($rst = $sgbd->open_data($table_name)){
+        while($v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst)){
+            if(isset($v_rst["id"]) && isset($v_rst[$index_name]) && ($v_rst[$index_name] == $index_value)){
+              if(!$sgbd->del_data($table_name, $v_rst["id"])){
+                $sgbd->close_data($rst);
+                return false;
+              }
+            }
+          }
+          else{
+            $sgbd->close_data($rst);
+            return false;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else return false;
+      return true;
+    }
+
+    # ----------------------------------------------------------------------------------------
+    #                                                                                     list
+    #
+
+    function data_list($params = array()){
+      $sgbd = $this->sgbd();
+      $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
+      if(!$table_name) return false;
+      $index_name = isset($params["index_name"]) ? $params["index_name"] : "";
+      $order_by = isset($params["order_by"]) ? $params["order_by"] : null;
+      $order = isset($params["order"]) ? $params["order"] : "ASC";
+      $limit = isset($params["limit"]) ? $params["limit"] : null;
+      $offset = isset($params["offset"]) ? $params["offset"] : null;
+      $list = array("list" => array(), "total" => 0);
+      $filters = isset($params["filters"]) ? $params["filters"] : array();
+      $list = array("list" => array(), "total" => 0);
+      $res = array();
+      if($rst = $sgbd->open_data($table_name)){
+        while($v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst)) $res[$v_rst["id"]] = $v_rst;
+          else{
+            $res = false;
+            break;
+          }
+        }
+        $sgbd->close_data($rst);
+        if($res === false) return false;
+        if(isset($order_by)) $res = $this->ordonne($res, $order_by, $order);
+        foreach($res as $id_res => $v_rst){
+          $MATCH = true;
+          foreach($filters as $filter){
+            $FILTER_MATCH = false;
+            if(isset($filter[0]) && isset($v_rst[$filter[0]]) && isset($filter[1])){
+              switch(strtolower($filter[1])){
+                case "eq":
+                  if(isset($filter[2])){
+                    $FILTER_MATCH = ($v_rst[$filter[0]] == $filter[2]);
+                    break;
+                  }
+                case "lt":
+                  if(isset($filter[2])){
+                    $FILTER_MATCH = ($v_rst[$filter[0]] < $filter[2]);
+                    break;
+                  }
+                case "lte":
+                  if(isset($filter[2])){
+                    $FILTER_MATCH = ($v_rst[$filter[0]] <= $filter[2]);
+                    break;
+                  }
+                case "gt":
+                  if(isset($filter[2])){
+                    $FILTER_MATCH = ($v_rst[$filter[0]] > $filter[2]);
+                    break;
+                  }
+                case "gte":
+                  if(isset($filter[2])){
+                    $FILTER_MATCH = ($v_rst[$filter[0]] >= $filter[2]);
+                    break;
+                  }
+              }
+            }
+            if(!$FILTER_MATCH){
+              $MATCH = false;
+              break;
+            }
+          }
+          if($MATCH){
+            $list["total"]++;
+            if(isset($offset)) $MATCH = $list["total"] > $offset;
+            if($MATCH && isset($limit)) $MATCH = $list["total"] <= $limit;
+          }
+          if($MATCH) $list["list"][$v_rst[$index_name ? $index_name : "id"]] = $v_rst;
+          if(isset($limit) && ($list["total"] > $limit)) break;
+        }
+      }
+      else return false;
+      return $list;
+    }
+
+  }
+
+?>
\ No newline at end of file