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

Last change on this file since 29531 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
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  Plugin     : Advanced Menu Manager
4  Author     : Grum
5    email    : grum@piwigo.org
6    website  : http://www.grum.fr
7
8    << May the Little SpaceFrog be with you ! >>
9  ------------------------------------------------------------------------------
10  See main.inc.php for release information
11
12  AMM_Install : classe to manage plugin install
13
14  --------------------------------------------------------------------------- */
15  include_once('amm_version.inc.php');
16  include_once('amm_root.class.inc.php');
17  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCTables.class.inc.php');
18
19
20  class AMM_install extends AMM_root
21  {
22    private $tablef;
23
24    public function __construct($prefixeTable, $filelocation)
25    {
26      parent::__construct($prefixeTable, $filelocation);
27      $this->tablef= new GPCTables($this->tables);
28    }
29
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     */
43    public function install()
44    {
45      $this->initConfig();
46      $this->loadConfig();
47      $this->config['installed']=AMM_VERSION2;
48      $this->config['newInstall']='y';
49      $this->saveConfig();
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',
60  `accessUsers` varchar(1024) NOT NULL,
61  `accessGroups` varchar(1024) NOT NULL,
62  PRIMARY KEY  (`id`),
63  KEY `order_key` (`position`)
64) DEFAULT CHARACTER SET ".DB_CHARSET." COLLATE utf8_general_ci",
65
66"CREATE TABLE  `".$this->tables['personalised']."` (
67  `id` int(11) NOT NULL auto_increment,
68  `visible` char(1) NOT NULL default 'y',
69  `nfo` varchar(255) NOT NULL default '',
70  PRIMARY KEY  (`id`)
71) DEFAULT CHARACTER SET ".DB_CHARSET." COLLATE utf8_general_ci",
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`)
79) DEFAULT CHARACTER SET ".DB_CHARSET." COLLATE utf8_general_ci",
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`)
88) DEFAULT CHARACTER SET ".DB_CHARSET." COLLATE utf8_general_ci"
89
90);
91      //$table_def array
92      //$tables_def = create_table_add_character_set($tables_def);
93      $result=$this->tablef->create($tables_def);
94      unset($tables_def);
95
96      GPCCore::register($this->getPluginName(), AMM_VERSION, AMM_GPC_NEEDED);
97
98      return($result);
99    }
100
101
102    /**
103     * function for uninstall process
104     */
105    public function uninstall()
106    {
107      $this->deleteConfig();
108      $this->tablef->drop();
109      GPCCore::unregister($this->getPluginName());
110    }
111
112    public function activate()
113    {
114      global $template;
115
116      $this->initConfig();
117      $this->loadConfig();
118
119      $this->udpateTablesDef();
120
121      $this->config['newInstall']='n';
122      $this->config['installed']=AMM_VERSION2; //update the installed release number
123      $this->saveConfig();
124
125      GPCCore::register($this->getPluginName(), AMM_VERSION, AMM_GPC_NEEDED);
126    }
127
128    public function deactivate()
129    {
130      $this->initConfig();
131      $this->loadConfig();
132      $this->restoreMenuConfig();
133    }
134
135
136    /**
137     * update tables & config between releases
138     *
139     */
140    protected function udpateTablesDef()
141    {
142      global $conf;
143
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       *
147       * These two parameters are replaced by a single parameter "amm_blocks_items"
148       *
149       * This function aim to import the old conf into the new conf property
150      */
151      if(isset($this->config['amm_sections_modspecials']))
152      {
153        foreach($this->config['amm_sections_modspecials'] as $key=>$val)
154        {
155          $this->config['amm_blocks_items'][$key]['visibility']=($val=="y")?"guest,generic,normal,admin/":"admin/";
156        }
157        unset($this->config['amm_sections_modspecials']);
158      }
159
160      if(isset($this->config['amm_sections_modmenu']))
161      {
162        foreach($this->config['amm_sections_modmenu'] as $key=>$val)
163        {
164          $this->config['amm_blocks_items'][$key]['visibility']=($val=="y")?"guest,generic,normal,admin/":"admin/";
165        }
166        unset($this->config['amm_sections_modmenu']);
167      }
168
169      if(!array_key_exists('installed', $this->config))
170      {
171        /*
172         * if key does not exist, probably try to update a plugin older than the
173         * 2.2.0 release
174         */
175        $this->config['installed']="02.01.06";
176      }
177
178      switch($this->config['installed'])
179      {
180        case '02.01.06':
181          $this->config['newInstall']='n';
182          $this->updateFrom_020106();
183        case '02.02.00':
184        case '02.02.01':
185        case '02.02.02':
186        case '02.02.03':
187          $this->config['newInstall']='n';
188          $this->updateFrom_020200();
189        case '03.00.00':
190          $this->config['newInstall']='n';
191          $this->updateFrom_030000();
192        default:
193          /*
194           * default is applied for fresh install
195           */
196
197          if($this->config['installed']<='02.02.03' or
198             $this->config['newInstall']=='y')
199          {
200            /*
201             * if    new install
202             *    or plugin updated from a release <= 2.2.3
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    {
256      global $user;
257
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`)
265)  DEFAULT CHARACTER SET ".DB_CHARSET." COLLATE utf8_general_ci",
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`)
274)  DEFAULT CHARACTER SET ".DB_CHARSET." COLLATE utf8_general_ci"
275      );
276
277      $this->tablef->setTables(array(
278        $this->tables['personalised_langs'],
279        $this->tables['blocks'])
280      );
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
289
290      if(!isset($user['language']) or $user['language']=='')
291      {
292        $sql="SELECT language FROM ".USER_INFOS_TABLE." WHERE user_id='1';";
293        $result=pwg_query($sql);
294        if($result)
295        {
296          while($row=pwg_db_fetch_assoc($result))
297          {
298            $user['language']=$row['language'];
299          }
300        }
301      }
302      $sql="DELETE FROM `".$this->tables['personalised']."` WHERE lang!='".$user['language']."';";
303      pwg_query($sql);
304
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`,
312            ADD COLUMN `accessGroups` VARCHAR(1024)  NOT NULL AFTER `accessUsers`;";
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      }
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      }
328    }
329
330
331
332    /**
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    /**
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
358      if($updateMenu and $conf['blk_menubar']!='')
359      {
360        $tmp=unserialize($conf['blk_menubar']);
361        foreach($tmp as $key => $val)
362        {
363          pwg_query("REPLACE INTO ".$this->tables['blocks']." VALUES ('$key', '".abs($val)."', '".($val<0?'guest,generic,normal,webmaster,admin':'')."', '');");
364        }
365      }
366    }
367
368    /**
369     * restore piwigo's menu
370     */
371    private function restoreMenuConfig()
372    {
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';");
375    }
376
377
378
379  } //class
380
381?>
Note: See TracBrowser for help on using the repository browser.