Index: extensions/AMenuManager/amm_install.class.inc.php
===================================================================
--- extensions/AMenuManager/amm_install.class.inc.php	(revision 5545)
+++ extensions/AMenuManager/amm_install.class.inc.php	(revision 8962)
@@ -3,5 +3,5 @@
   Plugin     : Advanced Menu Manager
   Author     : Grum
-    email    : grum@grum.dnsalias.com
+    email    : grum@piwigo.org
     website  : http://photos.grum.fr
     PWG user : http://forum.phpwebgallery.net/profile.php?id=3706
@@ -21,18 +21,25 @@
   class AMM_install extends AMM_root
   {
-    private $tablesManager;
-    private $exportfile;
-
-    public function AMM_install($prefixeTable, $filelocation)
+    private $tablef;
+
+    public function __construct($prefixeTable, $filelocation)
     {
       parent::__construct($prefixeTable, $filelocation);
-      $this->tablesManager= new GPCTables($this->tables);
-      $this->exportfile=dirname($this->getFileLocation()).'/'.$this->getPluginNameFiles().'.sql';
-    }
-
-    /*
-        function for installation process
-        return true if install process is ok, otherwise false
-    */
+      $this->tablef= new GPCTables($this->tables);
+    }
+
+    public function __destruct()
+    {
+      unset($this->tablesManager);
+      unset($this->tablef);
+      parent::__destruct();
+    }
+
+
+    /**
+     * function for installation process
+     *
+     * @return Bool : true if install process is ok, otherwise false
+     */
     public function install()
     {
@@ -40,4 +47,5 @@
       $this->loadConfig();
       $this->config['installed']=AMM_VERSION2;
+      $this->config['newInstall']='y';
       $this->saveConfig();
 
@@ -51,4 +59,6 @@
   `position` int(11) NOT NULL default '0',
   `visible` char(1) NOT NULL default 'y',
+  `accessUsers` varchar(1024) NOT NULL,
+  `accessGroups` varchar(1024) NOT NULL,
   PRIMARY KEY  (`id`),
   KEY `order_key` (`position`)
@@ -56,28 +66,47 @@
 
 "CREATE TABLE  `".$this->tables['personalised']."` (
-  `id` int(11) NOT NULL default '0',
-  `lang` varchar(5) NOT NULL default '',
-  `title` varchar(255) NOT NULL default '',
-  `content` text NOT NULL,
+  `id` int(11) NOT NULL auto_increment,
   `visible` char(1) NOT NULL default 'y',
   `nfo` varchar(255) NOT NULL default '',
-  PRIMARY KEY  (`id`,`lang`)
+  PRIMARY KEY  (`id`)
+)",
+
+"CREATE TABLE `".$this->tables['personalised_langs']."` (
+  `id` INTEGER UNSIGNED NOT NULL DEFAULT 0,
+  `lang` CHAR(5)  NOT NULL default '*',
+  `title` VARCHAR(255)  NOT NULL default '',
+  `content` TEXT  NOT NULL,
+  PRIMARY KEY (`id`, `lang`)
+)",
+
+"CREATE TABLE `".$this->tables['blocks']."` (
+  `id` VARCHAR(40)  NOT NULL,
+  `order` INTEGER UNSIGNED NOT NULL,
+  `users` VARCHAR(1024)  NOT NULL,
+  `groups` VARCHAR(1024)  NOT NULL,
+  PRIMARY KEY (`id`),
+  INDEX `byOrder`(`order`)
 )"
+
 );
       //$table_def array
       $tables_def = create_table_add_character_set($tables_def);
-      $result=$this->tablesManager->create($tables_def);
+      $result=$this->tablef->create($tables_def);
+      unset($tables_def);
+
+      GPCCore::register($this->getPluginName(), AMM_VERSION, AMM_GPC_NEEDED);
+
       return($result);
     }
 
 
-    /*
-        function for uninstall process
-    */
+    /**
+     * function for uninstall process
+     */
     public function uninstall()
     {
-      $this->tablesManager->export($this->exportfile);
       $this->deleteConfig();
-      $this->tablesManager->drop();
+      $this->tablef->drop();
+      GPCCore::unregister($this->getPluginName());
     }
 
@@ -91,10 +120,16 @@
       $this->udpateTablesDef();
 
+      $this->config['newInstall']='n';
       $this->config['installed']=AMM_VERSION2; //update the installed release number
       $this->saveConfig();
+
+      GPCCore::register($this->getPluginName(), AMM_VERSION, AMM_GPC_NEEDED);
     }
 
     public function deactivate()
     {
+      $this->initConfig();
+      $this->loadConfig();
+      $this->restoreMenuConfig();
     }
 
@@ -106,8 +141,10 @@
     protected function udpateTablesDef()
     {
+      global $conf;
+
       /* AMM release earlier than the 2.1.3 uses two parameters to manage the display
        * of the menu items ("amm_sections_modspecials" and "amm_sections_modmenu")
        *
-       * These two parameters are replaced by a single parameter "amm_sections_items"
+       * These two parameters are replaced by a single parameter "amm_blocks_items"
        *
        * This function aim to import the old conf into the new conf property
@@ -117,5 +154,5 @@
         foreach($this->config['amm_sections_modspecials'] as $key=>$val)
         {
-          $this->config['amm_sections_items'][$key]['visibility']=($val=="y")?"guest,generic,normal,admin/":"admin/";
+          $this->config['amm_blocks_items'][$key]['visibility']=($val=="y")?"guest,generic,normal,admin/":"admin/";
         }
         unset($this->config['amm_sections_modspecials']);
@@ -126,5 +163,5 @@
         foreach($this->config['amm_sections_modmenu'] as $key=>$val)
         {
-          $this->config['amm_sections_items'][$key]['visibility']=($val=="y")?"guest,generic,normal,admin/":"admin/";
+          $this->config['amm_blocks_items'][$key]['visibility']=($val=="y")?"guest,generic,normal,admin/":"admin/";
         }
         unset($this->config['amm_sections_modmenu']);
@@ -140,24 +177,157 @@
       }
 
-      if($this->config['installed']<="02.01.06")
-      {
-        /*
-         * 2.2.0 updates
-         *
-         * - update fields length for table 'personalised'
-         * - update config for menu translation
-         */
-        $sql="ALTER TABLE `".$this->tables['personalised']."`
-              MODIFY COLUMN `title` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
-              MODIFY COLUMN `nfo` VARCHAR(255)  CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;";
-        pwg_query($sql);
-
-        foreach($this->config['amm_sections_items'] as $key => $val)
+      switch($this->config['installed'])
+      {
+        case '02.01.06':
+          $this->config['newInstall']='n';
+          $this->updateFrom_020106();
+        case '02.02.00':
+          $this->config['newInstall']='n';
+          $this->updateFrom_020200();
+        default:
+          /*
+           * default is applied for fresh install
+           */
+
+          if($this->config['installed']<='02.02.02' or
+             $this->config['newInstall']=='y')
+          {
+            /*
+             * if    new install
+             *    or plugin updated from a release <= 2.2.0
+             *    or plugin
+             *
+             * update AMM menu from piwigo's menu
+             */
+            $this->backupMenuConfig(true);
+          }
+          else
+          {
+            /*
+             * plugin actived without being installed or updated, only backup
+             * the piwigo's menu
+             */
+            $this->backupMenuConfig(false);
+          }
+          break;
+      }
+
+    }
+
+    /**
+     * update the database from the release 2.1.6
+     *
+     * - update config for menu translation
+     * - update fields length for table 'personalised'
+     */
+    private function updateFrom_020106()
+    {
+      $sql="ALTER TABLE `".$this->tables['personalised']."`
+            MODIFY COLUMN `id` INTEGER  NOT NULL AUTO_INCREMENT,
+            MODIFY COLUMN `title` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
+            MODIFY COLUMN `nfo` VARCHAR(255)  CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;";
+      pwg_query($sql);
+
+      foreach($this->config['amm_blocks_items'] as $key => $val)
+      {
+        $this->config['amm_blocks_items'][$key]['translation'] = $this->defaultMenus[$key]['translation'];
+      }
+    }
+
+
+
+    /**
+     * update the database from the release 2.2.0
+     *
+     * - create 'personalised_lang' table ; filled from the 'personalised' table
+     *   values
+     * - modify 'personalised' table structure (remove lang attributes)
+     * - modify 'urls' table structure (add users&group access)
+     * - update 'urls' table values (default values for users access)
+     * - update config (parameter 'amm_sections_items' is renamed into 'amm_blocks_items')
+     */
+    private function updateFrom_020200()
+    {
+      $tables_def=array(
+"CREATE TABLE `".$this->tables['personalised_langs']."` (
+  `id` INTEGER UNSIGNED NOT NULL DEFAULT 0,
+  `lang` CHAR(5)  NOT NULL default '*',
+  `title` VARCHAR(255)  NOT NULL default '',
+  `content` TEXT  NOT NULL,
+  PRIMARY KEY (`id`, `lang`)
+)",
+
+"CREATE TABLE `".$this->tables['blocks']."` (
+  `id` VARCHAR(40)  NOT NULL,
+  `order` INTEGER UNSIGNED NOT NULL,
+  `users` VARCHAR(1024)  NOT NULL,
+  `groups` VARCHAR(1024)  NOT NULL,
+  PRIMARY KEY (`id`),
+  INDEX `byOrder`(`order`)
+)"
+      );
+
+      $tables_def = create_table_add_character_set($tables_def);
+      $result=$this->tablef->create($tables_def);
+
+      $sql="INSERT INTO `".$this->tables['personalised_langs']."`
+              SELECT pap.id, pap.lang, pap.title, pap.content
+              FROM `".$this->tables['personalised']."` pap
+              WHERE pap.title!='' OR pap.content!='';";
+      pwg_query($sql);
+
+      $sql="ALTER TABLE `".$this->tables['personalised']."` DROP COLUMN `lang`,
+             DROP COLUMN `title`,
+             DROP COLUMN `content`;";
+      pwg_query($sql);
+
+      $sql="ALTER TABLE `".$this->tables['urls']."`
+            ADD COLUMN `accessUsers` VARCHAR(1024)  NOT NULL AFTER `visible`,
+            ADD COLUMN `accessGroups` VARCHAR(1024)  NOT NULL AFTER `accessUser`;";
+      pwg_query($sql);
+
+
+      $users=new GPCUsers();
+      $sql="UPDATE `".$this->tables['urls']."`
+            SET accessUsers='".pwg_db_real_escape_string(implode(',', $users->getList()))."';";
+      pwg_query($sql);
+
+      if(isset($this->config['amm_sections_items']))
+      {
+        $this->config['amm_blocks_items']=$this->config['amm_sections_items'];
+        unset($this->config['amm_sections_items']);
+      }
+    }
+
+    /**
+     * report hidden menu from piwigo's config to AMM config
+     */
+    private function backupMenuConfig($updateMenu=false)
+    {
+      global $conf;
+
+      $this->config['amm_old_blk_menubar']=$conf['blk_menubar'];
+      pwg_query("UPDATE ".CONFIG_TABLE." SET value = '' WHERE param='blk_menubar';");
+
+      if($updateMenu)
+      {
+        $tmp=unserialize($conf['blk_menubar']);
+        foreach($tmp as $key => $val)
         {
-          $this->config['amm_sections_items'][$key]['translation'] = $this->defaultMenus[$key]['translation'];
+          pwg_query("REPLACE INTO ".$this->tables['blocks']." VALUES ('$key', '".abs($val)."', '".($val<0?'guest,generic,normal,webmaster,admin':'')."', '');");
         }
       }
     }
 
+    /**
+     * restore piwigo's menu
+     */
+    private function restoreMenuConfig()
+    {
+      pwg_query("UPDATE ".CONFIG_TABLE." SET value = '".pwg_db_real_escape_string($this->config['amm_old_blk_menubar'])."' WHERE param='blk_menubar';");
+    }
+
+
+
   } //class
 
