import version 0.1
[mw_pages] / app / data / modules / sql / mw_data_pages.php
1 <?php
2
3   class mw_data_pages extends mw_data
4   {
5
6     function page_ariane($id, $ariane = array())
7     { if($page = $this->page($id))
8       { if($page["id_parent"]) $ariane = $this->page_ariane($page["id_parent"], $ariane);
9         $ariane[] = $page;
10         return $ariane;
11       }
12       return false;
13     }
14
15     function pages($params = array())
16     { $id_parent = isset($params["id_parent"]) ? $params["id_parent"] : null;
17       $enabled = isset($params["enabled"]) ? ($params["enabled"] ? "1" : "0") : null;
18       $user = isset($params["user"]) ? $params["user"] : null;
19       $order_by = isset($params["order_by"]) ? $params["order_by"] : null;
20       $order = isset($params["order"]) ? $params["order"] : "ASC";
21       $start = isset($params["start"]) ? $params["start"] : null;
22       $max = isset($params["max"]) ? $params["max"] : null;
23       $sgbd = $this->sgbd();
24       $pages = array("list" => array(), "total" => 0);
25       $SELECT = "SELECT * FROM #--pages";
26       $COUNT_SELECT = "SELECT count(*) as n FROM #--pages";
27       $WHERE = "";
28       $WHERE .= (isset($id_parent) ? ($WHERE ? " AND" : " WHERE")." id_parent".(strlen($id_parent) > 0 ? "=".$this->eq($id_parent) : " IS NULL") : "");
29       $WHERE .= (isset($enabled) ? ($WHERE ? " AND" : " WHERE")." enabled=".$this->eq($enabled) : "");
30       $WHERE .= (isset($user) ? ($WHERE ? " AND" : " WHERE")." user_creation=".$this->eq($user) : "");
31       $ORDER_BY =
32       ( isset($order_by) ?
33           " ORDER BY ".$order_by.(isset($order) ? " ".$order : "")
34         : ""
35       );
36       $LIMIT = isset($max) ? " LIMIT ".$max.(isset($start) ? " OFFSET ".$start : "") : "";
37       $sql = $COUNT_SELECT.$WHERE;
38       $rst = $sgbd->query($sql);
39       if(!isset($rst)) return false;
40       if($v_rst = $sgbd->fetch_assoc($rst)) $pages["total"] = $v_rst["n"];
41       $sgbd->free_result($rst);
42       if($pages["total"])
43       { $sql = $SELECT.$WHERE.$ORDER_BY.$LIMIT;
44         $rst = $sgbd->query($sql);
45         if(!isset($rst)) return false;
46         while($v_rst = $sgbd->fetch_assoc($rst)) $pages["list"][$v_rst["id"]] = $v_rst;
47         $sgbd->free_result($rst);
48       }
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       $rst = $sgbd->query($sql);
77       if(!isset($rst)) return false;
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       return $arbo;
84     }
85
86     function page($id)
87     { $page = array();
88       $sgbd = $this->sgbd();
89       $sql = "SELECT * FROM #--pages WHERE id=".$this->eq($id);
90       $rst = $sgbd->query($sql);
91       if(!isset($rst)) return false;
92       if($v_rst = $sgbd->fetch_assoc($rst)) $page = $v_rst;
93       return $page;
94     }
95
96     function add_page($params)
97     { $id_parent = isset($params["id_parent"]) ? $params["id_parent"] : null;
98       $title = isset($params["title"]) ? $params["title"] : null;
99       $content = isset($params["content"]) ? $params["content"] : null;
100       $user = isset($params["user"]) ? $params["user"] : null;
101       $enabled = isset($params["enabled"]) ? ($params["enabled"] ? 1 : 0) : 1;
102       $position = isset($params["position"]) ? $params["position"] : 0;
103       $sgbd = $this->sgbd();
104       $sql =
105        "INSERT INTO #--pages"
106       ."(id_parent, title, content, date_creation, user_creation, date_last_update, user_last_update, enabled, position)"
107       ." VALUES"
108       ."( ".$this->eq($id_parent)
109       .", ".$this->eq($title)
110       .", ".$this->eq($content)
111       .", '".date("Y-m-d H:i:s")."'"
112       .", ".$this->eq($user)
113       .", '".date("Y-m-d H:i:s")."'"
114       .", ".$this->eq($user)
115       .", ".$this->eq($enabled)
116       .", ".$this->eq($position)
117       .")";
118       if($sgbd->query($sql)) return $sgbd->insert_id();
119       return false;
120     }
121
122     function set_page($id, $params, $RAZ = false)
123     { if($page = $this->page($id))
124       { $id_parent = isset($params["id_parent"]) ? $params["id_parent"] : ($RAZ ? null : $page["id_parent"]);
125         $title = isset($params["title"]) ? $params["title"] : ($RAZ ? null : $page["title"]);
126         $content = isset($params["content"]) ? $params["content"] : ($RAZ ? null : $page["content"]);
127         $user = isset($params["user"]) ? $params["user"] : null;
128         $enabled = isset($params["enabled"]) ? ($params["enabled"] ? 1 : 0) : ($RAZ ? 1 : $page["enabled"]);
129         $position = isset($params["position"]) ? $params["position"] : ($RAZ ? 0 : $page["position"]);
130         $sgbd = $this->sgbd();
131         $sql =
132          "UPDATE #--pages SET"
133         ."  id_parent=".$this->eq($id_parent)
134         .", title=".$this->eq($title)
135         .", content=".$this->eq($content)
136         .", date_last_update='".date("Y-m-d H:i:s")."'"
137         .", user_last_update=".$this->eq($user)
138         .", enabled=".$this->eq($enabled)
139         .", position=".$this->eq($position)
140         ." WHERE id=".$this->eq($id);
141         if($sgbd->query($sql)) return true;
142       }
143       return false;
144     }
145
146     function del_page($id)
147     { $sgbd = $this->sgbd();
148       if($sgbd->query("DELETE FROM #--pages WHERE id=".$this->eq($id)))
149       { return $sgbd->query("UPDATE #--pages SET id_parent=NULL WHERE id_parent=".$this->eq($id)) ? true : false;
150       }
151       return false;
152     }
153
154   }
155
156 ?>