forked from brendo/textboxfield
-
Notifications
You must be signed in to change notification settings - Fork 5
/
extension.driver.php
executable file
·385 lines (345 loc) · 9.03 KB
/
extension.driver.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
* @package textboxfield
*/
/**
* An enhanced text input field.
*/
class Extension_TextBoxField extends Extension {
/**
* The name of the field settings table.
*/
const FIELD_TABLE = 'tbl_fields_textbox';
/**
* Publish page headers.
*/
const PUBLISH_HEADERS = 1;
/**
* What headers have been appended?
*
* @var integer
*/
static protected $appendedHeaders = 0;
/**
* Add headers to the page.
*/
static public function appendHeaders($type) {
if (
(self::$appendedHeaders & $type) !== $type
&& class_exists('Administration', false)
&& Administration::instance() instanceof Administration
&& Administration::instance()->Page instanceof HTMLPage
) {
$page = Administration::instance()->Page;
if ($type === self::PUBLISH_HEADERS) {
$page->addStylesheetToHead(URL . '/extensions/textboxfield/assets/textboxfield.publish.css', 'screen', null, false);
$page->addScriptToHead(URL . '/extensions/textboxfield/assets/textboxfield.publish.js', null, false);
}
self::$appendedHeaders |= $type;
}
}
/**
* Create tables and configuration.
*
* @return boolean
*/
public function install() {
Symphony::Database()->query(sprintf("
CREATE TABLE IF NOT EXISTS `%s` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`field_id` INT(11) UNSIGNED NOT NULL,
`column_length` INT(11) UNSIGNED DEFAULT 75,
`text_size` ENUM('single', 'small', 'medium', 'large', 'huge') DEFAULT 'medium',
`text_formatter` VARCHAR(255) DEFAULT NULL,
`text_validator` VARCHAR(255) DEFAULT NULL,
`text_length` INT(11) UNSIGNED DEFAULT 0,
`text_cdata` ENUM('yes', 'no') DEFAULT 'no',
`text_handle` ENUM('yes', 'no') DEFAULT 'no',
`handle_unique` ENUM('yes', 'no') DEFAULT 'yes',
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;",
self::FIELD_TABLE
));
return true;
}
/**
* Cleanup installation.
*
* @return boolean
*/
public function uninstall() {
Symphony::Database()->query(sprintf(
"DROP TABLE `%s`",
self::FIELD_TABLE
));
return true;
}
/**
* Update extension from previous releases.
*
* @see toolkit.ExtensionManager#update()
* @param string $previousVersion
* @return boolean
*/
public function update($previousVersion=false) {
// Column length:
if ($this->updateHasColumn('show_full')) {
$this->updateRemoveColumn('show_full');
}
if (!$this->updateHasColumn('column_length')) {
$this->updateAddColumn('column_length', 'INT(11) UNSIGNED DEFAULT 75 AFTER `field_id`');
}
// Text size:
if ($this->updateHasColumn('size')) {
$this->updateRenameColumn('size', 'text_size');
}
// Text formatter:
if ($this->updateHasColumn('formatter')) {
$this->updateRenameColumn('formatter', 'text_formatter');
}
// Text validator:
if ($this->updateHasColumn('validator')) {
$this->updateRenameColumn('validator', 'text_validator');
}
// Text length:
if ($this->updateHasColumn('length')) {
$this->updateRenameColumn('length', 'text_length');
}
else if (!$this->updateHasColumn('text_length')) {
$this->updateAddColumn('text_length', 'INT(11) UNSIGNED DEFAULT 0 AFTER `text_formatter`');
}
// Text CDATA:
if (!$this->updateHasColumn('text_cdata')) {
$this->updateAddColumn('text_cdata', "ENUM('yes', 'no') DEFAULT 'no' AFTER `text_length`");
}
// Text handle:
if (!$this->updateHasColumn('text_handle')) {
$this->updateAddColumn('text_handle', "ENUM('yes', 'no') DEFAULT 'no' AFTER `text_cdata`");
}
// is handle unique:
if (!$this->updateHasColumn('handle_unique')) {
$this->updateAddColumn('handle_unique', "ENUM('yes', 'no') NOT NULL DEFAULT 'yes' AFTER `text_handle`");
}
// Add handle index to textbox entry tables:
$textbox_fields = FieldManager::fetch(null, null, 'ASC', 'sortorder', 'textbox');
foreach($textbox_fields as $field) {
$table = "tbl_entries_data_" . $field->get('id');
// Handle length
if ($this->updateHasIndex('handle', $table)) {
$this->updateDropIndex('handle', $table);
}
$this->updateModifyColumn('handle', 'VARCHAR(1024)', $table);
// Make sure we have an index on the handle
if ($this->updateHasColumn('text_handle') && !$this->updateHasIndex('handle', $table)) {
$this->updateAddIndex('handle', $table, 333);
}
// Make sure we have a unique key on `entry_id`
if ($this->updateHasColumn('entry_id', $table) && !$this->updateHasUniqueKey('entry_id', $table)) {
$this->updateAddUniqueKey('entry_id', $table);
}
}
return true;
}
/**
* Add a new Index. Note that this does not check to see if an
* index already exists.
*
* @param string $index
* @param string $table
* @return boolean
*/
public function updateAddIndex($index, $table, $limit = null) {
$col = "`{$index}`";
if ($limit) {
$col .= '(' . General::intval($limit) . ')';
}
return Symphony::Database()->query("
ALTER TABLE
`$table`
ADD INDEX
`{$index}` ($col)
");
}
/**
* Check if the given `$table` has the `$index`.
*
* @param string $index
* @param string $table
* @return boolean
*/
public function updateHasIndex($index, $table) {
return (boolean)Symphony::Database()->fetchVar(
'Key_name', 0,
"
SHOW INDEX FROM
`$table`
WHERE
Key_name = '{$index}'
"
);
}
/**
* Drop the given `$index` from `$table`.
*
* @param string $index
* @param string $table
* @return boolean
*/
public function updateDropIndex($index, $table)
{
return Symphony::Database()->query("
ALTER TABLE
`$table`
DROP INDEX
`{$index}`
");
}
/**
* Add a new Unique Key. Note that this does not check to see if an
* unique key already exists and will remove any existing key on the column.
*
* @param string $column
* @param string $table
* @return boolean
*/
public function updateAddUniqueKey($column, $table = self::FIELD_TABLE) {
try {
Symphony::Database()->query("
ALTER TABLE
`$table`
DROP KEY
`$column`
");
} catch (Exception $ex) {
// ignore
}
return Symphony::Database()->query("
ALTER TABLE
`$table`
ADD UNIQUE KEY
`$column` (`$column`)
");
}
/**
* Check if the given `$table` has a unique key on `$column`.
*
* @param string $column
* @param string $table
* @return boolean
*/
public function updateHasUniqueKey($column, $table = self::FIELD_TABLE) {
$db = Symphony::Configuration()->get('database', 'db');
return (boolean)Symphony::Database()->fetchVar(
'CONSTRAINT_NAME', 0,
"
SELECT DISTINCT CONSTRAINT_NAME
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = '$db' AND
CONSTRAINT_NAME = '$column' AND
table_name = '$table' AND
constraint_type = 'UNIQUE';
"
);
}
/**
* Add a new column to the settings table.
*
* @param string $column
* @param string $type
* @return boolean
*/
public function updateAddColumn($column, $type, $table = self::FIELD_TABLE) {
return Symphony::Database()->query(sprintf("
ALTER TABLE
`%s`
ADD COLUMN
`{$column}` {$type}
",
$table
));
}
/**
* Add a new column to the settings table.
*
* @param string $column
* @param string $type
* @return boolean
*/
public function updateModifyColumn($column, $type, $table = self::FIELD_TABLE) {
return Symphony::Database()->query(sprintf("
ALTER TABLE
`%s`
MODIFY COLUMN
`{$column}` {$type}
",
$table
));
}
/**
* Does the settings table have a column?
*
* @param string $column
* @return boolean
*/
public function updateHasColumn($column, $table = self::FIELD_TABLE) {
return (boolean)Symphony::Database()->fetchVar('Field', 0, sprintf("
SHOW COLUMNS FROM
`%s`
WHERE
Field = '{$column}'
",
$table
));
}
/**
* Remove a column from the settings table.
*
* @param string $column
* @return boolean
*/
public function updateRemoveColumn($column, $table = self::FIELD_TABLE) {
return Symphony::Database()->query(sprintf("
ALTER TABLE
`%s`
DROP COLUMN
`{$column}`
",
$table
));
}
/**
* Rename a column in the settings table.
*
* @param string $column
* @return boolean
*/
public function updateRenameColumn($from, $to, $table = self::FIELD_TABLE) {
$data = Symphony::Database()->fetchRow(0, sprintf("
SHOW COLUMNS FROM
`%s`
WHERE
Field = '{$from}'
",
$table
));
if (!is_null($data['Default'])) {
$type = 'DEFAULT ' . var_export($data['Default'], true);
}
else if ($data['Null'] == 'YES') {
$type .= 'DEFAULT NULL';
}
else {
$type .= 'NOT NULL';
}
return Symphony::Database()->query(sprintf("
ALTER TABLE
`%s`
CHANGE
`%s` `%s` %s
",
$table, $from, $to,
$data['Type'] . ' ' . $type
));
}
}