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