c7648d715849d5457bff0998286d679c62ecb1cc
[mw_pages] / app / data / modules / sql / mw_data_pages.php
1 <?php
2
3   class mw_data_pages extends mw_data{
4
5     function page_ariane($id, $ariane = array()){
6       if($page = $this->page($id)){
7         if($page["id_parent"]) $ariane = $this->page_ariane($page["id_parent"], $ariane);
8         $ariane[] = $page;
9         return $ariane;
10       }
11       return false;
12     }
13
14     function pages($params = array()){
15       $id_parent = isset($params["id_parent"]) ? $params["id_parent"] : null;
16       $enabled = isset($params["enabled"]) ? ($params["enabled"] ? "1" : "0") : null;
17       $user = isset($params["user"]) ? $params["user"] : null;
18       $order_by = isset($params["order_by"]) ? $params["order_by"] : null;
19       $order = isset($params["order"]) ? $params["order"] : "ASC";
20       $start = isset($params["start"]) ? $params["start"] : null;
21       $max = isset($params["max"]) ? $params["max"] : null;
22       $sgbd = $this->sgbd();
23       $pages = array("list" => array(), "total" => 0);
24       $SELECT = "SELECT * FROM #--pages";
25       $COUNT_SELECT = "SELECT count(*) as n FROM #--pages";
26       $WHERE = "";
27       $WHERE .= (isset($id_parent) ? ($WHERE ? " AND" : " WHERE")." id_parent".(strlen($id_parent) > 0 ? "=".$this->eq($id_parent) : " IS NULL") : "");
28       $WHERE .= (isset($enabled) ? ($WHERE ? " AND" : " WHERE")." enabled=".$this->eq($enabled) : "");
29       $WHERE .= (isset($user) ? ($WHERE ? " AND" : " WHERE")." user_creation=".$this->eq($user) : "");
30       $ORDER_BY = (
31         isset($order_by) ?
32           " ORDER BY ".$order_by.(isset($order) ? " ".$order : "")
33         : ""
34       );
35       $LIMIT = isset($max) ? " LIMIT ".$max.(isset($start) ? " OFFSET ".$start : "") : "";
36       $sql = $COUNT_SELECT.$WHERE;
37       try{
38         $rst = $sgbd->query($sql);
39         if($v_rst = $sgbd->fetch_assoc($rst)) $pages["total"] = $v_rst["n"];
40         $sgbd->free_result($rst);
41         if($pages["total"]){
42           $sql = $SELECT.$WHERE.$ORDER_BY.$LIMIT;
43           $rst = $sgbd->query($sql);
44           while($v_rst = $sgbd->fetch_assoc($rst)) $pages["list"][$v_rst["id"]] = $v_rst;
45           $sgbd->free_result($rst);
46         }
47       }
48       catch(Exception $e) { $pages = false; }
49       return $pages;
50     }
51
52     function pages_arbo($params = array()){
53       $id_parent = isset($params["id_parent"]) ? $params["id_parent"] : null;
54       $enabled = isset($params["enabled"]) ? ($params["enabled"] ? "1" : "0") : null;
55       $user = isset($params["user"]) ? $params["user"] : null;
56       $order_by = isset($params["order_by"]) ? $params["order_by"] : null;
57       $order = isset($params["order"]) ? $params["order"] : "ASC";
58       $sgbd = $this->sgbd();
59       $arbo = array();
60       if(isset($id_parent)){
61         if($parent = $this->page($id_parent)) $arbo = $parent;
62         else return false;
63       }
64       $arbo["subs"] = array();
65       $SELECT = "SELECT id FROM #--pages";
66       $WHERE = "";
67       $WHERE .= ($WHERE ? " AND" : " WHERE")." id_parent".(isset($id_parent) && $id_parent ? "=".$this->eq($id_parent) : " IS NULL");
68       $WHERE .= (isset($enabled) ? ($WHERE ? " AND" : " WHERE")." enabled=".$this->eq($enabled) : "");
69       $WHERE .= (isset($user) ? ($WHERE ? " AND" : " WHERE")." user_creation=".$this->eq($user) : "");
70       $ORDER_BY = (
71         isset($order_by) ?
72           " ORDER BY ".$order_by.(isset($order) ? " ".$order : "")
73         : ""
74       );
75       $sql = $SELECT.$WHERE.$ORDER_BY;
76       try{
77         $rst = $sgbd->query($sql);
78         while($v_rst = $sgbd->fetch_assoc($rst)){
79           $params["id_parent"] = $v_rst["id"];
80           $arbo["subs"][] = $this->pages_arbo($params);
81         }
82         $sgbd->free_result($rst);
83       }
84       catch(Exception $e) { $arbo = false; }
85       return $arbo;
86     }
87
88     function page($id){
89       $page = array();
90       $sgbd = $this->sgbd();
91       $sql = "SELECT * FROM #--pages WHERE id=".$this->eq($id);
92       try{
93         $rst = $sgbd->query($sql);
94         if($v_rst = $sgbd->fetch_assoc($rst)) $page = $v_rst;
95         $sgbd->free_result($rst);
96       }
97       catch(Exception $e) { $page = false; }
98       return $page;
99     }
100
101     function add_page($params){
102       $id_parent = isset($params["id_parent"]) ? $params["id_parent"] : null;
103       $title = isset($params["title"]) ? $params["title"] : null;
104       $content = isset($params["content"]) ? $params["content"] : null;
105       $user = isset($params["user"]) ? $params["user"] : null;
106       $enabled = isset($params["enabled"]) ? ($params["enabled"] ? 1 : 0) : 1;
107       $position = isset($params["position"]) ? $params["position"] : 0;
108       $sgbd = $this->sgbd();
109       $sql =
110        "INSERT INTO #--pages"
111       ."(id_parent, title, content, date_creation, user_creation, date_last_update, user_last_update, enabled, position)"
112       ." VALUES"
113       ."( ".$this->eq($id_parent)
114       .", ".$this->eq($title)
115       .", ".$this->eq($content)
116       .", '".date("Y-m-d H:i:s")."'"
117       .", ".$this->eq($user)
118       .", '".date("Y-m-d H:i:s")."'"
119       .", ".$this->eq($user)
120       .", ".$this->eq($enabled)
121       .", ".$this->eq($position)
122       .")";
123       try{
124         $sgbd->query($sql);
125         $id_page = $sgbd->insert_id();
126       }
127       catch(Exception $e) { $id_page = false; }
128       return $id_page;
129     }
130
131     function set_page($id, $params, $RAZ = false){
132       if($page = $this->page($id)){
133         $id_parent = isset($params["id_parent"]) ? $params["id_parent"] : ($RAZ ? null : $page["id_parent"]);
134         $title = isset($params["title"]) ? $params["title"] : ($RAZ ? null : $page["title"]);
135         $content = isset($params["content"]) ? $params["content"] : ($RAZ ? null : $page["content"]);
136         $user = isset($params["user"]) ? $params["user"] : null;
137         $enabled = isset($params["enabled"]) ? ($params["enabled"] ? 1 : 0) : ($RAZ ? 1 : $page["enabled"]);
138         $position = isset($params["position"]) ? $params["position"] : ($RAZ ? 0 : $page["position"]);
139         $sgbd = $this->sgbd();
140         $sql =
141          "UPDATE #--pages SET"
142         ."  id_parent=".$this->eq($id_parent)
143         .", title=".$this->eq($title)
144         .", content=".$this->eq($content)
145         .", date_last_update='".date("Y-m-d H:i:s")."'"
146         .", user_last_update=".$this->eq($user)
147         .", enabled=".$this->eq($enabled)
148         .", position=".$this->eq($position)
149         ." WHERE id=".$this->eq($id);
150         try{
151           $sgbd->query($sql);
152         }
153         catch(Exception $e) { return false; }
154         return true;
155       }
156       return false;
157     }
158
159     function del_page($id){
160       $sgbd = $this->sgbd();
161       try{
162         $sgbd->query("UPDATE #--pages SET id_parent=NULL WHERE id_parent=".$this->eq($id));
163         $sgbd->query("DELETE FROM #--pages WHERE id=".$this->eq($id));
164       }
165       catch(Exception $e) { return false; }
166       return true;
167     }
168
169   }
170
171 ?>