3 class mw_data_pages extends mw_data{
5 public 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);
14 public 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";
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) : "");
32 " ORDER BY ".$order_by.(isset($order) ? " ".$order : "")
35 $LIMIT = isset($max) ? " LIMIT ".$max.(isset($start) ? " OFFSET ".$start : "") : "";
36 $sql = $COUNT_SELECT.$WHERE;
38 $rst = $sgbd->query($sql);
39 if($v_rst = $sgbd->fetch_assoc($rst)) $pages["total"] = $v_rst["n"];
40 $sgbd->free_result($rst);
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);
48 catch(Exception $e) { $pages = false; }
52 public 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();
60 if(isset($id_parent)){
61 if($parent = $this->page($id_parent)) $arbo = $parent;
64 $arbo["subs"] = array();
65 $SELECT = "SELECT id FROM #--pages";
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) : "");
72 " ORDER BY ".$order_by.(isset($order) ? " ".$order : "")
75 $sql = $SELECT.$WHERE.$ORDER_BY;
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);
82 $sgbd->free_result($rst);
84 catch(Exception $e) { $arbo = false; }
88 public function page($id){
90 $sgbd = $this->sgbd();
91 $sql = "SELECT * FROM #--pages WHERE id=".$this->eq($id);
93 $rst = $sgbd->query($sql);
94 if($v_rst = $sgbd->fetch_assoc($rst)) $page = $v_rst;
95 $sgbd->free_result($rst);
97 catch(Exception $e) { $page = false; }
101 public 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();
110 "INSERT INTO #--pages"
111 ."(id_parent, title, content, date_creation, user_creation, date_last_update, user_last_update, enabled, position)"
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)
125 $id_page = $sgbd->insert_id();
127 catch(Exception $e) { $id_page = false; }
131 public 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();
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);
153 catch(Exception $e) { return false; }
159 public function del_page($id){
160 $sgbd = $this->sgbd();
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));
165 catch(Exception $e) { return false; }