adaptations pour pludieurs instances d'applications
[mtweb] / mw / app / data / modules / xml / mw_data_xml_crud.php
1 <?php
2
3   class mw_data_xml_crud extends mw_data{
4
5     # ----------------------------------------------------------------------------------------
6     #                                                                                   insert
7     #
8
9     function data_insert($params = array()){
10       $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
11       if(!$table_name) return false;
12       $values = (isset($params["values"]) && is_array($params["values"])) ? $params["values"] : false;
13       if(!$values) return false;
14       $sgbd = $this->sgbd();
15       if(($id = $sgbd->add_data($table_name, $values)) === false) return false;
16       return $id;
17     }
18
19     # ----------------------------------------------------------------------------------------
20     #                                                                                     read
21     #
22
23     function data_read($params = array()){
24       $sgbd = $this->sgbd();
25       $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
26       $index_name = isset($params["index_name"]) ? $params["index_name"] : "";
27       $index_value = isset($params["index_value"]) ? $params["index_value"] : "";
28       if(!$table_name || !$index_name || !$index_value) return false;
29       $item = array();
30       if($rst = $sgbd->open_data($table_name)){
31         while($v_rst = $sgbd->fetch_data($rst)){
32           if(isset($v_rst)){
33             if(isset($v_rst[$index_name]) && ($v_rst[$index_name] == $index_value)){
34               $item = $v_rst;
35             }
36           }
37           else{
38             $sgbd->close_data($rst);
39             return false;
40           }
41         }
42         $sgbd->close_data($rst);
43       }
44       else return false;
45       return $item;
46     }
47
48     # ----------------------------------------------------------------------------------------
49     #                                                                                   update
50     #
51
52     function data_update($params = array()){
53       $sgbd = $this->sgbd();
54       $table_name = isset($params["table_name"]) ? $params["table_name"] : false;
55       $values = (isset($params["values"]) && is_array($params["values"])) ? $params["values"] : false;
56       $index_name = isset($params["index_name"]) ? $params["index_name"] : false;
57       $index_value = isset($params["index_value"]) && (strlen($params["index_value"]) > 0) ? $params["index_value"] : false;
58       if(!$table_name || !$index_name || !$index_value || !$values) return false;
59       if($rst = $sgbd->open_data($table_name)){
60         while($v_rst = $sgbd->fetch_data($rst)){
61           if(isset($v_rst)){
62             if(isset($v_rst["id"]) && isset($v_rst[$index_name]) && ($v_rst[$index_name] == $index_value)){
63               foreach($values as $attribut_name => $attribut_value){
64                 $v_rst[$attribut_name] = $attribut_value;
65                 if(!$sgbd->set_data($table_name, $v_rst["id"], $v_rst)){
66                   $sgbd->close_data($rst);
67                   return false;
68                 }
69               }
70             }
71           }
72           else{
73             $sgbd->close_data($rst);
74             return false;
75           }
76         }
77         $sgbd->close_data($rst);
78       }
79       else return false;
80       return true;
81     }
82
83     # ----------------------------------------------------------------------------------------
84     #                                                                                   delete
85     #
86
87     function data_delete($params = array()){
88       $sgbd = $this->sgbd();
89       $table_name = isset($params["table_name"]) ? $params["table_name"] : false;
90       $index_name = isset($params["index_name"]) ? $params["index_name"] : false;
91       $index_value = isset($params["index_value"]) && (strlen($params["index_value"]) > 0) ? $params["index_value"] : false;
92       if(!$table_name || !$index_name || !$index_value) return false;
93       if($rst = $sgbd->open_data($table_name)){
94         while($v_rst = $sgbd->fetch_data($rst)){
95           if(isset($v_rst)){
96             if(isset($v_rst["id"]) && isset($v_rst[$index_name]) && ($v_rst[$index_name] == $index_value)){
97               if(!$sgbd->del_data($table_name, $v_rst["id"])){
98                 $sgbd->close_data($rst);
99                 return false;
100               }
101             }
102           }
103           else{
104             $sgbd->close_data($rst);
105             return false;
106           }
107         }
108         $sgbd->close_data($rst);
109       }
110       else return false;
111       return true;
112     }
113
114     # ----------------------------------------------------------------------------------------
115     #                                                                                     list
116     #
117
118     function data_list($params = array()){
119       $sgbd = $this->sgbd();
120       $table_name = isset($params["table_name"]) ? $params["table_name"] : "";
121       if(!$table_name) return false;
122       $index_name = isset($params["index_name"]) ? $params["index_name"] : "";
123       $order_by = isset($params["order_by"]) ? $params["order_by"] : null;
124       $order = isset($params["order"]) ? $params["order"] : "ASC";
125       $limit = isset($params["limit"]) ? $params["limit"] : null;
126       $offset = isset($params["offset"]) ? $params["offset"] : null;
127       $list = array("list" => array(), "total" => 0);
128       $filters = isset($params["filters"]) ? $params["filters"] : array();
129       $list = array("list" => array(), "total" => 0);
130       $res = array();
131       if($rst = $sgbd->open_data($table_name)){
132         while($v_rst = $sgbd->fetch_data($rst)){
133           if(isset($v_rst)) $res[$v_rst["id"]] = $v_rst;
134           else{
135             $res = false;
136             break;
137           }
138         }
139         $sgbd->close_data($rst);
140         if($res === false) return false;
141         if(isset($order_by)) $res = $this->ordonne($res, $order_by, $order);
142         foreach($res as $id_res => $v_rst){
143           $MATCH = true;
144           foreach($filters as $filter){
145             $FILTER_MATCH = false;
146             if(isset($filter[0]) && isset($v_rst[$filter[0]]) && isset($filter[1])){
147               switch(strtolower($filter[1])){
148                 case "eq":
149                   if(isset($filter[2])){
150                     $FILTER_MATCH = ($v_rst[$filter[0]] == $filter[2]);
151                     break;
152                   }
153                 case "lt":
154                   if(isset($filter[2])){
155                     $FILTER_MATCH = ($v_rst[$filter[0]] < $filter[2]);
156                     break;
157                   }
158                 case "lte":
159                   if(isset($filter[2])){
160                     $FILTER_MATCH = ($v_rst[$filter[0]] <= $filter[2]);
161                     break;
162                   }
163                 case "gt":
164                   if(isset($filter[2])){
165                     $FILTER_MATCH = ($v_rst[$filter[0]] > $filter[2]);
166                     break;
167                   }
168                 case "gte":
169                   if(isset($filter[2])){
170                     $FILTER_MATCH = ($v_rst[$filter[0]] >= $filter[2]);
171                     break;
172                   }
173               }
174             }
175             if(!$FILTER_MATCH){
176               $MATCH = false;
177               break;
178             }
179           }
180           if($MATCH){
181             $list["total"]++;
182             if(isset($offset)) $MATCH = $list["total"] > $offset;
183             if($MATCH && isset($limit)) $MATCH = $list["total"] <= $limit;
184           }
185           if($MATCH) $list["list"][$v_rst[$index_name ? $index_name : "id"]] = $v_rst;
186           if(isset($limit) && ($list["total"] > $limit)) break;
187         }
188       }
189       else return false;
190       return $list;
191     }
192
193   }
194
195 ?>