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