marche avec MySql, SQLite ou XML
[mw_sourceml] / app / data / modules / xml / sml_data_source_groupes.php
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