-
Notifications
You must be signed in to change notification settings - Fork 5
/
extension.driver.php
246 lines (198 loc) · 7.77 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
<?php
if( !defined('__IN_SYMPHONY__') ) die('<h2>Symphony Error</h2><p>You cannot directly access this file</p>');
define_safe(MUF_NAME, 'Field: Multilingual Upload');
define_safe(MUF_GROUP, 'multilingual_upload_field');
class Extension_Multilingual_Upload_Field extends Extension
{
const FIELD_TABLE = 'tbl_fields_multilingual_upload';
protected static $assets_loaded = false;
protected static $assets_settings_loaded = false;
/*------------------------------------------------------------------------------------------------*/
/* Installation */
/*------------------------------------------------------------------------------------------------*/
public function install()
{
return Symphony::Database()->query(sprintf(
"CREATE TABLE `%s` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
`destination` VARCHAR(255) NOT NULL,
`validator` VARCHAR(255),
`unique` enum('yes','no') DEFAULT 'yes',
`default_main_lang` enum('yes','no') NOT NULL DEFAULT 'no',
`required_languages` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;",
self::FIELD_TABLE
));
}
public function update($previous_version = false)
{
if(version_compare($previous_version, '1.2', '<')) {
Symphony::Database()->query("ALTER TABLE `tbl_fields_multilingualupload` ADD COLUMN `def_ref_lang` ENUM('yes','no') DEFAULT 'yes'");
Symphony::Database()->query("UPDATE `tbl_fields_multilingualupload` SET `def_ref_lang` = 'no'");
}
if(version_compare($previous_version, '1.6', '<')) {
Symphony::Database()->query(sprintf(
"RENAME TABLE `tbl_fields_multilingualupload` TO `%s`;",
self::FIELD_TABLE
));
}
if(version_compare($previous_version, '1.6.1', '<')) {
Symphony::Database()->query(sprintf(
"ALTER TABLE `%s` MODIFY `validator` VARCHAR(255);",
self::FIELD_TABLE
));
}
if (version_compare($previous_version, '2.0.0', '<')) {
Symphony::Database()->query(sprintf(
"ALTER TABLE `%s`
CHANGE COLUMN `def_ref_lang` `default_main_lang` ENUM('yes', 'no') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
ADD `required_languages` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL;",
self::FIELD_TABLE
));
}
return true;
}
public function uninstall()
{
return Symphony::Database()->query(sprintf(
"DROP TABLE IF EXISTS `%s`",
self::FIELD_TABLE
));
}
/*------------------------------------------------------------------------------------------------*/
/* Delegates */
/*------------------------------------------------------------------------------------------------*/
public function getSubscribedDelegates(){
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'dAddCustomPreferenceFieldsets'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'dSave'
),
array(
'page' => '/extensions/frontend_localisation/',
'delegate' => 'FLSavePreferences',
'callback' => 'dFLSavePreferences'
),
);
}
/*------------------------------------------------------------------------------------------------*/
/* System preferences */
/*------------------------------------------------------------------------------------------------*/
/**
* Display options on Preferences page.
*
* @param array $context
*/
public function dAddCustomPreferenceFieldsets($context){
$group = new XMLElement('fieldset');
$group->setAttribute('class', 'settings');
$group->appendChild(new XMLElement('legend', __(MUF_NAME)));
$label = Widget::Label(__('Consolidate entry data'));
$label->appendChild(Widget::Input('settings['.MUF_GROUP.'][consolidate]', 'yes', 'checkbox', array('checked' => 'checked')));
$group->appendChild($label);
$group->appendChild(new XMLElement('p', __('Check this field if you want to consolidate database by <b>keeping</b> entry values of removed/old Language Driver language codes. Entry values of current language codes will not be affected.'), array('class' => 'help')));
$context['wrapper']->appendChild($group);
}
/**
* Edits the preferences to be saved
*
* @param array $context
*/
public function dSave($context) {
// prevent the saving of the values
unset($context['settings'][MUF_GROUP]);
}
/**
* Save options from Preferences page
*
* @param array $context
*/
public function dFLSavePreferences($context){
$fields = Symphony::Database()->fetch(sprintf(
'SELECT `field_id` FROM `%s`',
self::FIELD_TABLE
));
if( is_array($fields) && !empty($fields) ){
$consolidate = $context['context']['settings'][MUF_GROUP]['consolidate'];
// Foreach field check multilanguage values foreach language
foreach( $fields as $field ){
$entries_table = 'tbl_entries_data_'.$field["field_id"];
try{
$show_columns = Symphony::Database()->fetch(sprintf(
"SHOW COLUMNS FROM `%s` LIKE 'file-%%'",
$entries_table
));
}
catch( DatabaseException $dbe ){
// Field doesn't exist. Better remove it's settings
Symphony::Database()->query(sprintf(
"DELETE FROM `%s` WHERE `field_id` = '%s';",
self::FIELD_TABLE, $field["field_id"]
));
continue;
}
$columns = array();
// Remove obsolete fields
if( is_array($show_columns) && !empty($show_columns) )
foreach( $show_columns as $column ){
$lc = substr($column['Field'], strlen($column['Field']) - 2);
// If not consolidate option AND column lang_code not in supported languages codes -> Drop Column
if( ($consolidate !== 'yes') && !in_array($lc, $context['new_langs']) )
Symphony::Database()->query(sprintf(
'ALTER TABLE `%1$s`
DROP COLUMN `file-%2$s`,
DROP COLUMN `size-%2$s`,
DROP COLUMN `mimetype-%2$s`,
DROP COLUMN `meta-%2$s`;',
$entries_table, $lc
));
else
$columns[] = $column['Field'];
}
// Add new fields
foreach( $context['new_langs'] as $lc )
if( !in_array('file-'.$lc, $columns) )
Symphony::Database()->query(sprintf(
'ALTER TABLE `%1$s`
ADD COLUMN `file-%2$s` varchar(255) default NULL,
ADD COLUMN `size-%2$s` int(11) unsigned NULL,
ADD COLUMN `mimetype-%2$s` varchar(50) default NULL,
ADD COLUMN `meta-%2$s` varchar(255) default NULL;',
$entries_table, $lc
));
}
}
}
/*------------------------------------------------------------------------------------------------*/
/* Public utilities */
/*------------------------------------------------------------------------------------------------*/
public static function appendAssets(){
if( self::$assets_loaded === false
&& class_exists('Administration')
&& Administration::instance() instanceof Administration
&& Administration::instance()->Page instanceof HTMLPage ){
self::$assets_loaded = true;
$page = Administration::instance()->Page;
$page->addScriptToHead(URL.'/extensions/'.MUF_GROUP.'/assets/'.MUF_GROUP.'.publish.js', null, false);
}
}
public static function appendSettingsAssets(){
if( self::$assets_settings_loaded === false
&& class_exists('Administration')
&& Administration::instance() instanceof Administration
&& Administration::instance()->Page instanceof HTMLPage ){
self::$assets_settings_loaded = true;
$page = Administration::instance()->Page;
$page->addScriptToHead(URL.'/extensions/'.MUF_GROUP.'/assets/'.MUF_GROUP.'.settings.js', null, false);
}
}
}