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

Last change on this file since 28566 was 16445, checked in by grum, 12 years ago

feature:2642- compatibility with Piwigo 2.4

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