titre du site par defaut
[mw_sourceml] / mw_sourceml.php
index 2769e77..66392d4 100644 (file)
@@ -2,15 +2,44 @@
 
   class mw_sourceml extends mw_plugin{
 
-    function title(){
+    public function title(){
       return "SourceML";
     }
 
-    function description(){
+    public function description(){
       return "Publication de sources musicales";
     }
 
-    function init($env){
+    public function loaded($env){
+      $AUTO_ACTIVATE = false;
+      if(!$env->bdd("sgbd") || ($plugin_data = $env->plugin_data("mw_sourceml")) === false) return;
+      if(!isset($plugin_data["installed"]) || !$plugin_data["installed"]){
+        $data = $env->data();
+        $sgbd = $data->sgbd();
+        if($env->bdd("sgbd") == "xml"){
+          $AUTO_ACTIVATE = $sgbd->data_exists("sources");
+        }
+        else{
+          $AUTO_ACTIVATE = $sgbd->table_exists("#--sources");
+        }
+      }
+      if($AUTO_ACTIVATE){
+        if(
+          $env->set_plugin_data(
+            "mw_sourceml",
+            array(
+              "installed" => true,
+              "enabled" => true,
+              "priorite" => 0
+            )
+          )
+        ){
+          $env->load_plugin("mw_sourceml");
+        }
+      }
+    }
+
+    public function init($env){
 
       $env->set_link("admin/sourceml", $env->url("admin/sourceml"), "SourceML", 50);
       $env->set_link("admin/sourceml/licences", $env->url("admin/licences"), "Licences", 10);
       $env->set_link("users/morceaux", $env->url("users/morceaux"), "Morceaux", 32);
       $env->set_link("users/pistes", $env->url("users/pistes"), "Sources", 33);
 
+      $invitations_title = "Invitations";
+      if($nb_invitations = $this->nb_invitations($env)) $invitations_title .= " (".$nb_invitations.")";
+      $env->set_link("users/sources_invitations", $env->url("users/sources_invitations"), $invitations_title, 34);
+
       $env->set_link("menu_top/groupes", $env->url("sources/groupe"), "Groupes", 10);
       $env->set_link("menu_top/albums", $env->url("sources/album"), "Albums", 11);
       $env->set_link("menu_top/morceaux", $env->url("sources/morceau"), "Morceaux", 12);
       return true;
     }
 
-    function enable($env){
-      return true;
+    public 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 disable($env){
+    public function enable($env){
+      $data = $env->data();
+      if($data->config("site_name") == "mtweb"){
+        $data->set_config("site_name", "SourceML");
+      }
       return true;
     }
 
-    // ---------------------------------------------------------------------------------
-    //                                                                           install
-    //
-
-    function install($env){
-      if(
-            $env->bdd("sgbd") == "mysql"
-        ||  $env->bdd("sgbd") == "pdo_mysql"
-      ) return $this->install_mysql($env);
-      return false;
-    }
-
-    function install_mysql($env){
+    function disable($env){
       $data = $env->data();
-      $sgbd = $data->sgbd();
-      try{
-        $EXISTS =
-            $sgbd->table_exists("#--sml_authors")
-        ||  $sgbd->table_exists("#--sml_classes")
-        ||  $sgbd->table_exists("#--sml_licences")
-        ||  $sgbd->table_exists("#--sml_sources")
-        ||  $sgbd->table_exists("#--sml_sources_access")
-        ||  $sgbd->table_exists("#--sml_sources_authors")
-        ||  $sgbd->table_exists("#--sml_sources_infos")
-        ||  $sgbd->table_exists("#--sml_source_cache")
-        ||  $sgbd->table_exists("#--sml_source_compositions")
-        ||  $sgbd->table_exists("#--sml_source_derivations")
-        ||  $sgbd->table_exists("#--sml_source_documents");
-      }
-      catch(Exception $e){
-        return "impossible de savoir si les tables existent deja";
-      }
-      if($EXISTS){
-        return "des tables existent deja en base. installation annulee";
-      }
-      try{
-        $sql =
-         "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\"";
-        $sgbd->query($sql);
-
-        $sql =
-         "CREATE TABLE `#--sml_authors`("
-        ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
-        ."  `id_user` int(11) NOT NULL,"
-        ."  `nom` varchar(255) NOT NULL,"
-        ."  `image` varchar(255) DEFAULT NULL,"
-        ."  `description` text,"
-        ."  `email` varchar(255) NOT NULL,"
-        ."  `contact_form` tinyint(4) NOT NULL,"
-        ."  `captcha` tinyint(4) NOT NULL,"
-        ."  PRIMARY KEY (`id`),"
-        ."  KEY `id_user` (`id_user`)"
-        .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
-        $sgbd->query($sql);
-
-        $sql =
-         "CREATE TABLE `#--sml_classes`("
-        ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
-        ."  `nom` varchar(255) NOT NULL,"
-        ."  PRIMARY KEY (`id`)"
-        .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
-        $sgbd->query($sql);
-
-        $sql =
-         "CREATE TABLE `#--sml_licences`("
-        ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
-        ."  `nom` varchar(255) NOT NULL,"
-        ."  `url` varchar(255) NOT NULL,"
-        ."  PRIMARY KEY (`id`)"
-        .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
-        $sgbd->query($sql);
-
-        $sql =
-         "CREATE TABLE `#--sml_sources`("
-        ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
-        ."  `id_class` int(11) NOT NULL,"
-        ."  `reference` varchar(255) DEFAULT NULL,"
-        ."  `titre` varchar(255) DEFAULT NULL,"
-        ."  `licence` int(11) DEFAULT NULL,"
-        ."  `date_creation` date DEFAULT NULL,"
-        ."  `date_inscription` datetime NOT NULL,"
-        ."  PRIMARY KEY (`id`),"
-        ."  KEY `id_class` (`id_class`),"
-        ."  KEY `licence` (`licence`)"
-        .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
-        $sgbd->query($sql);
-
-        $sql =
-         "CREATE TABLE `#--sml_sources_access`("
-        ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
-        ."  `nom` varchar(255) NOT NULL,"
-        ."  `intitule` varchar(255) NOT NULL,"
-        ."  PRIMARY KEY (`id`)"
-        .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
-        $sgbd->query($sql);
-
-        $sql =
-         "CREATE TABLE `#--sml_sources_authors`("
-        ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
-        ."  `id_source` int(11) NOT NULL,"
-        ."  `id_author` int(11) NOT NULL,"
-        ."  `id_sources_access` int(11) NOT NULL,"
-        ."  PRIMARY KEY (`id`),"
-        ."  KEY `id_object` (`id_source`),"
-        ."  KEY `id_author` (`id_author`),"
-        ."  KEY `id_sources_access` (`id_sources_access`)"
-        .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
-        $sgbd->query($sql);
-
-        $sql =
-         "CREATE TABLE `#--sml_sources_infos`("
-        ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
-        ."  `id_source` int(11) NOT NULL,"
-        ."  `key` varchar(255) NOT NULL,"
-        ."  `value` text NOT NULL,"
-        ."  PRIMARY KEY (`id`),"
-        ."  KEY `id_source` (`id_source`)"
-        .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
-        $sgbd->query($sql);
-
-        $sql =
-         "CREATE TABLE `#--sml_source_cache`("
-        ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
-        ."  `url` varchar(255) NOT NULL,"
-        ."  `id_source` int(11) NOT NULL,"
-        ."  `last_update` datetime NOT NULL,"
-        ."  PRIMARY KEY (`id`)"
-        .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
-        $sgbd->query($sql);
-
-        $sql =
-         "CREATE TABLE `#--sml_source_compositions`("
-        ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
-        ."  `id_source` int(11) NOT NULL,"
-        ."  `id_composition` int(11) NOT NULL,"
-        ."  PRIMARY KEY (`id`),"
-        ."  KEY `id_source` (`id_source`),"
-        ."  KEY `id_composition` (`id_composition`)"
-        .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
-        $sgbd->query($sql);
-
-        $sql =
-         "CREATE TABLE `#--sml_source_derivations`("
-        ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
-        ."  `id_source` int(11) NOT NULL,"
-        ."  `derivation` varchar(255) NOT NULL,"
-        ."  PRIMARY KEY (`id`),"
-        ."  KEY `derivation` (`derivation`)"
-        .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
-        $sgbd->query($sql);
-
-        $sql =
-         "CREATE TABLE `#--sml_source_documents`("
-        ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
-        ."  `id_source` int(11) NOT NULL,"
-        ."  `nom` varchar(255) NOT NULL,"
-        ."  `url` varchar(255) NOT NULL,"
-        ."  PRIMARY KEY (`id`),"
-        ."  KEY `id_source` (`id_source`)"
-        .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
-        $sgbd->query($sql);
-
-      }
-      catch(Exception $e){
-        return "imposible de creer les tables en base";
-      }
-      try{
-        $sql =
-         "INSERT INTO `#--sml_licences` (`id`, `nom`, `url`) VALUES"
-        ."(1, 'Creative commons by-sa 2.0', 'http://creativecommons.org/licenses/by-sa/2.0/deed.fr'),"
-        ."(2, 'Creative Commons by-nc-nd 2.5', 'http://creativecommons.org/licenses/by-nc-nd/2.5/'),"
-        ."(3, 'Creative Commons by-nc-sa 2.5', 'http://creativecommons.org/licenses/by-nc-sa/2.5/'),"
-        ."(4, 'Creative Commons by-nc 2.5', 'http://creativecommons.org/licenses/by-nc/2.5/'),"
-        ."(5, 'Creative Commons by-nd 2.5', 'http://creativecommons.org/licenses/by-nd/2.5/'),"
-        ."(6, 'Creative Commons by-sa 2.5', 'http://creativecommons.org/licenses/by-sa/2.5/'),"
-        ."(7, 'Creative Commons by 2.5', 'http://creativecommons.org/licenses/by/2.5/'),"
-        ."(8, 'Licence Art Libre', 'http://artlibre.org/licence/lal/'),"
-        ."(9, 'Licence C Reaction', 'http://morne.free.fr/Necktar7/creactionfr.htm'),"
-        ."(10, 'Yellow OpenMusic License', 'http://openmusic.linuxtag.org/yellow.html'),"
-        ."(11, 'Green OpenMusic License', 'http://openmusic.linuxtag.org/green.html')";
-        $sgbd->query($sql);
-
-        $sql =
-         "INSERT INTO `#--sml_classes` (`id`, `nom`) VALUES"
-        ."(1, 'album'),"
-        ."(2, 'morceau'),"
-        ."(3, 'piste')";
-        $sgbd->query($sql);
-
-        $sql =
-         "INSERT INTO `mw_sml_sources_access` (`id`, `nom`, `intitule`) VALUES"\r
-        ."(1, 'admin', 'administrateur'),"\r
-        ."(2, 'editeur', 'éditeur'),"\r
-        ."(3, 'contributeur', 'contributeur')";
-        $sgbd->query($sql);
-
-      }
-      catch(Exception $e){
-        return "les tables ont ete ajoutees en base mais impossible d'y enregistrer les valeurs par defaut.";
+      if($data->config("site_name") == "SourceML"){
+        $data->set_config("site_name", "mtweb");
       }
       return true;
     }
 
-    // ---------------------------------------------------------------------------------
-    //                                                                         uninstall
-    //
-
-    function uninstall($env){
+    public function install($env){
+      $data = $env->data();
+      $res = true;
       if(
             $env->bdd("sgbd") == "mysql"
         ||  $env->bdd("sgbd") == "pdo_mysql"
-      ) return $this->uninstall_mysql($env);
-      return false;
+      ){
+        $data->load_modules($env->path("mw_dir")."plugins/mw_sourceml/app/", "data/modules/sql/");
+        $res = $data->mw_sourceml_install_mysql($this, $env);
+      }
+      elseif(
+            $env->bdd("sgbd") == "pdo_sqlite"
+      ){
+        $data->load_modules($env->path("mw_dir")."plugins/mw_sourceml/app/", "data/modules/sql/");
+        $res = $data->mw_sourceml_install_sqlite($this, $env);
+      }
+      elseif(
+            $env->bdd("sgbd") == "xml"
+      ){
+        $data->load_modules($env->path("mw_dir")."plugins/mw_sourceml/app/", "data/modules/xml/");
+        $res = $data->mw_sourceml_install_xml($this, $env);
+      }
+      else{
+        $res = "Mode de stockage pour Mtweb (".$env->bdd("sgbd").") non supporté par SourceML";
+      }
+      return $res;
     }
 
-    function uninstall_mysql($env){
+    public function uninstall($env){
       $data = $env->data();
-      $sgbd = $data->sgbd();
-      if(!$this->disable($env)) return "impossible de desactiver le plugin";
-      try{
-        $sgbd->query("DROP TABLE #--sml_authors");
-      }
-      catch(Exception $e){
-        return "impossible de supprimer la table #--sml_authors";
-      }
-      try{
-        $sgbd->query("DROP TABLE #--sml_classes");
-      }
-      catch(Exception $e){
-        return "impossible de supprimer la table #--sml_classes";
-      }
-      try{
-        $sgbd->query("DROP TABLE #--sml_licences");
-      }
-      catch(Exception $e){
-        return "impossible de supprimer la table #--sml_licences";
-      }
-      try{
-        $sgbd->query("DROP TABLE #--sml_sources");
-      }
-      catch(Exception $e){
-        return "impossible de supprimer la table #--sml_sources";
-      }
-      try{
-        $sgbd->query("DROP TABLE #--sml_sources_access");
-      }
-      catch(Exception $e){
-        return "impossible de supprimer la table #--sml_sources_access";
-      }
-      try{
-        $sgbd->query("DROP TABLE #--sml_sources_authors");
-      }
-      catch(Exception $e){
-        return "impossible de supprimer la table #--sml_sources_authors";
-      }
-      try{
-        $sgbd->query("DROP TABLE #--sml_sources_infos");
-      }
-      catch(Exception $e){
-        return "impossible de supprimer la table #--sml_sources_infos";
-      }
-      try{
-        $sgbd->query("DROP TABLE #--sml_source_cache");
-      }
-      catch(Exception $e){
-        return "impossible de supprimer la table #--sml_source_cache";
-      }
-      try{
-        $sgbd->query("DROP TABLE #--sml_source_compositions");
-      }
-      catch(Exception $e){
-        return "impossible de supprimer la table #--sml_source_compositions";
-      }
-      try{
-        $sgbd->query("DROP TABLE #--sml_source_derivations");
-      }
-      catch(Exception $e){
-        return "impossible de supprimer la table #--sml_source_derivations";
+      $res = true;
+      if(
+            $env->bdd("sgbd") == "mysql"
+        ||  $env->bdd("sgbd") == "pdo_mysql"
+        ||  $env->bdd("sgbd") == "pdo_sqlite"
+      ){
+        $data->load_modules($env->path("mw_dir")."plugins/mw_sourceml/app/", "data/modules/sql/");
+        $res = $data->mw_sourceml_uninstall_sql($this, $env);
       }
-      try{
-        $sgbd->query("DROP TABLE #--sml_source_documents");
+      elseif(
+            $env->bdd("sgbd") == "xml"
+      ){
+        $data->load_modules($env->path("mw_dir")."plugins/mw_sourceml/app/", "data/modules/xml/");
+        $res = $data->mw_sourceml_uninstall_xml($this, $env);
       }
-      catch(Exception $e){
-        return "impossible de supprimer la table #--sml_source_documents";
+      else{
+        $res = "Mode de stockage pour Mtweb (".$env->bdd("sgbd").") non supporté par SourceML";
       }
-      return true;
+      return $res;
     }
 
   }
-
-?>
\ No newline at end of file