maj syntaxe accolades et correction param manquant
[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       $rst = $sgbd->query($sql);
38       if(!isset($rst)) return false;
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         if(!isset($rst)) return false;
45         while($v_rst = $sgbd->fetch_assoc($rst)) $pages["list"][$v_rst["id"]] = $v_rst;
46         $sgbd->free_result($rst);
47       }
48       return $pages;
49     }
50
51     function pages_arbo($params = array()){
52       $id_parent = isset($params["id_parent"]) ? $params["id_parent"] : null;
53       $enabled = isset($params["enabled"]) ? ($params["enabled"] ? "1" : "0") : null;
54       $user = isset($params["user"]) ? $params["user"] : null;
55       $order_by = isset($params["order_by"]) ? $params["order_by"] : null;
56       $order = isset($params["order"]) ? $params["order"] : "ASC";
57       $sgbd = $this->sgbd();
58       $arbo = array();
59       if(isset($id_parent)){
60         if($parent = $this->page($id_parent)) $arbo = $parent;
61         else return false;
62       }
63       $arbo["subs"] = array();
64       $SELECT = "SELECT id FROM #--pages";
65       $WHERE = "";
66       $WHERE .= ($WHERE ? " AND" : " WHERE")." id_parent".(isset($id_parent) && $id_parent ? "=".$this->eq($id_parent) : " IS NULL");
67       $WHERE .= (isset($enabled) ? ($WHERE ? " AND" : " WHERE")." enabled=".$this->eq($enabled) : "");
68       $WHERE .= (isset($user) ? ($WHERE ? " AND" : " WHERE")." user_creation=".$this->eq($user) : "");
69       $ORDER_BY = (
70         isset($order_by) ?
71           " ORDER BY ".$order_by.(isset($order) ? " ".$order : "")
72         : ""
73       );
74       $sql = $SELECT.$WHERE.$ORDER_BY;
75       $rst = $sgbd->query($sql);
76       if(!isset($rst)) return false;
77       while($v_rst = $sgbd->fetch_assoc($rst)){
78         $params["id_parent"] = $v_rst["id"];
79         $arbo["subs"][] = $this->pages_arbo($params);
80       }
81       $sgbd->free_result($rst);
82       return $arbo;
83     }
84
85     function page($id){
86       $page = array();
87       $sgbd = $this->sgbd();
88       $sql = "SELECT * FROM #--pages WHERE id=".$this->eq($id);
89       $rst = $sgbd->query($sql);
90       if(!isset($rst)) return false;
91       if($v_rst = $sgbd->fetch_assoc($rst)) $page = $v_rst;
92       return $page;
93     }
94
95     function add_page($params){
96       $id_parent = isset($params["id_parent"]) ? $params["id_parent"] : null;
97       $title = isset($params["title"]) ? $params["title"] : null;
98       $content = isset($params["content"]) ? $params["content"] : null;
99       $user = isset($params["user"]) ? $params["user"] : null;
100       $enabled = isset($params["enabled"]) ? ($params["enabled"] ? 1 : 0) : 1;
101       $position = isset($params["position"]) ? $params["position"] : 0;
102       $sgbd = $this->sgbd();
103       $sql =
104        "INSERT INTO #--pages"
105       ."(id_parent, title, content, date_creation, user_creation, date_last_update, user_last_update, enabled, position)"
106       ." VALUES"
107       ."( ".$this->eq($id_parent)
108       .", ".$this->eq($title)
109       .", ".$this->eq($content)
110       .", '".date("Y-m-d H:i:s")."'"
111       .", ".$this->eq($user)
112       .", '".date("Y-m-d H:i:s")."'"
113       .", ".$this->eq($user)
114       .", ".$this->eq($enabled)
115       .", ".$this->eq($position)
116       .")";
117       if($sgbd->query($sql)) return $sgbd->insert_id();
118       return false;
119     }
120
121     function set_page($id, $params, $RAZ = false){
122       if($page = $this->page($id)){
123         $id_parent = isset($params["id_parent"]) ? $params["id_parent"] : ($RAZ ? null : $page["id_parent"]);
124         $title = isset($params["title"]) ? $params["title"] : ($RAZ ? null : $page["title"]);
125         $content = isset($params["content"]) ? $params["content"] : ($RAZ ? null : $page["content"]);
126         $user = isset($params["user"]) ? $params["user"] : null;
127         $enabled = isset($params["enabled"]) ? ($params["enabled"] ? 1 : 0) : ($RAZ ? 1 : $page["enabled"]);
128         $position = isset($params["position"]) ? $params["position"] : ($RAZ ? 0 : $page["position"]);
129         $sgbd = $this->sgbd();
130         $sql =
131          "UPDATE #--pages SET"
132         ."  id_parent=".$this->eq($id_parent)
133         .", title=".$this->eq($title)
134         .", content=".$this->eq($content)
135         .", date_last_update='".date("Y-m-d H:i:s")."'"
136         .", user_last_update=".$this->eq($user)
137         .", enabled=".$this->eq($enabled)
138         .", position=".$this->eq($position)
139         ." WHERE id=".$this->eq($id);
140         if($sgbd->query($sql)) return true;
141       }
142       return false;
143     }
144
145     function del_page($id){
146       $sgbd = $this->sgbd();
147       if($sgbd->query("DELETE FROM #--pages WHERE id=".$this->eq($id))){
148         return $sgbd->query("UPDATE #--pages SET id_parent=NULL WHERE id_parent=".$this->eq($id)) ? true : false;
149       }
150       return false;
151     }
152
153   }
154
155 ?>