3 class sml_data_sources extends mw_data{
7 # ----------------------------------------------------------------------------------------
11 public function source_status(){
12 if(!isset($this->status)) $this->status = $this->init_sources_status();
16 public 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 public function init_sources_table(){
44 public 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();
91 if(isset($id_source)){
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)){
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
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 public 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 public 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 public 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;
295 public function add_source(
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){
326 $id_source_derivation = $this->add_source_derivation(
328 $source_derivation["xml"]["url"],
329 $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
337 if($reference["xml"]["use_edit_content"]){
338 if(!$this->set_edit_reference_content($id, $reference["xml"]["content"])){
344 foreach($documents as $document){
345 if(!$this->add_source_document($id, $document)) return false;
348 if(!$this->set_source_xml($id)) return false;
352 public function set_source(
358 $documents = array(),
359 $reference = array(),
360 $derivations = array(),
363 if($source = $this->source($id)){
364 $sgbd = $this->sgbd();
365 $source["reference"] = $reference ? $reference["xml"]["url"] : null;
366 $source["titre"] = $reference ? null : $titre;
367 $source["licence"] = $reference ? null : $licence;
368 $source["date_creation"] = isset($infos["date_creation"]) ? $infos["date_creation"] : null;
369 foreach($infos as $key => $value) $source[$key] = $value;
370 if(!$sgbd->set_data("sml_sources", $id, $source)) return false;
371 if(!$this->del_source_groupes($id)) return false;
372 foreach($groupes as $id_groupe => $groupe){
373 if($groupe["id"] && $groupe["id_groupe_status"]){
374 if(!$this->add_source_groupe($id, $groupe["id"], $groupe["id_groupe_status"])) return false;
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){
384 $id_source_derivation = $this->add_source_derivation(
386 $source_derivation["xml"]["url"],
387 $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
395 if($reference["xml"]["use_edit_content"]){
396 if(!$this->set_edit_reference_content($id, $reference["xml"]["content"])){
402 foreach($documents as $document){
403 if(!$this->add_source_document($id, $document)) return false;
406 if(!$this->set_source_xml($id)) return false;
412 public function set_source_info($id_source, $key, $value){
413 $sgbd = $this->sgbd();
414 if($source = $sgbd->get_data("sml_sources", $id_source)){
415 $source[$key] = $value;
416 if($sgbd->set_data("sml_sources", $id_source, $source)) return true;
421 public function del_source($id){
422 $sgbd = $this->sgbd();
423 if(!$this->del_source_compositions(array("id_source" => $id, "id_composition" => $id))) return false;
424 if(!$this->del_edit_reference_content($id)) return false;
425 if(!$this->del_source_derivations($id)) return false;
426 if(!$this->del_source_xml($id)) return false;
427 if(!$this->del_source_documents($id)) return false;
428 if(!$this->del_source_groupes($id)) return false;
429 if(!$sgbd->del_data("sml_sources", $id)) return false;
433 # ----------------------------------------------------------------------------------------
434 # derivations de sources
437 public function init_source_derivations(){
441 public function source_derivations($params){
442 $sgbd = $this->sgbd();
444 $derivations = array();
445 // sources dont "id_source" est une derivation
446 if(isset($params["id_source"])){
447 if($rst = $sgbd->open_data("sml_source_derivations")){
448 while($v_rst = $sgbd->fetch_data($rst)){
450 if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && $v_rst["id_source"] == $params["id_source"]){
451 $derivations[$v_rst["id"]] = array();
452 $derivation_edit_file = $this->derivation_edit_xml_path($v_rst["id_source"], $v_rst["id"]);
453 if(file_exists($derivation_edit_file)){
454 if(($derivation_edit_content = $this->get_edit_derivation_content($v_rst["id_source"], $v_rst["id"])) !== false){
455 if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"], $derivation_edit_content)) !==false){
456 $derivations[$v_rst["id"]]["xml"] = array(
457 "url" => $v_rst["derivation"],
458 "content" => $derivation_edit_content,
459 "use_edit_content" => true
473 if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"])) !==false){
474 $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
476 else $derivations[$v_rst["id"]] = $this->empty_source();
478 $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
486 $sgbd->close_data($rst);
488 if(!isset($derivations)) return false;
491 // sources qui derivent de "derivation"
492 elseif(isset($params["derivation"])){
493 $source_xml_url = $params["derivation"];
494 if(preg_match("/^[0-9]+$/", $source_xml_url)) $source_xml_url = $this->source_xml_url($source_xml_url);
495 $id_sources = array();
496 if($rst = $sgbd->open_data("sml_source_derivations")){
497 while($v_rst = $sgbd->fetch_data($rst)){
499 if(isset($v_rst["id_source"]) && isset($v_rst["derivation"]) && $v_rst["derivation"] == $source_xml_url){
500 $id_sources[$v_rst["id_source"]] = true;
508 $sgbd->close_data($rst);
510 else $id_sources = false;
511 if($id_sources === false) return false;
513 if($rst = $sgbd->open_data("sml_sources")){
514 while($v_rst = $sgbd->fetch_data($rst)){
516 if(isset($v_rst["id"]) && isset($id_sources[$v_rst["id"]])){
517 $derivations[$v_rst["id"]] = $v_rst;
521 $derivations = false;
525 $sgbd->close_data($rst);
527 else $derivations = false;
534 public function source_derivation($id){
535 $sgbd = $this->sgbd();
536 return $sgbd->get_data("sml_source_derivations", $id);
539 public function add_source_derivation($id_source, $derivation, $edit_content = ""){
540 $sgbd = $this->sgbd();
541 $id_source_derivation = $sgbd->add_data(
542 "sml_source_derivations",
544 "id_source" => $id_source,
545 "derivation" => $derivation
548 if(isset($id_source_derivation)){
550 if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content)){
551 $id_source_derivation = false;
555 else $id_source_derivation = false;
556 return $id_source_derivation;
559 public function set_source_derivation($id_source_derivation, $id_source, $derivation, $edit_content = ""){
560 $sgbd = $this->sgbd();
564 "sml_source_derivations",
565 $id_source_derivation,
567 "id_source" => $id_source,
568 "derivation" => $derivation
574 if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content)){
583 public function del_source_derivation($id_source_derivation){
584 if(($derivation = $this->source_derivation($id_source_derivation)) !== false){
585 if(($derivations = $this->source_derivations(array("id_source" => $derivation["id_source"]))) !== false){
586 $sgbd = $this->sgbd();
587 if(!$sgbd->del_data("sml_source_derivations", $id_source_derivation)) return false;
588 if(count($derivations) > 1){
589 return $this->del_edit_derivation_content($derivation["id_source"], $id_source_derivation);
591 else return $this->del_edit_derivations($derivation["id_source"]);
597 public function del_source_derivations($id_source){
598 $sgbd = $this->sgbd();
599 $id_source_derivations = array();
600 if($rst = $sgbd->open_data("sml_source_derivations")){
601 while($v_rst = $sgbd->fetch_data($rst)){
603 if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && $v_rst["id_source"] == $id_source){
604 $id_source_derivations[] = $v_rst["id"];
608 $id_source_derivations = false;
612 $sgbd->close_data($rst);
614 else $id_source_derivations = false;
615 if($id_source_derivations === false) return false;
616 if($id_source_derivations){
617 foreach($id_source_derivations as $id_source_derivation){
618 if(!$sgbd->del_data("sml_source_derivations", $id_source_derivation)) return false;
621 return $this->del_edit_derivations($id_source);
624 # ----------------------------------------------------------------------------------------
628 public function source_reference($source){
629 $reference = array();
630 if($source["reference"]){
631 if(!is_array($source["reference"])){
632 $source["reference"] = array(
633 "url" => $source["reference"],
635 "use_edit_content" => false
638 $reference_edit_file = $this->reference_edit_xml_path($source["id"]);
639 if(file_exists($reference_edit_file)){
640 if(($reference_edit_content = $this->get_edit_reference_content($source["id"])) !== false){
641 if(($reference = $this->source_xml_read($source["reference"], $reference_edit_content)) !==false){
642 $reference["xml"] = array(
643 "url" => $source["reference"]["xml"]["url"],
644 "content" => $reference_edit_content,
645 "use_edit_content" => true
653 if(($reference = $this->source_xml_read($source["reference"]["xml"]["url"])) ===false){
654 $reference = $this->empty_source();
661 # ----------------------------------------------------------------------------------------
665 public function source_documents($id_source){
666 $sgbd = $this->sgbd();
667 $documents = array();
668 if($sgbd->data_exists("sml_sources/".$id_source)){
669 if($rst = $sgbd->open_data("sml_sources/".$id_source)){
671 while($OK && ($document = $sgbd->fetch_data($rst))){
672 if(isset($document)) $documents[$document["id"]] = $document;
675 $sgbd->close_data($rst);
678 if(!$OK) return false;
683 public function add_source_document($id_source, $document){
684 $sgbd = $this->sgbd();
685 if(!$sgbd->data_exists("sml_sources/".$id_source)){
686 if(!$sgbd->create_data("sml_sources/".$id_source)) return false;
690 $id_document = $sgbd->add_data(
691 "sml_sources/".$id_source,
693 "nom" => $document["nom"],
694 "url" => $document["url"]
704 public function del_source_documents($id_source){
705 $sgbd = $this->sgbd();
706 if($sgbd->data_exists("sml_sources/".$id_source)){
707 if(!$sgbd->remove_data("sml_sources/".$id_source)) return false;
712 # ----------------------------------------------------------------------------------------
713 # compositions de sources
716 public function source_compositions($params){
717 $id_source = isset($params["id_source"]) ? $params["id_source"] : null;
718 $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
719 $sgbd = $this->sgbd();
721 $compositions = array();
722 if(isset($id_source)){
723 if($rst = $sgbd->open_data("sml_source_compositions")){
724 while($v_rst = $sgbd->fetch_data($rst)){
726 if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]) && $v_rst["id_source"] == $id_source){
727 if(!isset($compositions[$v_rst["id_source"]])) $compositions[$v_rst["id_source"]] = array();
728 $compositions[$v_rst["id_source"]][] = $v_rst["id_composition"];
732 $compositions = false;
736 $sgbd->close_data($rst);
738 else $compositions = false;
740 elseif(isset($id_composition)){
742 if($rst = $sgbd->open_data("sml_source_compositions")){
743 while($v_rst = $sgbd->fetch_data($rst)){
745 if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]) && $v_rst["id_composition"] == $id_composition){
746 if(!isset($compositions[$v_rst["id_composition"]])) $compositions[$v_rst["id_composition"]] = array();
747 $compositions[$v_rst["id_composition"]][] = $v_rst["id_source"];
751 $compositions = false;
755 $sgbd->close_data($rst);
757 else $compositions = false;
760 if($rst = $sgbd->open_data("sml_source_compositions")){
761 while($v_rst = $sgbd->fetch_data($rst)){
763 if(isset($v_rst["id_source"])) $compositions[$v_rst["id_source"]] = true;
766 $compositions = false;
770 $sgbd->close_data($rst);
772 else $compositions = false;
775 return $compositions;
778 public function set_source_composition($id_source, $id_composition){
779 $sgbd = $this->sgbd();
782 "sml_source_compositions",
784 "id_source" => $id_source,
785 "id_composition" => $id_composition
789 return $this->set_source_xml($id_composition);
794 public function del_source_compositions($params){
796 $id_source = isset($params["id_source"]) ? $params["id_source"] : null;
797 $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
798 $to_delete = array();
799 $to_update = array();
800 if(isset($id_composition)) $to_update[$id_composition] = true;
801 $sgbd = $this->sgbd();
802 if($rst = $sgbd->open_data("sml_source_compositions")){
803 while($v_rst = $sgbd->fetch_data($rst)){
805 if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && isset($v_rst["id_composition"])){
807 (isset($id_source) && $v_rst["id_source"] == $id_source)
808 || (isset($id_composition) && $v_rst["id_composition"] == $id_composition)
810 $to_delete[] = $v_rst["id"];
812 if(isset($id_source) && $v_rst["id_source"] == $id_source) $to_update[$v_rst["id_composition"]] = true;
820 $sgbd->close_data($rst);
823 if(!$OK) return false;
824 foreach($to_delete as $id_source_composition){
825 if(!$sgbd->del_data("sml_source_compositions", $id_source_composition)) return false;
827 foreach($to_update as $id_source_xml => $delete){
828 if(!$this->set_source_xml($id_source_xml)) return false;
833 public function has_sources($id_composition){
834 $sgbd = $this->sgbd();
837 if($rst = $sgbd->open_data("sml_source_compositions")){
838 while($v_rst = $sgbd->fetch_data($rst)){
840 if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"])){
841 if($v_rst["id_composition"] == $id_composition){
848 $has_sources = false;
852 $sgbd->close_data($rst);
854 else $has_sources = false;
858 public function source_ariane($id_source, $ariane = array()){
859 if(($compositions = $this->source_compositions(array("id_source" => $id_source))) !== false){
860 if(isset($compositions[$id_source]) && $compositions[$id_source]){
861 foreach($compositions[$id_source] as $id_composition){
862 if(($ariane = $this->source_ariane($id_composition, $ariane)) !== false){
863 if(($ariane[$id_composition] = $this->source($id_composition)) !== false){
865 else $ariane = false;
867 else $ariane = false;
872 else $ariane = false;