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 $order_by = isset($params["order_by"]) ? $params["order_by"] : "ordre";
60 $order = isset($params["order"]) ? $params["order"] : "ASC";
62 // -------------------------------------------------------------------------------
63 // infos pour verifier les filtres
65 if(isset($id_user) || isset($id_groupe))
66 { $in_source_ids = array();
67 $in_groupe_ids = array();
68 if(isset($id_groupe)) $in_groupe_ids[$id_groupe] = true;
70 { if(($groupes = $this->groupes($id_user)) !== false)
71 { foreach($groupes["list"] as $id_groupe => $groupe) $in_groupe_ids[$id_groupe] = true;
76 if($rst = $sgbd->open_data("sml_sources_authors"))
77 { while($OK && $v_rst = $sgbd->fetch_data($rst))
78 { if(isset($v_rst) && isset($v_rst["id_source"]) && isset($v_rst["id_author"]))
79 { if(isset($in_groupe_ids[$v_rst["id_author"]])) $in_source_ids[$v_rst["id_source"]] = true;
83 $sgbd->close_data($rst);
86 if(!$OK) return false;
89 $compositions = array();
91 { if(($compositions = $this->source_compositions(array("id_source" => $id_source))) === false)
95 elseif(isset($id_composition))
96 { if(($compositions = $this->source_compositions(array("id_composition" => $id_composition))) === false)
101 $derivations = array();
102 if(isset($id_source_derivation))
103 { if(($derivations = $this->source_derivations($id_source_derivation)) === false)
108 // -------------------------------------------------------------------------------
111 $sources = array("list" => array(), "total" => 0);
113 if($rst = $sgbd->open_data("sml_sources"))
114 { while($v_rst = $sgbd->fetch_data($rst))
115 { if(isset($v_rst)) $res[$v_rst["id"]] = $v_rst;
121 $sgbd->close_data($rst);
123 { $res = $this->ordonne($res, $order_by, $order);
125 foreach($res as $id_res => $v_rst)
126 { // ------------------------------- par defaut
129 // ------------------------------- filtre sur id_user et id_groupe
131 if(isset($in_source_ids))
132 { $MATCH = isset($in_source_ids[$v_rst["id"]]) && $in_source_ids[$v_rst["id"]];
135 // ------------------------------- filtre sur status
137 { $MATCH = !isset($status) || (isset($v_rst["id_class"]) && $v_rst["id_class"] == $status);
140 // -------------------------------- filtre sur id_source
142 { if(isset($id_source))
144 if($compositions && is_array($compositions[$id_source]))
145 { foreach($compositions[$id_source] as $id_composition)
146 { if(isset($v_rst["id"]) && $v_rst["id"] == $id_composition)
155 // -------------------------------- filtre sur id_composition
157 { if(isset($id_composition))
158 { if($id_composition)
160 if($compositions && is_array($compositions[$id_composition]))
161 { foreach($compositions[$id_composition] as $_id_source)
162 { if(isset($v_rst["id"]) && $v_rst["id"] == $_id_source)
171 { if(isset($compositions[$v_rst["id"]])) $MATCH = false;
177 // -------------------------------- filtre sur id_source_derivation
179 { if(isset($id_source_derivation))
181 if($derivations && is_array($derivations[$id_source_derivation]))
182 { foreach($derivations[$id_source_derivation] as $_id_derivation)
183 { if(isset($v_rst["id"]) && $v_rst["id"] == $_id_derivation)
192 // -------------------------------- filtre sur la licence
194 { if(isset($id_licence))
196 if(isset($v_rst["licence"]) && $v_rst["licence"] == $id_licence) $MATCH = true;
200 // -------------------------------- filtre sur quantite de resultats
202 { $sources["total"]++;
203 $MATCH = !isset($start) || !$env->config("max_list") || ($sources["total"] > $start && $sources["total"] <= ($start + $env->config("max_list")));
206 // -------------------------------- ajout aux resultats si MATCH
207 if($MATCH) $sources["list"][$v_rst["id"]] = $v_rst;
213 foreach($sources["list"] as $id_source => $source){
214 if(($sources["list"][$source["id"]] = $this->load_source($source)) === false) return false;
219 function source($id, $load = false)
220 { $sgbd = $this->sgbd();
221 if(($source = $sgbd->get_data("sml_sources", $id)) !== false){
222 $source["status"] = $source["id_class"];
223 $source = $load ? $this->load_source($source) : $this->get_source_from_v_rst($source);
229 function get_source_from_v_rst($v_rst)
230 { $sgbd = $this->sgbd();
231 $source = $this->empty_source();
232 foreach($v_rst as $rst_key => $rst_value) $source[$rst_key] = $rst_value;
233 $source["status"] = $source["id_class"];
234 if($source["reference"])
235 { if(!is_array($source["reference"]))
236 { $reference_url = $source["reference"];
237 $source["reference"] = $this->empty_source();
238 $source["reference"]["xml"]["url"] = $reference_url;
242 { if(!is_array($source["licence"]))
243 { $source["licence"] = array
244 ( "id" => $source["licence"]
251 function load_source($source){
253 if(!isset($source["id"])) return false;
254 $source = $this->get_source_from_v_rst($source);
255 $source["xml"] = array
256 ( "url" => $this->source_xml_url($source["id"]),
257 "content" => $this->get_source_xml($source["id"])
259 $source["documents"] = array();
260 if(($source["groupes"] = $this->source_groupes($source["id"])) === false) return false;
261 $source["image_uri"] =
262 ( isset($source["image"]) && $source["image"] ?
263 $env->path("content")."uploads/".$source["image"]
266 if(($source["has_sources"] = $this->has_sources($source["id"])) === false) return false;
267 if(($source["has_derivations"] = $this->source_derivations(array("derivation" => $source["id"]))) === false) return false;
268 if(($source["derivations"] = $this->source_derivations(array("id_source" => $source["id"]))) === false) return false;
269 if(($source["reference"] = $this->source_reference($source)) === false) return false;
270 if($user = $env->user()){
271 if(($source["permissions"] = $this->source_permissions($source, $user["id"])) === false) return false;
273 if(!$source["reference"])
274 { if(($source["documents"] = $this->source_documents($source["id"])) === false) return false;
284 $documents = array(),
285 $reference = array(),
286 $derivations = array(),
289 { $sgbd = $this->sgbd();
291 ( "id_class" => $status,
292 "reference" => $reference ? $reference["xml"]["url"] : null,
293 "titre" => $reference ? null : $titre,
294 "licence" => $reference ? null : $licence,
295 "date_creation" => isset($infos["date_creation"]) ? $infos["date_creation"] : null,
296 "date_inscription" => isset($infos["date_inscription"]) ? $infos["date_inscription"] : date("Y-m-d")
298 foreach($infos as $key => $value) if(!isset($source[$key])) $source[$key] = $value;
299 $id = $sgbd->add_data("sml_sources", $source);
300 if(!isset($id)) return false;
301 foreach($groupes as $id_groupe => $groupe)
302 { if($groupe["id"] && $groupe["id_groupe_status"])
303 { if(!$this->add_source_groupe($id, $groupe["id"], $groupe["id_groupe_status"])) return false;
307 foreach($derivations as $source_derivation)
309 ( ( $id_source_derivation = $this->add_source_derivation
311 $source_derivation["xml"]["url"],
312 $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
320 { if($reference["xml"]["use_edit_content"])
321 { if(!$this->set_edit_reference_content($id, $reference["xml"]["content"]))
327 { foreach($documents as $document)
328 { if(!$this->add_source_document($id, $document)) return false;
331 if(!$this->set_source_xml($id)) return false;
341 $documents = array(),
342 $reference = array(),
343 $derivations = array(),
346 { if($source = $this->source($id))
347 { $sgbd = $this->sgbd();
348 $source["reference"] = $reference ? $reference["xml"]["url"] : null;
349 $source["titre"] = $reference ? null : $titre;
350 $source["licence"] = $reference ? null : $licence;
351 $source["date_creation"] = isset($infos["date_creation"]) ? $infos["date_creation"] : null;
352 foreach($infos as $key => $value) $source[$key] = $value;
353 if(!$sgbd->set_data("sml_sources", $id, $source)) return false;
354 if(!$this->del_source_groupes($id)) return false;
355 foreach($groupes as $id_groupe => $groupe)
356 { if($groupe["id"] && $groupe["id_groupe_status"])
357 { if(!$this->add_source_groupe($id, $groupe["id"], $groupe["id_groupe_status"])) return false;
361 if(!$this->del_source_derivations($id)) return false;
362 if(!$this->del_edit_reference_content($id)) return false;
363 if(!$this->del_source_documents($id)) return false;
364 foreach($derivations as $source_derivation)
366 ( ( $id_source_derivation = $this->add_source_derivation
368 $source_derivation["xml"]["url"],
369 $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
377 { if($reference["xml"]["use_edit_content"])
378 { if(!$this->set_edit_reference_content($id, $reference["xml"]["content"]))
384 { foreach($documents as $document)
385 { if(!$this->add_source_document($id, $document)) return false;
388 if(!$this->set_source_xml($id)) return false;
394 function set_source_info($id_source, $key, $value)
395 { $sgbd = $this->sgbd();
396 if($source = $sgbd->get_data("sml_sources", $id_source))
397 { $source[$key] = $value;
398 if($sgbd->set_data("sml_sources", $id_source, $source)) return true;
403 function del_source($id)
404 { $sgbd = $this->sgbd();
405 if(!$this->del_source_compositions(array("id_source" => $id, "id_composition" => $id))) return false;
406 if(!$this->del_edit_reference_content($id)) return false;
407 if(!$this->del_source_derivations($id)) return false;
408 if(!$this->del_source_xml($id)) return false;
409 if(!$this->del_source_documents($id)) return false;
410 if(!$this->del_source_groupes($id)) return false;
411 if(!$sgbd->del_data("sml_sources", $id)) return false;
415 # ----------------------------------------------------------------------------------------
416 # derivations de sources
419 function init_source_derivations(){
423 function source_derivations($params)
424 { $sgbd = $this->sgbd();
426 $derivations = array();
427 // sources dont "id_source" est une derivation
428 if(isset($params["id_source"]))
429 { if($rst = $sgbd->open_data("sml_source_derivations"))
430 { while($v_rst = $sgbd->fetch_data($rst))
432 { if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && $v_rst["id_source"] == $params["id_source"])
433 { $derivations[$v_rst["id"]] = array();
434 $derivation_edit_file = $this->derivation_edit_xml_path($v_rst["id_source"], $v_rst["id"]);
435 if(file_exists($derivation_edit_file))
436 { if(($derivation_edit_content = $this->get_edit_derivation_content($v_rst["id_source"], $v_rst["id"])) !== false)
437 { if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"], $derivation_edit_content)) !==false)
438 { $derivations[$v_rst["id"]]["xml"] = array
439 ( "url" => $v_rst["derivation"],
440 "content" => $derivation_edit_content,
441 "use_edit_content" => true
445 { $derivations = null;
450 { $derivations = null;
455 { if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"])) !==false)
456 { $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
458 else $derivations[$v_rst["id"]] = $this->empty_source();
460 $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
464 { $derivations = null;
468 $sgbd->close_data($rst);
470 if(!isset($derivations)) return false;
473 // sources qui derivent de "derivation"
474 elseif(isset($params["derivation"]))
475 { $source_xml_url = $params["derivation"];
476 if(preg_match("/^[0-9]+$/", $source_xml_url)) $source_xml_url = $this->source_xml_url($source_xml_url);
477 $id_sources = array();
478 if($rst = $sgbd->open_data("sml_source_derivations"))
479 { while($v_rst = $sgbd->fetch_data($rst))
481 { if(isset($v_rst["id_source"]) && isset($v_rst["derivation"]) && $v_rst["derivation"] == $source_xml_url)
482 { $id_sources[$v_rst["id_source"]] = true;
485 else { $id_sources = false; break; }
487 $sgbd->close_data($rst);
489 else $id_sources = false;
490 if($id_sources === false) return false;
492 { if($rst = $sgbd->open_data("sml_sources"))
493 { while($v_rst = $sgbd->fetch_data($rst))
495 { if(isset($v_rst["id"]) && isset($id_sources[$v_rst["id"]]))
496 { $derivations[$v_rst["id"]] = $v_rst;
499 else { $derivations = false; break; }
501 $sgbd->close_data($rst);
503 else $derivations = false;
510 function source_derivation($id)
511 { $sgbd = $this->sgbd();
512 return $sgbd->get_data("sml_source_derivations", $id);
515 function add_source_derivation($id_source, $derivation, $edit_content = "")
516 { $sgbd = $this->sgbd();
517 $id_source_derivation = $sgbd->add_data
518 ( "sml_source_derivations",
520 ( "id_source" => $id_source,
521 "derivation" => $derivation
524 if(isset($id_source_derivation))
526 { if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content))
527 { $id_source_derivation = false;
531 else $id_source_derivation = false;
532 return $id_source_derivation;
535 function set_source_derivation($id_source_derivation, $id_source, $derivation, $edit_content = "")
536 { $sgbd = $this->sgbd();
539 ( "sml_source_derivations",
540 $id_source_derivation,
542 ( "id_source" => $id_source,
543 "derivation" => $derivation
549 { if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content))
558 function del_source_derivation($id_source_derivation)
559 { if(($derivation = $this->source_derivation($id_source_derivation)) !== false)
560 { if(($derivations = $this->source_derivations(array("id_source" => $derivation["id_source"]))) !== false)
561 { $sgbd = $this->sgbd();
562 if(!$sgbd->del_data("sml_source_derivations", $id_source_derivation)) return false;
563 if(count($derivations) > 1)
564 { return $this->del_edit_derivation_content($derivation["id_source"], $id_source_derivation);
566 else return $this->del_edit_derivations($derivation["id_source"]);
572 function del_source_derivations($id_source)
573 { $sgbd = $this->sgbd();
574 $id_source_derivations = array();
575 if($rst = $sgbd->open_data("sml_source_derivations"))
576 { while($v_rst = $sgbd->fetch_data($rst))
578 { if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && $v_rst["id_source"] == $id_source)
579 { $id_source_derivations[] = $v_rst["id"];
582 else { $id_source_derivations = false; break; }
584 $sgbd->close_data($rst);
586 else $id_source_derivations = false;
587 if($id_source_derivations === false) return false;
588 if($id_source_derivations)
589 { foreach($id_source_derivations as $id_source_derivation)
590 { if(!$sgbd->del_data("sml_source_derivations", $id_source_derivation)) return false;
593 return $this->del_edit_derivations($id_source);
596 # ----------------------------------------------------------------------------------------
600 function source_reference($source)
601 { $reference = array();
602 if($source["reference"])
603 { if(!is_array($source["reference"]))
604 { $source["reference"] = array
605 ( "url" => $source["reference"],
607 "use_edit_content" => false
610 $reference_edit_file = $this->reference_edit_xml_path($source["id"]);
611 if(file_exists($reference_edit_file))
612 { if(($reference_edit_content = $this->get_edit_reference_content($source["id"])) !== false)
613 { if(($reference = $this->source_xml_read($source["reference"], $reference_edit_content)) !==false)
614 { $reference["xml"] = array
615 ( "url" => $source["reference"]["xml"]["url"],
616 "content" => $reference_edit_content,
617 "use_edit_content" => true
625 { if(($reference = $this->source_xml_read($source["reference"]["xml"]["url"])) ===false)
626 { $reference = $this->empty_source();
633 # ----------------------------------------------------------------------------------------
637 function source_documents($id_source)
638 { $sgbd = $this->sgbd();
639 $documents = array();
640 if($sgbd->data_exists("sml_sources/".$id_source))
641 { if($rst = $sgbd->open_data("sml_sources/".$id_source))
643 while($OK && ($document = $sgbd->fetch_data($rst)))
644 { if(isset($document)) $documents[$document["id"]] = $document;
647 $sgbd->close_data($rst);
650 if(!$OK) return false;
655 function add_source_document($id_source, $document)
656 { $sgbd = $this->sgbd();
657 if(!$sgbd->data_exists("sml_sources/".$id_source))
658 { if(!$sgbd->create_data("sml_sources/".$id_source)) return false;
661 ( !( $id_document = $sgbd->add_data
662 ( "sml_sources/".$id_source,
664 ( "nom" => $document["nom"],
665 "url" => $document["url"]
673 function del_source_documents($id_source)
674 { $sgbd = $this->sgbd();
675 if($sgbd->data_exists("sml_sources/".$id_source))
676 { if(!$sgbd->remove_data("sml_sources/".$id_source)) return false;
681 # ----------------------------------------------------------------------------------------
682 # compositions de sources
685 function source_compositions($params)
686 { $id_source = isset($params["id_source"]) ? $params["id_source"] : null;
687 $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
688 $sgbd = $this->sgbd();
690 $compositions = array();
691 if(isset($id_source))
692 { if($rst = $sgbd->open_data("sml_source_compositions"))
693 { while($v_rst = $sgbd->fetch_data($rst))
695 { if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]) && $v_rst["id_source"] == $id_source)
696 { if(!isset($compositions[$v_rst["id_source"]])) $compositions[$v_rst["id_source"]] = array();
697 $compositions[$v_rst["id_source"]][] = $v_rst["id_composition"];
700 else { $compositions = false; break; }
702 $sgbd->close_data($rst);
704 else $compositions = false;
706 elseif(isset($id_composition))
707 { if($id_composition)
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_composition"] == $id_composition)
712 { if(!isset($compositions[$v_rst["id_composition"]])) $compositions[$v_rst["id_composition"]] = array();
713 $compositions[$v_rst["id_composition"]][] = $v_rst["id_source"];
716 else { $compositions = false; break; }
718 $sgbd->close_data($rst);
720 else $compositions = false;
723 { if($rst = $sgbd->open_data("sml_source_compositions"))
724 { while($v_rst = $sgbd->fetch_data($rst))
726 { if(isset($v_rst["id_source"])) $compositions[$v_rst["id_source"]] = true;
728 else { $compositions = false; break; }
730 $sgbd->close_data($rst);
732 else $compositions = false;
735 return $compositions;
738 function set_source_composition($id_source, $id_composition)
739 { $sgbd = $this->sgbd();
742 ( "sml_source_compositions",
744 ( "id_source" => $id_source,
745 "id_composition" => $id_composition
748 ) return $this->set_source_xml($id_composition);
752 function del_source_compositions($params)
754 $id_source = isset($params["id_source"]) ? $params["id_source"] : null;
755 $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
756 $to_delete = array();
757 $to_update = array();
758 if(isset($id_composition)) $to_update[$id_composition] = true;
759 $sgbd = $this->sgbd();
760 if($rst = $sgbd->open_data("sml_source_compositions"))
761 { while($v_rst = $sgbd->fetch_data($rst))
763 { if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && isset($v_rst["id_composition"]))
765 ( (isset($id_source) && $v_rst["id_source"] == $id_source)
766 || (isset($id_composition) && $v_rst["id_composition"] == $id_composition)
767 ) $to_delete[] = $v_rst["id"];
768 if(isset($id_source) && $v_rst["id_source"] == $id_source) $to_update[$v_rst["id_composition"]] = true;
771 else { $OK = false; break; }
773 $sgbd->close_data($rst);
776 if(!$OK) return false;
777 foreach($to_delete as $id_source_composition)
778 { if(!$sgbd->del_data("sml_source_compositions", $id_source_composition)) return false;
780 foreach($to_update as $id_source_xml => $delete)
781 { if(!$this->set_source_xml($id_source_xml)) return false;
786 function has_sources($id_composition)
787 { $sgbd = $this->sgbd();
790 if($rst = $sgbd->open_data("sml_source_compositions"))
791 { while($v_rst = $sgbd->fetch_data($rst))
793 { if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]))
794 { if($v_rst["id_composition"] == $id_composition)
801 { $has_sources = false;
805 $sgbd->close_data($rst);
807 else $has_sources = false;
811 function source_ariane($id_source, $ariane = array())
812 { if(($compositions = $this->source_compositions(array("id_source" => $id_source))) !== false)
813 { if(isset($compositions[$id_source]) && $compositions[$id_source])
814 { foreach($compositions[$id_source] as $id_composition)
815 { if(($ariane = $this->source_ariane($id_composition, $ariane)) !== false)
816 { if(($ariane[$id_composition] = $this->source($id_composition)) !== false)
819 else $ariane = false;
821 else $ariane = false;
826 else $ariane = false;