syntaxe POO (visibilite)
[mw_pages] / app / data / modules / xml / mw_data_pages.php
1 <?php
2
3   class mw_data_pages extends mw_data{
4
5     public function page_ariane($id, $ariane = array()){
6       if($page = $this->page($id)){
7         if(isset($page["id_parent"]) && $page["id_parent"]) $ariane = $this->page_ariane($page["id_parent"], $ariane);
8         $ariane[] = $page;
9         return $ariane;
10       }
11       return false;
12     }
13
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       $res = array();
25       if($rst = $sgbd->open_data("pages")){
26         while($v_rst = $sgbd->fetch_data($rst)){
27           if(isset($v_rst)) $res[$v_rst["id"]] = $v_rst;
28           else{
29             $res = false;
30             break;
31           }
32         }
33         $sgbd->close_data($rst);
34         if($res !== false){
35           if(isset($order_by)) $res = $this->ordonne($res, "position", isset($order) ? $order : "ASC");
36           foreach($res as $id_res => $v_rst){
37             $MATCH = true;
38             if(isset($id_parent)){
39               $MATCH = (
40                    ($id_parent && isset($v_rst["id_parent"]) && ($id_parent == $v_rst["id_parent"]))
41                 || ($id_parent == "" && (!isset($v_rst["id_parent"]) || !$v_rst["id_parent"]))
42               );
43             }
44             if($MATCH){
45               if(isset($enabled)){
46                 $MATCH = ($v_rst["enabled"] == $enabled);
47               }
48             }
49             if($MATCH){
50               if(isset($user)){
51                 $MATCH = ($v_rst["user_creation"] == $user);
52               }
53             }
54             if($MATCH){
55               $pages["total"]++;
56               $MATCH = !isset($start) || !$max || ($pages["total"] > $start && $pages["total"] <= ($start + $max));
57             }
58             if($MATCH) $pages["list"][$v_rst["id"]] = $v_rst;
59           }
60         }
61       }
62       else return false;
63       return $pages;
64     }
65
66     public function pages_arbo($params = array()){
67       $params["id_parent"] = isset($params["id_parent"]) ? $params["id_parent"] : "";
68       $params["enabled"] = isset($params["enabled"]) ? ($params["enabled"] ? "1" : "0") : null;
69       $params["user"] = isset($params["user"]) ? $params["user"] : null;
70       $params["order_by"] = isset($params["order_by"]) ? $params["order_by"] : null;
71       $params["order"] = isset($params["order"]) ? $params["order"] : "ASC";
72       $sgbd = $this->sgbd();
73       $arbo = array();
74       if($params["id_parent"]){
75         if($parent = $this->page($params["id_parent"])) $arbo = $parent;
76         else return false;
77       }
78       $arbo["subs"] = array();
79       if(($subs = $this->pages($params)) !== false){
80         foreach($subs["list"] as $id_sub => $sub){
81           $params["id_parent"] = $sub["id"];
82           $arbo["subs"][] = $this->pages_arbo($params);
83         }
84       }
85       return $arbo;
86     }
87
88     public function page($id){
89       $sgbd = $this->sgbd();
90       return ($page = $sgbd->get_data("pages", $id)) ? $page : false;
91     }
92
93     public function add_page($params){
94       $id_parent = isset($params["id_parent"]) ? $params["id_parent"] : null;
95       $title = isset($params["title"]) ? $params["title"] : null;
96       $content = isset($params["content"]) ? $params["content"] : null;
97       $user = isset($params["user"]) ? $params["user"] : null;
98       $enabled = isset($params["enabled"]) ? ($params["enabled"] ? 1 : 0) : 1;
99       $position = isset($params["position"]) ? $params["position"] : 0;
100       $sgbd = $this->sgbd();
101       return (
102         (
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     public 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 : (isset($page["id_parent"]) ? $page["id_parent"] : null));
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     public 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   }