d1c16640e55a7e1127cf2ee109cc47daef9fb739
[mw_pages] / mw_pages.php
1 <?php
2
3   class mw_pages extends mw_plugin{
4
5     function title(){
6       return "Pages";
7     }
8
9     function description(){
10       return "Pour ajouter des pages, avec un &eacute;diteur dans l'administration du site";
11     }
12
13     function init($env){
14       $env->set_link("plugins/admin/mw_pages", $env->url("pages/admin"), "&eacute;diter les pages");
15       $env->set_link("admin/pages", $env->url("pages/admin"), "Pages", 40);
16       $data = $env->data();
17       if(
18         (
19           $arbo = $data->pages_arbo(
20             array(
21               "enabled" => 1,
22               "order_by" => "position",
23               "order" => "ASC"
24             )
25           )
26         ) !== false
27       ){
28         $this->_set_links($env, "menu_top", $arbo);
29       }
30       return true;
31     }
32
33     function _set_links($env, $parent, $arbo){
34       if($arbo["subs"]){
35         foreach($arbo["subs"] as $page){
36           $env->set_link(
37             $parent."/mw_page_".$page["id"],
38             $env->url("pages/view/page", array("id" => $page["id"])),
39             $page["title"],
40             $page["position"]
41           );
42           $this->_set_links($env, $parent."/mw_page_".$page["id"], $page);
43         }
44       }
45     }
46
47     function enable($env){
48       $plugins_pages_start_id = $env->config("plugins_pages_start_id");
49       if($plugins_pages_start_id){
50         $data = $env->data();
51         if(
52              $data->set_config("start_action", "pages/view/page")
53           && $data->set_config("start_action_params", @serialize(array("id" => $plugins_pages_start_id)))
54         ){
55           return true;
56         }
57         return false;
58       }
59       return true;
60     }
61
62     function disable($env){
63       $start_action = $env->config("start_action");
64       if($start_action == "pages/view/page"){
65         $data = $env->data();
66         if(
67              $data->set_config("start_action", "")
68           && $data->set_config("start_action_params", "")
69         ){
70           return true;
71         }
72         return false;
73       }
74       return true;
75     }
76
77     // ---------------------------------------------------------------------------------
78     //                                                                           install
79     //
80
81     function install($env){
82       if($env->bdd("sgbd") == "xml") return $this->install_xml($env);
83       if($env->bdd("sgbd") == "sqlite") return $this->install_sqlite($env);
84       if($env->bdd("sgbd") == "mysql") return $this->install_mysql($env);
85     }
86
87     function install_xml($env){
88       $data = $env->data();
89       $sgbd = $data->sgbd();
90       $EXISTS = $sgbd->data_exists("pages");
91       if(!isset($EXISTS)){
92         return "impossible de savoir si la table #--pages existe";
93       }
94       if($EXISTS){
95         return "la table #--pages existe deja";
96       }
97       if(!$sgbd->create_data("pages")){
98         return "imposible de creer la table #--pages";
99       }
100       if(!$sgbd->add_data("actions_roles", array("action" => "pages/admin", "id_role" => "1"))){
101         $sgbd->remove_data("pages");
102         return "impossible d'ajouter un statut pour l'action pages/admin";
103       }
104       return true;
105     }
106
107     function install_sqlite($env){
108       $data = $env->data();
109       $sgbd = $data->sgbd();
110       try{
111         $EXISTS = $sgbd->table_exists("#--pages");
112       }
113       catch(Exception $e){
114         return "impossible de savoir si la table #--pages existe";
115       }
116       if($EXISTS){
117         return "la table #--pages existe deja";
118       }
119       try{
120         $sql =
121          "CREATE TABLE #--pages"
122         ."( id INTEGER NOT NULL PRIMARY KEY"
123         .", id_parent INTEGER NULL"
124         .", title VARCHAR NULL"
125         .", content TEXT NULL"
126         .", date_creation TEXT NOT NULL"
127         .", user_creation INTEGER NOT NULL"
128         .", date_last_update TEXT NOT NULL"
129         .", user_last_update INTEGER NOT NULL"
130         .", enabled INTEGER NOT NULL DEFAULT 1"
131         .", position INTEGER NOT NULL DEFAULT 0"
132         .")";
133         $sgbd->query($sql);
134       }
135       catch(Exception $e){
136         return "imposible de creer la table #--pages";
137       }
138       $DELETE_TABLE = false;
139       try{
140         $sgbd->query("INSERT INTO #--actions_roles(action, id_role) VALUES('pages/admin', 1)");
141       }
142       catch(Exception $e){
143         try{
144           $sgbd->query("DROP TABLE \"main\".\"#--pages\"");
145         }
146         catch(Exception $e){}
147         return "impossible d'ajouter un statut pour l'action pages/admin";
148       }
149       return true;
150     }
151
152     function install_mysql($env){
153       $data = $env->data();
154       $sgbd = $data->sgbd();
155       try{
156         $EXISTS = $sgbd->table_exists("#--pages");
157       }
158       catch(Exception $e){
159         return "impossible de savoir si la table #--pages existe";
160       }
161       if($EXISTS){
162         return "la table #--pages existe deja";
163       }
164       try{
165         $sql =
166          "CREATE TABLE #--pages"
167         ."( id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY"
168         .", id_parent INT(11) NULL"
169         .", title VARCHAR(255) NULL"
170         .", content TEXT NULL"
171         .", date_creation DATETIME NOT NULL"
172         .", user_creation INT(11) NOT NULL"
173         .", date_last_update DATETIME NOT NULL"
174         .", user_last_update INT(11) NOT NULL"
175         .", enabled TINYINT NOT NULL DEFAULT '1'"
176         .", position INT(11) NOT NULL DEFAULT '0'"
177         .") DEFAULT CHARSET=utf8";
178         $sgbd->query($sql);
179       }
180       catch(Exception $e){
181         return "imposible de creer la table #--pages";
182       }
183       try{
184         $sgbd->query("INSERT INTO #--actions_roles(action, id_role) VALUES('pages/admin', 1)");
185       }
186       catch(Exception $e){
187         try{
188           $sgbd->query("DROP TABLE #--pages");
189         }
190         catch(Exception $e){}
191         return "impossible d'ajouter un statut pour l'action pages/admin";
192       }
193       return true;
194     }
195
196     // ---------------------------------------------------------------------------------
197     //                                                                         uninstall
198     //
199
200     function uninstall($env){
201       if($env->bdd("sgbd") == "xml") return $this->uninstall_xml($env);
202       if($env->bdd("sgbd") == "sqlite") return $this->uninstall_sqlite($env);
203       if($env->bdd("sgbd") == "mysql") return $this->uninstall_mysql($env);
204     }
205
206     function uninstall_xml($env){
207       $data = $env->data();
208       $sgbd = $data->sgbd();
209       if(!$this->disable($env)) return "impossible de desactiver le plugin";
210       $data->del_config("plugins_pages_start_id");
211       $EXISTS = $sgbd->data_exists("pages");
212       if(!isset($EXISTS)){
213         return "impossible de savoir si la table #--pages existe";
214       }
215       if(!$EXISTS){
216         // return "la table #--pages n'existe pas";
217       }
218       elseif(!$sgbd->remove_data("pages")){
219         return "imposible de supprimer la table #--pages";
220       }
221       $ids = array();
222       if($rst = $sgbd->open_data("actions_roles")){
223         while($v_rst = $sgbd->fetch_data($rst)){
224           if(isset($v_rst)){
225             if(isset($v_rst["action"]) && isset($v_rst["id"]) && $v_rst["action"] == "pages/admin"){
226               $ids[] = $v_rst["id"];
227             }
228           }
229           else $ids = false;
230         }
231         $sgbd->close_data($rst);
232       }
233       if($ids !== false){
234         foreach($ids as $id){
235           $sgbd->del_data("actions_roles", $id);
236         }
237       }
238       return true;
239     }
240
241     function uninstall_sqlite($env){
242       $data = $env->data();
243       $sgbd = $data->sgbd();
244       if(!$this->disable($env)) return "impossible de desactiver le plugin";
245       $data->del_config("plugins_pages_start_id");
246       try{
247         $EXISTS = $sgbd->table_exists("#--pages");
248       }
249       catch(Exception $e){
250         return "impossible de savoir si la table #--pages existe";
251       }
252       if(!$EXISTS){
253         // return "la table #--pages n'existe pas";
254       }
255       else{
256         try{
257           $sgbd->query("DROP TABLE \"main\".\"#--pages\"");
258         }
259         catch(Exception $e){
260           return "imposible de supprimer la table #--pages";
261         }
262       }
263       try{
264         $sgbd->query("DELETE FROM #--actions_roles WHERE action='pages/admin'");
265       }
266       catch(Exception $e){
267         return
268          "Le plugin a bien &eacute;t&eacute; d&eacute;sinstall&eacute;"
269         ." mais impossible d'enlever les droits pages/admin/* de la table actions_roles";
270       }
271       return true;
272     }
273
274     function uninstall_mysql($env){
275       $data = $env->data();
276       $sgbd = $data->sgbd();
277       if(!$this->disable($env)) return "impossible de desactiver le plugin";
278       $data->del_config("plugins_pages_start_id");
279       try{
280         $EXISTS = $sgbd->table_exists("#--pages");
281       }
282       catch(Exception $e){
283         return "impossible de savoir si la table #--pages existe";
284       }
285       if(!$EXISTS){
286         // return "la table #--pages n'existe pas";
287       }
288       else{
289         try{
290           $sgbd->query("DROP TABLE #--pages");
291         }
292         catch(Exception $e){
293           return "imposible de supprimer la table #--pages";
294         }
295       }
296       try{
297         $sgbd->query("DELETE FROM #--actions_roles WHERE action='pages/admin'");
298       }
299       catch(Exception $e){
300         return
301          "Le plugin a bien &eacute;t&eacute; d&eacute;sinstall&eacute;"
302         ." mais impossible d'enlever les droits pages/admin/* de la table actions_roles";
303       }
304       return true;
305     }
306
307   }
308
309 ?>