marche avec MySql, SQLite ou XML sourceml.1.1.0
authordj3c1t <dj3c1t@free.fr>
Tue, 29 Oct 2013 19:21:47 +0000 (20:21 +0100)
committerdj3c1t <dj3c1t@free.fr>
Tue, 29 Oct 2013 19:21:47 +0000 (20:21 +0100)
app/controllers/users/sources_invitations.php
app/data/modules/share/sml_data_sources_cache.php
app/data/modules/sql/sml_data_source_groupes.php
app/data/modules/xml/sml_data_authors.php [new file with mode: 0644]
app/data/modules/xml/sml_data_licences.php [new file with mode: 0644]
app/data/modules/xml/sml_data_source_groupes.php [new file with mode: 0644]
app/data/modules/xml/sml_data_sources.php [new file with mode: 0644]
app/data/modules/xml/sml_data_sources_cache_db.php [new file with mode: 0644]
mw_sourceml.php

index 247c00b..6691283 100644 (file)
         $env->erreur("Impossible de lire la liste des groupes");
         return;
       }
-      $groupes_in = "";
-      foreach($groupes["list"] as $id_groupe => $groupe) $groupes_in .= ($groupes_in ? "," : "").$id_groupe;
-      $invitations = array("list" => array(), "total" => 0);
-      if($groupes_in){
-        if(
-          (
-            $invitations = $data->list_sml_sources_invitations(
-              array(
-                "index_name" => "id",
-                "where"=> "id_author IN(".$groupes_in.") OR id_user=".$this->user["id"],
-                "order_by" => "date_invitation",
-                "order" => "DESC"
-              )
-            )
-          ) === false
-        ){
-          $env->erreur("Impossible de lire la liste des invitations");
-          return;
-        }
+      if(($invitations = $data->sources_invitations($groupes["list"], $this->user["id"])) === false){
+        $env->erreur("Impossible de lire la liste des invitations");
+        return;
       }
       foreach($invitations["list"] as $id_invitation => $invitation){
         if(!($user = $data->user_by_id($invitation["id_user"]))){
         $env->erreur("Impossible de lire la liste des groupes");
         return;
       }
-      if(($invitations = $data->list_sml_sources_invitations(array("index_name" => "id", "where"=> "id_source=".$source["id"]))) === false){
+      if(
+        (
+          $invitations = $data->list_sml_sources_invitations(
+            array(
+              "index_name" => "id",
+              "filters" => array(
+                array("id_source", "eq", $source["id"])
+              )
+            )
+          )
+        ) === false
+      ){
         $env->erreur("Impossible de lire la liste des invitations pour cette source");
         return;
       }
index 7232c6c..aa65109 100644 (file)
      * vide le cache
      *
      */
-    function empty_source_cache()
-    { $OK = true;
-      if(($cache = $this->source_cache_db()) !== false)
-      { foreach($cache as $id_cache => $cache_infos)
-        { if(!$this->del_source_cache($id_cache, $cache_infos["id_source"]))
-          { $OK = false;
+    function empty_source_cache(){
+      $OK = true;
+      if(($cache = $this->source_cache_db()) !== false){
+        foreach($cache as $id_cache => $cache_infos){
+          if(!$this->del_source_cache($id_cache, $cache_infos["id_source"])){
+            $OK = false;
             $break;
           }
         }
index e2fb23f..0ada4f0 100644 (file)
       return $permissions;
     }
 
+// --------------------------------------------------------------------
+
+    function sources_invitations($groupes, $id_user){
+      $groupes_in = "";
+      foreach($groupes as $id_groupe => $groupe) $groupes_in .= ($groupes_in ? "," : "").$id_groupe;
+      return $this->list_sml_sources_invitations(
+        array(
+          "index_name" => "id",
+          "where"=> ($groupes_in ? "id_author IN(".$groupes_in.") OR " : "")."id_user=".$id_user,
+          "order_by" => "date_invitation",
+          "order" => "DESC"
+        )
+      );
+    }
+
   }
 
 ?>
\ No newline at end of file
diff --git a/app/data/modules/xml/sml_data_authors.php b/app/data/modules/xml/sml_data_authors.php
new file mode 100644 (file)
index 0000000..247bc3d
--- /dev/null
@@ -0,0 +1,165 @@
+<?php
+
+  class sml_data_authors extends mw_data
+  {
+
+    var $groupes;
+
+    # ----------------------------------------------------------------------------------------
+    #                                                                                  groupes
+    #
+
+    function groupes($id_user = null, $start = null, $alpha = null)
+    { $sgbd = $this->sgbd();
+      $env = $this->env();
+      $groupes = array("list" => array(), "total" => 0);
+      if(true || isset($id_user))
+      { if($rst = $sgbd->open_data("sml_authors"))
+        { while($v_rst = $sgbd->fetch_data($rst))
+          { if(isset($v_rst))
+            { if(!isset($alpha) || (isset($v_rst["nom"]) && substr($v_rst["nom"], 0, 1) == $alpha))
+              { if(!isset($id_user) || (isset($v_rst["id_user"]) && $v_rst["id_user"] == $id_user))
+                { $groupes["total"]++;
+                  $groupes["list"][$v_rst["id"]] = $v_rst;
+                }
+              }
+            }
+            else
+            { $groupes = false;
+              break;
+            }
+          }
+          $sgbd->close_data($rst);
+          if($groupes !== false)
+          { $n = -1;
+            foreach($groupes["list"] as $id_groupe => $groupe)
+            { $n++;
+              if(isset($start) && $env->config("max_list") && ($n < $start || $n >= ($start + $env->config("max_list"))))
+              { unset($groupes["list"][$id_groupe]);
+              }
+              else
+              { $groupes["list"][$id_groupe]["image_uri"] =
+                ( $groupe["image"] ?
+                    $env->path("content")."uploads/".$groupe["image"]
+                  : ""
+                );
+              }
+            }
+          }
+        }
+        else $groupes = false;
+      }
+      return $groupes;
+    }
+
+    function groupe($id)
+    { if(!isset($this->groupes)) $this->groupes = array();
+      if(isset($this->groupes[$id])) return $this->groupes[$id];
+      $sgbd = $this->sgbd();
+      $env = $this->env();
+      if(($groupe = $sgbd->get_data("sml_authors", $id)) !== null)
+      { $groupe["image_uri"] =
+        ( $groupe["image"] ?
+            $env->path("content")."uploads/".$groupe["image"]
+          : ""
+        );
+      }
+      else $groupe = false;
+      if($groupe != false) $this->groupes[$id] = $groupe;
+      return $groupe;
+    }
+
+    function groupe_exists($nom, $other_than_id = null)
+    { $sgbd = $this->sgbd();
+      $EXISTS = 0;
+      if($rst = $sgbd->open_data("sml_authors"))
+      { while($v_rst = $sgbd->fetch_data($rst))
+        { if(isset($v_rst))
+          { if(isset($v_rst["nom"]) && $v_rst["nom"] == $nom)
+            { if(isset($other_than_id))
+              { if($v_rst["id"] != $other_than_id) $EXISTS++;
+              }
+              else $EXISTS++;
+            }
+          }
+          else
+          { $EXISTS = false;
+            break;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      return $EXISTS;
+    }
+
+    function add_groupe($id_user, $nom, $image, $description, $email, $contact_form, $captcha)
+    { $sgbd = $this->sgbd();
+      return $sgbd->add_data
+      ( "sml_authors",
+        array
+        ( "id_user" => $id_user,
+          "nom" => $nom,
+          "image" => $image,
+          "description" => $description,
+          "email" => $email,
+          "contact_form" => $contact_form,
+          "captcha" => $captcha
+        )
+      );
+    }
+
+    function set_groupe($id, $nom, $image, $description, $email, $contact_form, $captcha)
+    { if(($groupe = $this->groupe($id)) !== false)
+      { $sgbd = $this->sgbd();
+        if($nom != $groupe["nom"])
+        { $groupe["nom"] = $nom;
+          if(!$this->maj_source_xml_groupe($groupe)) return false;
+        }
+        return $sgbd->set_data
+        ( "sml_authors",
+          $id,
+          array
+          ( "id_user" => $groupe["id_user"],
+            "nom" => $nom,
+            "image" => $image,
+            "description" => $description,
+            "email" => $email,
+            "contact_form" => $contact_form,
+            "captcha" => $captcha
+          )
+        );
+      }
+      return false;
+    }
+
+    function del_groupe($id)
+    { $OK = true;
+      $USED = false;
+      $sgbd = $this->sgbd();
+      $env = $this->env();
+      if($rst = $sgbd->open_data("sml_sources_authors"))
+      { while($source_author = $sgbd->fetch_data($rst))
+        { if(isset($source_author))
+          { if($source_author["id_author"] == $id)
+            { $USED = true;
+              break;
+            }
+          }
+          else
+          { $OK = false;
+            break;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $OK = false;
+      if($OK)
+      { if($USED) return 1;
+        return $sgbd->del_data("sml_authors", $id) ? true : false;
+      }
+      return false;
+    }
+
+  }
+
+?>
\ No newline at end of file
diff --git a/app/data/modules/xml/sml_data_licences.php b/app/data/modules/xml/sml_data_licences.php
new file mode 100644 (file)
index 0000000..0953652
--- /dev/null
@@ -0,0 +1,96 @@
+<?php
+
+  class sml_data_licences extends mw_data
+  {
+
+    function licences($start = null)
+    { $sgbd = $this->sgbd();
+      $env = $this->env();
+      $licences = array("list" => array(), "total" => 0);
+      if($rst = $sgbd->open_data("sml_licences"))
+      { while($v_rst = $sgbd->fetch_data($rst))
+        { if(isset($v_rst))
+          { $licences["total"]++;
+            if(!isset($start) || !$env->config("max_list") || ($licences["total"] > $start && $licences["total"] < ($start + $env->config("max_list"))))
+            { $licences["list"][$v_rst["id"]] = $v_rst;
+            }
+          }
+          else
+          { $licences = false;
+            break;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $licences = false;
+      return $licences;
+    }
+
+    function licence($id)
+    { if($id == 0) return $id;
+      $sgbd = $this->sgbd();
+      return $sgbd->get_data("sml_licences", $id);
+    }
+
+    function add_licence($nom, $url)
+    { $sgbd = $this->sgbd();
+      return $sgbd->add_data
+      ( "sml_licences",
+        array
+        ( "nom" => $nom,
+          "url" => $url
+        )
+      );
+    }
+
+    function set_licence($id, $nom, $url)
+    { if(($licence = $this->licence($id)) !== false)
+      { $sgbd = $this->sgbd();
+        if($nom != $licence["nom"] || $url != $licence["url"])
+        { $licence["nom"] = $nom;
+          $licence["url"] = $url;
+          if(!$this->maj_source_xml_licence($licence)) return false;
+        }
+        return $sgbd->set_data
+        ( "sml_licences",
+          $id,
+          array
+          ( "nom" => $nom,
+            "url" => $url
+          )
+        );
+      }
+      return false;
+    }
+
+    function del_licence($id)
+    { $OK = true;
+      $USED = false;
+      $sgbd = $this->sgbd();
+      $env = $this->env();
+      if($rst = $sgbd->open_data("sml_sources"))
+      { while($source = $sgbd->fetch_data($rst))
+        { if(isset($source))
+          { if($source["licence"] == $id)
+            { $USED = true;
+              break;
+            }
+          }
+          else
+          { $OK = false;
+            break;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $OK = false;
+      if($OK)
+      { if($USED) return 1;
+        return $sgbd->del_data("sml_licences", $id) ? true : false;
+      }
+      return false;
+    }
+
+  }
+
+?>
\ No newline at end of file
diff --git a/app/data/modules/xml/sml_data_source_groupes.php b/app/data/modules/xml/sml_data_source_groupes.php
new file mode 100644 (file)
index 0000000..f85b617
--- /dev/null
@@ -0,0 +1,343 @@
+<?php
+
+  class sml_data_source_groupes extends mw_data{
+
+    function init_groupe_status(){
+      return true;
+    }
+
+    function source_groupes($id_source)
+    { $env = $this->env();
+      $sgbd = $this->sgbd();
+      $groupes = array();
+      $groupes_status = array();
+      if($rst = $sgbd->open_data("sml_sources_authors"))
+      { while($v_rst = $sgbd->fetch_data($rst))
+        { if(isset($v_rst) && isset($v_rst["id_author"]) && isset($v_rst["id_source"]) && isset($v_rst["id_sources_access"]))
+          { if($v_rst["id_source"] == $id_source) $groupes_status[$v_rst["id_author"]] = $v_rst["id_sources_access"];
+          }
+          else
+          { $groupes_status = false;
+            break;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $groupes_status = false;
+      if($groupes_status === false) return false;
+      if($rst = $sgbd->open_data("sml_authors"))
+      { while($v_rst = $sgbd->fetch_data($rst))
+        { if(isset($v_rst) && isset($v_rst["id"]))
+          { if(isset($groupes_status[$v_rst["id"]]))
+            { $groupes[$v_rst["id"]] = $v_rst;
+              $groupes[$v_rst["id"]]["id_groupe_status"] = $groupes_status[$v_rst["id"]];
+              $groupes[$v_rst["id"]]["image_uri"] =
+              ( $v_rst["image"] ?
+                $env->path("content")."uploads/".$v_rst["image"]
+                : ""
+              );
+            }
+          }
+          else
+          { $groupes = false;
+            break;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $groupes = false;
+      return $groupes;
+    }
+
+    function groupes_sources($params){
+      if(!isset($params["groupes"])) return false;
+      if(!isset($params["id_source_access"])) return false;
+      $satisfy_all_groupes_access = true;
+      if(isset($params["satisfy_all_groupes_access"])){
+        $satisfy_all_groupes_access = $params["satisfy_all_groupes_access"] ? true : false;
+      }
+      $source_access_ids = array();
+      if($params["id_source_access"] == $this->id_groupe_status_contributeur()){
+        $source_access_ids[$this->id_groupe_status_contributeur()] = true;
+        $source_access_ids[$this->id_groupe_status_editeur()] = true;
+        $source_access_ids[$this->id_groupe_status_admin()] = true;
+      }
+      elseif($params["id_source_access"] == $this->id_groupe_status_editeur()){
+        $source_access_ids[$this->id_groupe_status_editeur()] = true;
+        $source_access_ids[$this->id_groupe_status_admin()] = true;
+      }
+      elseif($params["id_source_access"] == $this->id_groupe_status_admin()){
+        $source_access_ids[$this->id_groupe_status_admin()] = true;
+      }
+      if(!$source_access_ids) return false;
+      $sources = array();
+      if(!($groupes = $params["groupes"])) return $sources;
+      $author_ids = array();
+      foreach($groupes as $i => $groupe) $author_ids[$groupe["id"]] = true;
+      $sgbd = $this->sgbd();
+      $_sources = array();
+      if($rst = $sgbd->open_data("sml_sources_authors")){
+        while(($_sources !== false) && $v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst)){
+            if(
+                  isset($v_rst["id"])
+              &&  isset($v_rst["id_author"])
+              &&  isset($v_rst["id_source"])
+              &&  isset($v_rst["id_sources_access"])
+            ){
+              if(
+                    isset($source_access_ids[$v_rst["id_sources_access"]])
+                &&  isset($author_ids[$v_rst["id_author"]])
+              ){
+                $_sources[$v_rst["id_source"]] = array(
+                  "id_source" => $v_rst["id_source"],
+                  "id_author" => $v_rst["id_author"],
+                );
+              }
+            }
+          }
+          else $_sources = false;
+        }
+        $sgbd->close_data($rst);
+      }
+      else $_sources = false;
+      if($_sources === false) return false;
+      if($rst = $sgbd->open_data("sml_sources")){
+        while(($sources !== false) && $v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst) && isset($v_rst["id"])){
+            if(isset($_sources[$v_rst["id"]])){
+              if(!isset($sources[$v_rst["id"]])){
+                $sources[$v_rst["id"]] = $v_rst;
+                $sources[$v_rst["id"]]["authors"] = array();
+              }
+              $sources[$v_rst["id"]]["authors"][] = $_sources[$v_rst["id"]]["id_author"];
+            }
+          }
+          else $sources = false;
+        }
+        $sgbd->close_data($rst);
+      }
+      else $sources = false;
+      if($_sources === false) return false;
+      foreach($sources as $id_source => $source){
+        if($satisfy_all_groupes_access){
+          if(count($source["authors"]) < count($groupes)){
+            unset($sources[$id_source]);
+            continue;
+          }
+        }
+        if(!($sources[$id_source] = $this->load_source($source))) return false;
+      }
+      return $sources;
+    }
+
+    function source_has_groupe($id_source, $id_groupe){
+      $sgbd = $this->sgbd();
+      $HAS_THIS_GROUP = 0;
+      if($rst = $sgbd->open_data("sml_sources_authors")){
+        while(($HAS_THIS_GROUP !== false) && $v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst) && isset($v_rst["id"]) && isset($v_rst["id_author"]) && isset($v_rst["id_source"])){
+            if(
+                  ($v_rst["id_source"] == $id_source)
+              &&  ($v_rst["id_author"] == $id_groupe)
+            ){
+              $HAS_THIS_GROUP++;
+            }
+          }
+          else $HAS_THIS_GROUP = false;
+        }
+        $sgbd->close_data($rst);
+      }
+      else $HAS_THIS_GROUP = false;
+      return $HAS_THIS_GROUP;
+    }
+
+    function add_source_groupe($id_source, $id_groupe, $id_groupe_status)
+    { $sgbd = $this->sgbd();
+      if
+      ( $sgbd->add_data
+        ( "sml_sources_authors",
+          array
+          ( "id_source" => $id_source,
+            "id_author" => $id_groupe,
+            "id_sources_access" => $id_groupe_status
+          )
+        )
+      )
+      { return true;
+      }
+      return false;
+    }
+
+    function set_source_groupe($id, $id_groupe_status)
+    { if(($groupe = $this->groupe($id)) !== false)
+      { if
+        ( $sgbd->set_data
+          ( "sml_sources_authors",
+            $id,
+            array(
+              "id_sources_access" => $id_groupe_status
+            )
+          )
+        )
+        { return true;
+        }
+      }
+      return false;
+    }
+
+    function del_source_groupes($id_source){
+      $sgbd = $this->sgbd();
+      $OK = true;
+      if($rst = $sgbd->open_data("sml_sources_authors")){
+        while($OK && $v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst) && isset($v_rst["id"]) && isset($v_rst["id_source"])){
+            if($v_rst["id_source"] == $id_source) if(!$sgbd->del_data("sml_sources_authors", $v_rst["id"])) $OK = false;
+          }
+          else $OK = false;
+        }
+        $sgbd->close_data($rst);
+      }
+      else $OK = false;
+      return $OK;
+    }
+
+    function del_source_groupe($id_source, $id_author){
+      $sgbd = $this->sgbd();
+      $OK = true;
+      if($rst = $sgbd->open_data("sml_sources_authors")){
+        while($OK && $v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst) && isset($v_rst["id"]) && isset($v_rst["id_author"]) && isset($v_rst["id_source"])){
+            if(
+                  ($v_rst["id_source"] == $id_source)
+              &&  ($v_rst["id_author"] == $id_author)
+              &&  !$sgbd->del_data("sml_sources_authors", $v_rst["id"])
+            ) $OK = false;
+          }
+          else $OK = false;
+        }
+        $sgbd->close_data($rst);
+      }
+      else $OK = false;
+      return $OK;
+    }
+
+// --------------------------------------------------------------------
+
+    function sources_access(){
+      $sgbd = $this->sgbd();
+      $sources_access = array();
+      $sgbd = $this->sgbd();
+      $OK = true;
+      if($rst = $sgbd->open_data("sml_sources_access")){
+        while($OK && $v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst) && isset($v_rst["id"])){
+            $sources_access[$v_rst["id"]] = $v_rst;
+          }
+          else $OK = false;
+        }
+        $sgbd->close_data($rst);
+      }
+      else $OK = false;
+      return $sources_access;
+    }
+
+    function id_groupe_status_admin()        { return 1; }
+    function id_groupe_status_editeur()      { return 2; }
+    function id_groupe_status_contributeur() { return 3; }
+
+    function get_admin_groupe($groupes)
+    { $groupe = array();
+      if(is_array($groupes)) foreach($groupes as $source_groupe)
+      { if($source_groupe["id_groupe_status"] == $this->id_groupe_status_admin())
+        { $groupe = $source_groupe;
+          break;
+        }
+      }
+      return $groupe;
+    }
+
+    function get_editor_groupes($groupes){
+      $editor_groupes = array();
+      if(is_array($groupes)) foreach($groupes as $source_groupe){
+        if(
+              $source_groupe["id_groupe_status"] == $this->id_groupe_status_admin()
+          ||  $source_groupe["id_groupe_status"] == $this->id_groupe_status_editeur()
+        ){
+          $editor_groupes[] = $source_groupe;
+        }
+      }
+      return $editor_groupes;
+    }
+
+    function source_permissions($source, $id_user)
+    { $permissions = array
+      ( "admin" => false,
+        "editeur" => false,
+        "contributeur" => false
+      );
+      foreach($source["groupes"] as $id_groupe => $source_groupe)
+      { if($source_groupe["id_user"] == $id_user)
+        { if($source_groupe["id_groupe_status"] == $this->id_groupe_status_admin())
+          { $permissions["admin"] = true;
+            $permissions["editeur"] = true;
+            $permissions["contributeur"] = true;
+          }
+          elseif($source_groupe["id_groupe_status"] == $this->id_groupe_status_editeur())
+          { $permissions["editeur"] = true;
+            $permissions["contributeur"] = true;
+          }
+          elseif($source_groupe["id_groupe_status"] == $this->id_groupe_status_contributeur())
+          { $permissions["contributeur"] = true;
+          }
+        }
+      }
+      return $permissions;
+    }
+
+// --------------------------------------------------------------------
+
+    function sources_invitations($groupes, $id_user){
+      $sgbd = $this->sgbd();
+      $invitations = array("list" => array(), "total" => 0);
+      $groupes_in = array();
+      foreach($groupes as $id_groupe => $groupe) $groupes_in[$id_groupe] = true;
+      if($rst = $sgbd->open_data("sml_sources_invitations")){
+        while($v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst)){
+            if(!isset($v_rst["id"])) continue;
+            $MATCH = false;
+            if($groupes_in){
+              if(
+                    isset($v_rst["id_author"])
+                &&  isset($groupes_in[$v_rst["id_author"]])
+              ){
+                $MATCH = true;
+              }
+            }
+            if(
+                  !$MATCH
+              &&  isset($v_rst["id_user"])
+              &&  $id_user == $v_rst["id_user"]
+            ){
+              $MATCH = true;
+            }
+            if($MATCH){
+              $invitations["list"][$v_rst["id"]] = $v_rst;
+              $invitations["total"]++;
+            }
+          }
+          else{
+            $invitations = false;
+            break;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $invitations = false;
+      return $invitations;
+    }
+
+  }
+
+?>
\ No newline at end of file
diff --git a/app/data/modules/xml/sml_data_sources.php b/app/data/modules/xml/sml_data_sources.php
new file mode 100644 (file)
index 0000000..b4a4d1b
--- /dev/null
@@ -0,0 +1,832 @@
+<?php
+
+  class sml_data_sources extends mw_data{
+
+    var $status;
+
+    # ----------------------------------------------------------------------------------------
+    #                                                                         status de source
+    #
+
+    function source_status()
+    { if(!isset($this->status)) $this->status = $this->init_sources_status();
+      return $this->status;
+    }
+
+    function init_sources_status()
+    { $sgbd = $this->sgbd();
+      $env = $this->env();
+      $status = array();
+      if($rst = $sgbd->open_data("sml_classes"))
+      { while($v_rst = $sgbd->fetch_data($rst))
+        { if(isset($v_rst))
+          { $status[$v_rst["id"]] = $v_rst;
+          }
+          else
+          { $status = false;
+            break;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $status = false;
+      return $status;
+    }
+
+    # ----------------------------------------------------------------------------------------
+    #                                                                                  sources
+    #
+
+    function init_sources_table(){
+      return true;
+    }
+
+    function sources($params)
+    { $sgbd = $this->sgbd();
+      $env = $this->env();
+
+      // -------------------------------------------------------------------------------
+      //                                                                         filtres
+
+      $start = isset($params["start"]) ? $params["start"] : null;
+      $id_user = isset($params["id_user"]) ? $params["id_user"] : null;
+      $id_groupe = isset($params["id_groupe"]) ? $params["id_groupe"] : null;
+      $status = isset($params["status"]) ? $params["status"] : null;
+      $id_source = isset($params["id_source"]) ? $params["id_source"] : null;
+      $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
+      $id_source_derivation = isset($params["id_source_derivation"]) ? $params["id_source_derivation"] : null;
+      $id_licence = isset($params["id_licence"]) ? $params["id_licence"] : null;
+      $order_by = isset($params["order_by"]) ? $params["order_by"] : "ordre";
+      $order = isset($params["order"]) ? $params["order"] : "ASC";
+
+      // -------------------------------------------------------------------------------
+      //                                                 infos pour verifier les filtres
+
+      if(isset($id_user) || isset($id_groupe))
+      { $in_source_ids = array();
+        $in_groupe_ids = array();
+        if(isset($id_groupe)) $in_groupe_ids[$id_groupe] = true;
+        else
+        { if(($groupes = $this->groupes($id_user)) !== false)
+          { foreach($groupes["list"] as $id_groupe => $groupe) $in_groupe_ids[$id_groupe] = true;
+          }
+          else return false;
+        }
+        $OK = true;
+        if($rst = $sgbd->open_data("sml_sources_authors"))
+        { while($OK && $v_rst = $sgbd->fetch_data($rst))
+          { if(isset($v_rst) && isset($v_rst["id_source"]) && isset($v_rst["id_author"]))
+            { if(isset($in_groupe_ids[$v_rst["id_author"]])) $in_source_ids[$v_rst["id_source"]] = true;
+            }
+            else $OK = false;
+          }
+          $sgbd->close_data($rst);
+        }
+        else $OK = false;
+        if(!$OK) return false;
+      }
+
+      $compositions = array();
+      if(isset($id_source))
+      { if(($compositions = $this->source_compositions(array("id_source" => $id_source))) === false)
+        { return false;
+        }
+      }
+      elseif(isset($id_composition))
+      { if(($compositions = $this->source_compositions(array("id_composition" => $id_composition))) === false)
+        { return false;
+        }
+      }
+
+      $derivations = array();
+      if(isset($id_source_derivation))
+      { if(($derivations = $this->source_derivations($id_source_derivation)) === false)
+        { return false;
+        }
+      }
+
+      // -------------------------------------------------------------------------------
+      //                                                               boucle principale
+
+      $sources = array("list" => array(), "total" => 0);
+      $res = array();
+      if($rst = $sgbd->open_data("sml_sources"))
+      { while($v_rst = $sgbd->fetch_data($rst))
+        { if(isset($v_rst)) $res[$v_rst["id"]] = $v_rst;
+          else
+          { $res = false;
+            break;
+          }
+        }
+        $sgbd->close_data($rst);
+        if($res !== false)
+        { $res = $this->ordonne($res, $order_by, $order);
+
+          foreach($res as $id_res => $v_rst)
+          { // ------------------------------- par defaut
+            $MATCH = true;
+
+            // ------------------------------- filtre sur id_user et id_groupe
+
+            if(isset($in_source_ids))
+            { $MATCH = isset($in_source_ids[$v_rst["id"]]) && $in_source_ids[$v_rst["id"]];
+            }
+
+            // ------------------------------- filtre sur status
+            if($MATCH)
+            { $MATCH = !isset($status) || (isset($v_rst["id_class"]) && $v_rst["id_class"] == $status);
+            }
+
+            // -------------------------------- filtre sur id_source
+            if($MATCH)
+            { if(isset($id_source))
+              { $MATCH = false;
+                if($compositions && is_array($compositions[$id_source]))
+                { foreach($compositions[$id_source] as $id_composition)
+                  { if(isset($v_rst["id"]) && $v_rst["id"] == $id_composition)
+                    { $MATCH = true;
+                      break;
+                    }
+                  }
+                }
+              }
+            }
+
+            // -------------------------------- filtre sur id_composition
+            if($MATCH)
+            { if(isset($id_composition))
+              { if($id_composition)
+                { $MATCH = false;
+                  if($compositions && is_array($compositions[$id_composition]))
+                  { foreach($compositions[$id_composition] as $_id_source)
+                    { if(isset($v_rst["id"]) && $v_rst["id"] == $_id_source)
+                      { $MATCH = true;
+                        break;
+                      }
+                    }
+                  }
+                }
+                else
+                { if($compositions)
+                  { if(isset($compositions[$v_rst["id"]])) $MATCH = false;
+                  }
+                }
+              }
+            }
+
+            // -------------------------------- filtre sur id_source_derivation
+            if($MATCH)
+            { if(isset($id_source_derivation))
+              { $MATCH = false;
+                if($derivations && is_array($derivations[$id_source_derivation]))
+                { foreach($derivations[$id_source_derivation] as $_id_derivation)
+                  { if(isset($v_rst["id"]) && $v_rst["id"] == $_id_derivation)
+                    { $MATCH = true;
+                      break;
+                    }
+                  }
+                }
+              }
+            }
+
+            // -------------------------------- filtre sur la licence
+            if($MATCH)
+            { if(isset($id_licence))
+              { $MATCH = false;
+                if(isset($v_rst["licence"]) && $v_rst["licence"] == $id_licence) $MATCH = true;
+              }
+            }
+
+            // -------------------------------- filtre sur quantite de resultats
+            if($MATCH)
+            { $sources["total"]++;
+              $MATCH = !isset($start) || !$env->config("max_list") || ($sources["total"] > $start && $sources["total"] <= ($start + $env->config("max_list")));
+            }
+
+            // -------------------------------- ajout aux resultats si MATCH
+            if($MATCH) $sources["list"][$v_rst["id"]] = $v_rst;
+          }
+
+        }
+      }
+      else return false;
+      foreach($sources["list"] as $id_source => $source){
+        if(($sources["list"][$source["id"]] = $this->load_source($source)) === false) return false;
+      }
+      return $sources;
+    }
+
+    function source($id, $load = false)
+    { $sgbd = $this->sgbd();
+      if(($source = $sgbd->get_data("sml_sources", $id)) !== false){
+        $source["status"] = $source["id_class"];
+        $source = $load ? $this->load_source($source) : $this->get_source_from_v_rst($source);
+        return $source;
+      }
+      return false;
+    }
+
+    function get_source_from_v_rst($v_rst)
+    { $sgbd = $this->sgbd();
+      $source = $this->empty_source();
+      foreach($v_rst as $rst_key => $rst_value) $source[$rst_key] = $rst_value;
+      $source["status"] = $source["id_class"];
+      if($source["reference"])
+      { if(!is_array($source["reference"]))
+        { $reference_url = $source["reference"];
+          $source["reference"] = $this->empty_source();
+          $source["reference"]["xml"]["url"] = $reference_url;
+        }
+      }
+      else
+      { if(!is_array($source["licence"]))
+        { $source["licence"] = array
+          ( "id" => $source["licence"]
+          );
+        }
+      }
+      return $source;
+    }
+
+    function load_source($source){
+      $env = $this->env();
+      if(!isset($source["id"])) return false;
+      $source = $this->get_source_from_v_rst($source);
+      $source["xml"] = array
+      ( "url" => $this->source_xml_url($source["id"]),
+        "content" => $this->get_source_xml($source["id"])
+      );
+      $source["documents"] = array();
+      if(($source["groupes"] = $this->source_groupes($source["id"])) === false) return false;
+      $source["image_uri"] =
+      ( isset($source["image"]) && $source["image"] ?
+        $env->path("content")."uploads/".$source["image"]
+        : ""
+      );
+      if(($source["has_sources"] = $this->has_sources($source["id"])) === false) return false;
+      if(($source["has_derivations"] = $this->source_derivations(array("derivation" => $source["id"]))) === false) return false;
+      if(($source["derivations"] = $this->source_derivations(array("id_source" => $source["id"]))) === false) return false;
+      if(($source["reference"] = $this->source_reference($source)) === false) return false;
+      if($user = $env->user()){
+        if(($source["permissions"] = $this->source_permissions($source, $user["id"])) === false) return false;
+      }
+      if(!$source["reference"])
+      { if(($source["documents"] = $this->source_documents($source["id"])) === false) return false;
+      }
+      return $source;
+    }
+
+    function add_source
+    ( $groupes,
+      $titre,
+      $status,
+      $licence,
+      $documents = array(),
+      $reference = array(),
+      $derivations = array(),
+      $infos = array()
+    )
+    { $sgbd = $this->sgbd();
+      $source = array
+      ( "id_class" => $status,
+        "reference" => $reference ? $reference["xml"]["url"] : null,
+        "titre" => $reference ? null : $titre,
+        "licence" => $reference ? null : $licence,
+        "date_creation" => isset($infos["date_creation"]) ? $infos["date_creation"] : null,
+        "date_inscription" => isset($infos["date_inscription"]) ? $infos["date_inscription"] : date("Y-m-d")
+      );
+      foreach($infos as $key => $value) if(!isset($source[$key])) $source[$key] = $value;
+      $id = $sgbd->add_data("sml_sources", $source);
+      if(!isset($id)) return false;
+      foreach($groupes as $id_groupe => $groupe)
+      { if($groupe["id"] && $groupe["id_groupe_status"])
+        { if(!$this->add_source_groupe($id, $groupe["id"], $groupe["id_groupe_status"])) return false;
+        }
+        else return false;
+      }
+      foreach($derivations as $source_derivation)
+      { if
+        ( ( $id_source_derivation = $this->add_source_derivation
+            ( $id,
+              $source_derivation["xml"]["url"],
+              $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
+            )
+          ) === false
+        )
+        { return false;
+        }
+      }
+      if($reference)
+      { if($reference["xml"]["use_edit_content"])
+        { if(!$this->set_edit_reference_content($id, $reference["xml"]["content"]))
+          { return false;
+          }
+        }
+      }
+      else
+      { foreach($documents as $document)
+        { if(!$this->add_source_document($id, $document)) return false;
+        }
+      }
+      if(!$this->set_source_xml($id)) return false;
+      return $id;
+    }
+
+    function set_source
+    ( $id,
+      $groupes,
+      $titre,
+      $status,
+      $licence,
+      $documents = array(),
+      $reference = array(),
+      $derivations = array(),
+      $infos = array()
+    )
+    { if($source = $this->source($id))
+      { $sgbd = $this->sgbd();
+        $source["reference"] = $reference ? $reference["xml"]["url"] : null;
+        $source["titre"] = $reference ? null : $titre;
+        $source["licence"] = $reference ? null : $licence;
+        $source["date_creation"] = isset($infos["date_creation"]) ? $infos["date_creation"] : null;
+        foreach($infos as $key => $value) $source[$key] = $value;
+        if(!$sgbd->set_data("sml_sources", $id, $source)) return false;
+        if(!$this->del_source_groupes($id)) return false;
+        foreach($groupes as $id_groupe => $groupe)
+        { if($groupe["id"] && $groupe["id_groupe_status"])
+          { if(!$this->add_source_groupe($id, $groupe["id"], $groupe["id_groupe_status"])) return false;
+          }
+          else return false;
+        }
+        if(!$this->del_source_derivations($id)) return false;
+        if(!$this->del_edit_reference_content($id)) return false;
+        if(!$this->del_source_documents($id)) return false;
+        foreach($derivations as $source_derivation)
+        { if
+          ( ( $id_source_derivation = $this->add_source_derivation
+              ( $id,
+                $source_derivation["xml"]["url"],
+                $source_derivation["xml"]["use_edit_content"] ? $source_derivation["xml"]["content"] : ""
+              )
+            ) === false
+          )
+          { return false;
+          }
+        }
+        if($reference)
+        { if($reference["xml"]["use_edit_content"])
+          { if(!$this->set_edit_reference_content($id, $reference["xml"]["content"]))
+            { return false;
+            }
+          }
+        }
+        else
+        { foreach($documents as $document)
+          { if(!$this->add_source_document($id, $document)) return false;
+          }
+        }
+        if(!$this->set_source_xml($id)) return false;
+        return true;
+      }
+      return false;
+    }
+
+    function set_source_info($id_source, $key, $value)
+    { $sgbd = $this->sgbd();
+      if($source = $sgbd->get_data("sml_sources", $id_source))
+      { $source[$key] = $value;
+        if($sgbd->set_data("sml_sources", $id_source, $source)) return true;
+      }
+      return false;
+    }
+
+    function del_source($id)
+    { $sgbd = $this->sgbd();
+      if(!$this->del_source_compositions(array("id_source" => $id, "id_composition" => $id))) return false;
+      if(!$this->del_edit_reference_content($id)) return false;
+      if(!$this->del_source_derivations($id)) return false;
+      if(!$this->del_source_xml($id)) return false;
+      if(!$this->del_source_documents($id)) return false;
+      if(!$this->del_source_groupes($id)) return false;
+      if(!$sgbd->del_data("sml_sources", $id)) return false;
+      return true;
+    }
+
+    # ----------------------------------------------------------------------------------------
+    #                                                                   derivations de sources
+    #
+
+    function init_source_derivations(){
+      return true;
+    }
+
+    function source_derivations($params)
+    { $sgbd = $this->sgbd();
+      $env = $this->env();
+      $derivations = array();
+      // sources dont "id_source" est une derivation
+      if(isset($params["id_source"]))
+      { if($rst = $sgbd->open_data("sml_source_derivations"))
+        { while($v_rst = $sgbd->fetch_data($rst))
+          { if(isset($v_rst))
+            { if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && $v_rst["id_source"] == $params["id_source"])
+              { $derivations[$v_rst["id"]] = array();
+                $derivation_edit_file = $this->derivation_edit_xml_path($v_rst["id_source"], $v_rst["id"]);
+                if(file_exists($derivation_edit_file))
+                { if(($derivation_edit_content = $this->get_edit_derivation_content($v_rst["id_source"], $v_rst["id"])) !== false)
+                  { if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"], $derivation_edit_content)) !==false)
+                    { $derivations[$v_rst["id"]]["xml"] = array
+                      ( "url" => $v_rst["derivation"],
+                        "content" => $derivation_edit_content,
+                        "use_edit_content" => true
+                      );
+                    }
+                    else
+                    { $derivations = null;
+                      break;
+                    }
+                  }
+                  else
+                  { $derivations = null;
+                    break;
+                  }
+                }
+                else
+                { if(($derivations[$v_rst["id"]] = $this->source_xml_read($v_rst["derivation"])) !==false)
+                  { $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
+                  }
+                  else $derivations[$v_rst["id"]] = $this->empty_source();
+                }
+                $derivations[$v_rst["id"]]["id_source"] = $v_rst["id_source"];
+              }
+            }
+            else
+            { $derivations = null;
+              break;
+            }
+          }
+          $sgbd->close_data($rst);
+        }
+        if(!isset($derivations)) return false;
+        return $derivations;
+      }
+      // sources qui derivent de "derivation"
+      elseif(isset($params["derivation"]))
+      { $source_xml_url = $params["derivation"];
+        if(preg_match("/^[0-9]+$/", $source_xml_url)) $source_xml_url = $this->source_xml_url($source_xml_url);
+        $id_sources = array();
+        if($rst = $sgbd->open_data("sml_source_derivations"))
+        { while($v_rst = $sgbd->fetch_data($rst))
+          { if(isset($v_rst))
+            { if(isset($v_rst["id_source"]) && isset($v_rst["derivation"]) && $v_rst["derivation"] == $source_xml_url)
+              { $id_sources[$v_rst["id_source"]] = true;
+              }
+            }
+            else { $id_sources = false; break; }
+          }
+          $sgbd->close_data($rst);
+        }
+        else $id_sources = false;
+        if($id_sources === false) return false;
+        if($id_sources)
+        { if($rst = $sgbd->open_data("sml_sources"))
+          { while($v_rst = $sgbd->fetch_data($rst))
+            { if(isset($v_rst))
+              { if(isset($v_rst["id"]) && isset($id_sources[$v_rst["id"]]))
+                { $derivations[$v_rst["id"]] = $v_rst;
+                }
+              }
+              else { $derivations = false; break; }
+            }
+            $sgbd->close_data($rst);
+          }
+          else $derivations = false;
+        }
+        return $derivations;
+      }
+      return false;
+    }
+
+    function source_derivation($id)
+    { $sgbd = $this->sgbd();
+      return $sgbd->get_data("sml_source_derivations", $id);
+    }
+
+    function add_source_derivation($id_source, $derivation, $edit_content = "")
+    { $sgbd = $this->sgbd();
+      $id_source_derivation = $sgbd->add_data
+      ( "sml_source_derivations",
+        array
+        ( "id_source" => $id_source,
+          "derivation" => $derivation
+        )
+      );
+      if(isset($id_source_derivation))
+      { if($edit_content)
+        { if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content))
+          { $id_source_derivation = false;
+          }
+        }
+      }
+      else $id_source_derivation = false;
+      return $id_source_derivation;
+    }
+
+    function set_source_derivation($id_source_derivation, $id_source, $derivation, $edit_content = "")
+    { $sgbd = $this->sgbd();
+      if
+      ( ( $sgbd->set_data
+          ( "sml_source_derivations",
+            $id_source_derivation,
+            array
+            ( "id_source" => $id_source,
+              "derivation" => $derivation
+            )
+          )
+        )
+      )
+      { if($edit_content)
+        { if(!$this->set_edit_derivation_content($id_source, $id_source_derivation, $edit_content))
+          { return false;
+          }
+        }
+        return true;
+      }
+      return false;
+    }
+
+    function del_source_derivation($id_source_derivation)
+    { if(($derivation = $this->source_derivation($id_source_derivation)) !== false)
+      { if(($derivations = $this->source_derivations(array("id_source" => $derivation["id_source"]))) !== false)
+        { $sgbd = $this->sgbd();
+          if(!$sgbd->del_data("sml_source_derivations", $id_source_derivation)) return false;
+          if(count($derivations) > 1)
+          { return $this->del_edit_derivation_content($derivation["id_source"], $id_source_derivation);
+          }
+          else return $this->del_edit_derivations($derivation["id_source"]);
+        }
+      }
+      return false;
+    }
+
+    function del_source_derivations($id_source)
+    { $sgbd = $this->sgbd();
+      $id_source_derivations = array();
+      if($rst = $sgbd->open_data("sml_source_derivations"))
+      { while($v_rst = $sgbd->fetch_data($rst))
+        { if(isset($v_rst))
+          { if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && $v_rst["id_source"] == $id_source)
+            { $id_source_derivations[] = $v_rst["id"];
+            }
+          }
+          else { $id_source_derivations = false; break; }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $id_source_derivations = false;
+      if($id_source_derivations === false) return false;
+      if($id_source_derivations)
+      { foreach($id_source_derivations as $id_source_derivation)
+        { if(!$sgbd->del_data("sml_source_derivations", $id_source_derivation)) return false;
+        }
+      }
+      return $this->del_edit_derivations($id_source);
+    }
+
+    # ----------------------------------------------------------------------------------------
+    #                                                                               references
+    #
+
+    function source_reference($source)
+    { $reference = array();
+      if($source["reference"])
+      { if(!is_array($source["reference"]))
+        { $source["reference"] = array
+          ( "url" => $source["reference"],
+            "content" => "",
+            "use_edit_content" => false
+          );
+        }
+        $reference_edit_file = $this->reference_edit_xml_path($source["id"]);
+        if(file_exists($reference_edit_file))
+        { if(($reference_edit_content = $this->get_edit_reference_content($source["id"])) !== false)
+          { if(($reference = $this->source_xml_read($source["reference"], $reference_edit_content)) !==false)
+            { $reference["xml"] = array
+              ( "url" => $source["reference"]["xml"]["url"],
+                "content" => $reference_edit_content,
+                "use_edit_content" => true
+              );
+            }
+            else return false;
+          }
+          else return false;
+        }
+        else
+        { if(($reference = $this->source_xml_read($source["reference"]["xml"]["url"])) ===false)
+          { $reference = $this->empty_source();
+          }
+        }
+      }
+      return $reference;
+    }
+
+    # ----------------------------------------------------------------------------------------
+    #                                                                                documents
+    #
+
+    function source_documents($id_source)
+    { $sgbd = $this->sgbd();
+      $documents = array();
+      if($sgbd->data_exists("sml_sources/".$id_source))
+      { if($rst = $sgbd->open_data("sml_sources/".$id_source))
+        { $OK = true;
+          while($OK && ($document = $sgbd->fetch_data($rst)))
+          { if(isset($document)) $documents[$document["id"]] = $document;
+            else $OK = false;
+          }
+          $sgbd->close_data($rst);
+        }
+        else $OK = false;
+        if(!$OK) return false;
+      }
+      return $documents;
+    }
+
+    function add_source_document($id_source, $document)
+    { $sgbd = $this->sgbd();
+      if(!$sgbd->data_exists("sml_sources/".$id_source))
+      { if(!$sgbd->create_data("sml_sources/".$id_source)) return false;
+      }
+      if
+      ( !( $id_document = $sgbd->add_data
+          ( "sml_sources/".$id_source,
+            array
+            ( "nom" => $document["nom"],
+              "url" => $document["url"]
+            )
+          )
+        )
+      ) return false;
+      return $id_document;
+    }
+
+    function del_source_documents($id_source)
+    { $sgbd = $this->sgbd();
+      if($sgbd->data_exists("sml_sources/".$id_source))
+      { if(!$sgbd->remove_data("sml_sources/".$id_source)) return false;
+      }
+      return true;
+    }
+
+    # ----------------------------------------------------------------------------------------
+    #                                                                  compositions de sources
+    #
+
+    function source_compositions($params)
+    { $id_source = isset($params["id_source"]) ? $params["id_source"] : null;  
+      $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
+      $sgbd = $this->sgbd();
+      $env = $this->env();
+      $compositions = array();
+      if(isset($id_source))
+      { if($rst = $sgbd->open_data("sml_source_compositions"))
+        { while($v_rst = $sgbd->fetch_data($rst))
+          { if(isset($v_rst))
+            { if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]) && $v_rst["id_source"] == $id_source)
+              { if(!isset($compositions[$v_rst["id_source"]])) $compositions[$v_rst["id_source"]] = array();
+                $compositions[$v_rst["id_source"]][] = $v_rst["id_composition"];
+              }
+            }
+            else { $compositions = false; break; }
+          }
+          $sgbd->close_data($rst);
+        }
+        else $compositions = false;
+      }
+      elseif(isset($id_composition))
+      { if($id_composition)
+        { if($rst = $sgbd->open_data("sml_source_compositions"))
+          { while($v_rst = $sgbd->fetch_data($rst))
+            { if(isset($v_rst))
+              { if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]) && $v_rst["id_composition"] == $id_composition)
+                { if(!isset($compositions[$v_rst["id_composition"]])) $compositions[$v_rst["id_composition"]] = array();
+                  $compositions[$v_rst["id_composition"]][] = $v_rst["id_source"];
+                }
+              }
+              else { $compositions = false; break; }
+            }
+            $sgbd->close_data($rst);
+          }
+          else $compositions = false;
+        }
+        else
+        { if($rst = $sgbd->open_data("sml_source_compositions"))
+          { while($v_rst = $sgbd->fetch_data($rst))
+            { if(isset($v_rst))
+              { if(isset($v_rst["id_source"])) $compositions[$v_rst["id_source"]] = true;
+              }
+              else { $compositions = false; break; }
+            }
+            $sgbd->close_data($rst);
+          }
+          else $compositions = false;
+        }
+      }
+      return $compositions;
+    }
+
+    function set_source_composition($id_source, $id_composition)
+    { $sgbd = $this->sgbd();
+      if
+      ( $sgbd->add_data
+        ( "sml_source_compositions",
+          array
+          ( "id_source" => $id_source,
+            "id_composition" => $id_composition
+          )
+        )
+      ) return $this->set_source_xml($id_composition);
+      return false;
+    }
+
+    function del_source_compositions($params)
+    { $OK = true;
+      $id_source = isset($params["id_source"]) ? $params["id_source"] : null;  
+      $id_composition = isset($params["id_composition"]) ? $params["id_composition"] : null;
+      $to_delete = array();
+      $to_update = array();
+      if(isset($id_composition)) $to_update[$id_composition] = true;
+      $sgbd = $this->sgbd();
+      if($rst = $sgbd->open_data("sml_source_compositions"))
+      { while($v_rst = $sgbd->fetch_data($rst))
+        { if(isset($v_rst))
+          { if(isset($v_rst["id"]) && isset($v_rst["id_source"]) && isset($v_rst["id_composition"]))
+            { if
+              (    (isset($id_source) && $v_rst["id_source"] == $id_source)
+                || (isset($id_composition) && $v_rst["id_composition"] == $id_composition)
+              ) $to_delete[] = $v_rst["id"];
+              if(isset($id_source) && $v_rst["id_source"] == $id_source) $to_update[$v_rst["id_composition"]] = true;
+            }
+          }
+          else { $OK = false; break; }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $OK = false;
+      if(!$OK) return false;
+      foreach($to_delete as $id_source_composition)
+      { if(!$sgbd->del_data("sml_source_compositions", $id_source_composition)) return false;
+      }
+      foreach($to_update as $id_source_xml => $delete)
+      { if(!$this->set_source_xml($id_source_xml)) return false;
+      }
+      return true;
+    }
+
+    function has_sources($id_composition)
+    { $sgbd = $this->sgbd();
+      $env = $this->env();
+      $has_sources = 0;
+      if($rst = $sgbd->open_data("sml_source_compositions"))
+      { while($v_rst = $sgbd->fetch_data($rst))
+        { if(isset($v_rst))
+          { if(isset($v_rst["id_source"]) && isset($v_rst["id_composition"]))
+            { if($v_rst["id_composition"] == $id_composition)
+              { $has_sources = 1;
+                break;
+              }
+            }
+          }
+          else
+          { $has_sources = false;
+            break;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $has_sources = false;
+      return $has_sources;
+    }
+
+    function source_ariane($id_source, $ariane = array())
+    { if(($compositions = $this->source_compositions(array("id_source" => $id_source))) !== false)
+      { if(isset($compositions[$id_source]) && $compositions[$id_source])
+        { foreach($compositions[$id_source] as $id_composition)
+          { if(($ariane = $this->source_ariane($id_composition, $ariane)) !== false)
+            { if(($ariane[$id_composition] = $this->source($id_composition)) !== false)
+              {
+              }
+              else $ariane = false;
+            }
+            else $ariane = false;
+            break;
+          }
+        }
+      }
+      else $ariane = false;
+      return $ariane;
+    }
+
+  }
+
+?>
\ No newline at end of file
diff --git a/app/data/modules/xml/sml_data_sources_cache_db.php b/app/data/modules/xml/sml_data_sources_cache_db.php
new file mode 100644 (file)
index 0000000..9700fdd
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+
+  class sml_data_sources_cache_db extends mw_data
+  {
+
+    function source_cache_db()
+    { $sgbd = $this->sgbd();
+      $source_cache = array();
+      if($rst = $sgbd->open_data("sml_source_cache"))
+      { while($v_rst = $sgbd->fetch_data($rst))
+        { if(isset($v_rst))
+          { if(isset($v_rst["id"])) $source_cache[$v_rst["id"]] = $v_rst;
+          }
+          else
+          { $source_cache = false;
+            $break;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $source_cache = false;
+      return $source_cache;
+    }
+
+    function source_cache_infos_db($url)
+    { $sgbd = $this->sgbd();
+      $cache_infos = array();
+      if($rst = $sgbd->open_data("sml_source_cache"))
+      { while($v_rst = $sgbd->fetch_data($rst))
+        { if(isset($v_rst))
+          { if(isset($v_rst["id"]) && isset($v_rst["url"]))
+            { if($v_rst["url"] == $url)
+              { $cache_infos = $v_rst;
+                $break;
+              }
+            }
+          }
+          else
+          { $cache_infos = false;
+            $break;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $cache_infos = false;
+      return $cache_infos;
+    }
+
+    function add_source_cache_db($url, $cache_index)
+    { $sgbd = $this->sgbd();
+      return $sgbd->add_data
+      ( "sml_source_cache",
+        array
+        ( "url" => $url,
+          "id_source" => $cache_index,
+          "last_update" => date("Y-m-d H:i:s")
+        )
+      ) ? true : false;
+    }
+
+    function del_source_cache_db($id_cache_data)
+    { $sgbd = $this->sgbd();
+      return $sgbd->del_data("sml_source_cache", $id_cache_data) ? true : false;
+    }
+
+  }
+
+?>
\ No newline at end of file
index 47c3d4e..a9e27f2 100644 (file)
       return true;
     }
 
+    function nb_invitations($env){
+      if($user = $env->user()){
+        $data = $env->data();
+        if($groupes = $data->groupes($user["id"])){
+          if($invitations = $data->sources_invitations($groupes["list"], $user["id"])){
+            return $invitations["total"];
+          }
+        }
+      }
+      return 0;
+    }
+
     function enable($env){
       return true;
     }
@@ -54,6 +66,9 @@
       elseif(
             $env->bdd("sgbd") == "pdo_sqlite"
       ) return $this->install_sqlite($env);
+      elseif(
+            $env->bdd("sgbd") == "xml"
+      ) return $this->install_xml($env);
       return "Mode de stockage pour Mtweb (".$env->bdd("sgbd").") non supporté par SourceML";
     }
 
         $sgbd->query($sql);
 
         $sql =
-         "INSERT INTO `mw_sml_sources_access` (`id`, `nom`, `intitule`) VALUES"\r
+         "INSERT INTO `#--sml_sources_access` (`id`, `nom`, `intitule`) VALUES"\r
         ."(1, 'admin', 'administrateur'),"\r
         ."(2, 'editeur', 'éditeur'),"\r
         ."(3, 'contributeur', 'contributeur')";
         $sgbd->query($sql);
 
         $sql =
-         "INSERT INTO `mw_sml_sources_access` (`id`, `nom`, `intitule`) VALUES (1, 'admin', 'administrateur')";\r
+         "INSERT INTO `#--sml_sources_access` (`id`, `nom`, `intitule`) VALUES (1, 'admin', 'administrateur')";\r
         $sgbd->query($sql);
 
         $sql =
-         "INSERT INTO `mw_sml_sources_access` (`id`, `nom`, `intitule`) VALUES (2, 'editeur', 'éditeur')";
+         "INSERT INTO `#--sml_sources_access` (`id`, `nom`, `intitule`) VALUES (2, 'editeur', 'éditeur')";
         $sgbd->query($sql);
 
         $sql =
-         "INSERT INTO `mw_sml_sources_access` (`id`, `nom`, `intitule`) VALUES (3, 'contributeur', 'contributeur')";
+         "INSERT INTO `#--sml_sources_access` (`id`, `nom`, `intitule`) VALUES (3, 'contributeur', 'contributeur')";
         $sgbd->query($sql);
 
       }
       return true;
     }
 
+    function install_xml($env){
+      $data = $env->data();
+      $sgbd = $data->sgbd();
+
+      $RES = true;
+      $res = $sgbd->data_exists("sml_authors"); if(isset($res)){ if($res) $RES = 1; } else $RES = -1;
+      $res = $sgbd->data_exists("sml_classes"); if(isset($res)){ if($res) $RES = 1; } else $RES = -1;
+      $res = $sgbd->data_exists("sml_licences"); if(isset($res)){ if($res) $RES = 1; } else $RES = -1;
+      $res = $sgbd->data_exists("sml_sources"); if(isset($res)){ if($res) $RES = 1; } else $RES = -1;
+      $res = $sgbd->data_exists("sml_sources_access"); if(isset($res)){ if($res) $RES = 1; } else $RES = -1;
+      $res = $sgbd->data_exists("sml_sources_authors"); if(isset($res)){ if($res) $RES = 1; } else $RES = -1;
+      $res = $sgbd->data_exists("sml_sources_infos"); if(isset($res)){ if($res) $RES = 1; } else $RES = -1;
+      $res = $sgbd->data_exists("sml_source_cache"); if(isset($res)){ if($res) $RES = 1; } else $RES = -1;
+      $res = $sgbd->data_exists("sml_source_compositions"); if(isset($res)){ if($res) $RES = 1; } else $RES = -1;
+      $res = $sgbd->data_exists("sml_source_derivations"); if(isset($res)){ if($res) $RES = 1; } else $RES = -1;
+      $res = $sgbd->data_exists("sml_source_documents"); if(isset($res)){ if($res) $RES = 1; } else $RES = -1;
+      $res = $sgbd->data_exists("sml_sources_invitations"); if(isset($res)){ if($res) $RES = 1; } else $RES = -1;
+
+      if($RES === -1) return "impossible de savoir si les tables existent deja. installation annulee";
+      if($RES === 1) return "des tables existent deja en base. installation annulee";
+
+      if(!$sgbd->create_data("sml_authors")) return "impossible de creer la table sml_authors";
+      if(!$sgbd->create_data("sml_classes")) return "impossible de creer la table sml_classes";
+      if(!$sgbd->create_data("sml_licences")) return "impossible de creer la table sml_licences";
+      if(!$sgbd->create_data("sml_sources")) return "impossible de creer la table sml_sources";
+      if(!$sgbd->create_data("sml_sources_access")) return "impossible de creer la table sml_sources_access";
+      if(!$sgbd->create_data("sml_sources_authors")) return "impossible de creer la table sml_sources_authors";
+      if(!$sgbd->create_data("sml_sources_infos")) return "impossible de creer la table sml_sources_infos";
+      if(!$sgbd->create_data("sml_source_cache")) return "impossible de creer la table sml_source_cache";
+      if(!$sgbd->create_data("sml_source_compositions")) return "impossible de creer la table sml_source_compositions";
+      if(!$sgbd->create_data("sml_source_derivations")) return "impossible de creer la table sml_source_derivations";
+      if(!$sgbd->create_data("sml_source_documents")) return "impossible de creer la table sml_source_documents";
+      if(!$sgbd->create_data("sml_sources_invitations")) return "impossible de creer la table sml_sources_invitations";
+
+      $ERROR = false;
+
+      // ------------------------------------ sml_licences
+
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_licences",
+          array(
+            "nom" => "Creative commons by-sa 2.0",
+            "url" => "http://creativecommons.org/licenses/by-sa/2.0/deed.fr"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_licences",
+          array(
+            "nom" => "Creative Commons by-nc-nd 2.5",
+            "url" => "http://creativecommons.org/licenses/by-nc-nd/2.5/"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_licences",
+          array(
+            "nom" => "Creative Commons by-nc-sa 2.5",
+            "url" => "http://creativecommons.org/licenses/by-nc-sa/2.5/"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_licences",
+          array(
+            "nom" => "Creative Commons by-nc 2.5",
+            "url" => "http://creativecommons.org/licenses/by-nc/2.5/"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_licences",
+          array(
+            "nom" => "Creative Commons by-nd 2.5",
+            "url" => "http://creativecommons.org/licenses/by-nd/2.5/"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_licences",
+          array(
+            "nom" => "Creative Commons by-sa 2.5",
+            "url" => "http://creativecommons.org/licenses/by-sa/2.5/"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_licences",
+          array(
+            "nom" => "Creative Commons by 2.5",
+            "url" => "http://creativecommons.org/licenses/by/2.5/"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_licences",
+          array(
+            "nom" => "Licence Art Libre",
+            "url" => "http://artlibre.org/licence/lal/"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_licences",
+          array(
+            "nom" => "Licence C Reaction",
+            "url" => "http://morne.free.fr/Necktar7/creactionfr.htm"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_licences",
+          array(
+            "nom" => "Yellow OpenMusic License",
+            "url" => "http://openmusic.linuxtag.org/yellow.html"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_licences",
+          array(
+            "nom" => "Green OpenMusic License",
+            "url" => "http://openmusic.linuxtag.org/green.html"
+          )
+        )
+      ) $ERROR = true;
+
+      // ------------------------------------ sml_classes
+
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_classes",
+          array(
+            "nom" => "album"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_classes",
+          array(
+            "nom" => "morceau"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_classes",
+          array(
+            "nom" => "piste"
+          )
+        )
+      ) $ERROR = true;
+
+      // ------------------------------------ sml_sources_access
+
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_sources_access",
+          array(
+            "nom" => "admin",
+            "intitule" => "administrateur"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_sources_access",
+          array(
+            "nom" => "editeur",
+            "intitule" => "éditeur"
+          )
+        )
+      ) $ERROR = true;
+      if(!$ERROR) if(
+        !$sgbd->add_data(
+          "sml_sources_access",
+          array(
+            "nom" => "contributeur",
+            "intitule" => "contributeur"
+          )
+        )
+      ) $ERROR = true;
+
+      if($ERROR){
+        return "les tables ont ete ajoutees en base mais impossible d'y enregistrer les valeurs par defaut.";
+      }
+
+      return true;
+    }
+
     // ---------------------------------------------------------------------------------
     //                                                                         uninstall
     //
         ||  $env->bdd("sgbd") == "pdo_mysql"
         ||  $env->bdd("sgbd") == "pdo_sqlite"
       ) return $this->uninstall_sql($env);
+      if(
+            $env->bdd("sgbd") == "xml"
+      ) return $this->uninstall_xml($env);
       return "Mode de stockage pour Mtweb (".$env->bdd("sgbd").") non supporté par SourceML";
     }
 
       return true;
     }
 
-    // ---------------------------------------------------------------------------------
-    //                                                                fonctions internes
-    //
+    function uninstall_xml($env){
+      $data = $env->data();
+      $sgbd = $data->sgbd();
+      if(!$this->disable($env)) return "impossible de desactiver le plugin";
 
-    function nb_invitations($env){
-      if($user = $env->user()){
-        $data = $env->data();
-        if($groupes = $data->groupes($user["id"])){
-          $groupes_in = "";
-          foreach($groupes["list"] as $id_groupe => $groupe) $groupes_in .= ($groupes_in ? "," : "").$id_groupe;
-          if($groupes_in){
-            if(
-              (
-                $invitations = $data->list_sml_sources_invitations(
-                  array(
-                    "index_name" => "id",
-                    "where"=> "id_author IN(".$groupes_in.") OR id_user=".$user["id"],
-                    "order_by" => "date_invitation",
-                    "order" => "DESC"
-                  )
-                )
-              ) !== false
-            ){
-              return $invitations["total"];
-            }
-          }
-        }
+      $ERROR = false;
+
+      if(!$ERROR) if(!$sgbd->remove_data("sml_authors")) $ERROR = true;
+      if(!$ERROR) if(!$sgbd->remove_data("sml_classes")) $ERROR = true;
+      if(!$ERROR) if(!$sgbd->remove_data("sml_licences")) $ERROR = true;
+      if(!$ERROR) if(!$sgbd->remove_data("sml_sources")) $ERROR = true;
+      if(!$ERROR) if(!$sgbd->remove_data("sml_sources_access")) $ERROR = true;
+      if(!$ERROR) if(!$sgbd->remove_data("sml_sources_authors")) $ERROR = true;
+      if(!$ERROR) if(!$sgbd->remove_data("sml_sources_infos")) $ERROR = true;
+      if(!$ERROR) if(!$sgbd->remove_data("sml_source_cache")) $ERROR = true;
+      if(!$ERROR) if(!$sgbd->remove_data("sml_source_compositions")) $ERROR = true;
+      if(!$ERROR) if(!$sgbd->remove_data("sml_source_derivations")) $ERROR = true;
+      if(!$ERROR) if(!$sgbd->remove_data("sml_source_documents")) $ERROR = true;
+      if(!$ERROR) if(!$sgbd->remove_data("sml_sources_invitations")) $ERROR = true;
+
+
+      if($ERROR){
+        return "erreur lors de la suppression des tables";
       }
-      return 0;
+
+      return true;
     }
 
   }