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