delegation tiny_mce et install pour sqlite
[mw_pages] / mw_pages.php
index 056efbc..c33eb05 100644 (file)
@@ -1,66 +1,73 @@
 <?php
 
-  class mw_pages extends mw_plugin
-  {
+  class mw_pages extends mw_plugin{
 
-    function title($env)
-    { return "Pages";
+    function title(){
+      return "Pages";
     }
 
-    function description($env)
-    { return "Pour ajouter des pages, avec un &eacute;diteur dans l'administration du site";
+    function description(){
+      return "Pour ajouter des pages, avec un &eacute;diteur dans l'administration du site";
     }
 
-    function init($env)
-    { $env->set_link("plugins/admin/mw_pages", $env->url("pages/admin"), "&eacute;diter les pages");
+    function init($env){
+      $env->set_link("plugins/admin/mw_pages", $env->url("pages/admin"), "&eacute;diter les pages");
+      $env->set_link("admin/pages", $env->url("pages/admin"), "Pages", 40);
       $data = $env->data();
-      if
-      ( ( $pages = $data->pages
-          ( array
-            ( "id_parent" => "",
-              "enabled"   => 1,
-              "order_by"  => "position",
-              "order"     => "ASC"
+      if(
+        (
+          $arbo = $data->pages_arbo(
+            array(
+              "enabled" => 1,
+              "order_by" => "position",
+              "order" => "ASC"
             )
           )
         ) !== false
-      )
-      { foreach($pages["list"] as $id_page => $page)
-        { $env->set_link
-          ( "menu_top/mw_page_".$id_page,
-            $env->url("pages/view/page", array("id" => $id_page)),
+      ){
+        $this->_set_links($env, "menu_top", $arbo);
+      }
+      return true;
+    }
+
+    function _set_links($env, $parent, $arbo){
+      if($arbo["subs"]){
+        foreach($arbo["subs"] as $page){
+          $env->set_link(
+            $parent."/mw_page_".$page["id"],
+            $env->url("pages/view/page", array("id" => $page["id"])),
             $page["title"],
             $page["position"]
           );
+          $this->_set_links($env, $parent."/mw_page_".$page["id"], $page);
         }
       }
-      return true;
     }
 
-    function enable($env)
-    { $plugins_pages_start_id = $env->config("plugins_pages_start_id");
-      if($plugins_pages_start_id)
-      { $data = $env->data();
-        if
-        (    $data->set_config("start_action", "pages/view/page")
+    function enable($env){
+      $plugins_pages_start_id = $env->config("plugins_pages_start_id");
+      if($plugins_pages_start_id){
+        $data = $env->data();
+        if(
+             $data->set_config("start_action", "pages/view/page")
           && $data->set_config("start_action_params", @serialize(array("id" => $plugins_pages_start_id)))
-        )
-        { return true;
+        ){
+          return true;
         }
         return false;
       }
       return true;
     }
 
-    function disable($env)
-    { $start_action = $env->config("start_action");
-      if($start_action == "pages/view/page")
-      { $data = $env->data();
-        if
-        (    $data->set_config("start_action", "")
+    function disable($env){
+      $start_action = $env->config("start_action");
+      if($start_action == "pages/view/page"){
+        $data = $env->data();
+        if(
+             $data->set_config("start_action", "")
           && $data->set_config("start_action_params", "")
-        )
-        { return true;
+        ){
+          return true;
         }
         return false;
       }
     //                                                                           install
     //
 
-    function install($env)
-    { if($env->bdd("sgbd") == "xml") return $this->install_xml($env);
-      if($env->bdd("sgbd") == "sqlite") return $this->install_sqlite($env);
-      if($env->bdd("sgbd") == "mysql") return $this->install_mysql($env);
+    function install($env){
+      if(
+            $env->bdd("sgbd") == "mysql"
+        ||  $env->bdd("sgbd") == "pdo_mysql"
+      ){
+        return $this->install_mysql($env);
+      }
+      elseif(
+            $env->bdd("sgbd") == "pdo_sqlite"
+      ){
+        return $this->install_sqlite($env);
+      }
+      elseif(
+            $env->bdd("sgbd") == "xml"
+      ){
+        return $this->install_xml($env);
+      }
+      else{
+        return "Mode de stockage pour Mtweb (".$env->bdd("sgbd").") non supporté par mw_pages";
+      }
+      return true;
     }
 
-    function install_xml($env)
-    { $data = $env->data();
+    function install_mysql($env){
+      $data = $env->data();
       $sgbd = $data->sgbd();
-      $EXISTS = $sgbd->data_exists("pages");
-      if(!isset($EXISTS))
-      { return "impossible de savoir si la table #--pages existe";
+      try{
+        $EXISTS = $sgbd->table_exists("#--pages");
+      }
+      catch(Exception $e){
+        return "impossible de savoir si la table #--pages existe";
+      }
+      if($EXISTS){
+        return "la table #--pages existe deja";
+      }
+      try{
+        $sql =
+         "CREATE TABLE #--pages"
+        ."( id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY"
+        .", id_parent INT(11) NULL"
+        .", title VARCHAR(255) NULL"
+        .", content TEXT NULL"
+        .", date_creation DATETIME NOT NULL"
+        .", user_creation INT(11) NOT NULL"
+        .", date_last_update DATETIME NOT NULL"
+        .", user_last_update INT(11) NOT NULL"
+        .", enabled TINYINT NOT NULL DEFAULT '1'"
+        .", position INT(11) NOT NULL DEFAULT '0'"
+        .") DEFAULT CHARSET=utf8";
+        $sgbd->query($sql);
       }
-      if($EXISTS)
-      { return "la table #--pages existe deja";
+      catch(Exception $e){
+        return "imposible de creer la table #--pages";
       }
-      if(!$sgbd->create_data("pages"))
-      { return "imposible de creer la table #--pages";
+      try{
+        $sgbd->query("INSERT INTO #--actions_roles(action, id_role) VALUES('pages/admin', 1)");
       }
-      if(!$sgbd->add_data("action_status", array("action" => "pages/admin", "id_status" => "1")))
-      { $sgbd->remove_data("pages");
+      catch(Exception $e){
+        try{
+          $sgbd->query("DROP TABLE #--pages");
+        }
+        catch(Exception $e){}
         return "impossible d'ajouter un statut pour l'action pages/admin";
       }
       return true;
     }
 
-    function install_sqlite($env)
-    { $data = $env->data();
+    function install_sqlite($env){
+      $data = $env->data();
       $sgbd = $data->sgbd();
-      $EXISTS = $sgbd->table_exists("#--pages");
-      if(!isset($EXISTS))
-      { return "impossible de savoir si la table #--pages existe";
-      }
-      if($EXISTS)
-      { return "la table #--pages existe deja";
-      }
-      $sql =
-       "CREATE TABLE #--pages"
-      ."( id INTEGER NOT NULL PRIMARY KEY"
-      .", id_parent INTEGER NULL"
-      .", title VARCHAR NULL"
-      .", content TEXT NULL"
-      .", date_creation TEXT NOT NULL"
-      .", user_creation INTEGER NOT NULL"
-      .", date_last_update TEXT NOT NULL"
-      .", user_last_update INTEGER NOT NULL"
-      .", enabled INTEGER NOT NULL DEFAULT 1"
-      .", position INTEGER NOT NULL DEFAULT 0"
-      .")";
-      if(!$sgbd->query($sql))
-      { return "imposible de creer la table #--pages";
-      }
-      if(!$sgbd->query("INSERT INTO #--action_status(action, id_status) VALUES('pages/admin', 1)"))
-      { $sgbd->query("DROP TABLE \"main\".\"#--pages\"");
+      try{
+        $EXISTS = $sgbd->table_exists("#--pages");
+      }
+      catch(Exception $e){
+        return "impossible de savoir si la table #--pages existe";
+      }
+      if($EXISTS){
+        return "la table #--pages existe deja";
+      }
+      try{
+        $sql =
+         "CREATE TABLE #--pages("
+        ." `id` INTEGER PRIMARY KEY AUTOINCREMENT"
+        .", id_parent INTEGER NULL"
+        .", title TEXT NULL"
+        .", content TEXT NULL"
+        .", date_creation TEXT NOT NULL"
+        .", user_creation INTEGER NOT NULL"
+        .", date_last_update TEXT NOT NULL"
+        .", user_last_update INTEGER NOT NULL"
+        .", enabled INTEGER NOT NULL DEFAULT '1'"
+        .", position INTEGER NOT NULL DEFAULT '0'"
+        .")";
+        $sgbd->query($sql);
+      }
+      catch(Exception $e){
+        return "imposible de creer la table #--pages";
+      }
+      try{
+        $sgbd->query("INSERT INTO #--actions_roles(action, id_role) VALUES('pages/admin', 1)");
+      }
+      catch(Exception $e){
+        try{
+          $sgbd->query("DROP TABLE #--pages");
+        }
+        catch(Exception $e){}
         return "impossible d'ajouter un statut pour l'action pages/admin";
       }
       return true;
     }
 
-    function install_mysql($env)
-    { $data = $env->data();
+    function install_xml($env){
+      $data = $env->data();
       $sgbd = $data->sgbd();
-      $EXISTS = $sgbd->table_exists("#--pages");
-      if(!isset($EXISTS))
-      { return "impossible de savoir si la table #--pages existe";
-      }
-      if($EXISTS)
-      { return "la table #--pages existe deja";
-      }
-      $sql =
-       "CREATE TABLE #--pages"
-      ."( id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY"
-      .", id_parent INT(11) NULL"
-      .", title VARCHAR(255) NULL"
-      .", content TEXT NULL"
-      .", date_creation DATETIME NOT NULL"
-      .", user_creation INT(11) NOT NULL"
-      .", date_last_update DATETIME NOT NULL"
-      .", user_last_update INT(11) NOT NULL"
-      .", enabled TINYINT NOT NULL DEFAULT '1'"
-      .", position INT(11) NOT NULL DEFAULT '0'"
-      .") DEFAULT CHARSET=utf8";
-      if(!$sgbd->query($sql))
-      { return "imposible de creer la table #--pages";
-      }
-      if(!$sgbd->query("INSERT INTO #--action_status(action, id_status) VALUES('pages/admin', 1)"))
-      { $sgbd->query("DROP TABLE #--pages");
+      $EXISTS = $sgbd->data_exists("pages");
+      if(!isset($EXISTS)){
+        return "impossible de savoir si la table #--pages existe";
+      }
+      if($EXISTS){
+        return "la table #--pages existe deja";
+      }
+      if(!$sgbd->create_data("pages")){
+        return "imposible de creer la table #--pages";
+      }
+      if(!$sgbd->add_data("actions_roles", array("action" => "pages/admin", "id_role" => "1"))){
+        $sgbd->remove_data("pages");
         return "impossible d'ajouter un statut pour l'action pages/admin";
       }
       return true;
     //                                                                         uninstall
     //
 
-    function uninstall($env)
-    { if($env->bdd("sgbd") == "xml") return $this->uninstall_xml($env);
-      if($env->bdd("sgbd") == "sqlite") return $this->uninstall_sqlite($env);
-      if($env->bdd("sgbd") == "mysql") return $this->uninstall_mysql($env);
+    function uninstall($env){
+      if($env->bdd("sgbd") == "xml") return $this->uninstall_xml($env);
+      else return $this->uninstall_sql($env);
     }
 
-    function uninstall_xml($env)
-    { $data = $env->data();
+    function uninstall_xml($env){
+      $data = $env->data();
       $sgbd = $data->sgbd();
       if(!$this->disable($env)) return "impossible de desactiver le plugin";
       $data->del_config("plugins_pages_start_id");
       $EXISTS = $sgbd->data_exists("pages");
-      if(!isset($EXISTS))
-      { return "impossible de savoir si la table #--pages existe";
+      if(!isset($EXISTS)){
+        return "impossible de savoir si la table #--pages existe";
       }
-      if(!$EXISTS)
-      { // return "la table #--pages n'existe pas";
+      if(!$EXISTS){
+        // return "la table #--pages n'existe pas";
       }
-      elseif(!$sgbd->remove_data("pages"))
-      { return "imposible de supprimer la table #--pages";
+      elseif(!$sgbd->remove_data("pages")){
+        return "imposible de supprimer la table #--pages";
       }
       $ids = array();
-      if($rst = $sgbd->open_data("action_status"))
-      { while($v_rst = $sgbd->fetch_data($rst))
-        { if(isset($v_rst))
-          { if(isset($v_rst["action"]) && isset($v_rst["id"]) && $v_rst["action"] == "pages/admin")
-            { $ids[] = $v_rst["id"];
+      if($rst = $sgbd->open_data("actions_roles")){
+        while($v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst)){
+            if(isset($v_rst["action"]) && isset($v_rst["id"]) && $v_rst["action"] == "pages/admin"){
+              $ids[] = $v_rst["id"];
             }
           }
           else $ids = false;
         }
         $sgbd->close_data($rst);
       }
-      if($ids !== false)
-      { foreach($ids as $id)
-        { $sgbd->del_data("action_status", $id);
+      if($ids !== false){
+        foreach($ids as $id){
+          $sgbd->del_data("actions_roles", $id);
         }
       }
       return true;
     }
 
-    function uninstall_sqlite($env)
-    { $data = $env->data();
+    function uninstall_sql($env){
+      $data = $env->data();
       $sgbd = $data->sgbd();
       if(!$this->disable($env)) return "impossible de desactiver le plugin";
       $data->del_config("plugins_pages_start_id");
-      $EXISTS = $sgbd->table_exists("#--pages");
-      if(!isset($EXISTS))
-      { return "impossible de savoir si la table #--pages existe";
+      try{
+        $EXISTS = $sgbd->table_exists("#--pages");
       }
-      if(!$EXISTS)
-      { // return "la table #--pages n'existe pas";
+      catch(Exception $e){
+        return "impossible de savoir si la table #--pages existe";
       }
-      elseif(!$sgbd->query("DROP TABLE \"main\".\"#--pages\""))
-      { return "imposible de supprimer la table #--pages";
+      if(!$EXISTS){
+        // return "la table #--pages n'existe pas";
       }
-      $sgbd->query("DELETE FROM #--action_status WHERE action='pages/admin'");
-      return true;
-    }
-
-    function uninstall_mysql($env)
-    { $data = $env->data();
-      $sgbd = $data->sgbd();
-      if(!$this->disable($env)) return "impossible de desactiver le plugin";
-      $data->del_config("plugins_pages_start_id");
-      $EXISTS = $sgbd->table_exists("#--pages");
-      if(!isset($EXISTS))
-      { return "impossible de savoir si la table #--pages existe";
+      else{
+        try{
+          $sgbd->query("DROP TABLE #--pages");
+        }
+        catch(Exception $e){
+          return "imposible de supprimer la table #--pages";
+        }
       }
-      if(!$EXISTS)
-      { // return "la table #--pages n'existe pas";
+      try{
+        $sgbd->query("DELETE FROM #--actions_roles WHERE action='pages/admin'");
       }
-      elseif(!$sgbd->query("DROP TABLE #--pages"))
-      { return "imposible de supprimer la table #--pages";
+      catch(Exception $e){
+        return
+         "Le plugin a bien &eacute;t&eacute; d&eacute;sinstall&eacute;"
+        ." mais impossible d'enlever les droits pages/admin/* de la table actions_roles";
       }
-      $sgbd->query("DELETE FROM #--action_status WHERE action='pages/admin'");
       return true;
     }
 
   }
 
-
 ?>
\ No newline at end of file