diff --git a/assets/multilingual_upload_field.publish.js b/assets/multilingual_upload_field.publish.js index 561f84a..9830a62 100644 --- a/assets/multilingual_upload_field.publish.js +++ b/assets/multilingual_upload_field.publish.js @@ -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) { - $('' + Symphony.Language.get('Remove File') + '').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) { + $('' + Symphony.Language.get('Remove File') + '').appendTo($('.frame', t)).click(function (event) { + event.preventDefault(); - div.empty().append(''); - }); - } + var div = $(this).parent(), + name = div.find('input').attr('name'); + + div.empty().append(''); + }); + } + }); }); }(this.jQuery, this.Symphony, this)); diff --git a/extension.driver.php b/extension.driver.php index a2bd82e..75baebe 100644 --- a/extension.driver.php +++ b/extension.driver.php @@ -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'); @@ -22,50 +22,100 @@ 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; @@ -73,10 +123,11 @@ public function update($previous_version = false) 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(); } @@ -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 keeping entry values of removed/old Language Driver language codes. Entry values of current language codes will not be affected.'), array('class' => 'help'))); @@ -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']; @@ -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; } @@ -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(); + } + } } } } diff --git a/extension.meta.xml b/extension.meta.xml index 2db42bc..4c4daac 100644 --- a/extension.meta.xml +++ b/extension.meta.xml @@ -25,6 +25,10 @@ frontend_localisation + diff --git a/fields/field.multilingual_upload.php b/fields/field.multilingual_upload.php index 34b9d91..6eee753 100644 --- a/fields/field.multilingual_upload.php +++ b/fields/field.multilingual_upload.php @@ -5,6 +5,7 @@ require_once(TOOLKIT.'/fields/field.upload.php'); require_once(EXTENSIONS.'/frontend_localisation/extension.driver.php'); require_once(EXTENSIONS.'/frontend_localisation/lib/class.FLang.php'); + require_once(EXTENSIONS.'/multilingual_upload_field/lib/class.entryquerymultilingualuploadadapter.php'); final class fieldMultilingual_Upload extends fieldUpload { @@ -15,36 +16,68 @@ final class fieldMultilingual_Upload extends fieldUpload public function __construct(){ parent::__construct(); + $this->entryQueryFieldAdapter = new EntryQueryMultilingualUploadAdapter($this); $this->_name = __('Multilingual File Upload'); } - public function createTable(){ - $query = " - CREATE TABLE IF NOT EXISTS `tbl_entries_data_{$this->get('id')}` ( - `id` int(11) unsigned NOT NULL auto_increment, - `entry_id` int(11) unsigned NOT NULL, - `file` varchar(255) default NULL, - `size` int(11) unsigned NULL, - `mimetype` varchar(50) default NULL, - `meta` varchar(255) default NULL,"; - - foreach( FLang::getLangs() as $lc ){ - $query .= sprintf(' - `file-%1$s` varchar(255) default NULL, - `size-%1$s` int(11) unsigned NULL, - `mimetype-%1$s` varchar(50) default NULL, - `meta-%1$s` varchar(255) default NULL,', - $lc - ); - } - - $query .= " - PRIMARY KEY (`id`), - UNIQUE KEY `entry_id` (`entry_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; + public static function generateTableColumns($langs = null) + { + $cols = array(); + foreach (FLang::getLangs() as $lc) { + $cols['file-' . $lc] = [ + 'type' => 'varchar(255)', + 'null' => true, + ]; + $cols['size-' . $lc] = [ + 'type' => 'int(11)', + 'null' => true, + ]; + $cols['mimetype-' . $lc] = [ + 'type' => 'varchar(50)', + 'null' => true, + ]; + $cols['meta-' . $lc] = [ + 'type' => 'varchar(255)', + 'null' => true, + ]; + } + return $cols; + } - return Symphony::Database()->query($query); + public function createTable(){ + return Symphony::Database() + ->create('tbl_entries_data_' . $this->get('id')) + ->ifNotExists() + ->fields(array_merge([ + 'id' => [ + 'type' => 'int(11)', + 'auto' => true, + ], + 'entry_id' => 'int(11)', + 'file' => [ + 'type' => 'varchar(255)', + 'null' => true, + ], + 'size' => [ + 'type' => 'int(11)', + 'null' => true, + ], + 'mimetype' => [ + 'type' => 'varchar(50)', + 'null' => true, + ], + 'meta' => [ + 'type' => 'varchar(255)', + 'null' => true, + ], + ], self::generateTableColumns())) + ->keys([ + 'id' => 'primary', + 'entry_id' => 'unique', + ]) + ->execute() + ->success(); } @@ -182,21 +215,16 @@ public function commit() return false; } - return Symphony::Database()->query(sprintf(" - UPDATE - `tbl_fields_%s` - SET - `default_main_lang` = '%s', - `required_languages` = '%s', - `unique` = '%s' - WHERE - `field_id` = '%s';", - $this->handle(), - $this->get('default_main_lang') === 'yes' ? 'yes' : 'no', - implode(',', $this->get('required_languages')), - $this->get('unique'), - $this->get('id') - )); + return Symphony::Database() + ->update('tbl_fields_' . $this->handle()) + ->set([ + 'default_main_lang' => $this->get('default_main_lang') === 'yes' ? 'yes' : 'no', + 'required_languages' => implode(',', $this->get('required_languages')), + 'unique' => $this->get('unique'), + ]) + ->where(['field_id' => $this->get('id')]) + ->execute() + ->success(); } @@ -205,7 +233,7 @@ public function commit() /* Publish */ /*------------------------------------------------------------------------------------------------*/ - public function displayPublishPanel(XMLElement &$wrapper, $data = NULL, $flagWithError = NULL, $fieldnamePrefix = NULL, $fieldnamePostfix = NULL, $entry_id = NULL){ + public function displayPublishPanel(XMLElement &$wrapper, $data = null, $flagWithError = null, $fieldnamePrefix = null, $fieldnamePostfix = null, $entry_id = null){ Extension_Frontend_Localisation::appendAssets(); Extension_Multilingual_Upload_Field::appendAssets(); @@ -238,7 +266,7 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = NULL, $flagWit $optional_langs[] = $all_langs[$lang]; } } - + foreach ($optional_langs as $idx => $lang) { $optional .= ' ' . __($lang); if ($idx < count($optional_langs) - 2) { @@ -275,8 +303,9 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = NULL, $flagWit /*------------------------------------------------------------------------------------------------*/ $ul = new XMLElement('ul', null, array('class' => 'tabs')); + foreach ($langs as $lc) { - $li = new XMLElement('li', $all_langs[$lc], array('class' => $lc)); + $li = new XMLElement('li', $lc, array('class' => $lc)); $lc === $main_lang ? $ul->prependChild($li) : $ul->appendChild($li); } $container->appendChild($ul); @@ -286,19 +315,20 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = NULL, $flagWit /*------------------------------------------------------------------------------------------------*/ foreach ($langs as $lc) { - $div = new XMLElement('div', NULL, array('class' => 'file tab-panel tab-'.$lc)); + $div = new XMLElement('div', null, array('class' => 'file tab-panel tab-'.$lc)); + $frame = new XMLElement('span', null, array('class' => 'frame')); $file = 'file-'.$lc; if( $data[$file] ){ $filePath = $this->get('destination').'/'.$data[$file]; - - $div->appendChild( + + $frame->appendChild( Widget::Anchor($filePath, URL.$filePath) ); } - $div->appendChild( + $frame->appendChild( Widget::Input( 'fields'.$fieldnamePrefix.'['.$this->get('element_name').']['.$lc.']'.$fieldnamePostfix, $data[$file], @@ -306,6 +336,7 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = NULL, $flagWit ) ); + $div->appendChild($frame); $container->appendChild($div); } @@ -321,7 +352,7 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = NULL, $flagWit $flagWithError = __('Destination folder, %s, is not writable. Please check permissions.', array($this->get('destination'))); } - if ($flagWithError != NULL ) { + if ($flagWithError != null ) { $wrapper->appendChild(Widget::Error($container, $flagWithError)); } else { @@ -335,7 +366,7 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = NULL, $flagWit /* Input */ /*------------------------------------------------------------------------------------------------*/ - public function checkPostFieldData($data, &$message, $entry_id = NULL){ + public function checkPostFieldData($data, &$message, $entry_id = null){ $error = self::__OK__; $field_data = $data; $all_langs = FLang::getAllLangs(); @@ -374,7 +405,7 @@ public function checkPostFieldData($data, &$message, $entry_id = NULL){ return $error; } - public function processRawFieldData($data, &$status, &$message = NULL, $simulate = false, $entry_id = NULL){ + public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null){ if(!is_array($data) || empty($data)) { return parent::processRawFieldData($data, $status, $message, $simulate, $entry_id); } @@ -400,17 +431,17 @@ public function processRawFieldData($data, &$status, &$message = NULL, $simulate // Make this language the default for now // parent::processRawFieldData needs this. if ($entry_id) { - Symphony::Database()->query(sprintf( - "UPDATE `tbl_entries_data_%d` - SET - `file` = `file-$lc`, - `mimetype` = `mimetype-$lc`, - `size` = `size-$lc`, - `meta` = `meta-$lc` - WHERE `entry_id` = %d", - $this->get('id'), - $entry_id - )); + Symphony::Database() + ->update('tbl_entries_data_' . $this->get('id')) + ->set([ + 'file' => '$file-' . $lc, + 'mimetype' => '$mimetype-' . $lc, + 'size' => '$size-' . $lc, + 'meta' => '$meta-' . $lc, + ]) + ->where(['entry_id' => $entry_id]) + ->execute() + ->success(); } $local_status = self::__OK__; @@ -449,21 +480,20 @@ public function processRawFieldData($data, &$status, &$message = NULL, $simulate } protected function getCurrentData($entry_id) { - $query = sprintf( - 'SELECT * FROM `tbl_entries_data_%d` - WHERE `entry_id` = %d', - $this->get('id'), - $entry_id - ); + return Symphony::Database() + ->select(['*']) + ->from('tbl_entries_data_' . $this->get('id')) + ->where(['entry_id' => $entry_id]) + ->execute() + ->next(); - return Symphony::Database()->fetchRow(0, $query); } /*------------------------------------------------------------------------------------------------*/ /* Output */ /*------------------------------------------------------------------------------------------------*/ - public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = NULL, $entry_id = NULL){ + public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = false, $mode = null, $entry_id = null){ $lang_code = $this->getLang($data); $data['file'] = $data["file-$lang_code"]; $data['size'] = $data["size-$lang_code"]; @@ -473,7 +503,7 @@ public function appendFormattedElement(XMLElement &$wrapper, $data, $encode = fa } // @todo: remove and fallback to default (Symphony 2.5 only?) - public function prepareTableValue($data, XMLElement $link = NULL, $entry_id = null){ + public function prepareTableValue($data, XMLElement $link = null, $entry_id = null){ $lang_code = $this->getLang($data); $data['file'] = $data["file-$lang_code"]; $data['size'] = $data["size-$lang_code"]; @@ -487,7 +517,7 @@ public function prepareTextValue($data, $entry_id = null) { return strip_tags($data["file-$lc"]); } - public function getParameterPoolValue(array $data, $entry_id = NULL) { + public function getParameterPoolValue(array $data, $entry_id = null) { $lc = $this->getLang(); return $data["file-$lc"]; } @@ -514,7 +544,7 @@ public function getExampleFormMarkup(){ /* Utilities */ /*------------------------------------------------------------------------------------------------*/ - public function entryDataCleanup($entry_id, $data = NULL) + public function entryDataCleanup($entry_id, $data = null) { foreach( FLang::getLangs() as $lc ){ $file_location = WORKSPACE.'/'.ltrim($data['file-'.$lc], '/'); diff --git a/lib/class.entryquerymultilingualuploadadapter.php b/lib/class.entryquerymultilingualuploadadapter.php new file mode 100644 index 0000000..43ee2c5 --- /dev/null +++ b/lib/class.entryquerymultilingualuploadadapter.php @@ -0,0 +1,35 @@ +