456e039257c71da13e38fbb7c0b6336b11766674
[mtweb] / mw / app / data / upgrades / xml / mtweb_xml_version_0_11_2.php
1 <?php
2
3   class mtweb_xml_version_0_11_2 extends mw_data_version{
4
5     function version(){
6       return "0.11.2";
7     }
8
9     function do_upgrade(){
10
11       $env = $this->env();
12       $data = $env->data();
13       $sgbd = $data->sgbd();
14
15       /* ---------------------------------------------------------------------- */
16       /*                                                          action_status */
17       /*                                                  devient actions_roles */
18       /*                                      element id_status devient id_role */
19
20       if(!$sgbd->data_exists("actions_roles")){
21         if(!$sgbd->create_data("actions_roles")){
22           return "impossible de créer la table actions_roles";
23         }
24       }
25       if($sgbd->data_exists("action_status")){
26         $action_status = array();
27         if($rst = $sgbd->open_data("action_status")){
28           while($v_rst = $sgbd->fetch_data($rst)){
29             if(isset($v_rst)){
30               $action_status[$v_rst["id"]] = $v_rst;
31             }
32             else{
33               $action_status = false;
34               break;
35             }
36           }
37           $sgbd->close_data($rst);
38         }
39         else{
40           return "impossible d'ouvrir la table action_status";
41         }
42         if($action_status === false){
43           return "impossible de lire la table action_status";
44         }
45         if($action_status){
46           foreach($action_status as $status){
47             if(
48               !$sgbd->add_data(
49                 "actions_roles",
50                 array(
51                   "action" => $status["action"],
52                   "id_role" => $status["id_status"]
53                 )
54               )
55             ){
56               return "erreur lors de la migration de action_status vers actions_roles";
57             }
58           }
59         }
60         if(
61           !$sgbd->add_data(
62             "actions_roles",
63             array(
64               "action" => "config",
65               "id_role" => 3
66             )
67           )
68         ){
69           return "impossible d'ajouter la protection d'accès webmaster dans actions_roles";
70         }
71         if(!$sgbd->remove_data("action_status")){
72           return "impossible de supprimer la table action_status, dans ".get_class($this);
73         }
74       }
75
76
77       /* ---------------------------------------------------------------------- */
78       /*                                                                 config */
79       /*                                                  elements key et value */
80       /*                                                                        */
81
82       $OK = true;
83       if($rst = $sgbd->open_data("config")){
84         while($v_rst = $sgbd->fetch_data($rst)){
85           if(isset($v_rst)){
86             if(!isset($v_rst["key"]) || !isset($v_rst["value"])){
87               $config_key = "";
88               $config_value = "";
89               foreach($v_rst as $key => $value){
90                 if($key != "id"){
91                   $config_key = $key;
92                   $config_value = $value;
93                   break;
94                 }
95               }
96               if($config_key){
97                 if(
98                   !$sgbd->set_data(
99                     "config",
100                     $v_rst["id"],
101                     array(
102                       "key" => $config_key,
103                       "value" => $config_value
104                     )
105                   )
106                 ){
107                   $OK = false;
108                   break;
109                 }
110               }
111             }
112           }
113           else{
114             $OK = false;
115             break;
116           }
117         }
118         $sgbd->close_data($rst);
119       }
120       else{
121         return "impossible d'ouvrir la table config";
122       }
123       if(!$OK){
124         return "impossible de mettre à jour la table config";
125       }
126
127       /* ---------------------------------------------------------------------- */
128       /*                                                            user_status */
129       /*                                                          devient roles */
130       /*                                                                        */
131       if(!$sgbd->data_exists("roles")){
132         if(!$sgbd->create_data("roles")){
133           return "impossible de creer la table roles";
134         }
135
136         $ERROR = false;
137         if(!$ERROR){
138           $res = $sgbd->add_data(
139             "roles",
140             array(
141               "nom" => "guest",
142               "intitule" => "invité"
143             ),
144             0
145           );
146           if(!isset($res)) $ERROR = true;
147         } 
148         if(!$ERROR) if(
149           !$sgbd->add_data(
150             "roles",
151             array(
152               "nom" => "admin",
153               "intitule" => "administrateur"
154             )
155           )
156         ) $ERROR = true;
157         if(!$ERROR) if(
158           !$sgbd->add_data(
159             "roles",
160             array(
161               "nom" => "membre",
162               "intitule" => "membre"
163             )
164           )
165         ) $ERROR = true;
166         if(!$ERROR) if(
167           !$sgbd->add_data(
168             "roles",
169             array(
170               "nom" => "webmaster",
171               "intitule" => "webmaster"
172             )
173           )
174         ) $ERROR = true;
175
176         if($ERROR){
177           return "impossible de remplir la table roles";
178         }
179
180       }
181       if($sgbd->data_exists("user_status")){
182         if(!$sgbd->remove_data("user_status")){
183           return "impossible de supprimer la table user_status";
184         }
185       }
186
187       /* ---------------------------------------------------------------------- */
188       /*                                             nouvelle table users_roles */
189       /*                         importer users.status dans users_roles.id_role */
190
191       if(!$sgbd->data_exists("users_roles")){
192         if(!$sgbd->create_data("users_roles")){
193           return "impossible de creer la table users_roles";
194         }
195         $OK = true;
196         if($rst = $sgbd->open_data("users")){
197           while($v_rst = $sgbd->fetch_data($rst)){
198             if(isset($v_rst)){
199               if(isset($v_rst["status"])){
200                 if(
201                   !$sgbd->add_data(
202                     "users_roles",
203                     array(
204                       "id_user" => $v_rst["id"],
205                       "id_role" => $v_rst["status"]
206                     )
207                   )
208                 ){
209                   $OK = false;
210                   break;
211                 }
212               }
213             }
214             else{
215               $OK = false;
216               break;
217             }
218           }
219           $sgbd->close_data($rst);
220         }
221         else{
222           return "impossible d'ouvrir la table users";
223         }
224         if(!$OK){
225           return "impossible de remplir la table users_roles";
226         }
227       }
228
229       /* ---------------------------------------------------------------------- */
230       /*                                                nouvelle table versions */
231       /*                                                                        */
232
233       if(!$sgbd->data_exists("versions")){
234         if(!$sgbd->create_data("versions")){
235           return "impossible de creer la table versions";
236         }
237       }
238
239       return true;
240     }
241
242   }