source: extensions/Subscribe_to_comments/include/install.inc.php @ 17922

Last change on this file since 17922 was 17922, checked in by mistic100, 12 years ago

use new upgrade process

File size: 2.9 KB
Line 
1<?php
2defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
3
4function stc_install() 
5{
6  global $conf, $prefixeTable;
7 
8  $stc_table = $prefixeTable . 'subscribe_to_comments';
9 
10  // config parameter
11  if (empty($conf['Subscribe_to_Comments']))
12  {
13    $stc_default_config = serialize(array(
14      'notify_admin_on_subscribe' => false,
15      'allow_global_subscriptions' => true,
16      ));
17 
18    conf_update_param('Subscribe_to_Comments', $stc_default_config);
19    $conf['Subscribe_to_Comments'] = $stc_default_config;
20  }
21 
22  // subscriptions table
23        pwg_query('
24CREATE TABLE IF NOT EXISTS `'.$stc_table.'` (
25  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
26  `type` enum("image","album-images","album","all-images","all-albums") NOT NULL DEFAULT "image",
27  `element_id` mediumint(8) DEFAULT NULL,
28  `language` varchar(64) NOT NULL,
29  `email` varchar(255) NOT NULL,
30  `registration_date` datetime NOT NULL,
31  `validated` enum("true","false") NOT NULL DEFAULT "false",
32  PRIMARY KEY (`id`),
33  UNIQUE KEY `UNIQUE` (`email` , `type` , `element_id`)
34) DEFAULT CHARSET=utf8
35;');
36
37  // table structure upgrade in 1.1
38  $result = pwg_query('SHOW COLUMNS FROM `'.$stc_table.'` LIKE "element_id";');
39  if (!pwg_db_num_rows($result))
40  {
41    // backup subscriptions
42    $query = 'SELECT * FROM `'.$stc_table.'` ORDER BY registration_date;';
43    $subscriptions = hash_from_query($query, 'id');
44   
45    // upgrade the table
46    pwg_query('TRUNCATE TABLE `'.$stc_table.'`;');
47    pwg_query('ALTER TABLE `'.$stc_table.'` DROP `image_id`, DROP `category_id`;');
48    pwg_query('ALTER TABLE `'.$stc_table.'` AUTO_INCREMENT = 1;');
49   
50    $query = '
51ALTER TABLE `'.$stc_table.'`
52  ADD `type` enum("image", "album-images", "album", "all-images", "all-albums") NOT NULL DEFAULT "image" AFTER `id`,
53  ADD `element_id` mediumint(8) NULL AFTER `type`,
54  ADD `language` varchar(64) NOT NULL AFTER `element_id`
55;';
56    pwg_query($query);
57   
58    pwg_query('ALTER TABLE `'.$stc_table.'`  DROP INDEX `UNIQUE`, ADD UNIQUE `UNIQUE` (`email`, `type`, `element_id`);');
59   
60    // convert datas and fill the table
61    $inserts = array();
62   
63    foreach ($subscriptions as $row)
64    {
65      if (!empty($row['category_id']))
66      {
67        $row['type'] = 'album';
68        $row['element_id'] = $row['category_id'];
69      }
70      else if (!empty($row['image_id']))
71      {
72        $row['type'] = 'image';
73        $row['element_id'] = $row['image_id'];
74      }
75      else
76      {
77        continue;
78      }
79     
80      unset($row['id'], $row['image_id'], $row['category_id']);
81      $row['language'] = 'en_UK';
82     
83      array_push($inserts, $row);
84    }
85   
86    if (count($inserts) > 0)
87    {
88      $dbfields = array('type', 'element_id', 'language', 'email', 'registration_date', 'validated');
89      mass_inserts($stc_table, $dbfields, $inserts);
90    }
91  }
92}
93
94?>
Note: See TracBrowser for help on using the repository browser.