forked from symphonists/textboxfield
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.driver.php
executable file
·279 lines (247 loc) · 6.44 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
<?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')
&& 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',
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`");
}
// Add handle index to textbox entry tables:
require_once TOOLKIT . '/class.fieldmanager.php';
$textbox_fields = FieldManager::fetch(null, null, 'ASC', 'sortorder', 'textbox');
foreach($textbox_fields as $field) {
$table = "tbl_entries_data_" . $field->get('id');
if ($this->updateHasColumn('text_handle', $table) && !$this->updateHasIndex('handle', $table)) {
$this->updateAddIndex('handle', $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) {
return Symphony::Database()->query("
ALTER TABLE
`$table`
ADD INDEX
`{$index}` (`{$index}`)
");
}
/**
* 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}'
"
);
}
/**
* 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
));
}
/**
* 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
));
}
}