f85b61742b7b6167fc6521bcbb74ee910881d920
[mw_sourceml] / app / data / modules / xml / sml_data_source_groupes.php
1 <?php
2
3   class sml_data_source_groupes extends mw_data{
4
5     function init_groupe_status(){
6       return true;
7     }
8
9     function source_groupes($id_source)
10     { $env = $this->env();
11       $sgbd = $this->sgbd();
12       $groupes = array();
13       $groupes_status = array();
14       if($rst = $sgbd->open_data("sml_sources_authors"))
15       { while($v_rst = $sgbd->fetch_data($rst))
16         { if(isset($v_rst) && isset($v_rst["id_author"]) && isset($v_rst["id_source"]) && isset($v_rst["id_sources_access"]))
17           { if($v_rst["id_source"] == $id_source) $groupes_status[$v_rst["id_author"]] = $v_rst["id_sources_access"];
18           }
19           else
20           { $groupes_status = false;
21             break;
22           }
23         }
24         $sgbd->close_data($rst);
25       }
26       else $groupes_status = false;
27       if($groupes_status === false) return false;
28       if($rst = $sgbd->open_data("sml_authors"))
29       { while($v_rst = $sgbd->fetch_data($rst))
30         { if(isset($v_rst) && isset($v_rst["id"]))
31           { if(isset($groupes_status[$v_rst["id"]]))
32             { $groupes[$v_rst["id"]] = $v_rst;
33               $groupes[$v_rst["id"]]["id_groupe_status"] = $groupes_status[$v_rst["id"]];
34               $groupes[$v_rst["id"]]["image_uri"] =
35               ( $v_rst["image"] ?
36                 $env->path("content")."uploads/".$v_rst["image"]
37                 : ""
38               );
39             }
40           }
41           else
42           { $groupes = false;
43             break;
44           }
45         }
46         $sgbd->close_data($rst);
47       }
48       else $groupes = false;
49       return $groupes;
50     }
51
52     function groupes_sources($params){
53       if(!isset($params["groupes"])) return false;
54       if(!isset($params["id_source_access"])) return false;
55       $satisfy_all_groupes_access = true;
56       if(isset($params["satisfy_all_groupes_access"])){
57         $satisfy_all_groupes_access = $params["satisfy_all_groupes_access"] ? true : false;
58       }
59       $source_access_ids = array();
60       if($params["id_source_access"] == $this->id_groupe_status_contributeur()){
61         $source_access_ids[$this->id_groupe_status_contributeur()] = true;
62         $source_access_ids[$this->id_groupe_status_editeur()] = true;
63         $source_access_ids[$this->id_groupe_status_admin()] = true;
64       }
65       elseif($params["id_source_access"] == $this->id_groupe_status_editeur()){
66         $source_access_ids[$this->id_groupe_status_editeur()] = true;
67         $source_access_ids[$this->id_groupe_status_admin()] = true;
68       }
69       elseif($params["id_source_access"] == $this->id_groupe_status_admin()){
70         $source_access_ids[$this->id_groupe_status_admin()] = true;
71       }
72       if(!$source_access_ids) return false;
73       $sources = array();
74       if(!($groupes = $params["groupes"])) return $sources;
75       $author_ids = array();
76       foreach($groupes as $i => $groupe) $author_ids[$groupe["id"]] = true;
77       $sgbd = $this->sgbd();
78       $_sources = array();
79       if($rst = $sgbd->open_data("sml_sources_authors")){
80         while(($_sources !== false) && $v_rst = $sgbd->fetch_data($rst)){
81           if(isset($v_rst)){
82             if(
83                   isset($v_rst["id"])
84               &&  isset($v_rst["id_author"])
85               &&  isset($v_rst["id_source"])
86               &&  isset($v_rst["id_sources_access"])
87             ){
88               if(
89                     isset($source_access_ids[$v_rst["id_sources_access"]])
90                 &&  isset($author_ids[$v_rst["id_author"]])
91               ){
92                 $_sources[$v_rst["id_source"]] = array(
93                   "id_source" => $v_rst["id_source"],
94                   "id_author" => $v_rst["id_author"],
95                 );
96               }
97             }
98           }
99           else $_sources = false;
100         }
101         $sgbd->close_data($rst);
102       }
103       else $_sources = false;
104       if($_sources === false) return false;
105       if($rst = $sgbd->open_data("sml_sources")){
106         while(($sources !== false) && $v_rst = $sgbd->fetch_data($rst)){
107           if(isset($v_rst) && isset($v_rst["id"])){
108             if(isset($_sources[$v_rst["id"]])){
109               if(!isset($sources[$v_rst["id"]])){
110                 $sources[$v_rst["id"]] = $v_rst;
111                 $sources[$v_rst["id"]]["authors"] = array();
112               }
113               $sources[$v_rst["id"]]["authors"][] = $_sources[$v_rst["id"]]["id_author"];
114             }
115           }
116           else $sources = false;
117         }
118         $sgbd->close_data($rst);
119       }
120       else $sources = false;
121       if($_sources === false) return false;
122       foreach($sources as $id_source => $source){
123         if($satisfy_all_groupes_access){
124           if(count($source["authors"]) < count($groupes)){
125             unset($sources[$id_source]);
126             continue;
127           }
128         }
129         if(!($sources[$id_source] = $this->load_source($source))) return false;
130       }
131       return $sources;
132     }
133
134     function source_has_groupe($id_source, $id_groupe){
135       $sgbd = $this->sgbd();
136       $HAS_THIS_GROUP = 0;
137       if($rst = $sgbd->open_data("sml_sources_authors")){
138         while(($HAS_THIS_GROUP !== false) && $v_rst = $sgbd->fetch_data($rst)){
139           if(isset($v_rst) && isset($v_rst["id"]) && isset($v_rst["id_author"]) && isset($v_rst["id_source"])){
140             if(
141                   ($v_rst["id_source"] == $id_source)
142               &&  ($v_rst["id_author"] == $id_groupe)
143             ){
144               $HAS_THIS_GROUP++;
145             }
146           }
147           else $HAS_THIS_GROUP = false;
148         }
149         $sgbd->close_data($rst);
150       }
151       else $HAS_THIS_GROUP = false;
152       return $HAS_THIS_GROUP;
153     }
154
155     function add_source_groupe($id_source, $id_groupe, $id_groupe_status)
156     { $sgbd = $this->sgbd();
157       if
158       ( $sgbd->add_data
159         ( "sml_sources_authors",
160           array
161           ( "id_source" => $id_source,
162             "id_author" => $id_groupe,
163             "id_sources_access" => $id_groupe_status
164           )
165         )
166       )
167       { return true;
168       }
169       return false;
170     }
171
172     function set_source_groupe($id, $id_groupe_status)
173     { if(($groupe = $this->groupe($id)) !== false)
174       { if
175         ( $sgbd->set_data
176           ( "sml_sources_authors",
177             $id,
178             array(
179               "id_sources_access" => $id_groupe_status
180             )
181           )
182         )
183         { return true;
184         }
185       }
186       return false;
187     }
188
189     function del_source_groupes($id_source){
190       $sgbd = $this->sgbd();
191       $OK = true;
192       if($rst = $sgbd->open_data("sml_sources_authors")){
193         while($OK && $v_rst = $sgbd->fetch_data($rst)){
194           if(isset($v_rst) && isset($v_rst["id"]) && isset($v_rst["id_source"])){
195             if($v_rst["id_source"] == $id_source) if(!$sgbd->del_data("sml_sources_authors", $v_rst["id"])) $OK = false;
196           }
197           else $OK = false;
198         }
199         $sgbd->close_data($rst);
200       }
201       else $OK = false;
202       return $OK;
203     }
204
205     function del_source_groupe($id_source, $id_author){
206       $sgbd = $this->sgbd();
207       $OK = true;
208       if($rst = $sgbd->open_data("sml_sources_authors")){
209         while($OK && $v_rst = $sgbd->fetch_data($rst)){
210           if(isset($v_rst) && isset($v_rst["id"]) && isset($v_rst["id_author"]) && isset($v_rst["id_source"])){
211             if(
212                   ($v_rst["id_source"] == $id_source)
213               &&  ($v_rst["id_author"] == $id_author)
214               &&  !$sgbd->del_data("sml_sources_authors", $v_rst["id"])
215             ) $OK = false;
216           }
217           else $OK = false;
218         }
219         $sgbd->close_data($rst);
220       }
221       else $OK = false;
222       return $OK;
223     }
224
225 // --------------------------------------------------------------------
226
227     function sources_access(){
228       $sgbd = $this->sgbd();
229       $sources_access = array();
230       $sgbd = $this->sgbd();
231       $OK = true;
232       if($rst = $sgbd->open_data("sml_sources_access")){
233         while($OK && $v_rst = $sgbd->fetch_data($rst)){
234           if(isset($v_rst) && isset($v_rst["id"])){
235             $sources_access[$v_rst["id"]] = $v_rst;
236           }
237           else $OK = false;
238         }
239         $sgbd->close_data($rst);
240       }
241       else $OK = false;
242       return $sources_access;
243     }
244
245     function id_groupe_status_admin()        { return 1; }
246     function id_groupe_status_editeur()      { return 2; }
247     function id_groupe_status_contributeur() { return 3; }
248
249     function get_admin_groupe($groupes)
250     { $groupe = array();
251       if(is_array($groupes)) foreach($groupes as $source_groupe)
252       { if($source_groupe["id_groupe_status"] == $this->id_groupe_status_admin())
253         { $groupe = $source_groupe;
254           break;
255         }
256       }
257       return $groupe;
258     }
259
260     function get_editor_groupes($groupes){
261       $editor_groupes = array();
262       if(is_array($groupes)) foreach($groupes as $source_groupe){
263         if(
264               $source_groupe["id_groupe_status"] == $this->id_groupe_status_admin()
265           ||  $source_groupe["id_groupe_status"] == $this->id_groupe_status_editeur()
266         ){
267           $editor_groupes[] = $source_groupe;
268         }
269       }
270       return $editor_groupes;
271     }
272
273     function source_permissions($source, $id_user)
274     { $permissions = array
275       ( "admin" => false,
276         "editeur" => false,
277         "contributeur" => false
278       );
279       foreach($source["groupes"] as $id_groupe => $source_groupe)
280       { if($source_groupe["id_user"] == $id_user)
281         { if($source_groupe["id_groupe_status"] == $this->id_groupe_status_admin())
282           { $permissions["admin"] = true;
283             $permissions["editeur"] = true;
284             $permissions["contributeur"] = true;
285           }
286           elseif($source_groupe["id_groupe_status"] == $this->id_groupe_status_editeur())
287           { $permissions["editeur"] = true;
288             $permissions["contributeur"] = true;
289           }
290           elseif($source_groupe["id_groupe_status"] == $this->id_groupe_status_contributeur())
291           { $permissions["contributeur"] = true;
292           }
293         }
294       }
295       return $permissions;
296     }
297
298 // --------------------------------------------------------------------
299
300     function sources_invitations($groupes, $id_user){
301       $sgbd = $this->sgbd();
302       $invitations = array("list" => array(), "total" => 0);
303       $groupes_in = array();
304       foreach($groupes as $id_groupe => $groupe) $groupes_in[$id_groupe] = true;
305       if($rst = $sgbd->open_data("sml_sources_invitations")){
306         while($v_rst = $sgbd->fetch_data($rst)){
307           if(isset($v_rst)){
308             if(!isset($v_rst["id"])) continue;
309             $MATCH = false;
310             if($groupes_in){
311               if(
312                     isset($v_rst["id_author"])
313                 &&  isset($groupes_in[$v_rst["id_author"]])
314               ){
315                 $MATCH = true;
316               }
317             }
318             if(
319                   !$MATCH
320               &&  isset($v_rst["id_user"])
321               &&  $id_user == $v_rst["id_user"]
322             ){
323               $MATCH = true;
324             }
325             if($MATCH){
326               $invitations["list"][$v_rst["id"]] = $v_rst;
327               $invitations["total"]++;
328             }
329           }
330           else{
331             $invitations = false;
332             break;
333           }
334         }
335         $sgbd->close_data($rst);
336       }
337       else $invitations = false;
338       return $invitations;
339     }
340
341   }
342
343 ?>