-
Notifications
You must be signed in to change notification settings - Fork 5
/
extension.driver.php
200 lines (156 loc) · 5.97 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
<?php
if( !defined('__IN_SYMPHONY__') ) die('<h2>Symphony Error</h2><p>You cannot directly access this file</p>');
define_safe(MTF_NAME, 'Field: Multilingual Tag List');
define_safe(MTF_GROUP, 'multilingual_tag_field');
class Extension_Multilingual_Tag_Field extends Extension
{
const FIELD_TABLE = 'tbl_fields_multilingual_tag';
protected static $assets_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,
`validator` varchar(50),
`pre_populate_source` varchar(15),
`def_ref_lang` enum('yes','no') NOT NULL default 'no',
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;",
self::FIELD_TABLE
));
}
public function update($previousVersion = false){
if( version_compare($previousVersion, '1.2', '<') ){
Symphony::Database()->query(sprintf(
"RENAME TABLE `tbl_fields_multilingualtag` TO `%s`;",
self::FIELD_TABLE
));
Symphony::Database()->query(sprintf(
"UPDATE tbl_fields SET type = 'multilingual_tag' WHERE type = '%s;",
'multilingualtag'
));
}
return true;
}
public function uninstall(){
try{
Symphony::Database()->query(sprintf(
"DROP TABLE `%s`",
self::FIELD_TABLE
));
}
catch( DatabaseException $dbe ){
// table deosn't exist
}
return true;
}
/*------------------------------------------------------------------------------------------------*/
/* Delegates */
/*------------------------------------------------------------------------------------------------*/
public function getSubscribedDelegates(){
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'dAddCustomPreferenceFieldsets'
),
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', __(MTF_NAME)));
$label = Widget::Label(__('Consolidate entry data'));
$label->appendChild(Widget::Input('settings['.MTF_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);
}
/**
* 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'][MTF_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 'handle-%%'",
$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 `handle-%2$s`,
DROP COLUMN `value-%2$s`;',
$entries_table, $lc
));
else
$columns[] = $column['Field'];
}
// Add new fields
foreach( $context['new_langs'] as $lc )
if( !in_array('handle-'.$lc, $columns) )
Symphony::Database()->query(sprintf(
'ALTER TABLE `%1$s`
ADD COLUMN `handle-%2$s` varchar(255) default NULL,
ADD COLUMN `value-%2$s` varchar(50) 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->addStylesheetToHead(URL.'/extensions/'.MTF_GROUP.'/assets/'.MTF_GROUP.'.publish.css', 'screen', null, false);
}
}
}