Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Symphony 4.x #9

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
20 changes: 12 additions & 8 deletions assets/multilingual_upload_field.publish.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
(function ($, Symphony, window, undefined) {

$(document).ready(function(){
if (Symphony.Elements.contents.find('.field-multilingual_upload div.file:has(a):has(em)').length === 0) {
$('<em>' + Symphony.Language.get('Remove File') + '</em>').appendTo('div.file:has(a)').click(function (event) {
event.preventDefault();
$('div.field-multilingual_upload .file').each(function () {
var t = $(this);

var div = $(this).parent(),
name = div.find('input').attr('name');
if (t.find('a').length) {
$('<em>' + Symphony.Language.get('Remove File') + '</em>').appendTo($('.frame', t)).click(function (event) {
event.preventDefault();

div.empty().append('<input name="' + name + '" type="file">');
});
}
var div = $(this).parent(),
name = div.find('input').attr('name');

div.empty().append('<input name="' + name + '" type="file">');
});
}
});
});

}(this.jQuery, this.Symphony, this));
212 changes: 142 additions & 70 deletions extension.driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@



define_safe(MUF_NAME, 'Field: Multilingual Upload');
define_safe(MUF_GROUP, 'multilingual_upload_field');
define_safe('MUF_NAME', 'Field: Multilingual Upload');
define_safe('MUF_GROUP', 'multilingual_upload_field');



Expand All @@ -22,61 +22,112 @@ 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()
->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 @@ -122,7 +173,7 @@ public function dAddCustomPreferenceFieldsets($context){
$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')));
$label->prependChild(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')));

Expand All @@ -145,10 +196,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 +210,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 +237,47 @@ 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 => [
'type' => 'varchar(255)',
'null' => true,
],
'size-' . $lc => [
'type' => 'int(11)',
'null' => true,
],
'mimetype-' . $lc => [
'type' => 'varchar(50)',
'null' => true,
],
'meta-' . $lc => [
'type' => 'varchar(255)',
'null' => true,
],
])
->execute()
->success();
}
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions extension.meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<dependency version="1.6">frontend_localisation</dependency>
</dependencies>
<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
]]></release>
Expand Down
Loading