3 class sml_data_sources extends mw_data{
7 # ----------------------------------------------------------------------------------------
11 function source_status()
12 { if(!isset($this->status)) $this->status = $this->init_sources_status();
16 function init_sources_status()
17 { $sgbd = $this->sgbd();
20 if($rst = $sgbd->open_data("sml_classes"))
21 { while($v_rst = $sgbd->fetch_data($rst))
23 { $status[$v_rst["id"]] = $v_rst;
30 $sgbd->close_data($rst);
36 # ----------------------------------------------------------------------------------------
40 function init_sources_table(){
44 function sources($params)
45 { $sgbd = $this->sgbd();
48 // -------------------------------------------------------------------------------
51 $start = isset($params["start"]) ? $params["start"] : null;
52 $id_user = isset($params["id_user"]) ? $params["id_user"] : null;
53 $id_groupe = isset($params["id_groupe"]) ? $params["id_groupe"] : null;
54 $status = isset($params["status"]) ? $params["status"] : null;
55 $id_source = isset($params["id_source"]) ? $params["id_source"] : null;
56 $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
57 $id_source_derivation = isset($params["id_source_derivation"]) ? $params["id_source_derivation"] : null;
58 $id_licence = isset($params["id_licence"]) ? $params["id_licence"] : null;
59 $is_reference = isset($params["is_reference"]) ? $params["is_reference"] : null;
60 $order_by = isset($params["order_by"]) ? $params["order_by"] : "ordre";
61 $order = isset($params["order"]) ? $params["order"] : "ASC";
63 // -------------------------------------------------------------------------------
64 // infos pour verifier les filtres
66 if(isset($id_user) || isset($id_groupe))
67 { $in_source_ids = array();
68 $in_groupe_ids = array();
69 if(isset($id_groupe)) $in_groupe_ids[$id_groupe] = true;
71 { if(($groupes = $this->groupes($id_user)) !== false)
72 { foreach($groupes["list"] as $id_groupe => $groupe) $in_groupe_ids[$id_groupe] = true;
77 if($rst = $sgbd->open_data("sml_sources_authors"))
78 { while($OK && $v_rst = $sgbd->fetch_data($rst))
79 { if(isset($v_rst) && isset($v_rst["id_source"]) && isset($v_rst["id_author"]))
80 { if(isset($in_groupe_ids[$v_rst["id_author"]])) $in_source_ids[$v_rst["id_source"]] = true;
84 $sgbd->close_data($rst);
87 if(!$OK) return false;
90 $compositions = array();
92 { if(($compositions = $this->source_compositions(array("id_source" => $id_source))) === false)
96 elseif(isset($id_composition))
97 { if(($compositions = $this->source_compositions(array("id_composition" => $id_composition))) === false)
102 $derivations = array();
103 if(isset($id_source_derivation))
104 { if(($derivations = $this->source_derivations($id_source_derivation)) === false)
109 // -------------------------------------------------------------------------------
112 $sources = array("list" => array(), "total" => 0);
114 if($rst = $sgbd->open_data("sml_sources"))
115 { while($v_rst = $sgbd->fetch_data($rst))
117 $res[$v_rst["id"]] = $v_rst;
118 if(!isset($res[$v_rst["id"]]["ordre"])) $res[$v_rst["id"]]["ordre"] = 0;
125 $sgbd->close_data($rst);
127 { $res = $this->ordonne($res, $order_by, $order);
129 foreach($res as $id_res => $v_rst)
130 { // ------------------------------- par defaut
133 // ------------------------------- filtre sur id_user et id_groupe
135 if(isset($in_source_ids))
136 { $MATCH = isset($in_source_ids[$v_rst["id"]]) && $in_source_ids[$v_rst["id"]];
139 // ------------------------------- filtre sur status
141 { $MATCH = !isset($status) || (isset($v_rst["id_class"]) && $v_rst["id_class"] == $status);
144 // -------------------------------- filtre sur id_source
146 { if(isset($id_source))
148 if($compositions && is_array($compositions[$id_source]))
149 { foreach($compositions[$id_source] as $id_composition)
150 { if(isset($v_rst["id"]) && $v_rst["id"] == $id_composition)
159 // -------------------------------- filtre sur id_composition
161 { if(isset($id_composition))
162 { if($id_composition)
164 if($compositions && is_array($compositions[$id_composition]))
165 { foreach($compositions[$id_composition] as $_id_source)
166 { if(isset($v_rst["id"]) && $v_rst["id"] == $_id_source)
175 { if(isset($compositions[$v_rst["id"]])) $MATCH = false;
181 // -------------------------------- filtre sur id_source_derivation
183 { if(isset($id_source_derivation))
185 if($derivations && is_array($derivations[$id_source_derivation]))
186 { foreach($derivations[$id_source_derivation] as $_id_derivation)
187 { if(isset($v_rst["id"]) && $v_rst["id"] == $_id_derivation)
196 // -------------------------------- filtre sur la licence
198 { if(isset($id_licence))
200 if(isset($v_rst["licence"]) && $v_rst["licence"] == $id_licence) $MATCH = true;
204 // -------------------------------- filtre sur is_reference
206 if(isset($is_reference)){
208 $MATCH = isset($v_rst["reference"]) && $v_rst["reference"];
211 $MATCH = !isset($v_rst["reference"]) || !$v_rst["reference"];
216 // -------------------------------- filtre sur quantite de resultats
218 { $sources["total"]++;
219 $MATCH = !isset($start) || !$env->config("max_list") || ($sources["total"] > $start && $sources["total"] <= ($start + $env->config("max_list")));
222 // -------------------------------- ajout aux resultats si MATCH
223 if($MATCH) $sources["list"][$v_rst["id"]] = $v_rst;
229 foreach($sources["list"] as $id_source => $source){
230 if(($sources["list"][$source["id"]] = $this->load_source($source)) === false) return false;
235 function source($id, $load = false)
236 { $sgbd = $this->sgbd();
237 if(($source = $sgbd->get_data("sml_sources", $id)) !== false){
238 $source["status"] = $source["id_class"];
239 $source = $load ? $this->load_source($source) : $this->get_source_from_v_rst($source);
245 function get_source_from_v_rst($v_rst)
246 { $sgbd = $this->sgbd();
247 $source = $this->empty_source();
248 foreach($v_rst as $rst_key => $rst_value) $source[$rst_key] = $rst_value;
249 $source["status"] = $source["id_class"];
250 if($source["reference"])
251 { if(!is_array($source["reference"]))
252 { $reference_url = $source["reference"];
253 $source["reference"] = $this->empty_source();
254 $source["reference"]["xml"]["url"] = $reference_url;
258 { if(!is_array($source["licence"]))
259 { $source["licence"] = array
260 ( "id" => $source["licence"]
267 function load_source($source){
269 if(!isset($source["id"])) return false;
270 $source = $this->get_source_from_v_rst($source);
271 $source["xml"] = array
272 ( "url" => $this->source_xml_url($source["id"]),
273 "content" => $this->get_source_xml($source["id"])
275 $source["documents"] = array();
276 if(($source["groupes"] = $this->source_groupes($source["id"])) === false) return false;
277 $source["image_uri"] =
278 ( isset($source["image"]) && $source["image"] ?
279 $env->path("content")."uploads/".$source["image"]
282 if(($source["has_sources"] = $this->has_sources($source["id"])) === false) return false;
283 if(($source["has_derivations"] = $this->source_derivations(array("derivation" => $source["id"]))) === false) return false;
284 if(($source["derivations"] = $this->source_derivations(array("id_source" => $source["id"]))) === false) return false;
285 if(($source["reference"] = $this->source_reference($source)) === false) return false;
286 if($user = $env->user()){
287 if(($source["permissions"] = $this->source_permissions($source, $user["id"])) === false) return false;
289 if(!$source["reference"])
290 { if(($source["documents"] = $this->source_documents($source["id"])) === false) return false;
300 $documents = array(),
301 $reference = array(),
302 $derivations = array(),
305 { $sgbd = $this->sgbd();
307 ( "id_class" => $status,
308 "reference" => $reference ? $reference["xml"]["url"] : null,
309 "titre" => $reference ? null : $titre,
310 "licence" => $reference ? null : $licence,
311 "date_creation" => isset($infos["date_creation"]) ? $infos["date_creation"] : null,
312 "date_inscription" => isset($infos["date_inscription"]) ? $infos["date_inscription"] : date("Y-m-d")
314 foreach($infos as $key => $value) if(!isset($source[$key])) $source[$key] = $value;
315 $id = $sgbd->add_data("sml_sources", $source);
316 if(!isset($id)) return false;
317 foreach($groupes as $id_groupe => $groupe)
318 { if($groupe["id"] && $groupe["id_groupe_status"])
319 { if(!$this->add_source_groupe($id, $groupe["id"], $groupe["id_groupe_status"])) return false;
323 foreach($derivations as $source_derivation)
325 ( ( $id_source_derivation = $this->add_source_derivation
327 $source_derivation["xml"]["url"],
328 $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
336 { if($reference["xml"]["use_edit_content"])
337 { if(!$this->set_edit_reference_content($id, $reference["xml"]["content"]))
343 { foreach($documents as $document)
344 { if(!$this->add_source_document($id, $document)) return false;
347 if(!$this->set_source_xml($id)) return false;
357 $documents = array(),
358 $reference = array(),
359 $derivations = array(),
362 { if($source = $this->source($id))
363 { $sgbd = $this->sgbd();
364 $source["reference"] = $reference ? $reference["xml"]["url"] : null;
365 $source["titre"] = $reference ? null : $titre;
366 $source["licence"] = $reference ? null : $licence;
367 $source["date_creation"] = isset($infos["date_creation"]) ? $infos["date_creation"] : null;
368 foreach($infos as $key => $value) $source[$key] = $value;
369 if(!$sgbd->set_data("sml_sources", $id, $source)) return false;
370 if(!$this->del_source_groupes($id)) return false;
371 foreach($groupes as $id_groupe => $groupe)
372 { if($groupe["id"] && $groupe["id_groupe_status"])
373 { if(!$this->add_source_groupe($id, $groupe["id"], $groupe["id_groupe_status"])) return false;
377 if(!$this->del_source_derivations($id)) return false;
378 if(!$this->del_edit_reference_content($id)) return false;
379 if(!$this->del_source_documents($id)) return false;
380 foreach($derivations as $source_derivation)
382 ( ( $id_source_derivation = $this->add_source_derivation
384 $source_derivation["xml"]["url"],
385 $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
393 { if($reference["xml"]["use_edit_content"])
394 { if(!$this->set_edit_reference_content($id, $reference["xml"]["content"]))
400 { foreach($documents as $document)
401 { if(!$this->add_source_document($id, $document)) return false;
404 if(!$this->set_source_xml($id)) return false;
410 function set_source_info($id_source, $key, $value)
411 { $sgbd = $this->sgbd();
412 if($source = $sgbd->get_data("sml_sources", $id_source))
413 { $source[$key] = $value;
414 if($sgbd->set_data("sml_sources", $id_source, $source)) return true;
419 function del_source($id)
420 { $sgbd = $this->sgbd();
421 if(!$this->del_source_compositions(array("id_source" => $id, "id_composition" => $id))) return false;
422 if(!$this->del_edit_reference_content($id)) return false;
423 if(!$this->del_source_derivations($id)) return false;
424 if(!$this->del_source_xml($id)) return false;
425 if(!$this->del_source_documents($id)) return false;
426 if(!$this->del_source_groupes($id)) return false;
427 if(!$sgbd->del_data("sml_sources", $id)) return false;
431 # ----------------------------------------------------------------------------------------
432 # derivations de sources
435 function init_source_derivations(){
439 function source_derivations($params)
440 { $sgbd = $this->sgbd();
442 $derivations = array();
443 // sources dont "id_source" est une derivation
444 if(isset($params["id_source"]))
445 { if($rst = $sgbd->open_data("sml_source_derivations"))
446 { while($v_rst = $sgbd->fetch_data($rst))
448 { if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && $v_rst["id_source"] == $params["id_source"])
449 { $derivations[$v_rst["id"]] = array();
450 $derivation_edit_file = $this->derivation_edit_xml_path($v_rst["id_source"], $v_rst["id"]);
451 if(file_exists($derivation_edit_file))
452 { if(($derivation_edit_content = $this->get_edit_derivation_content($v_rst["id_source"], $v_rst["id"])) !== false)
453 { if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"], $derivation_edit_content)) !==false)
454 { $derivations[$v_rst["id"]]["xml"] = array
455 ( "url" => $v_rst["derivation"],
456 "content" => $derivation_edit_content,
457 "use_edit_content" => true
461 { $derivations = null;
466 { $derivations = null;
471 { if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"])) !==false)
472 { $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
474 else $derivations[$v_rst["id"]] = $this->empty_source();
476 $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
480 { $derivations = null;
484 $sgbd->close_data($rst);
486 if(!isset($derivations)) return false;
489 // sources qui derivent de "derivation"
490 elseif(isset($params["derivation"]))
491 { $source_xml_url = $params["derivation"];
492 if(preg_match("/^[0-9]+$/", $source_xml_url)) $source_xml_url = $this->source_xml_url($source_xml_url);
493 $id_sources = array();
494 if($rst = $sgbd->open_data("sml_source_derivations"))
495 { while($v_rst = $sgbd->fetch_data($rst))
497 { if(isset($v_rst["id_source"]) && isset($v_rst["derivation"]) && $v_rst["derivation"] == $source_xml_url)
498 { $id_sources[$v_rst["id_source"]] = true;
501 else { $id_sources = false; break; }
503 $sgbd->close_data($rst);
505 else $id_sources = false;
506 if($id_sources === false) return false;
508 { if($rst = $sgbd->open_data("sml_sources"))
509 { while($v_rst = $sgbd->fetch_data($rst))
511 { if(isset($v_rst["id"]) && isset($id_sources[$v_rst["id"]]))
512 { $derivations[$v_rst["id"]] = $v_rst;
515 else { $derivations = false; break; }
517 $sgbd->close_data($rst);
519 else $derivations = false;
526 function source_derivation($id)
527 { $sgbd = $this->sgbd();
528 return $sgbd->get_data("sml_source_derivations", $id);
531 function add_source_derivation($id_source, $derivation, $edit_content = "")
532 { $sgbd = $this->sgbd();
533 $id_source_derivation = $sgbd->add_data
534 ( "sml_source_derivations",
536 ( "id_source" => $id_source,
537 "derivation" => $derivation
540 if(isset($id_source_derivation))
542 { if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content))
543 { $id_source_derivation = false;
547 else $id_source_derivation = false;
548 return $id_source_derivation;
551 function set_source_derivation($id_source_derivation, $id_source, $derivation, $edit_content = "")
552 { $sgbd = $this->sgbd();
555 ( "sml_source_derivations",
556 $id_source_derivation,
558 ( "id_source" => $id_source,
559 "derivation" => $derivation
565 { if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content))
574 function del_source_derivation($id_source_derivation)
575 { if(($derivation = $this->source_derivation($id_source_derivation)) !== false)
576 { if(($derivations = $this->source_derivations(array("id_source" => $derivation["id_source"]))) !== false)
577 { $sgbd = $this->sgbd();
578 if(!$sgbd->del_data("sml_source_derivations", $id_source_derivation)) return false;
579 if(count($derivations) > 1)
580 { return $this->del_edit_derivation_content($derivation["id_source"], $id_source_derivation);
582 else return $this->del_edit_derivations($derivation["id_source"]);
588 function del_source_derivations($id_source)
589 { $sgbd = $this->sgbd();
590 $id_source_derivations = array();
591 if($rst = $sgbd->open_data("sml_source_derivations"))
592 { while($v_rst = $sgbd->fetch_data($rst))
594 { if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && $v_rst["id_source"] == $id_source)
595 { $id_source_derivations[] = $v_rst["id"];
598 else { $id_source_derivations = false; break; }
600 $sgbd->close_data($rst);
602 else $id_source_derivations = false;
603 if($id_source_derivations === false) return false;
604 if($id_source_derivations)
605 { foreach($id_source_derivations as $id_source_derivation)
606 { if(!$sgbd->del_data("sml_source_derivations", $id_source_derivation)) return false;
609 return $this->del_edit_derivations($id_source);
612 # ----------------------------------------------------------------------------------------
616 function source_reference($source)
617 { $reference = array();
618 if($source["reference"])
619 { if(!is_array($source["reference"]))
620 { $source["reference"] = array
621 ( "url" => $source["reference"],
623 "use_edit_content" => false
626 $reference_edit_file = $this->reference_edit_xml_path($source["id"]);
627 if(file_exists($reference_edit_file))
628 { if(($reference_edit_content = $this->get_edit_reference_content($source["id"])) !== false)
629 { if(($reference = $this->source_xml_read($source["reference"], $reference_edit_content)) !==false)
630 { $reference["xml"] = array
631 ( "url" => $source["reference"]["xml"]["url"],
632 "content" => $reference_edit_content,
633 "use_edit_content" => true
641 { if(($reference = $this->source_xml_read($source["reference"]["xml"]["url"])) ===false)
642 { $reference = $this->empty_source();
649 # ----------------------------------------------------------------------------------------
653 function source_documents($id_source)
654 { $sgbd = $this->sgbd();
655 $documents = array();
656 if($sgbd->data_exists("sml_sources/".$id_source))
657 { if($rst = $sgbd->open_data("sml_sources/".$id_source))
659 while($OK && ($document = $sgbd->fetch_data($rst)))
660 { if(isset($document)) $documents[$document["id"]] = $document;
663 $sgbd->close_data($rst);
666 if(!$OK) return false;
671 function add_source_document($id_source, $document)
672 { $sgbd = $this->sgbd();
673 if(!$sgbd->data_exists("sml_sources/".$id_source))
674 { if(!$sgbd->create_data("sml_sources/".$id_source)) return false;
677 ( !( $id_document = $sgbd->add_data
678 ( "sml_sources/".$id_source,
680 ( "nom" => $document["nom"],
681 "url" => $document["url"]
689 function del_source_documents($id_source)
690 { $sgbd = $this->sgbd();
691 if($sgbd->data_exists("sml_sources/".$id_source))
692 { if(!$sgbd->remove_data("sml_sources/".$id_source)) return false;
697 # ----------------------------------------------------------------------------------------
698 # compositions de sources
701 function source_compositions($params)
702 { $id_source = isset($params["id_source"]) ? $params["id_source"] : null;
703 $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
704 $sgbd = $this->sgbd();
706 $compositions = array();
707 if(isset($id_source))
708 { if($rst = $sgbd->open_data("sml_source_compositions"))
709 { while($v_rst = $sgbd->fetch_data($rst))
711 { if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]) && $v_rst["id_source"] == $id_source)
712 { if(!isset($compositions[$v_rst["id_source"]])) $compositions[$v_rst["id_source"]] = array();
713 $compositions[$v_rst["id_source"]][] = $v_rst["id_composition"];
716 else { $compositions = false; break; }
718 $sgbd->close_data($rst);
720 else $compositions = false;
722 elseif(isset($id_composition))
723 { if($id_composition)
724 { if($rst = $sgbd->open_data("sml_source_compositions"))
725 { while($v_rst = $sgbd->fetch_data($rst))
727 { if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]) && $v_rst["id_composition"] == $id_composition)
728 { if(!isset($compositions[$v_rst["id_composition"]])) $compositions[$v_rst["id_composition"]] = array();
729 $compositions[$v_rst["id_composition"]][] = $v_rst["id_source"];
732 else { $compositions = false; break; }
734 $sgbd->close_data($rst);
736 else $compositions = false;
739 { if($rst = $sgbd->open_data("sml_source_compositions"))
740 { while($v_rst = $sgbd->fetch_data($rst))
742 { if(isset($v_rst["id_source"])) $compositions[$v_rst["id_source"]] = true;
744 else { $compositions = false; break; }
746 $sgbd->close_data($rst);
748 else $compositions = false;
751 return $compositions;
754 function set_source_composition($id_source, $id_composition)
755 { $sgbd = $this->sgbd();
758 ( "sml_source_compositions",
760 ( "id_source" => $id_source,
761 "id_composition" => $id_composition
764 ) return $this->set_source_xml($id_composition);
768 function del_source_compositions($params)
770 $id_source = isset($params["id_source"]) ? $params["id_source"] : null;
771 $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
772 $to_delete = array();
773 $to_update = array();
774 if(isset($id_composition)) $to_update[$id_composition] = true;
775 $sgbd = $this->sgbd();
776 if($rst = $sgbd->open_data("sml_source_compositions"))
777 { while($v_rst = $sgbd->fetch_data($rst))
779 { if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && isset($v_rst["id_composition"]))
781 ( (isset($id_source) && $v_rst["id_source"] == $id_source)
782 || (isset($id_composition) && $v_rst["id_composition"] == $id_composition)
783 ) $to_delete[] = $v_rst["id"];
784 if(isset($id_source) && $v_rst["id_source"] == $id_source) $to_update[$v_rst["id_composition"]] = true;
787 else { $OK = false; break; }
789 $sgbd->close_data($rst);
792 if(!$OK) return false;
793 foreach($to_delete as $id_source_composition)
794 { if(!$sgbd->del_data("sml_source_compositions", $id_source_composition)) return false;
796 foreach($to_update as $id_source_xml => $delete)
797 { if(!$this->set_source_xml($id_source_xml)) return false;
802 function has_sources($id_composition)
803 { $sgbd = $this->sgbd();
806 if($rst = $sgbd->open_data("sml_source_compositions"))
807 { while($v_rst = $sgbd->fetch_data($rst))
809 { if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]))
810 { if($v_rst["id_composition"] == $id_composition)
817 { $has_sources = false;
821 $sgbd->close_data($rst);
823 else $has_sources = false;
827 function source_ariane($id_source, $ariane = array())
828 { if(($compositions = $this->source_compositions(array("id_source" => $id_source))) !== false)
829 { if(isset($compositions[$id_source]) && $compositions[$id_source])
830 { foreach($compositions[$id_source] as $id_composition)
831 { if(($ariane = $this->source_ariane($id_composition, $ariane)) !== false)
832 { if(($ariane[$id_composition] = $this->source($id_composition)) !== false)
835 else $ariane = false;
837 else $ariane = false;
842 else $ariane = false;