source: extensions/AMenuManager/amm_install.class.inc.php @ 9484

Last change on this file since 9484 was 9113, checked in by grum, 13 years ago

release 3.0.3
fix bug:2166,bug:2182

  • Property svn:executable set to *
File size: 11.2 KB
RevLine 
[3681]1<?php
2/* -----------------------------------------------------------------------------
3  Plugin     : Advanced Menu Manager
4  Author     : Grum
[8962]5    email    : grum@piwigo.org
[5421]6    website  : http://photos.grum.fr
[3681]7    PWG user : http://forum.phpwebgallery.net/profile.php?id=3706
8
9    << May the Little SpaceFrog be with you ! >>
10  ------------------------------------------------------------------------------
11  See main.inc.php for release information
12
13  MyPolls_Install : classe to manage plugin install
14
15  --------------------------------------------------------------------------- */
[5421]16  include_once('amm_version.inc.php');
17  include_once('amm_root.class.inc.php');
[5545]18  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCTables.class.inc.php');
[3681]19
20
21  class AMM_install extends AMM_root
22  {
[8962]23    private $tablef;
[3681]24
[8962]25    public function __construct($prefixeTable, $filelocation)
[3681]26    {
27      parent::__construct($prefixeTable, $filelocation);
[8962]28      $this->tablef= new GPCTables($this->tables);
[3681]29    }
30
[8962]31    public function __destruct()
32    {
33      unset($this->tablesManager);
34      unset($this->tablef);
35      parent::__destruct();
36    }
37
38
39    /**
40     * function for installation process
41     *
42     * @return Bool : true if install process is ok, otherwise false
43     */
[3681]44    public function install()
45    {
[5545]46      $this->initConfig();
47      $this->loadConfig();
48      $this->config['installed']=AMM_VERSION2;
[8962]49      $this->config['newInstall']='y';
[5545]50      $this->saveConfig();
[3681]51
52      $tables_def=array(
53"CREATE TABLE  `".$this->tables['urls']."` (
54  `id` int(11) NOT NULL auto_increment,
55  `label` varchar(50) NOT NULL default '',
56  `url` varchar(255) NOT NULL default '',
57  `mode` int(11) NOT NULL default '0',
58  `icon` varchar(50) NOT NULL default '',
59  `position` int(11) NOT NULL default '0',
60  `visible` char(1) NOT NULL default 'y',
[8962]61  `accessUsers` varchar(1024) NOT NULL,
62  `accessGroups` varchar(1024) NOT NULL,
[3681]63  PRIMARY KEY  (`id`),
64  KEY `order_key` (`position`)
65)",
66
67"CREATE TABLE  `".$this->tables['personalised']."` (
[8962]68  `id` int(11) NOT NULL auto_increment,
[3681]69  `visible` char(1) NOT NULL default 'y',
[5421]70  `nfo` varchar(255) NOT NULL default '',
[8962]71  PRIMARY KEY  (`id`)
72)",
73
74"CREATE TABLE `".$this->tables['personalised_langs']."` (
75  `id` INTEGER UNSIGNED NOT NULL DEFAULT 0,
76  `lang` CHAR(5)  NOT NULL default '*',
77  `title` VARCHAR(255)  NOT NULL default '',
78  `content` TEXT  NOT NULL,
79  PRIMARY KEY (`id`, `lang`)
80)",
81
82"CREATE TABLE `".$this->tables['blocks']."` (
83  `id` VARCHAR(40)  NOT NULL,
84  `order` INTEGER UNSIGNED NOT NULL,
85  `users` VARCHAR(1024)  NOT NULL,
86  `groups` VARCHAR(1024)  NOT NULL,
87  PRIMARY KEY (`id`),
88  INDEX `byOrder`(`order`)
[3681]89)"
[8962]90
[3681]91);
92      //$table_def array
93      $tables_def = create_table_add_character_set($tables_def);
[8962]94      $result=$this->tablef->create($tables_def);
95      unset($tables_def);
96
97      GPCCore::register($this->getPluginName(), AMM_VERSION, AMM_GPC_NEEDED);
98
[3681]99      return($result);
100    }
101
102
[8962]103    /**
104     * function for uninstall process
105     */
[3681]106    public function uninstall()
107    {
[5545]108      $this->deleteConfig();
[8962]109      $this->tablef->drop();
110      GPCCore::unregister($this->getPluginName());
[3681]111    }
112
113    public function activate()
114    {
115      global $template;
116
[5545]117      $this->initConfig();
118      $this->loadConfig();
[5421]119
120      $this->udpateTablesDef();
121
[8962]122      $this->config['newInstall']='n';
[5545]123      $this->config['installed']=AMM_VERSION2; //update the installed release number
124      $this->saveConfig();
[8962]125
126      GPCCore::register($this->getPluginName(), AMM_VERSION, AMM_GPC_NEEDED);
[5421]127    }
128
129    public function deactivate()
130    {
[8962]131      $this->initConfig();
132      $this->loadConfig();
133      $this->restoreMenuConfig();
[5421]134    }
135
136
137    /**
138     * update tables & config between releases
139     *
140     */
141    protected function udpateTablesDef()
142    {
[8962]143      global $conf;
144
[4391]145      /* AMM release earlier than the 2.1.3 uses two parameters to manage the display
146       * of the menu items ("amm_sections_modspecials" and "amm_sections_modmenu")
147       *
[8962]148       * These two parameters are replaced by a single parameter "amm_blocks_items"
[4391]149       *
150       * This function aim to import the old conf into the new conf property
151      */
[5545]152      if(isset($this->config['amm_sections_modspecials']))
[4391]153      {
[5545]154        foreach($this->config['amm_sections_modspecials'] as $key=>$val)
[4391]155        {
[8962]156          $this->config['amm_blocks_items'][$key]['visibility']=($val=="y")?"guest,generic,normal,admin/":"admin/";
[4391]157        }
[5545]158        unset($this->config['amm_sections_modspecials']);
[4391]159      }
160
[5545]161      if(isset($this->config['amm_sections_modmenu']))
[4391]162      {
[5545]163        foreach($this->config['amm_sections_modmenu'] as $key=>$val)
[4391]164        {
[8962]165          $this->config['amm_blocks_items'][$key]['visibility']=($val=="y")?"guest,generic,normal,admin/":"admin/";
[4391]166        }
[5545]167        unset($this->config['amm_sections_modmenu']);
[4391]168      }
169
[5545]170      if(!array_key_exists('installed', $this->config))
[5421]171      {
172        /*
173         * if key does not exist, probably try to update a plugin older than the
174         * 2.2.0 release
175         */
[5545]176        $this->config['installed']="02.01.06";
[5421]177      }
[3681]178
[8962]179      switch($this->config['installed'])
[5421]180      {
[8962]181        case '02.01.06':
182          $this->config['newInstall']='n';
183          $this->updateFrom_020106();
184        case '02.02.00':
[8990]185        case '02.02.01':
186        case '02.02.02':
[8991]187        case '02.02.03':
[8962]188          $this->config['newInstall']='n';
189          $this->updateFrom_020200();
[9026]190        case '03.00.00':
191          $this->config['newInstall']='n';
192          $this->updateFrom_030000();
[8962]193        default:
194          /*
195           * default is applied for fresh install
196           */
[5421]197
[8991]198          if($this->config['installed']<='02.02.03' or
[8962]199             $this->config['newInstall']=='y')
200          {
201            /*
202             * if    new install
[8991]203             *    or plugin updated from a release <= 2.2.3
[8962]204             *    or plugin
205             *
206             * update AMM menu from piwigo's menu
207             */
208            $this->backupMenuConfig(true);
209          }
210          else
211          {
212            /*
213             * plugin actived without being installed or updated, only backup
214             * the piwigo's menu
215             */
216            $this->backupMenuConfig(false);
217          }
218          break;
219      }
220
221    }
222
223    /**
224     * update the database from the release 2.1.6
225     *
226     * - update config for menu translation
227     * - update fields length for table 'personalised'
228     */
229    private function updateFrom_020106()
230    {
231      $sql="ALTER TABLE `".$this->tables['personalised']."`
232            MODIFY COLUMN `id` INTEGER  NOT NULL AUTO_INCREMENT,
233            MODIFY COLUMN `title` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
234            MODIFY COLUMN `nfo` VARCHAR(255)  CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;";
235      pwg_query($sql);
236
237      foreach($this->config['amm_blocks_items'] as $key => $val)
238      {
239        $this->config['amm_blocks_items'][$key]['translation'] = $this->defaultMenus[$key]['translation'];
240      }
241    }
242
243
244
245    /**
246     * update the database from the release 2.2.0
247     *
248     * - create 'personalised_lang' table ; filled from the 'personalised' table
249     *   values
250     * - modify 'personalised' table structure (remove lang attributes)
251     * - modify 'urls' table structure (add users&group access)
252     * - update 'urls' table values (default values for users access)
253     * - update config (parameter 'amm_sections_items' is renamed into 'amm_blocks_items')
254     */
255    private function updateFrom_020200()
256    {
[9025]257      global $user;
258
[8962]259      $tables_def=array(
260"CREATE TABLE `".$this->tables['personalised_langs']."` (
261  `id` INTEGER UNSIGNED NOT NULL DEFAULT 0,
262  `lang` CHAR(5)  NOT NULL default '*',
263  `title` VARCHAR(255)  NOT NULL default '',
264  `content` TEXT  NOT NULL,
265  PRIMARY KEY (`id`, `lang`)
[9031]266)  DEFAULT CHARACTER SET ".DB_CHARSET." COLLATE ".DB_COLLATE,
[8962]267
268"CREATE TABLE `".$this->tables['blocks']."` (
269  `id` VARCHAR(40)  NOT NULL,
270  `order` INTEGER UNSIGNED NOT NULL,
271  `users` VARCHAR(1024)  NOT NULL,
272  `groups` VARCHAR(1024)  NOT NULL,
273  PRIMARY KEY (`id`),
274  INDEX `byOrder`(`order`)
[9031]275)  DEFAULT CHARACTER SET ".DB_CHARSET." COLLATE ".DB_COLLATE
[8962]276      );
277
[8994]278      $this->tablef->setTables(array(
279        $this->tables['personalised_langs'],
280        $this->tables['blocks'])
281      );
[8962]282      $result=$this->tablef->create($tables_def);
283
284      $sql="INSERT INTO `".$this->tables['personalised_langs']."`
285              SELECT pap.id, pap.lang, pap.title, pap.content
286              FROM `".$this->tables['personalised']."` pap
287              WHERE pap.title!='' OR pap.content!='';";
288      pwg_query($sql);
289
[9027]290
291      if(!isset($user['language']) or $user['language']=='')
292      {
[9028]293        $sql="SELECT language FROM ".USER_INFOS_TABLE." WHERE user_id='1';";
294        $result=pwg_query($sql);
[9027]295        if($result)
296        {
297          while($row=pwg_db_fetch_assoc($result))
298          {
299            $user['language']=$row['language'];
300          }
301        }
302      }
[9025]303      $sql="DELETE FROM `".$this->tables['personalised']."` WHERE lang!='".$user['language']."';";
[9023]304      pwg_query($sql);
305
[8962]306      $sql="ALTER TABLE `".$this->tables['personalised']."` DROP COLUMN `lang`,
307             DROP COLUMN `title`,
308             DROP COLUMN `content`;";
309      pwg_query($sql);
310
311      $sql="ALTER TABLE `".$this->tables['urls']."`
312            ADD COLUMN `accessUsers` VARCHAR(1024)  NOT NULL AFTER `visible`,
[8995]313            ADD COLUMN `accessGroups` VARCHAR(1024)  NOT NULL AFTER `accessUsers`;";
[8962]314      pwg_query($sql);
315
316      if(isset($this->config['amm_sections_items']))
317      {
318        $this->config['amm_blocks_items']=$this->config['amm_sections_items'];
319        unset($this->config['amm_sections_items']);
320      }
[8997]321
322
323      $usersList=array('guest', 'generic', 'normal', 'webmaster', 'admin');
324      foreach($this->config['amm_blocks_items'] as $key => $item)
325      {
326        $tmp0=explode('/', $item['visibility']);
327        $this->config['amm_blocks_items'][$key]['visibility']=implode(',', array_diff($usersList, explode(',', $tmp0[0]))).'/'.$tmp0[1];
328      }
[8962]329    }
330
[9026]331
332
[8962]333    /**
[9026]334     * update the database from the release 3.0.0
335     *
336     * - add auto increment on personnalised_lang table
337     */
338    private function updateFrom_030000()
339    {
340      global $user;
341
342      $sql="ALTER TABLE `".$this->tables['personalised']."` MODIFY COLUMN `id` INTEGER  NOT NULL AUTO_INCREMENT;";
343      pwg_query($sql);
344    }
345
346
347
348
349    /**
[8962]350     * report hidden menu from piwigo's config to AMM config
351     */
352    private function backupMenuConfig($updateMenu=false)
353    {
354      global $conf;
355
356      $this->config['amm_old_blk_menubar']=$conf['blk_menubar'];
357      pwg_query("UPDATE ".CONFIG_TABLE." SET value = '' WHERE param='blk_menubar';");
358
[8971]359      if($updateMenu and $conf['blk_menubar']!='')
[8962]360      {
361        $tmp=unserialize($conf['blk_menubar']);
362        foreach($tmp as $key => $val)
[5421]363        {
[8962]364          pwg_query("REPLACE INTO ".$this->tables['blocks']." VALUES ('$key', '".abs($val)."', '".($val<0?'guest,generic,normal,webmaster,admin':'')."', '');");
[5421]365        }
366      }
[3681]367    }
368
[8962]369    /**
370     * restore piwigo's menu
371     */
372    private function restoreMenuConfig()
373    {
[8971]374      if($this->config['amm_old_blk_menubar']!='')
375        pwg_query("UPDATE ".CONFIG_TABLE." SET value = '".pwg_db_real_escape_string($this->config['amm_old_blk_menubar'])."' WHERE param='blk_menubar';");
[8962]376    }
377
[3681]378  } //class
379
[9019]380
381
382
[3681]383?>
Note: See TracBrowser for help on using the repository browser.