Skip to content

Commit

Permalink
Code refactoring for Database and EQFA
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandra Nantel committed Jun 6, 2018
1 parent 5073d2b commit 59731cd
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 125 deletions.
194 changes: 128 additions & 66 deletions extension.driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,114 @@ class Extension_Multilingual_Upload_Field extends Extension

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
));
return Symphony::Database()
->create(self::FIELD_TABLE)
->ifNotExists()
->charset('utf8')
->collate('utf8_unicode_ci')
->fields([
'id' => [
'type' => 'int(11)',
'auto' => true,
],
'field_id' => 'int(11)',
'destination' => 'varchar(255)',
'validator' => 'varchar(255)',
'unique' => [
'type' => 'enum',
'values' => ['yes','no'],
'default' => 'yes',
],
'default_main_lang' => [
'type' => 'enum',
'values' => ['yes','no'],
'default' => 'no',
],
'required_languages' => [
'type' => 'varchar(255)',
'null' => true,
],
])
->keys([
'id' => 'primary',
'field_id' => 'key',
])
->execute()
->success();
}

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'");
Symphony::Database()
->alter(self::FIELD_TABLE)
->add([
'def_ref_lang' => [
'type' => 'enum',
'values' => ['yes','no'],
'default' => 'yes',
],
])
->execute()
->success();

Symphony::Database()
->update(self::FIELD_TABLE)
->set([
'def_ref_lang' => 'no',
])
->execute()
->success();
}

if(version_compare($previous_version, '1.6', '<')) {
Symphony::Database()->query(sprintf(
"RENAME TABLE `tbl_fields_multilingualupload` TO `%s`;",
self::FIELD_TABLE
));
Symphony::Database()
->rename('tbl_fields_multilingualupload')
->to(self::FIELD_TABLE)
->execute()
->success();
}

if(version_compare($previous_version, '1.6.1', '<')) {
Symphony::Database()->query(sprintf(
"ALTER TABLE `%s` MODIFY `validator` VARCHAR(255);",
self::FIELD_TABLE
));
Symphony::Database()
->alter(self::FIELD_TABLE)
->modify([
'validator' => 'varchar(255)'
])
->execute()
->success();
}

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
));
Symphony::Database()
->alter(self::FIELD_TABLE)
->change('def_ref_lang', [
'default_main_lang' => [
'type' => 'enum',
'values' => ['yes', 'no'],
'default' => 'no',
],
])
->add([
'required_languages' => [
'type' => 'varchar(255)',
'null' => true,
],
])
->execute()
->success();
}

return true;
}

public function uninstall()
{
return Symphony::Database()->query(sprintf(
"DROP TABLE IF EXISTS `%s`",
self::FIELD_TABLE
));
return Symphony::Database()
->drop(self::FIELD_TABLE)
->ifExists()
->execute()
->success();
}


Expand Down Expand Up @@ -145,10 +198,11 @@ public function dSave($context) {
* @param array $context
*/
public function dFLSavePreferences($context){
$fields = Symphony::Database()->fetch(sprintf(
'SELECT `field_id` FROM `%s`',
self::FIELD_TABLE
));
$fields = Symphony::Database()
->select(['field_id'])
->from(self::FIELD_TABLE)
->execute()
->rows();

if( is_array($fields) && !empty($fields) ){
$consolidate = $context['context']['settings'][MUF_GROUP]['consolidate'];
Expand All @@ -158,17 +212,20 @@ public function dFLSavePreferences($context){
$entries_table = 'tbl_entries_data_'.$field["field_id"];

try{
$show_columns = Symphony::Database()->fetch(sprintf(
"SHOW COLUMNS FROM `%s` LIKE 'file-%%'",
$entries_table
));
$show_columns = Symphony::Database()
->showColumns()
->from($entries_table)
->like('file-%%')
->execute()
->rows();
}
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"]
));
Symphony::Database()
->delete(self::FIELD_TABLE)
->where(['field_id' => $field['field_id']])
->execute()
->success();

continue;
}

Expand All @@ -182,30 +239,35 @@ public function dFLSavePreferences($context){

// 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
));
Symphony::Database()
->alter($entries_table)
->drop([
'file-' . $lc,
'size-' . $lc,
'mimetype-' . $lc,
'meta-' . $lc,
])
->execute()
->success();
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
));
foreach( $context['new_langs'] as $lc ) {
if( !in_array('file-'.$lc, $columns) ) {
Symphony::Database()
->alter($entries_table)
->add([
'file-' . $lc,
'size-' . $lc,
'mimetype-' . $lc,
'meta-' . $lc,
])
->execute()
->success();
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions extension.meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<releases>
<release version="3.0.0" date="TBA" min="4.0.0" max="4.x.x" php-min="5.6.x" php-max="7.x.x"><![CDATA[
* Update for Symphony 4.x
* Code refactoring for Database and EQFA
]]></release>
<release version="2.0.4" date="2017-09-22" min="2.5.0" max="2.x.x"><![CDATA[
* Fixes for PHP 7
Expand Down
Loading

0 comments on commit 59731cd

Please sign in to comment.