18a36130acbe7169f228d3345f383000aab64b18
[mw_pages] / app / data / modules / xml / 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       $res = array();
26       if($rst = $sgbd->open_data("pages"))
27       { while($v_rst = $sgbd->fetch_data($rst))
28         { if(isset($v_rst)) $res[$v_rst["id"]] = $v_rst;
29           else
30           { $res = false;
31             break;
32           }
33         }
34         $sgbd->close_data($rst);
35         if($res !== false)
36         { if(isset($order_by)) $res = $this->ordonne($res, "position", isset($order) ? $order : "ASC");
37           foreach($res as $id_res => $v_rst)
38           { $MATCH = true;
39             if(isset($id_parent))
40             { $MATCH =
41               (    ($id_parent && isset($v_rst["id_parent"]) && ($id_parent == $v_rst["id_parent"]))
42                 || ($id_parent == "" && !$v_rst["id_parent"])
43               );
44             }
45             if($MATCH)
46             { if(isset($enabled))
47               { $MATCH = ($v_rst["enabled"] == $enabled);
48               }
49             }
50             if($MATCH)
51             { if(isset($user))
52               { $MATCH = ($v_rst["user_creation"] == $user);
53               }
54             }
55             if($MATCH)
56             { $pages["total"]++;
57               $MATCH = !isset($start) || !$max || ($pages["total"] > $start && $pages["total"] <= ($start + $max));
58             }
59             if($MATCH) $pages["list"][$v_rst["id"]] = $v_rst;
60           }
61         }
62       }
63       else return false;
64       return $pages;
65     }
66
67     function pages_arbo($params = array())
68     { $params["id_parent"] = isset($params["id_parent"]) ? $params["id_parent"] : "";
69       $params["enabled"] = isset($params["enabled"]) ? ($params["enabled"] ? "1" : "0") : null;
70       $params["user"] = isset($params["user"]) ? $params["user"] : null;
71       $params["order_by"] = isset($params["order_by"]) ? $params["order_by"] : null;
72       $params["order"] = isset($params["order"]) ? $params["order"] : "ASC";
73       $sgbd = $this->sgbd();
74       $arbo = array();
75       if($params["id_parent"])
76       { if($parent = $this->page($params["id_parent"])) $arbo = $parent;
77         else return false;
78       }
79       $arbo["subs"] = array();
80       if(($subs = $this->pages($params)) !== false)
81       { foreach($subs["list"] as $id_sub => $sub)
82         { $params["id_parent"] = $sub["id"];
83           $arbo["subs"][] = $this->pages_arbo($params);
84         }
85       }
86       return $arbo;
87     }
88
89     function page($id)
90     { $sgbd = $this->sgbd();
91       return ($page = $sgbd->get_data("pages", $id)) ? $page : false;
92     }
93
94     function add_page($params)
95     { $id_parent = isset($params["id_parent"]) ? $params["id_parent"] : null;
96       $title = isset($params["title"]) ? $params["title"] : null;
97       $content = isset($params["content"]) ? $params["content"] : null;
98       $user = isset($params["user"]) ? $params["user"] : null;
99       $enabled = isset($params["enabled"]) ? ($params["enabled"] ? 1 : 0) : 1;
100       $position = isset($params["position"]) ? $params["position"] : 0;
101       $sgbd = $this->sgbd();
102       return
103       ( ( $id_page = $sgbd->add_data
104           ( "pages",
105             array
106             ( "id_parent" => $id_parent,
107               "title" => $title,
108               "content" => $content,
109               "date_creation" => date("Y-m-d H:i:s"),
110               "user_creation" => $user,
111               "date_last_update" => date("Y-m-d H:i:s"),
112               "user_last_update" => $user,
113               "enabled" => $enabled,
114               "position" => $position
115             )
116           )
117         ) ? $id_page : false
118       );
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         return $sgbd->set_data
131         ( "pages",
132           $id,
133           array
134           ( "id_parent" => $id_parent,
135             "title" => $title,
136             "content" => $content,
137             "date_creation" => $page["date_creation"],
138             "user_creation" => $page["user_creation"],
139             "date_last_update" => date("Y-m-d H:i:s"),
140             "user_last_update" => $user,
141             "enabled" => $enabled,
142             "position" => $position
143           )
144         );
145       }
146       return false;
147     }
148
149     function del_page($id)
150     { $sgbd = $this->sgbd();
151       if($sgbd->del_data("pages", $id))
152       { $OK = true;
153         if($rst = $sgbd->open_data("pages"))
154         { while($v_rst = $sgbd->fetch_data($rst))
155           { if(isset($v_rst))
156             { if(isset($v_rst["id_parent"]) && isset($v_rst["id"]) && $v_rst["id_parent"] == $id)
157               { unset($v_rst["id_parent"]);
158                 if(!$sgbd->set_data("pages", $v_rst["id"], $v_rst))
159                 { $OK = false;
160                   break;
161                 }
162               }
163             }
164             else $OK = false;
165           }
166           $sgbd->close_data($rst);
167         }
168         return $OK;
169       }
170       return false;
171     }
172
173   }
174
175 ?>