syntaxe POO (visibilite) et maj indentation
[mw_sourceml] / app / data / modules / xml / sml_data_sources.php
1 <?php
2
3   class sml_data_sources extends mw_data{
4
5     public $status;
6
7     # ----------------------------------------------------------------------------------------
8     #                                                                         status de source
9     #
10
11     public function source_status(){
12       if(!isset($this->status)) $this->status = $this->init_sources_status();
13       return $this->status;
14     }
15
16     public function init_sources_status(){
17       $sgbd = $this->sgbd();
18       $env = $this->env();
19       $status = array();
20       if($rst = $sgbd->open_data("sml_classes")){
21         while($v_rst = $sgbd->fetch_data($rst)){
22           if(isset($v_rst)){
23             $status[$v_rst["id"]] = $v_rst;
24           }
25           else{
26             $status = false;
27             break;
28           }
29         }
30         $sgbd->close_data($rst);
31       }
32       else $status = false;
33       return $status;
34     }
35
36     # ----------------------------------------------------------------------------------------
37     #                                                                                  sources
38     #
39
40     public function init_sources_table(){
41       return true;
42     }
43
44     public function sources($params){
45       $sgbd = $this->sgbd();
46       $env = $this->env();
47
48       // -------------------------------------------------------------------------------
49       //                                                                         filtres
50
51       $start = isset($params["start"]) ? $params["start"] : null;
52       $id_user = isset($params["id_user"]) ? $params["id_user"] : null;
53       $id_groupe = isset($params["id_groupe"]) ? $params["id_groupe"] : null;
54       $status = isset($params["status"]) ? $params["status"] : null;
55       $id_source = isset($params["id_source"]) ? $params["id_source"] : null;
56       $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
57       $id_source_derivation = isset($params["id_source_derivation"]) ? $params["id_source_derivation"] : null;
58       $id_licence = isset($params["id_licence"]) ? $params["id_licence"] : null;
59       $is_reference = isset($params["is_reference"]) ? $params["is_reference"] : null;
60       $order_by = isset($params["order_by"]) ? $params["order_by"] : "ordre";
61       $order = isset($params["order"]) ? $params["order"] : "ASC";
62
63       // -------------------------------------------------------------------------------
64       //                                                 infos pour verifier les filtres
65
66       if(isset($id_user) || isset($id_groupe)){
67         $in_source_ids = array();
68         $in_groupe_ids = array();
69         if(isset($id_groupe)) $in_groupe_ids[$id_groupe] = true;
70         else{
71           if(($groupes = $this->groupes($id_user)) !== false){
72             foreach($groupes["list"] as $id_groupe => $groupe) $in_groupe_ids[$id_groupe] = true;
73           }
74           else return false;
75         }
76         $OK = true;
77         if($rst = $sgbd->open_data("sml_sources_authors")){
78           while($OK && $v_rst = $sgbd->fetch_data($rst)){
79             if(isset($v_rst) && isset($v_rst["id_source"]) && isset($v_rst["id_author"])){
80               if(isset($in_groupe_ids[$v_rst["id_author"]])) $in_source_ids[$v_rst["id_source"]] = true;
81             }
82             else $OK = false;
83           }
84           $sgbd->close_data($rst);
85         }
86         else $OK = false;
87         if(!$OK) return false;
88       }
89
90       $compositions = array();
91       if(isset($id_source)){
92         if(($compositions = $this->source_compositions(array("id_source" => $id_source))) === false){
93           return false;
94         }
95       }
96       elseif(isset($id_composition)){
97         if(($compositions = $this->source_compositions(array("id_composition" => $id_composition))) === false){
98           return false;
99         }
100       }
101
102       $derivations = array();
103       if(isset($id_source_derivation)){
104         if(($derivations = $this->source_derivations($id_source_derivation)) === false){
105           return false;
106         }
107       }
108
109       // -------------------------------------------------------------------------------
110       //                                                               boucle principale
111
112       $sources = array("list" => array(), "total" => 0);
113       $res = array();
114       if($rst = $sgbd->open_data("sml_sources")){
115         while($v_rst = $sgbd->fetch_data($rst)){
116           if(isset($v_rst)){
117             $res[$v_rst["id"]] = $v_rst;
118             if(!isset($res[$v_rst["id"]]["ordre"])) $res[$v_rst["id"]]["ordre"] = 0;
119           }
120           else{
121             $res = false;
122             break;
123           }
124         }
125         $sgbd->close_data($rst);
126         if($res !== false){
127           $res = $this->ordonne($res, $order_by, $order);
128
129           foreach($res as $id_res => $v_rst){
130             // ------------------------------- par defaut
131             $MATCH = true;
132
133             // ------------------------------- filtre sur id_user et id_groupe
134
135             if(isset($in_source_ids)){
136               $MATCH = isset($in_source_ids[$v_rst["id"]]) && $in_source_ids[$v_rst["id"]];
137             }
138
139             // ------------------------------- filtre sur status
140             if($MATCH){
141               $MATCH = !isset($status) || (isset($v_rst["id_class"]) && $v_rst["id_class"] == $status);
142             }
143
144             // -------------------------------- filtre sur id_source
145             if($MATCH){
146               if(isset($id_source)){
147                 $MATCH = false;
148                 if($compositions && is_array($compositions[$id_source])){
149                   foreach($compositions[$id_source] as $id_composition){
150                     if(isset($v_rst["id"]) && $v_rst["id"] == $id_composition){
151                       $MATCH = true;
152                       break;
153                     }
154                   }
155                 }
156               }
157             }
158
159             // -------------------------------- filtre sur id_composition
160             if($MATCH){
161               if(isset($id_composition)){
162                 if($id_composition){
163                   $MATCH = false;
164                   if($compositions && is_array($compositions[$id_composition])){
165                     foreach($compositions[$id_composition] as $_id_source){
166                       if(isset($v_rst["id"]) && $v_rst["id"] == $_id_source){
167                         $MATCH = true;
168                         break;
169                       }
170                     }
171                   }
172                 }
173                 else{
174                   if($compositions){
175                     if(isset($compositions[$v_rst["id"]])) $MATCH = false;
176                   }
177                 }
178               }
179             }
180
181             // -------------------------------- filtre sur id_source_derivation
182             if($MATCH){
183               if(isset($id_source_derivation)){
184                 $MATCH = false;
185                 if($derivations && is_array($derivations[$id_source_derivation])){
186                   foreach($derivations[$id_source_derivation] as $_id_derivation){
187                     if(isset($v_rst["id"]) && $v_rst["id"] == $_id_derivation){
188                       $MATCH = true;
189                       break;
190                     }
191                   }
192                 }
193               }
194             }
195
196             // -------------------------------- filtre sur la licence
197             if($MATCH){
198               if(isset($id_licence)){
199                 $MATCH = false;
200                 if(isset($v_rst["licence"]) && $v_rst["licence"] == $id_licence) $MATCH = true;
201               }
202             }
203
204             // -------------------------------- filtre sur is_reference
205             if($MATCH){
206               if(isset($is_reference)){
207                 if($is_reference){
208                   $MATCH = isset($v_rst["reference"]) && $v_rst["reference"];
209                 }
210                 else{
211                   $MATCH = !isset($v_rst["reference"]) || !$v_rst["reference"];
212                 }
213               }
214             }
215
216             // -------------------------------- filtre sur quantite de resultats
217             if($MATCH){
218               $sources["total"]++;
219               $MATCH = !isset($start) || !$env->config("max_list") || ($sources["total"] > $start && $sources["total"] <= ($start + $env->config("max_list")));
220             }
221
222             // -------------------------------- ajout aux resultats si MATCH
223             if($MATCH) $sources["list"][$v_rst["id"]] = $v_rst;
224           }
225
226         }
227       }
228       else return false;
229       foreach($sources["list"] as $id_source => $source){
230         if(($sources["list"][$source["id"]] = $this->load_source($source)) === false) return false;
231       }
232       return $sources;
233     }
234
235     public function source($id, $load = false){
236       $sgbd = $this->sgbd();
237       if(($source = $sgbd->get_data("sml_sources", $id)) !== false){
238         $source["status"] = $source["id_class"];
239         $source = $load ? $this->load_source($source) : $this->get_source_from_v_rst($source);
240         return $source;
241       }
242       return false;
243     }
244
245     public function get_source_from_v_rst($v_rst){
246       $sgbd = $this->sgbd();
247       $source = $this->empty_source();
248       foreach($v_rst as $rst_key => $rst_value) $source[$rst_key] = $rst_value;
249       $source["status"] = $source["id_class"];
250       if($source["reference"]){
251         if(!is_array($source["reference"])){
252           $reference_url = $source["reference"];
253           $source["reference"] = $this->empty_source();
254           $source["reference"]["xml"]["url"] = $reference_url;
255         }
256       }
257       else{
258         if(!is_array($source["licence"])){
259           $source["licence"] = array(
260             "id" => $source["licence"]
261           );
262         }
263       }
264       return $source;
265     }
266
267     public function load_source($source){
268       $env = $this->env();
269       if(!isset($source["id"])) return false;
270       $source = $this->get_source_from_v_rst($source);
271       $source["xml"] = array(
272         "url" => $this->source_xml_url($source["id"]),
273         "content" => $this->get_source_xml($source["id"])
274       );
275       $source["documents"] = array();
276       if(($source["groupes"] = $this->source_groupes($source["id"])) === false) return false;
277       $source["image_uri"] = (
278         isset($source["image"]) && $source["image"] ?
279         $env->path("content")."uploads/".$source["image"]
280         : ""
281       );
282       if(($source["has_sources"] = $this->has_sources($source["id"])) === false) return false;
283       if(($source["has_derivations"] = $this->source_derivations(array("derivation" => $source["id"]))) === false) return false;
284       if(($source["derivations"] = $this->source_derivations(array("id_source" => $source["id"]))) === false) return false;
285       if(($source["reference"] = $this->source_reference($source)) === false) return false;
286       if($user = $env->user()){
287         if(($source["permissions"] = $this->source_permissions($source, $user["id"])) === false) return false;
288       }
289       if(!$source["reference"]){
290         if(($source["documents"] = $this->source_documents($source["id"])) === false) return false;
291       }
292       return $source;
293     }
294
295     public function add_source(
296       $groupes,
297       $titre,
298       $status,
299       $licence,
300       $documents = array(),
301       $reference = array(),
302       $derivations = array(),
303       $infos = array()
304     ){
305       $sgbd = $this->sgbd();
306       $source = array(
307         "id_class" => $status,
308         "reference" => $reference ? $reference["xml"]["url"] : null,
309         "titre" => $reference ? null : $titre,
310         "licence" => $reference ? null : $licence,
311         "date_creation" => isset($infos["date_creation"]) ? $infos["date_creation"] : null,
312         "date_inscription" => isset($infos["date_inscription"]) ? $infos["date_inscription"] : date("Y-m-d")
313       );
314       foreach($infos as $key => $value) if(!isset($source[$key])) $source[$key] = $value;
315       $id = $sgbd->add_data("sml_sources", $source);
316       if(!isset($id)) return false;
317       foreach($groupes as $id_groupe => $groupe){
318         if($groupe["id"] && $groupe["id_groupe_status"]){
319           if(!$this->add_source_groupe($id, $groupe["id"], $groupe["id_groupe_status"])) return false;
320         }
321         else return false;
322       }
323       foreach($derivations as $source_derivation){
324         if(
325           (
326             $id_source_derivation = $this->add_source_derivation(
327               $id,
328               $source_derivation["xml"]["url"],
329               $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
330             )
331           ) === false
332         ){
333           return false;
334         }
335       }
336       if($reference){
337         if($reference["xml"]["use_edit_content"]){
338           if(!$this->set_edit_reference_content($id, $reference["xml"]["content"])){
339             return false;
340           }
341         }
342       }
343       else{
344         foreach($documents as $document){
345           if(!$this->add_source_document($id, $document)) return false;
346         }
347       }
348       if(!$this->set_source_xml($id)) return false;
349       return $id;
350     }
351
352     public function set_source(
353       $id,
354       $groupes,
355       $titre,
356       $status,
357       $licence,
358       $documents = array(),
359       $reference = array(),
360       $derivations = array(),
361       $infos = array()
362     ){
363       if($source = $this->source($id)){
364         $sgbd = $this->sgbd();
365         $source["reference"] = $reference ? $reference["xml"]["url"] : null;
366         $source["titre"] = $reference ? null : $titre;
367         $source["licence"] = $reference ? null : $licence;
368         $source["date_creation"] = isset($infos["date_creation"]) ? $infos["date_creation"] : null;
369         foreach($infos as $key => $value) $source[$key] = $value;
370         if(!$sgbd->set_data("sml_sources", $id, $source)) return false;
371         if(!$this->del_source_groupes($id)) return false;
372         foreach($groupes as $id_groupe => $groupe){
373           if($groupe["id"] && $groupe["id_groupe_status"]){
374             if(!$this->add_source_groupe($id, $groupe["id"], $groupe["id_groupe_status"])) return false;
375           }
376           else return false;
377         }
378         if(!$this->del_source_derivations($id)) return false;
379         if(!$this->del_edit_reference_content($id)) return false;
380         if(!$this->del_source_documents($id)) return false;
381         foreach($derivations as $source_derivation){
382           if(
383             (
384               $id_source_derivation = $this->add_source_derivation(
385                 $id,
386                 $source_derivation["xml"]["url"],
387                 $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
388               )
389             ) === false
390           ){
391             return false;
392           }
393         }
394         if($reference){
395           if($reference["xml"]["use_edit_content"]){
396             if(!$this->set_edit_reference_content($id, $reference["xml"]["content"])){
397               return false;
398             }
399           }
400         }
401         else{
402           foreach($documents as $document){
403             if(!$this->add_source_document($id, $document)) return false;
404           }
405         }
406         if(!$this->set_source_xml($id)) return false;
407         return true;
408       }
409       return false;
410     }
411
412     public function set_source_info($id_source, $key, $value){
413       $sgbd = $this->sgbd();
414       if($source = $sgbd->get_data("sml_sources", $id_source)){
415         $source[$key] = $value;
416         if($sgbd->set_data("sml_sources", $id_source, $source)) return true;
417       }
418       return false;
419     }
420
421     public function del_source($id){
422       $sgbd = $this->sgbd();
423       if(!$this->del_source_compositions(array("id_source" => $id, "id_composition" => $id))) return false;
424       if(!$this->del_edit_reference_content($id)) return false;
425       if(!$this->del_source_derivations($id)) return false;
426       if(!$this->del_source_xml($id)) return false;
427       if(!$this->del_source_documents($id)) return false;
428       if(!$this->del_source_groupes($id)) return false;
429       if(!$sgbd->del_data("sml_sources", $id)) return false;
430       return true;
431     }
432
433     # ----------------------------------------------------------------------------------------
434     #                                                                   derivations de sources
435     #
436
437     public function init_source_derivations(){
438       return true;
439     }
440
441     public function source_derivations($params){
442       $sgbd = $this->sgbd();
443       $env = $this->env();
444       $derivations = array();
445       // sources dont "id_source" est une derivation
446       if(isset($params["id_source"])){
447         if($rst = $sgbd->open_data("sml_source_derivations")){
448           while($v_rst = $sgbd->fetch_data($rst)){
449             if(isset($v_rst)){
450               if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && $v_rst["id_source"] == $params["id_source"]){
451                 $derivations[$v_rst["id"]] = array();
452                 $derivation_edit_file = $this->derivation_edit_xml_path($v_rst["id_source"], $v_rst["id"]);
453                 if(file_exists($derivation_edit_file)){
454                   if(($derivation_edit_content = $this->get_edit_derivation_content($v_rst["id_source"], $v_rst["id"])) !== false){
455                     if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"], $derivation_edit_content)) !==false){
456                       $derivations[$v_rst["id"]]["xml"] = array(
457                         "url" => $v_rst["derivation"],
458                         "content" => $derivation_edit_content,
459                         "use_edit_content" => true
460                       );
461                     }
462                     else{
463                       $derivations = null;
464                       break;
465                     }
466                   }
467                   else{
468                     $derivations = null;
469                     break;
470                   }
471                 }
472                 else{
473                   if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"])) !==false){
474                     $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
475                   }
476                   else $derivations[$v_rst["id"]] = $this->empty_source();
477                 }
478                 $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
479               }
480             }
481             else{
482               $derivations = null;
483               break;
484             }
485           }
486           $sgbd->close_data($rst);
487         }
488         if(!isset($derivations)) return false;
489         return $derivations;
490       }
491       // sources qui derivent de "derivation"
492       elseif(isset($params["derivation"])){
493         $source_xml_url = $params["derivation"];
494         if(preg_match("/^[0-9]+$/", $source_xml_url)) $source_xml_url = $this->source_xml_url($source_xml_url);
495         $id_sources = array();
496         if($rst = $sgbd->open_data("sml_source_derivations")){
497           while($v_rst = $sgbd->fetch_data($rst)){
498             if(isset($v_rst)){
499               if(isset($v_rst["id_source"]) && isset($v_rst["derivation"]) && $v_rst["derivation"] == $source_xml_url){
500                 $id_sources[$v_rst["id_source"]] = true;
501               }
502             }
503             else{
504               $id_sources = false;
505               break;
506             }
507           }
508           $sgbd->close_data($rst);
509         }
510         else $id_sources = false;
511         if($id_sources === false) return false;
512         if($id_sources){
513           if($rst = $sgbd->open_data("sml_sources")){
514             while($v_rst = $sgbd->fetch_data($rst)){
515               if(isset($v_rst)){
516                 if(isset($v_rst["id"]) && isset($id_sources[$v_rst["id"]])){
517                   $derivations[$v_rst["id"]] = $v_rst;
518                 }
519               }
520               else{
521                 $derivations = false;
522                 break;
523               }
524             }
525             $sgbd->close_data($rst);
526           }
527           else $derivations = false;
528         }
529         return $derivations;
530       }
531       return false;
532     }
533
534     public function source_derivation($id){
535       $sgbd = $this->sgbd();
536       return $sgbd->get_data("sml_source_derivations", $id);
537     }
538
539     public function add_source_derivation($id_source, $derivation, $edit_content = ""){
540       $sgbd = $this->sgbd();
541       $id_source_derivation = $sgbd->add_data(
542         "sml_source_derivations",
543         array(
544           "id_source" => $id_source,
545           "derivation" => $derivation
546         )
547       );
548       if(isset($id_source_derivation)){
549         if($edit_content){
550           if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content)){
551             $id_source_derivation = false;
552           }
553         }
554       }
555       else $id_source_derivation = false;
556       return $id_source_derivation;
557     }
558
559     public function set_source_derivation($id_source_derivation, $id_source, $derivation, $edit_content = ""){
560       $sgbd = $this->sgbd();
561       if(
562         (
563           $sgbd->set_data(
564             "sml_source_derivations",
565             $id_source_derivation,
566             array(
567               "id_source" => $id_source,
568               "derivation" => $derivation
569             )
570           )
571         )
572       ){
573         if($edit_content){
574           if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content)){
575             return false;
576           }
577         }
578         return true;
579       }
580       return false;
581     }
582
583     public function del_source_derivation($id_source_derivation){
584       if(($derivation = $this->source_derivation($id_source_derivation)) !== false){
585         if(($derivations = $this->source_derivations(array("id_source" => $derivation["id_source"]))) !== false){
586           $sgbd = $this->sgbd();
587           if(!$sgbd->del_data("sml_source_derivations", $id_source_derivation)) return false;
588           if(count($derivations) > 1){
589             return $this->del_edit_derivation_content($derivation["id_source"], $id_source_derivation);
590           }
591           else return $this->del_edit_derivations($derivation["id_source"]);
592         }
593       }
594       return false;
595     }
596
597     public function del_source_derivations($id_source){
598       $sgbd = $this->sgbd();
599       $id_source_derivations = array();
600       if($rst = $sgbd->open_data("sml_source_derivations")){
601         while($v_rst = $sgbd->fetch_data($rst)){
602           if(isset($v_rst)){
603             if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && $v_rst["id_source"] == $id_source){
604               $id_source_derivations[] = $v_rst["id"];
605             }
606           }
607           else{
608             $id_source_derivations = false;
609             break;
610           }
611         }
612         $sgbd->close_data($rst);
613       }
614       else $id_source_derivations = false;
615       if($id_source_derivations === false) return false;
616       if($id_source_derivations){
617         foreach($id_source_derivations as $id_source_derivation){
618           if(!$sgbd->del_data("sml_source_derivations", $id_source_derivation)) return false;
619         }
620       }
621       return $this->del_edit_derivations($id_source);
622     }
623
624     # ----------------------------------------------------------------------------------------
625     #                                                                               references
626     #
627
628     public function source_reference($source){
629       $reference = array();
630       if($source["reference"]){
631         if(!is_array($source["reference"])){
632           $source["reference"] = array(
633             "url" => $source["reference"],
634             "content" => "",
635             "use_edit_content" => false
636           );
637         }
638         $reference_edit_file = $this->reference_edit_xml_path($source["id"]);
639         if(file_exists($reference_edit_file)){
640           if(($reference_edit_content = $this->get_edit_reference_content($source["id"])) !== false){
641             if(($reference = $this->source_xml_read($source["reference"], $reference_edit_content)) !==false){
642               $reference["xml"] = array(
643                 "url" => $source["reference"]["xml"]["url"],
644                 "content" => $reference_edit_content,
645                 "use_edit_content" => true
646               );
647             }
648             else return false;
649           }
650           else return false;
651         }
652         else{
653           if(($reference = $this->source_xml_read($source["reference"]["xml"]["url"])) ===false){
654             $reference = $this->empty_source();
655           }
656         }
657       }
658       return $reference;
659     }
660
661     # ----------------------------------------------------------------------------------------
662     #                                                                                documents
663     #
664
665     public function source_documents($id_source){
666       $sgbd = $this->sgbd();
667       $documents = array();
668       if($sgbd->data_exists("sml_sources/".$id_source)){
669         if($rst = $sgbd->open_data("sml_sources/".$id_source)){
670           $OK = true;
671           while($OK && ($document = $sgbd->fetch_data($rst))){
672             if(isset($document)) $documents[$document["id"]] = $document;
673             else $OK = false;
674           }
675           $sgbd->close_data($rst);
676         }
677         else $OK = false;
678         if(!$OK) return false;
679       }
680       return $documents;
681     }
682
683     public function add_source_document($id_source, $document){
684       $sgbd = $this->sgbd();
685       if(!$sgbd->data_exists("sml_sources/".$id_source)){
686         if(!$sgbd->create_data("sml_sources/".$id_source)) return false;
687       }
688       if(
689         !(
690           $id_document = $sgbd->add_data(
691             "sml_sources/".$id_source,
692             array(
693               "nom" => $document["nom"],
694               "url" => $document["url"]
695             )
696           )
697         )
698       ){
699         return false;
700       }
701       return $id_document;
702     }
703
704     public function del_source_documents($id_source){
705       $sgbd = $this->sgbd();
706       if($sgbd->data_exists("sml_sources/".$id_source)){
707         if(!$sgbd->remove_data("sml_sources/".$id_source)) return false;
708       }
709       return true;
710     }
711
712     # ----------------------------------------------------------------------------------------
713     #                                                                  compositions de sources
714     #
715
716     public function source_compositions($params){
717       $id_source = isset($params["id_source"]) ? $params["id_source"] : null;  
718       $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
719       $sgbd = $this->sgbd();
720       $env = $this->env();
721       $compositions = array();
722       if(isset($id_source)){
723         if($rst = $sgbd->open_data("sml_source_compositions")){
724           while($v_rst = $sgbd->fetch_data($rst)){
725             if(isset($v_rst)){
726               if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]) && $v_rst["id_source"] == $id_source){
727                 if(!isset($compositions[$v_rst["id_source"]])) $compositions[$v_rst["id_source"]] = array();
728                 $compositions[$v_rst["id_source"]][] = $v_rst["id_composition"];
729               }
730             }
731             else{
732               $compositions = false;
733               break;
734             }
735           }
736           $sgbd->close_data($rst);
737         }
738         else $compositions = false;
739       }
740       elseif(isset($id_composition)){
741         if($id_composition){
742           if($rst = $sgbd->open_data("sml_source_compositions")){
743             while($v_rst = $sgbd->fetch_data($rst)){
744               if(isset($v_rst)){
745                 if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]) && $v_rst["id_composition"] == $id_composition){
746                   if(!isset($compositions[$v_rst["id_composition"]])) $compositions[$v_rst["id_composition"]] = array();
747                   $compositions[$v_rst["id_composition"]][] = $v_rst["id_source"];
748                 }
749               }
750               else{
751                 $compositions = false;
752                 break;
753               }
754             }
755             $sgbd->close_data($rst);
756           }
757           else $compositions = false;
758         }
759         else{
760           if($rst = $sgbd->open_data("sml_source_compositions")){
761             while($v_rst = $sgbd->fetch_data($rst)){
762               if(isset($v_rst)){
763                 if(isset($v_rst["id_source"])) $compositions[$v_rst["id_source"]] = true;
764               }
765               else{
766                 $compositions = false;
767                 break;
768               }
769             }
770             $sgbd->close_data($rst);
771           }
772           else $compositions = false;
773         }
774       }
775       return $compositions;
776     }
777
778     public function set_source_composition($id_source, $id_composition){
779       $sgbd = $this->sgbd();
780       if(
781         $sgbd->add_data(
782           "sml_source_compositions",
783           array(
784             "id_source" => $id_source,
785             "id_composition" => $id_composition
786           )
787         )
788       ){
789         return $this->set_source_xml($id_composition);
790       }
791       return false;
792     }
793
794     public function del_source_compositions($params){
795       $OK = true;
796       $id_source = isset($params["id_source"]) ? $params["id_source"] : null;  
797       $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
798       $to_delete = array();
799       $to_update = array();
800       if(isset($id_composition)) $to_update[$id_composition] = true;
801       $sgbd = $this->sgbd();
802       if($rst = $sgbd->open_data("sml_source_compositions")){
803         while($v_rst = $sgbd->fetch_data($rst)){
804           if(isset($v_rst)){
805             if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && isset($v_rst["id_composition"])){
806               if(
807                    (isset($id_source) && $v_rst["id_source"] == $id_source)
808                 || (isset($id_composition) && $v_rst["id_composition"] == $id_composition)
809               ){
810                 $to_delete[] = $v_rst["id"];
811               }
812               if(isset($id_source) && $v_rst["id_source"] == $id_source) $to_update[$v_rst["id_composition"]] = true;
813             }
814           }
815           else{
816             $OK = false;
817             break;
818           }
819         }
820         $sgbd->close_data($rst);
821       }
822       else $OK = false;
823       if(!$OK) return false;
824       foreach($to_delete as $id_source_composition){
825         if(!$sgbd->del_data("sml_source_compositions", $id_source_composition)) return false;
826       }
827       foreach($to_update as $id_source_xml => $delete){
828         if(!$this->set_source_xml($id_source_xml)) return false;
829       }
830       return true;
831     }
832
833     public function has_sources($id_composition){
834       $sgbd = $this->sgbd();
835       $env = $this->env();
836       $has_sources = 0;
837       if($rst = $sgbd->open_data("sml_source_compositions")){
838         while($v_rst = $sgbd->fetch_data($rst)){
839           if(isset($v_rst)){
840             if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"])){
841               if($v_rst["id_composition"] == $id_composition){
842                 $has_sources = 1;
843                 break;
844               }
845             }
846           }
847           else{
848             $has_sources = false;
849             break;
850           }
851         }
852         $sgbd->close_data($rst);
853       }
854       else $has_sources = false;
855       return $has_sources;
856     }
857
858     public function source_ariane($id_source, $ariane = array()){
859       if(($compositions = $this->source_compositions(array("id_source" => $id_source))) !== false){
860         if(isset($compositions[$id_source]) && $compositions[$id_source]){
861           foreach($compositions[$id_source] as $id_composition){
862             if(($ariane = $this->source_ariane($id_composition, $ariane)) !== false){
863               if(($ariane[$id_composition] = $this->source($id_composition)) !== false){
864               }
865               else $ariane = false;
866             }
867             else $ariane = false;
868             break;
869           }
870         }
871       }
872       else $ariane = false;
873       return $ariane;
874     }
875
876   }