fb8dc2bb46cae13fad336c33f8e5a41f502b5e6b
[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       $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     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     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     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     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         ( ( $id_source_derivation = $this->add_source_derivation
326             ( $id,
327               $source_derivation["xml"]["url"],
328               $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
329             )
330           ) === false
331         )
332         { return false;
333         }
334       }
335       if($reference)
336       { if($reference["xml"]["use_edit_content"])
337         { if(!$this->set_edit_reference_content($id, $reference["xml"]["content"]))
338           { return false;
339           }
340         }
341       }
342       else
343       { foreach($documents as $document)
344         { if(!$this->add_source_document($id, $document)) return false;
345         }
346       }
347       if(!$this->set_source_xml($id)) return false;
348       return $id;
349     }
350
351     function set_source
352     ( $id,
353       $groupes,
354       $titre,
355       $status,
356       $licence,
357       $documents = array(),
358       $reference = array(),
359       $derivations = array(),
360       $infos = array()
361     )
362     { if($source = $this->source($id))
363       { $sgbd = $this->sgbd();
364         $source["reference"] = $reference ? $reference["xml"]["url"] : null;
365         $source["titre"] = $reference ? null : $titre;
366         $source["licence"] = $reference ? null : $licence;
367         $source["date_creation"] = isset($infos["date_creation"]) ? $infos["date_creation"] : null;
368         foreach($infos as $key => $value) $source[$key] = $value;
369         if(!$sgbd->set_data("sml_sources", $id, $source)) return false;
370         if(!$this->del_source_groupes($id)) return false;
371         foreach($groupes as $id_groupe => $groupe)
372         { if($groupe["id"] && $groupe["id_groupe_status"])
373           { if(!$this->add_source_groupe($id, $groupe["id"], $groupe["id_groupe_status"])) return false;
374           }
375           else return false;
376         }
377         if(!$this->del_source_derivations($id)) return false;
378         if(!$this->del_edit_reference_content($id)) return false;
379         if(!$this->del_source_documents($id)) return false;
380         foreach($derivations as $source_derivation)
381         { if
382           ( ( $id_source_derivation = $this->add_source_derivation
383               ( $id,
384                 $source_derivation["xml"]["url"],
385                 $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
386               )
387             ) === false
388           )
389           { return false;
390           }
391         }
392         if($reference)
393         { if($reference["xml"]["use_edit_content"])
394           { if(!$this->set_edit_reference_content($id, $reference["xml"]["content"]))
395             { return false;
396             }
397           }
398         }
399         else
400         { foreach($documents as $document)
401           { if(!$this->add_source_document($id, $document)) return false;
402           }
403         }
404         if(!$this->set_source_xml($id)) return false;
405         return true;
406       }
407       return false;
408     }
409
410     function set_source_info($id_source, $key, $value)
411     { $sgbd = $this->sgbd();
412       if($source = $sgbd->get_data("sml_sources", $id_source))
413       { $source[$key] = $value;
414         if($sgbd->set_data("sml_sources", $id_source, $source)) return true;
415       }
416       return false;
417     }
418
419     function del_source($id)
420     { $sgbd = $this->sgbd();
421       if(!$this->del_source_compositions(array("id_source" => $id, "id_composition" => $id))) return false;
422       if(!$this->del_edit_reference_content($id)) return false;
423       if(!$this->del_source_derivations($id)) return false;
424       if(!$this->del_source_xml($id)) return false;
425       if(!$this->del_source_documents($id)) return false;
426       if(!$this->del_source_groupes($id)) return false;
427       if(!$sgbd->del_data("sml_sources", $id)) return false;
428       return true;
429     }
430
431     # ----------------------------------------------------------------------------------------
432     #                                                                   derivations de sources
433     #
434
435     function init_source_derivations(){
436       return true;
437     }
438
439     function source_derivations($params)
440     { $sgbd = $this->sgbd();
441       $env = $this->env();
442       $derivations = array();
443       // sources dont "id_source" est une derivation
444       if(isset($params["id_source"]))
445       { if($rst = $sgbd->open_data("sml_source_derivations"))
446         { while($v_rst = $sgbd->fetch_data($rst))
447           { if(isset($v_rst))
448             { if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && $v_rst["id_source"] == $params["id_source"])
449               { $derivations[$v_rst["id"]] = array();
450                 $derivation_edit_file = $this->derivation_edit_xml_path($v_rst["id_source"], $v_rst["id"]);
451                 if(file_exists($derivation_edit_file))
452                 { if(($derivation_edit_content = $this->get_edit_derivation_content($v_rst["id_source"], $v_rst["id"])) !== false)
453                   { if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"], $derivation_edit_content)) !==false)
454                     { $derivations[$v_rst["id"]]["xml"] = array
455                       ( "url" => $v_rst["derivation"],
456                         "content" => $derivation_edit_content,
457                         "use_edit_content" => true
458                       );
459                     }
460                     else
461                     { $derivations = null;
462                       break;
463                     }
464                   }
465                   else
466                   { $derivations = null;
467                     break;
468                   }
469                 }
470                 else
471                 { if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"])) !==false)
472                   { $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
473                   }
474                   else $derivations[$v_rst["id"]] = $this->empty_source();
475                 }
476                 $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
477               }
478             }
479             else
480             { $derivations = null;
481               break;
482             }
483           }
484           $sgbd->close_data($rst);
485         }
486         if(!isset($derivations)) return false;
487         return $derivations;
488       }
489       // sources qui derivent de "derivation"
490       elseif(isset($params["derivation"]))
491       { $source_xml_url = $params["derivation"];
492         if(preg_match("/^[0-9]+$/", $source_xml_url)) $source_xml_url = $this->source_xml_url($source_xml_url);
493         $id_sources = array();
494         if($rst = $sgbd->open_data("sml_source_derivations"))
495         { while($v_rst = $sgbd->fetch_data($rst))
496           { if(isset($v_rst))
497             { if(isset($v_rst["id_source"]) && isset($v_rst["derivation"]) && $v_rst["derivation"] == $source_xml_url)
498               { $id_sources[$v_rst["id_source"]] = true;
499               }
500             }
501             else { $id_sources = false; break; }
502           }
503           $sgbd->close_data($rst);
504         }
505         else $id_sources = false;
506         if($id_sources === false) return false;
507         if($id_sources)
508         { if($rst = $sgbd->open_data("sml_sources"))
509           { while($v_rst = $sgbd->fetch_data($rst))
510             { if(isset($v_rst))
511               { if(isset($v_rst["id"]) && isset($id_sources[$v_rst["id"]]))
512                 { $derivations[$v_rst["id"]] = $v_rst;
513                 }
514               }
515               else { $derivations = false; break; }
516             }
517             $sgbd->close_data($rst);
518           }
519           else $derivations = false;
520         }
521         return $derivations;
522       }
523       return false;
524     }
525
526     function source_derivation($id)
527     { $sgbd = $this->sgbd();
528       return $sgbd->get_data("sml_source_derivations", $id);
529     }
530
531     function add_source_derivation($id_source, $derivation, $edit_content = "")
532     { $sgbd = $this->sgbd();
533       $id_source_derivation = $sgbd->add_data
534       ( "sml_source_derivations",
535         array
536         ( "id_source" => $id_source,
537           "derivation" => $derivation
538         )
539       );
540       if(isset($id_source_derivation))
541       { if($edit_content)
542         { if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content))
543           { $id_source_derivation = false;
544           }
545         }
546       }
547       else $id_source_derivation = false;
548       return $id_source_derivation;
549     }
550
551     function set_source_derivation($id_source_derivation, $id_source, $derivation, $edit_content = "")
552     { $sgbd = $this->sgbd();
553       if
554       ( ( $sgbd->set_data
555           ( "sml_source_derivations",
556             $id_source_derivation,
557             array
558             ( "id_source" => $id_source,
559               "derivation" => $derivation
560             )
561           )
562         )
563       )
564       { if($edit_content)
565         { if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content))
566           { return false;
567           }
568         }
569         return true;
570       }
571       return false;
572     }
573
574     function del_source_derivation($id_source_derivation)
575     { if(($derivation = $this->source_derivation($id_source_derivation)) !== false)
576       { if(($derivations = $this->source_derivations(array("id_source" => $derivation["id_source"]))) !== false)
577         { $sgbd = $this->sgbd();
578           if(!$sgbd->del_data("sml_source_derivations", $id_source_derivation)) return false;
579           if(count($derivations) > 1)
580           { return $this->del_edit_derivation_content($derivation["id_source"], $id_source_derivation);
581           }
582           else return $this->del_edit_derivations($derivation["id_source"]);
583         }
584       }
585       return false;
586     }
587
588     function del_source_derivations($id_source)
589     { $sgbd = $this->sgbd();
590       $id_source_derivations = array();
591       if($rst = $sgbd->open_data("sml_source_derivations"))
592       { while($v_rst = $sgbd->fetch_data($rst))
593         { if(isset($v_rst))
594           { if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && $v_rst["id_source"] == $id_source)
595             { $id_source_derivations[] = $v_rst["id"];
596             }
597           }
598           else { $id_source_derivations = false; break; }
599         }
600         $sgbd->close_data($rst);
601       }
602       else $id_source_derivations = false;
603       if($id_source_derivations === false) return false;
604       if($id_source_derivations)
605       { foreach($id_source_derivations as $id_source_derivation)
606         { if(!$sgbd->del_data("sml_source_derivations", $id_source_derivation)) return false;
607         }
608       }
609       return $this->del_edit_derivations($id_source);
610     }
611
612     # ----------------------------------------------------------------------------------------
613     #                                                                               references
614     #
615
616     function source_reference($source)
617     { $reference = array();
618       if($source["reference"])
619       { if(!is_array($source["reference"]))
620         { $source["reference"] = array
621           ( "url" => $source["reference"],
622             "content" => "",
623             "use_edit_content" => false
624           );
625         }
626         $reference_edit_file = $this->reference_edit_xml_path($source["id"]);
627         if(file_exists($reference_edit_file))
628         { if(($reference_edit_content = $this->get_edit_reference_content($source["id"])) !== false)
629           { if(($reference = $this->source_xml_read($source["reference"], $reference_edit_content)) !==false)
630             { $reference["xml"] = array
631               ( "url" => $source["reference"]["xml"]["url"],
632                 "content" => $reference_edit_content,
633                 "use_edit_content" => true
634               );
635             }
636             else return false;
637           }
638           else return false;
639         }
640         else
641         { if(($reference = $this->source_xml_read($source["reference"]["xml"]["url"])) ===false)
642           { $reference = $this->empty_source();
643           }
644         }
645       }
646       return $reference;
647     }
648
649     # ----------------------------------------------------------------------------------------
650     #                                                                                documents
651     #
652
653     function source_documents($id_source)
654     { $sgbd = $this->sgbd();
655       $documents = array();
656       if($sgbd->data_exists("sml_sources/".$id_source))
657       { if($rst = $sgbd->open_data("sml_sources/".$id_source))
658         { $OK = true;
659           while($OK && ($document = $sgbd->fetch_data($rst)))
660           { if(isset($document)) $documents[$document["id"]] = $document;
661             else $OK = false;
662           }
663           $sgbd->close_data($rst);
664         }
665         else $OK = false;
666         if(!$OK) return false;
667       }
668       return $documents;
669     }
670
671     function add_source_document($id_source, $document)
672     { $sgbd = $this->sgbd();
673       if(!$sgbd->data_exists("sml_sources/".$id_source))
674       { if(!$sgbd->create_data("sml_sources/".$id_source)) return false;
675       }
676       if
677       ( !( $id_document = $sgbd->add_data
678           ( "sml_sources/".$id_source,
679             array
680             ( "nom" => $document["nom"],
681               "url" => $document["url"]
682             )
683           )
684         )
685       ) return false;
686       return $id_document;
687     }
688
689     function del_source_documents($id_source)
690     { $sgbd = $this->sgbd();
691       if($sgbd->data_exists("sml_sources/".$id_source))
692       { if(!$sgbd->remove_data("sml_sources/".$id_source)) return false;
693       }
694       return true;
695     }
696
697     # ----------------------------------------------------------------------------------------
698     #                                                                  compositions de sources
699     #
700
701     function source_compositions($params)
702     { $id_source = isset($params["id_source"]) ? $params["id_source"] : null;  
703       $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
704       $sgbd = $this->sgbd();
705       $env = $this->env();
706       $compositions = array();
707       if(isset($id_source))
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_source"] == $id_source)
712               { if(!isset($compositions[$v_rst["id_source"]])) $compositions[$v_rst["id_source"]] = array();
713                 $compositions[$v_rst["id_source"]][] = $v_rst["id_composition"];
714               }
715             }
716             else { $compositions = false; break; }
717           }
718           $sgbd->close_data($rst);
719         }
720         else $compositions = false;
721       }
722       elseif(isset($id_composition))
723       { if($id_composition)
724         { if($rst = $sgbd->open_data("sml_source_compositions"))
725           { while($v_rst = $sgbd->fetch_data($rst))
726             { if(isset($v_rst))
727               { if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]) && $v_rst["id_composition"] == $id_composition)
728                 { if(!isset($compositions[$v_rst["id_composition"]])) $compositions[$v_rst["id_composition"]] = array();
729                   $compositions[$v_rst["id_composition"]][] = $v_rst["id_source"];
730                 }
731               }
732               else { $compositions = false; break; }
733             }
734             $sgbd->close_data($rst);
735           }
736           else $compositions = false;
737         }
738         else
739         { if($rst = $sgbd->open_data("sml_source_compositions"))
740           { while($v_rst = $sgbd->fetch_data($rst))
741             { if(isset($v_rst))
742               { if(isset($v_rst["id_source"])) $compositions[$v_rst["id_source"]] = true;
743               }
744               else { $compositions = false; break; }
745             }
746             $sgbd->close_data($rst);
747           }
748           else $compositions = false;
749         }
750       }
751       return $compositions;
752     }
753
754     function set_source_composition($id_source, $id_composition)
755     { $sgbd = $this->sgbd();
756       if
757       ( $sgbd->add_data
758         ( "sml_source_compositions",
759           array
760           ( "id_source" => $id_source,
761             "id_composition" => $id_composition
762           )
763         )
764       ) return $this->set_source_xml($id_composition);
765       return false;
766     }
767
768     function del_source_compositions($params)
769     { $OK = true;
770       $id_source = isset($params["id_source"]) ? $params["id_source"] : null;  
771       $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
772       $to_delete = array();
773       $to_update = array();
774       if(isset($id_composition)) $to_update[$id_composition] = true;
775       $sgbd = $this->sgbd();
776       if($rst = $sgbd->open_data("sml_source_compositions"))
777       { while($v_rst = $sgbd->fetch_data($rst))
778         { if(isset($v_rst))
779           { if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && isset($v_rst["id_composition"]))
780             { if
781               (    (isset($id_source) && $v_rst["id_source"] == $id_source)
782                 || (isset($id_composition) && $v_rst["id_composition"] == $id_composition)
783               ) $to_delete[] = $v_rst["id"];
784               if(isset($id_source) && $v_rst["id_source"] == $id_source) $to_update[$v_rst["id_composition"]] = true;
785             }
786           }
787           else { $OK = false; break; }
788         }
789         $sgbd->close_data($rst);
790       }
791       else $OK = false;
792       if(!$OK) return false;
793       foreach($to_delete as $id_source_composition)
794       { if(!$sgbd->del_data("sml_source_compositions", $id_source_composition)) return false;
795       }
796       foreach($to_update as $id_source_xml => $delete)
797       { if(!$this->set_source_xml($id_source_xml)) return false;
798       }
799       return true;
800     }
801
802     function has_sources($id_composition)
803     { $sgbd = $this->sgbd();
804       $env = $this->env();
805       $has_sources = 0;
806       if($rst = $sgbd->open_data("sml_source_compositions"))
807       { while($v_rst = $sgbd->fetch_data($rst))
808         { if(isset($v_rst))
809           { if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]))
810             { if($v_rst["id_composition"] == $id_composition)
811               { $has_sources = 1;
812                 break;
813               }
814             }
815           }
816           else
817           { $has_sources = false;
818             break;
819           }
820         }
821         $sgbd->close_data($rst);
822       }
823       else $has_sources = false;
824       return $has_sources;
825     }
826
827     function source_ariane($id_source, $ariane = array())
828     { if(($compositions = $this->source_compositions(array("id_source" => $id_source))) !== false)
829       { if(isset($compositions[$id_source]) && $compositions[$id_source])
830         { foreach($compositions[$id_source] as $id_composition)
831           { if(($ariane = $this->source_ariane($id_composition, $ariane)) !== false)
832             { if(($ariane[$id_composition] = $this->source($id_composition)) !== false)
833               {
834               }
835               else $ariane = false;
836             }
837             else $ariane = false;
838             break;
839           }
840         }
841       }
842       else $ariane = false;
843       return $ariane;
844     }
845
846   }
847
848 ?>