f4d78bd7558d1090e3f7c04f1c62db3aa0053225
[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(!$source["reference"])
210       { if(($source["documents"] = $this->source_documents($source["id"])) === false) return false;
211       }
212       return $source;
213     }
214
215     function add_source
216     ( $groupes,
217       $titre,
218       $status,
219       $licence,
220       $documents = array(),
221       $reference = array(),
222       $derivations = array(),
223       $infos = array()
224     )
225     { $sgbd = $this->sgbd();
226       $source = array
227       ( "groupes" => $groupes,
228         "titre" => $reference ? null : $titre,
229         "status" => $status,
230         "licence" => $reference ? null : $licence,
231         "reference" => $reference ? $reference : null,
232         "date_creation" => isset($infos["date_creation"]) ? $infos["date_creation"] : null,
233         "date_inscription" => isset($infos["date_inscription"]) ? $infos["date_inscription"] : date("Y-m-d")
234       );
235       try{
236         $sql =
237          "INSERT INTO #--sml_sources(id_class, reference, titre, licence, date_creation, date_inscription)"
238         ." VALUES"
239         ."( ".$this->eq($source["status"])
240         .", ".$this->eq($source["reference"] ? $source["reference"]["xml"]["url"] : null)
241         .", ".$this->eq($source["titre"])
242         .", ".$this->eq($source["licence"])
243         .", ".$this->eq($source["date_creation"])
244         .", ".$this->eq($source["date_inscription"])
245         .")";
246         $sgbd->query($sql);
247         $id = $sgbd->insert_id();
248         foreach($source["groupes"] as $id_groupe => $groupe)
249         { if($groupe["id"] && $groupe["id_groupe_status"])
250           { if(!$this->add_source_groupe($id, $groupe["id"], $groupe["id_groupe_status"])) return false;
251           }
252           else return false;
253         }
254         if(isset($infos["date_creation"])) unset($infos["date_creation"]);
255         if(isset($infos["date_inscription"])) unset($infos["date_inscription"]);
256         foreach($infos as $key => $value){
257           $sql =
258            "INSERT INTO #--sml_sources_infos(id_source, `key`,`value`)"
259           ." VALUES"
260           ."( ".$this->eq($id)
261           .", ".$this->eq($key)
262           .", ".$this->eq($value)
263           .")";
264           $sgbd->query($sql);
265         }
266       }
267       catch(Exception $e) { return false; }
268       foreach($derivations as $source_derivation)
269       { if
270         ( ( $id_source_derivation = $this->add_source_derivation
271             ( $id,
272               $source_derivation["xml"]["url"],
273               $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
274             )
275           ) === false
276         )
277         { return false;
278         }
279       }
280       if($reference)
281       { if($reference["xml"]["use_edit_content"])
282         { if(!$this->set_edit_reference_content($id, $reference["xml"]["content"]))
283           { return false;
284           }
285         }
286       }
287       else
288       { foreach($documents as $document)
289         { if(!$this->add_source_document($id, $document)) return false;
290         }
291       }
292       if(!$this->set_source_xml($id)) return false;
293       return $id;
294     }
295
296     function set_source
297     ( $id,
298       $groupes,
299       $titre,
300       $status,
301       $licence,
302       $documents = array(),
303       $reference = array(),
304       $derivations = array(),
305       $infos = array()
306     )
307     { if($source = $this->source($id))
308       { $sgbd = $this->sgbd();
309         $source = array
310         ( "groupes" => $groupes,
311           "titre" => $reference ? null : $titre,
312           "licence" => $reference ? null : $licence,
313           "reference" => $reference ? $reference : null,
314           "date_creation" => isset($infos["date_creation"]) ? $infos["date_creation"] : null
315         );
316         try{
317           $sql =
318            "UPDATE #--sml_sources SET"
319           ."  reference=".$this->eq($source["reference"] ? $source["reference"]["xml"]["url"] : null)
320           .", titre=".$this->eq($source["titre"])
321           .", licence=".$this->eq($source["licence"])
322           .", date_creation=".$this->eq($source["date_creation"])
323           ." WHERE id=".$this->eq($id);
324           $sgbd->query($sql);
325           if(!$this->del_source_groupes($id)) return false;
326           foreach($source["groupes"] as $id_groupe => $groupe)
327           { if($groupe["id"] && $groupe["id_groupe_status"]){
328               if(!$this->add_source_groupe($id, $groupe["id"], $groupe["id_groupe_status"])) return false;
329             }
330             else return false;
331           }
332           $sql = "DELETE FROM #--sml_sources_infos WHERE id_source=".$this->eq($id);
333           $sgbd->query($sql);
334           if(isset($infos["date_creation"])) unset($infos["date_creation"]);
335           if(isset($infos["date_inscription"])) unset($infos["date_inscription"]);
336           foreach($infos as $key => $value)
337           { $sql =
338              "INSERT INTO #--sml_sources_infos(id_source, `key`,`value`)"
339             ." VALUES"
340             ."( ".$this->eq($id)
341             .", ".$this->eq($key)
342             .", ".$this->eq($value)
343             .")";
344             $sgbd->query($sql);
345           }
346         }
347         catch(Exception $e) { return false; }
348         if(!$this->del_source_derivations($id)) return false;
349         if(!$this->del_edit_reference_content($id)) return false;
350         if(!$this->del_source_documents($id)) return false;
351         foreach($derivations as $source_derivation)
352         { if
353           ( ( $id_source_derivation = $this->add_source_derivation
354               ( $id,
355                 $source_derivation["xml"]["url"],
356                 $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
357               )
358             ) === false
359           )
360           { return false;
361           }
362         }
363         if($reference)
364         { if($reference["xml"]["use_edit_content"])
365           { if(!$this->set_edit_reference_content($id, $reference["xml"]["content"]))
366             { return false;
367             }
368           }
369         }
370         else{
371           foreach($documents as $document){
372             if(!$this->add_source_document($id, $document)) return false;
373           }
374         }
375         if(!$this->set_source_xml($id)) return false;
376         return true;
377       }
378       return false;
379     }
380
381     function set_source_info($id_source, $key, $value){
382       $sgbd = $this->sgbd();
383       try{
384         $sql =
385          "SELECT id FROM #--sml_sources_infos"
386         ." WHERE id_source=".$this->eq($id_source)
387         ." AND `key`=".$this->eq($key);
388         $rst = $sgbd->query($sql);
389         $id = null;
390         if($v_rst = $sgbd->fetch_assoc($rst)) $id = $v_rst["id"];
391         $sgbd->free_result($rst);
392         if(isset($id)) $sql =
393          "UPDATE #--sml_sources_infos SET"
394         ." `value`=".$this->eq($value)
395         ." WHERE id=".$this->eq($id);
396         else $sql =
397          "INSERT INTO #--sml_sources_infos(id_source, `key`,`value`)"
398         ." VALUES"
399         ."( ".$this->eq($id_source)
400         .", ".$this->eq($key)
401         .", ".$this->eq($value)
402         .")";
403         $sgbd->query($sql);
404         if(!isset($id)) $id = $sgbd->insert_id();
405       }
406       catch(Exception $e) { return false; }
407       return $id;
408     }
409
410     function del_source($id){
411       $sgbd = $this->sgbd();
412       if(!$this->del_source_compositions(array("id_source" => $id, "id_composition" => $id))) return false;
413       if(!$this->del_edit_reference_content($id)) return false;
414       if(!$this->del_source_derivations($id)) return false;
415       if(!$this->del_source_xml($id)) return false;
416       if(!$this->del_source_documents($id)) return false;
417       if(!$this->del_source_groupes($id)) return false;
418       try{
419         $sql = "DELETE FROM #--sml_sources_infos WHERE id_source=".$this->eq($id);
420         $sgbd->query($sql);
421         $sql = "DELETE FROM #--sml_sources WHERE id=".$this->eq($id);
422         $sgbd->query($sql);
423       }
424       catch(Exception $e) { return false; }
425       return true;
426     }
427
428     # ----------------------------------------------------------------------------------------
429     #                                                                   derivations de sources
430     #
431
432     function init_source_derivations()
433     { $sgbd = $this->sgbd();
434       $rst = $sgbd->table_exists("#--sml_source_derivations");
435       if(isset($rst))
436       { if(!$rst)
437         { return
438            "table manquante"
439           ."<br />"
440           ."<br />"
441           ."<pre>"
442           ."(table_prefix)source_derivations :\n"
443           ."\n"
444           ."  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,\n"
445           ."  id_source INT NOT NULL,\n"
446           ."  derivation VARCHAR NOT NULL\n"
447           ."\n"
448           ."</pre>";
449         }
450       }
451       else return "impossible de chercher la table #--sml_source_derivations";
452       return true;
453     }
454
455     function source_derivations($params)
456     { $sgbd = $this->sgbd();
457       $env = $this->env();
458       $derivations = array();
459       // sources dont "id_source" est une derivation
460       if(isset($params["id_source"])){
461         try{
462           $sql =
463            "SELECT *"
464           ." FROM #--sml_source_derivations"
465           ." WHERE #--sml_source_derivations.id_source=".$this->eq($params["id_source"]);
466           $rst = $sgbd->query($sql);
467           while($v_rst = $sgbd->fetch_assoc($rst))
468           { $derivations[$v_rst["id"]] = array();
469             $derivation_edit_file = $this->derivation_edit_xml_path($v_rst["id_source"], $v_rst["id"]);
470             if(file_exists($derivation_edit_file))
471             { if(($derivation_edit_content = $this->get_edit_derivation_content($v_rst["id_source"], $v_rst["id"])) !== false)
472               { if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"], $derivation_edit_content)) !==false)
473                 { $derivations[$v_rst["id"]]["xml"] = array
474                   ( "url" => $v_rst["derivation"],
475                     "content" => $derivation_edit_content,
476                     "use_edit_content" => true
477                   );
478                 }
479                 else return false;
480               }
481               else return false;
482             }
483             else
484             { if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"])) !==false)
485               { $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
486               }
487               else $derivations[$v_rst["id"]] = $this->empty_source();
488             }
489             $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
490           }
491           $sgbd->free_result($rst);
492         }
493         catch(Exception $e) { return false; }
494         return $derivations;
495       }
496       // sources qui derivent de "derivation"
497       elseif(isset($params["derivation"]))
498       { $source_xml_url = $params["derivation"];
499         if(preg_match("/^[0-9]+$/", $source_xml_url)) $source_xml_url = $this->source_xml_url($source_xml_url);
500         try{
501           $sql =
502            "SELECT #--sml_sources.*"
503           ." FROM #--sml_sources, #--sml_source_derivations"
504           ." WHERE #--sml_sources.id=#--sml_source_derivations.id_source"
505           ." AND #--sml_source_derivations.derivation=".$this->eq($source_xml_url);
506           $rst = $sgbd->query($sql);
507           if(!isset($rst)) return false;
508           while($v_rst = $sgbd->fetch_assoc($rst)) $derivations[$v_rst["id"]] = $v_rst;
509           $sgbd->free_result($rst);
510         }
511         catch(Exception $e) { return false; }
512         return $derivations;
513       }
514       return false;
515     }
516
517     function source_derivation($id){
518       $sgbd = $this->sgbd();
519       $env = $this->env();
520       $derivation = array();
521       try{
522         $sql =
523          "SELECT *"
524         ." FROM #--sml_source_derivations"
525         ." WHERE #--sml_source_derivations.id=".$this->eq($id);
526         $rst = $sgbd->query($sql);
527         if($v_rst = $sgbd->fetch_assoc($rst)) $derivation = $v_rst;
528         $sgbd->free_result($rst);
529       }
530       catch(Exception $e) { return false; }
531       return $derivation;
532     }
533
534     function add_source_derivation($id_source, $derivation, $edit_content = "")
535     { $sgbd = $this->sgbd();
536       try{
537         $sql =
538          "INSERT INTO #--sml_source_derivations(id_source, derivation)"
539         ." VALUES"
540         ."( ".$this->eq($id_source)
541         .", ".$this->eq($derivation)
542         .")";
543         $sgbd->query($sql);
544         $id_source_derivation = $sgbd->insert_id();
545       }
546       catch(Exception $e) { return false; }
547       if($edit_content)
548       { if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content))
549         { return false;
550         }
551       }
552       return $id_source_derivation;
553     }
554
555     function set_source_derivation($id_source_derivation, $id_source, $derivation, $edit_content = "")
556     { $sgbd = $this->sgbd();
557       try{
558         $sql =
559          "UPDATE #--sml_source_derivations SET"
560         ."  id_source=".$this->eq($id_source)
561         .", derivation=".$this->eq($derivation)
562         ." WHERE id=".$this->eq($id_source_derivation);
563         $sgbd->query($sql);
564       }
565       catch(Exception $e) { return false; }
566       if($edit_content)
567       { if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content))
568         { return false;
569         }
570       }
571       return true;
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           try{
579             $sql = "DELETE FROM #--sml_source_derivations WHERE id=".$this->eq($id_source_derivation);
580             $sgbd->query($sql);
581           }
582           catch(Exception $e) { return false; }
583           if(count($derivations) > 1)
584           { return $this->del_edit_derivation_content($derivation["id_source"], $id_source_derivation);
585           }
586           else return $this->del_edit_derivations($derivation["id_source"]);
587         }
588       }
589       return false;
590     }
591
592     function del_source_derivations($id_source)
593     { $sgbd = $this->sgbd();
594       try{
595         $sql = "DELETE FROM #--sml_source_derivations WHERE id_source=".$this->eq($id_source);
596         $sgbd->query($sql);
597       }
598       catch(Exception $e) { return false; }
599       return $this->del_edit_derivations($id_source);
600     }
601
602     # ----------------------------------------------------------------------------------------
603     #                                                                               references
604     #
605
606     function source_reference($source)
607     { $reference = array();
608       if($source["reference"])
609       { if(!is_array($source["reference"]))
610         { $source["reference"] = array
611           ( "url" => $source["reference"],
612             "content" => "",
613             "use_edit_content" => false
614           );
615         }
616         $reference_edit_file = $this->reference_edit_xml_path($source["id"]);
617         if(file_exists($reference_edit_file))
618         { if(($reference_edit_content = $this->get_edit_reference_content($source["id"])) !== false)
619           { if(($reference = $this->source_xml_read($source["reference"], $reference_edit_content)) !==false)
620             { $reference["xml"] = array
621               ( "url" => $source["reference"]["xml"]["url"],
622                 "content" => $reference_edit_content,
623                 "use_edit_content" => true
624               );
625             }
626             else return false;
627           }
628           else return false;
629         }
630         else
631         { if(($reference = $this->source_xml_read($source["reference"]["xml"]["url"])) ===false)
632           { $reference = $this->empty_source();
633           }
634         }
635       }
636       return $reference;
637     }
638
639     # ----------------------------------------------------------------------------------------
640     #                                                                                documents
641     #
642
643     function source_documents($id_source)
644     { $sgbd = $this->sgbd();
645       $documents = array();
646       try{
647         $sql = "SELECT * FROM #--sml_source_documents WHERE id_source=".$this->eq($id_source);
648         $rst = $sgbd->query($sql);
649         if(!isset($rst)) return false;
650         while($v_rst = $sgbd->fetch_assoc($rst)) $documents[$v_rst["id"]] = $v_rst;
651         $sgbd->free_result($rst);
652       }
653       catch(Exception $e) { return false; }
654       return $documents;
655     }
656
657     function add_source_document($id_source, $document)
658     { $sgbd = $this->sgbd();
659       try{
660         $sql =
661          "INSERT INTO #--sml_source_documents(id_source, nom, url)"
662         ." VALUES"
663         ."( ".$this->eq($id_source)
664         .", ".$this->eq($document["nom"])
665         .", ".$this->eq($document["url"])
666         .")";
667         $sgbd->query($sql);
668       }
669       catch(Exception $e) { return false; }
670       return $sgbd->insert_id();
671     }
672
673     function del_source_documents($id_source)
674     { $sgbd = $this->sgbd();
675       try{
676         $sql = "DELETE FROM #--sml_source_documents WHERE id_source=".$this->eq($id_source);
677         $sgbd->query($sql);
678       }
679       catch(Exception $e) { return false; }
680       return true;
681     }
682
683     # ----------------------------------------------------------------------------------------
684     #                                                                  compositions de sources
685     #
686
687     function source_compositions($params)
688     { $id_source = isset($params["id_source"]) ? $params["id_source"] : null;  
689       $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
690       $sgbd = $this->sgbd();
691       $env = $this->env();
692       $compositions = array();
693       if(isset($id_source)){
694         try{
695           $sql = "SELECT * FROM #--sml_source_compositions WHERE id_source=".$this->eq($id_source);
696           $rst = $sgbd->query($sql);
697           while($v_rst = $sgbd->fetch_assoc($rst))
698           { if(!isset($compositions[$v_rst["id_source"]])) $compositions[$v_rst["id_source"]] = array();
699             $compositions[$v_rst["id_source"]][] = $v_rst["id_composition"];
700           }
701           $sgbd->free_result($rst);
702         }
703         catch(Exception $e) { return false; }
704         return $compositions;
705       }
706       elseif(isset($id_composition))
707       { if($id_composition){
708           try{
709             $sql =
710              "SELECT * FROM #--sml_source_compositions WHERE id_composition=".$this->eq($id_composition);
711             $rst = $sgbd->query($sql);
712             while($v_rst = $sgbd->fetch_assoc($rst))
713             { if(!isset($compositions[$v_rst["id_composition"]])) $compositions[$v_rst["id_composition"]] = array();
714               $compositions[$v_rst["id_composition"]][] = $v_rst["id_source"];
715             }
716             $sgbd->free_result($rst);
717           }
718           catch(Exception $e) { return false; }
719           return $compositions;
720         }
721         else{
722           try{
723             $sql =
724              "SELECT DISTINCT id_source FROM #--sml_source_compositions";
725             $rst = $sgbd->query($sql);
726             while($v_rst = $sgbd->fetch_assoc($rst)) $compositions[] = $v_rst["id_source"];
727             $sgbd->free_result($rst);
728           }
729           catch(Exception $e) { return false; }
730           return $compositions;
731         }
732       }
733       return false;
734     }
735
736     function set_source_composition($id_source, $id_composition){
737       $sgbd = $this->sgbd();
738       try{
739         $sql =
740          "INSERT INTO #--sml_source_compositions(id_source, id_composition) VALUES"
741         ."( ".$this->eq($id_source)
742         .", ".$this->eq($id_composition)
743         .")";
744         $sgbd->query($sql);
745       }
746       catch(Exception $e) { return false; }
747       return $this->set_source_xml($id_composition);
748     }
749
750     function del_source_compositions($params){
751       $res = true;
752       $id_source = isset($params["id_source"]) ? $params["id_source"] : null;  
753       $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
754       $to_delete = array();
755       $to_update = array();
756       if(isset($id_composition)) $to_update[] = $id_composition;
757       $sgbd = $this->sgbd();
758       if(isset($id_source)){
759         try{
760           $sql = "SELECT * FROM #--sml_source_compositions WHERE id_source=".$this->eq($id_source);
761           $rst = $sgbd->query($sql);
762           while($v_rst = $sgbd->fetch_assoc($rst)) $to_update[] = $v_rst["id_composition"];
763           $sgbd->free_result($rst);
764         }
765         catch(Exception $e) { return false; }
766       }
767       try{
768         $sql = "DELETE FROM #--sml_source_compositions";
769         $WHERE = "";
770         $WHERE .= (isset($id_source) ? ($WHERE ? " OR " : " WHERE ")."id_source=".$this->eq($id_source) : "");
771         $WHERE .= (isset($id_composition) ? ($WHERE ? " OR " : " WHERE ")."id_composition=".$this->eq($id_composition) : "");
772         $sql .= $WHERE;
773         $sgbd->query($sql);
774       }
775       catch(Exception $e) { return false; }
776       foreach($to_update as $id_source_xml){
777         if(!$this->set_source_xml($id_source_xml)) return false;
778       }
779       return true;
780     }
781
782     function has_sources($id_composition)
783     { $sgbd = $this->sgbd();
784       $env = $this->env();
785       try{
786         $sql = "SELECT count(*) as n FROM #--sml_source_compositions WHERE id_composition=".$this->eq($id_composition);
787         $rst = $sgbd->query($sql);
788         $has_sources = 0;
789         if($v_rst = $sgbd->fetch_assoc($rst)) $has_sources = $v_rst["n"];
790         $sgbd->free_result($rst);
791       }
792       catch(Exception $e) { return false; }
793       return $has_sources;
794     }
795
796     function source_ariane($id_source, $ariane = array())
797     { if(($compositions = $this->source_compositions(array("id_source" => $id_source))) !== false)
798       { if(isset($compositions[$id_source]) && $compositions[$id_source])
799         { foreach($compositions[$id_source] as $id_composition)
800           { if(($ariane = $this->source_ariane($id_composition, $ariane)) !== false)
801             { if(($ariane[$id_composition] = $this->source($id_composition)) !== false)
802               {
803               }
804               else $ariane = false;
805             }
806             else $ariane = false;
807             break;
808           }
809         }
810       }
811       else $ariane = false;
812       return $ariane;
813     }
814
815   }
816
817 ?>