-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdetails.php
353 lines (313 loc) · 9.94 KB
/
details.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php defined('BASEPATH') or exit('No direct script access allowed');
class Module_Polls extends Module {
public $version = '1.1.1';
const MIN_PHP_VERSION = '5.3.0';
/**
* Module information
*
* @access public
* @return void
*/
public function info()
{
return array(
'name' => array(
'br' => 'Enquetes',
'en' => 'Polls',
'es' => 'Encuestas',
'lt' => 'Apklausos',
'it' => 'Sondaggi',
),
'description' => array(
'br' => 'Cria impresionates enquetes',
'en' => 'Create totally awesome polls.',
'es' => 'Crea encuestas impresionantes',
'lt' => 'Kurkite apklausas.',
'it' => 'Crea fantastici Sondaggi',
),
'frontend' => TRUE,
'backend' => TRUE,
'menu' => 'content',
'shortcuts' => array(
array(
'name' => 'polls:new_poll_label',
'uri' => 'admin/polls/insert',
),
),
);
}
/**
* Check the current version of PHP and throw an error if it's not good enough
*
* @access private
* @return boolean
*/
private function check_php_version()
{
// If current version of PHP is not up snuff
if (version_compare(PHP_VERSION, self::MIN_PHP_VERSION) < 0)
{
show_error('This add-on requires PHP version ' . self::MIN_PHP_VERSION . ' or higher.');
return FALSE;
}
}
/**
* Install module
*
* @access public
* @return bool
*/
public function install()
{
$this->check_php_version();
// Make sure all tables are gone first
$this->uninstall();
// Start transaction
$this->db->trans_start();
// Create polls table
$this->db->query("
CREATE TABLE IF NOT EXISTS `" . $this->db->dbprefix('polls') . "` (
`id` tinyint(11) unsigned NOT NULL AUTO_INCREMENT,
`slug` varchar(64) NOT NULL,
`title` varchar(64) NOT NULL,
`type` enum('single','multiple') NOT NULL DEFAULT 'single',
`description` text,
`open_date` int(16) unsigned DEFAULT NULL,
`close_date` int(16) unsigned DEFAULT NULL,
`created` int(16) unsigned NOT NULL,
`last_updated` int(16) unsigned DEFAULT NULL,
`multiple_votes` tinyint(1) unsigned NOT NULL DEFAULT '0',
`comments_enabled` tinyint(1) unsigned NOT NULL DEFAULT '0',
`members_only` tinyint(1) unsigned NOT NULL DEFAULT '0',
`active` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
// Create poll_options table
$this->db->query("
CREATE TABLE IF NOT EXISTS `" . $this->db->dbprefix('poll_options') . "` (
`id` smallint(11) unsigned NOT NULL AUTO_INCREMENT,
`poll_id` tinyint(11) unsigned NOT NULL,
`type` enum('defined','other') NOT NULL DEFAULT 'defined',
`title` varchar(256) NOT NULL,
`order` tinyint(2) unsigned DEFAULT NULL,
`votes` mediumint(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `poll_id` (`poll_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
// Create poll_other_votes table
$this->db->query("
CREATE TABLE IF NOT EXISTS `" . $this->db->dbprefix('poll_other_votes') . "` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` smallint(11) unsigned NOT NULL,
`text` tinytext NOT NULL,
`created` int(16) NOT NULL,
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
// Create poll_voters table
$this->db->query("
CREATE TABLE IF NOT EXISTS `" . $this->db->dbprefix('poll_voters') . "` (
`id` mediumint(32) unsigned NOT NULL AUTO_INCREMENT,
`poll_id` tinyint(11) unsigned NOT NULL,
`user_id` smallint(5) unsigned DEFAULT NULL,
`session_id` varchar(40) NOT NULL,
`ip_address` varchar(16) NOT NULL,
`timestamp` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `poll_id` (`poll_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
// Referental integrity fo' sho
$this->db->query("
ALTER TABLE `" . $this->db->dbprefix('poll_options') . "`
ADD CONSTRAINT `poll_options_ibfk_1`
FOREIGN KEY (`poll_id`)
REFERENCES `" . $this->db->dbprefix('polls') . "` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
");
$this->db->query("
ALTER TABLE `" . $this->db->dbprefix('poll_other_votes') . "`
ADD CONSTRAINT `poll_other_votes_ibfk_1`
FOREIGN KEY (`parent_id`)
REFERENCES `" . $this->db->dbprefix('poll_options') . "` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
");
$this->db->query("
ALTER TABLE `" . $this->db->dbprefix('poll_voters') . "`
ADD CONSTRAINT `poll_votes_ibfk_1`
FOREIGN KEY (`poll_id`)
REFERENCES `" . $this->db->dbprefix('polls') . "` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
");
// End transaction
$this->db->trans_complete();
// If transaction was successful return TRUE, else FALSE
return $this->db->trans_status() ? TRUE : FALSE;
}
/**
* Uninstall module
*
* Due to foreign key constraints we must drop tables in a very specific
* order.
*
* @access public
* @return bool
*/
public function uninstall()
{
$this->dbforge->drop_table('poll_voters');
$this->dbforge->drop_table('poll_other_votes');
$this->dbforge->drop_table('poll_options');
$this->dbforge->drop_table('polls');
return TRUE;
}
/**
* Upgrade module
*
* @access public
* @param string
* @return bool
*/
public function upgrade($old_version)
{
$this->check_php_version();
// Start transaction
$this->db->trans_start();
// Version 0.4 (the first official release)
if ($old_version == '0.4')
{
// Update polls table
$this->db->query("
ALTER TABLE `polls` ADD `type` enum('single','multiple') NOT NULL DEFAULT 'single'
");
// Update poll_options table
$this->db->query("
ALTER TABLE `poll_options` ADD `type` enum('defined','other') NOT NULL DEFAULT 'defined'
");
// Add poll_other_votes table
$this->db->query("
CREATE TABLE IF NOT EXISTS `poll_other_votes` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` smallint(11) unsigned NOT NULL,
`text` tinytext NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
// Add poll_voters table
$this->db->query("
CREATE TABLE IF NOT EXISTS `poll_voters` (
`id` mediumint(32) unsigned NOT NULL AUTO_INCREMENT,
`poll_id` tinyint(11) unsigned NOT NULL,
`user_id` smallint(5) unsigned DEFAULT NULL,
`session_id` varchar(40) NOT NULL,
`ip_address` varchar(16) NOT NULL,
`timestamp` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `poll_id` (`poll_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
// Referental integrity fo' sho
$this->db->query("
ALTER TABLE `poll_other_votes`
ADD CONSTRAINT `poll_other_votes_ibfk_1`
FOREIGN KEY (`parent_id`)
REFERENCES `poll_options` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
");
$this->db->query("
ALTER TABLE `poll_voters`
ADD CONSTRAINT `poll_votes_ibfk_1`
FOREIGN KEY (`poll_id`)
REFERENCES `polls` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
");
}
// Version 0.5
elseif ($old_version == '0.5')
{
$this->db->query("
ALTER TABLE `polls` ADD `multiple_votes` TINYINT(1) NOT NULL DEFAULT '0' AFTER `last_updated`
");
}
// If less than version 0.8
if ($old_version < '0.8')
{
// Rename all tables to add prefix
$this->db->query("RENAME TABLE `polls` TO `" . $this->db->dbprefix('polls') . "`");
$this->db->query("RENAME TABLE `poll_options` TO `" . $this->db->dbprefix('poll_options') . "`");
$this->db->query("RENAME TABLE `poll_other_votes` TO `" . $this->db->dbprefix('poll_other_votes') . "`");
$this->db->query("RENAME TABLE `poll_voters` TO `" . $this->db->dbprefix('poll_voters') . "`");
$this->db->query("RENAME TABLE `poll_options` TO `" . $this->db->dbprefix('poll_options') . "`");
$this->db->query("RENAME TABLE `poll_other_votes` TO `" . $this->db->dbprefix('poll_other_votes') . "`");
$this->db->query("RENAME TABLE `poll_voters` TO `" . $this->db->dbprefix('poll_voters') . "`");
}
if ($old_version < '1.0.1')
{
/**
* Versions less than 1.0 had a TIMESTAMP type for
* poll_other_options (makes more sense IMO, but everything else in
* PyroCMS is using UNIX timestamps)
*/
// Create temp table
$this->db->query("
CREATE TEMPORARY TABLE poll_other_votes_timestamps (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` smallint(11) unsigned NOT NULL,
`text` tinytext NOT NULL,
`created` int(16) NOT NULL,
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET utf8
");
// Insert data into temp table
$this->db->query("
INSERT INTO poll_other_votes_timestamps
SELECT id, parent_id, text, UNIX_TIMESTAMP(created)
FROM " . $this->db->dbprefix('poll_other_votes')
);
// Empty other votes table
$this->db->query("TRUNCATE TABLE " . $this->db->dbprefix('poll_other_votes'));
// Alter other votes table table
$this->db->query("
ALTER TABLE `" . $this->db->dbprefix('poll_other_votes') . "` CHANGE `created` `created` INT(16) NOT NULL
");
// Insert data from temp table into newly altered other votes table
$this->db->query("INSERT INTO " . $this->db->dbprefix('poll_other_votes') . " SELECT * FROM poll_other_votes_timestamps");
// Make poll option titles longer
$this->db->query("
ALTER TABLE `" . $this->db->dbprefix('poll_options') . "` CHANGE `title` `title` VARCHAR(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL
");
// Add "active" status for polls
$this->db->query("
ALTER TABLE `" . $this->db->dbprefix('polls') . "` ADD `active` TINYINT(1) NOT NULL DEFAULT '0'
");
}
// End transaction
$this->db->trans_complete();
// If transaction was successful return TRUE, else FALSE
return $this->db->trans_status();
}
/**
* Help
*
* @access public
* @return string
*/
public function help()
{
return '<a href="https://github.com/vmichnowicz/polls">View Source on Github</a>';
}
}
// EOF