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