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