1 | <?php |
---|
2 | defined('PHPWG_ROOT_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | class Subscribe_to_Comments_maintain extends PluginMaintain |
---|
5 | { |
---|
6 | private $installed = false; |
---|
7 | |
---|
8 | private $default_conf = array( |
---|
9 | 'notify_admin_on_subscribe' => false, |
---|
10 | 'allow_global_subscriptions' => true, |
---|
11 | ); |
---|
12 | |
---|
13 | private $table; |
---|
14 | |
---|
15 | function __construct($plugin_id) |
---|
16 | { |
---|
17 | parent::__construct($plugin_id); |
---|
18 | |
---|
19 | global $prefixeTable; |
---|
20 | $this->table = $prefixeTable . 'subscribe_to_comments'; |
---|
21 | } |
---|
22 | |
---|
23 | function install($plugin_version, &$errors=array()) |
---|
24 | { |
---|
25 | global $conf; |
---|
26 | |
---|
27 | // config parameter |
---|
28 | if (empty($conf['Subscribe_to_Comments'])) |
---|
29 | { |
---|
30 | $conf['Subscribe_to_Comments'] = serialize($this->default_config); |
---|
31 | conf_update_param('Subscribe_to_Comments', $conf['Subscribe_to_Comments']); |
---|
32 | } |
---|
33 | |
---|
34 | // subscriptions table |
---|
35 | pwg_query(' |
---|
36 | CREATE TABLE IF NOT EXISTS `'. $this->table .'` ( |
---|
37 | `id` int(11) unsigned NOT NULL AUTO_INCREMENT, |
---|
38 | `type` enum("image","album-images","album","all-images","all-albums") NOT NULL DEFAULT "image", |
---|
39 | `element_id` mediumint(8) DEFAULT NULL, |
---|
40 | `language` varchar(64) NOT NULL, |
---|
41 | `email` varchar(255) NOT NULL, |
---|
42 | `registration_date` datetime NOT NULL, |
---|
43 | `validated` enum("true","false") NOT NULL DEFAULT "false", |
---|
44 | PRIMARY KEY (`id`), |
---|
45 | UNIQUE KEY `UNIQUE` (`email` , `type` , `element_id`) |
---|
46 | ) DEFAULT CHARSET=utf8 |
---|
47 | ;'); |
---|
48 | |
---|
49 | // table structure upgrade in 1.1 |
---|
50 | $result = pwg_query('SHOW COLUMNS FROM `'. $this->table .'` LIKE "element_id";'); |
---|
51 | if (!pwg_db_num_rows($result)) |
---|
52 | { |
---|
53 | // backup subscriptions |
---|
54 | $query = 'SELECT * FROM `'. $this->table .'` ORDER BY registration_date;'; |
---|
55 | $subscriptions = query2array($query, 'id'); |
---|
56 | |
---|
57 | // upgrade the table |
---|
58 | pwg_query('TRUNCATE TABLE `'. $this->table .'`;'); |
---|
59 | pwg_query('ALTER TABLE `'. $this->table .'` DROP `image_id`, DROP `category_id`;'); |
---|
60 | pwg_query('ALTER TABLE `'. $this->table .'` AUTO_INCREMENT = 1;'); |
---|
61 | |
---|
62 | $query = ' |
---|
63 | ALTER TABLE `'. $this->table .'` |
---|
64 | ADD `type` enum("image", "album-images", "album", "all-images", "all-albums") NOT NULL DEFAULT "image" AFTER `id`, |
---|
65 | ADD `element_id` mediumint(8) NULL AFTER `type`, |
---|
66 | ADD `language` varchar(64) NOT NULL AFTER `element_id` |
---|
67 | ;'; |
---|
68 | pwg_query($query); |
---|
69 | |
---|
70 | pwg_query('ALTER TABLE `'. $this->table .'` DROP INDEX `UNIQUE`, ADD UNIQUE `UNIQUE` (`email`, `type`, `element_id`);'); |
---|
71 | |
---|
72 | // convert datas and fill the table |
---|
73 | $inserts = array(); |
---|
74 | |
---|
75 | foreach ($subscriptions as $row) |
---|
76 | { |
---|
77 | if (!empty($row['category_id'])) |
---|
78 | { |
---|
79 | $row['type'] = 'album'; |
---|
80 | $row['element_id'] = $row['category_id']; |
---|
81 | } |
---|
82 | else if (!empty($row['image_id'])) |
---|
83 | { |
---|
84 | $row['type'] = 'image'; |
---|
85 | $row['element_id'] = $row['image_id']; |
---|
86 | } |
---|
87 | else |
---|
88 | { |
---|
89 | continue; |
---|
90 | } |
---|
91 | |
---|
92 | unset($row['id'], $row['image_id'], $row['category_id']); |
---|
93 | $row['language'] = 'en_UK'; |
---|
94 | |
---|
95 | array_push($inserts, $row); |
---|
96 | } |
---|
97 | |
---|
98 | if (count($inserts) > 0) |
---|
99 | { |
---|
100 | $dbfields = array('type', 'element_id', 'language', 'email', 'registration_date', 'validated'); |
---|
101 | mass_inserts($this->table, $dbfields, $inserts); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | $this->installed = true; |
---|
106 | } |
---|
107 | |
---|
108 | function activate($plugin_version, &$errors=array()) |
---|
109 | { |
---|
110 | if (!$this->installed) |
---|
111 | { |
---|
112 | $this->install($plugin_version, $errors); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | function deactivate() |
---|
117 | { |
---|
118 | } |
---|
119 | |
---|
120 | function uninstall() |
---|
121 | { |
---|
122 | conf_delete_param('Subscribe_to_Comments'); |
---|
123 | |
---|
124 | pwg_query('DROP TABLE `'. $this->table .'`;'); |
---|
125 | } |
---|
126 | } |
---|