From 37add9252e04f7dd9beae1981f2aa2f204e9c695 Mon Sep 17 00:00:00 2001 From: Vlad Ghita Date: Tue, 15 May 2012 17:44:12 +0300 Subject: [PATCH] Release 1.6 --- README.markdown | 12 -- extension.driver.php | 115 +++++++++----- extension.meta.xml | 6 +- ...load.php => field.multilingual_upload.php} | 141 +++++++++--------- 4 files changed, 152 insertions(+), 122 deletions(-) rename fields/{field.multilingualupload.php => field.multilingual_upload.php} (76%) diff --git a/README.markdown b/README.markdown index c04c4d7..84e5a60 100644 --- a/README.markdown +++ b/README.markdown @@ -1,18 +1,6 @@ Field: Multilingual File Upload ============== -A field that allows file upload for different frontend languages. - -* Version: 1.5 -* Build Date: 2012-05-14 -* Authors: - - Vlad Ghita -* Requirements: - - Symphony 2.3 - - [Frontend Localisation 1.5](https://github.com/vlad-ghita/frontend_localisation) - - - ## 1 About ## This is the multilingual version of the classic upload field. diff --git a/extension.driver.php b/extension.driver.php index c8926f1..1fdc92f 100644 --- a/extension.driver.php +++ b/extension.driver.php @@ -4,15 +4,16 @@ - define_safe(MUF_NAME, 'Field: Multilingual File Upload'); + define_safe(MUF_NAME, 'Field: Multilingual Upload'); define_safe(MUF_GROUP, 'multilingual_upload_field'); - class extension_multilingual_upload_field extends Extension + class Extension_Multilingual_Upload_Field extends Extension { + const FIELD_TABLE = 'tbl_fields_multilingual_upload'; - protected $assets_loaded = false; + protected static $assets_loaded = false; @@ -21,8 +22,8 @@ class extension_multilingual_upload_field extends Extension /*------------------------------------------------------------------------------------------------*/ public function install(){ - return Symphony::Database()->query(" - CREATE TABLE `tbl_fields_multilingualupload` ( + 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, @@ -31,8 +32,9 @@ public function install(){ `def_ref_lang` enum('yes','no') default 'yes', PRIMARY KEY (`id`), KEY `field_id` (`field_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; - "); + ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;", + self::FIELD_TABLE + )); } public function update($previous_version){ @@ -41,11 +43,28 @@ public function update($previous_version){ 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 + )); + } + return true; } public function uninstall(){ - Symphony::Database()->query("DROP TABLE `tbl_fields_multilingualupload`"); + try{ + Symphony::Database()->query(sprintf( + "DROP TABLE `%s`", + self::FIELD_TABLE + )); + } + catch( DatabaseException $dbe ){ + // table deosn't exist + } + + return true; } @@ -99,52 +118,67 @@ public function dAddCustomPreferenceFieldsets($context){ * @param array $context */ public function dFLSavePreferences($context){ - $fields = Symphony::Database()->fetch('SELECT `field_id` FROM `tbl_fields_multilingualupload`'); + $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']; - if( $fields ){ // 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("SHOW COLUMNS FROM `{$entries_table}` LIKE 'file-%';"); + $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("DELETE FROM `tbl_fields_multilingualupload` WHERE `field_id` = {$field["field_id"]};"); + Symphony::Database()->query(sprintf( + "DELETE FROM `%s` WHERE `field_id` = '%s';", + self::FIELD_TABLE, $field["field_id"] + )); continue; } $columns = array(); - if( $show_columns ){ + // 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( ($_POST['settings'][MUF_GROUP]['consolidate'] !== 'yes') && !in_array($lc, $context['new_langs']) ){ - Symphony::Database()->query("ALTER TABLE `{$entries_table}` DROP COLUMN `file-{$lc}`"); - Symphony::Database()->query("ALTER TABLE `{$entries_table}` DROP COLUMN `size-{$lc}`"); - Symphony::Database()->query("ALTER TABLE `{$entries_table}` DROP COLUMN `mimetype-{$lc}`"); - Symphony::Database()->query("ALTER TABLE `{$entries_table}` DROP COLUMN `meta-{$lc}`"); - } else{ + 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 column lang_code dosen't exist in the laguange drop columns - - if( !in_array('file-'.$lc, $columns) ){ - Symphony::Database()->query("ALTER TABLE `{$entries_table}` ADD COLUMN `file-{$lc}` varchar(255) default NULL"); - Symphony::Database()->query("ALTER TABLE `{$entries_table}` ADD COLUMN `size-{$lc}` int(11) unsigned NULL"); - Symphony::Database()->query("ALTER TABLE `{$entries_table}` ADD COLUMN `mimetype-{$lc}` varchar(50) default NULL"); - Symphony::Database()->query("ALTER TABLE `{$entries_table}` ADD COLUMN `meta-{$lc}` varchar(255) default NULL"); - } - } - + 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 + )); } } } @@ -152,21 +186,20 @@ public function dFLSavePreferences($context){ /*------------------------------------------------------------------------------------------------*/ - /* Utilities */ + /* Public utilities */ /*------------------------------------------------------------------------------------------------*/ - public function appendAssets(){ - if( $this->assets_loaded === false ){ - $this->assets_loaded = true; + 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); - - // multilingual stuff - $fl_assets = URL.'/extensions/frontend_localisation/assets/frontend_localisation.multilingual_tabs'; - $page->addStylesheetToHead($fl_assets.'.css', 'screen', null, false); - $page->addScriptToHead($fl_assets.'_init.js', null, false); } } } diff --git a/extension.meta.xml b/extension.meta.xml index f6818a3..6583e9c 100644 --- a/extension.meta.xml +++ b/extension.meta.xml @@ -24,10 +24,14 @@ - https://github.com/vlad-ghita/frontend_localisation + https://github.com/vlad-ghita/frontend_localisation + + diff --git a/fields/field.multilingualupload.php b/fields/field.multilingual_upload.php similarity index 76% rename from fields/field.multilingualupload.php rename to fields/field.multilingual_upload.php index ac5c352..921287b 100644 --- a/fields/field.multilingualupload.php +++ b/fields/field.multilingual_upload.php @@ -5,44 +5,47 @@ require_once(TOOLKIT.'/fields/field.upload.php'); + require_once(EXTENSIONS.'/frontend_localisation/extension.driver.php'); require_once(EXTENSIONS.'/frontend_localisation/lib/class.FLang.php'); - final class fieldMultilingualUpload extends fieldUpload + final class fieldMultilingual_Upload extends fieldUpload { /*------------------------------------------------------------------------------------------------*/ /* Definition */ /*------------------------------------------------------------------------------------------------*/ - protected $_driver; - public function __construct(){ parent::__construct(); - $this->_name = __('Multilingual File Upload'); - $this->_driver = Symphony::ExtensionManager()->create('multilingual_upload_field'); + $this->_name = __('Multilingual 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,"; + $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 .= "`file-{$lc}` varchar(255) default NULL, - `size-{$lc}` int(11) unsigned NULL, - `mimetype-{$lc}` varchar(50) default NULL, - `meta-{$lc}` varchar(255) default NULL,"; + $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`) + $query .= " + PRIMARY KEY (`id`), + UNIQUE KEY `entry_id` (`entry_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; return Symphony::Database()->query($query); @@ -96,71 +99,63 @@ private function _appendDefLangValCheckbox(XMLElement &$wrapper){ } public function commit(){ - if( !Field::commit() ) return false; - - $id = $this->get('id'); - - if( $id === false ) return false; - - $settings = array(); - - $settings['field_id'] = $id; - $settings['destination'] = $this->get('destination'); - $settings['validator'] = ($settings['validator'] == 'custom' ? NULL : $this->get('validator')); - $settings['unique'] = $this->get('unique'); - $settings['def_ref_lang'] = $this->get('def_ref_lang'); - - Symphony::Database()->query("DELETE FROM `tbl_fields_".$this->handle()."` WHERE `field_id` = '$id' LIMIT 1"); - return Symphony::Database()->insert($settings, 'tbl_fields_'.$this->handle()); + if( !parent::commit() ) return false; + + return Symphony::Database()->query(sprintf(" + UPDATE + `tbl_fields_%s` + SET + `def_ref_lang` = '%s', + `unique` = '%s' + WHERE + `field_id` = '%s';", + $this->handle(), $this->get('def_ref_lang'), $this->get('unique'), $this->get('id') + )); } /*------------------------------------------------------------------------------------------------*/ - /* Input */ + /* Publish */ /*------------------------------------------------------------------------------------------------*/ public function displayPublishPanel(XMLElement &$wrapper, $data = NULL, $flagWithError = NULL, $fieldnamePrefix = NULL, $fieldnamePostfix = NULL){ - $this->_driver->appendAssets(); + Extension_Frontend_Localisation::appendAssets(); + Extension_Multilingual_Upload_Field::appendAssets(); - $wrapper->setAttribute('class', $wrapper->getAttribute('class').' field-multilingual_upload field-multilingual'); + $main_lang = FLang::getMainLang(); + $all_langs = FLang::getAllLangs(); + $langs = FLang::getLangs(); + $wrapper->setAttribute('class', $wrapper->getAttribute('class').' field-multilingual_upload field-multilingual'); $container = new XMLElement('div', null, array('class' => 'container')); - /* Label */ + /*------------------------------------------------------------------------------------------------*/ + /* Label */ + /*------------------------------------------------------------------------------------------------*/ - $label = Widget::Label($this->get('label')); - $class = 'file'; - $label->setAttribute('class', $class); + $label = Widget::Label($this->get('label'), null, 'file'); if( $this->get('required') != 'yes' ) $label->appendChild(new XMLElement('i', __('Optional'))); - $container->appendChild($label); - $main_lang = FLang::getMainLang(); - $all_langs = FLang::getAllLangs(); - $langs = FLang::getLangs(); - - - /* Tabs */ + /*------------------------------------------------------------------------------------------------*/ + /* Tabs */ + /*------------------------------------------------------------------------------------------------*/ $ul = new XMLElement('ul', null, array('class' => 'tabs')); - foreach( $langs as $lc ){ - $li = new XMLElement( - 'li', - ($all_langs[$lc] ? $all_langs[$lc] : __('Unknown language')), - array('class' => $lc.($lc == $main_lang ? ' active' : '')) - ); - + $li = new XMLElement('li', $all_langs[$lc], array('class' => $lc)); $lc === $main_lang ? $ul->prependChild($li) : $ul->appendChild($li); } $container->appendChild($ul); - /* Inputs */ + /*------------------------------------------------------------------------------------------------*/ + /* Panels */ + /*------------------------------------------------------------------------------------------------*/ foreach( $langs as $lc ){ $div = new XMLElement('div', NULL, array('class' => 'file tab-panel tab-'.$lc)); @@ -185,7 +180,9 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = NULL, $flagWit } - /* Directory check */ + /*------------------------------------------------------------------------------------------------*/ + /* Errors */ + /*------------------------------------------------------------------------------------------------*/ if( !is_dir(DOCROOT.$this->get('destination').'/') ){ $flagWithError = __('The destination directory, %s, does not exist.', array($this->get('destination'))); @@ -202,9 +199,16 @@ public function displayPublishPanel(XMLElement &$wrapper, $data = NULL, $flagWit } } + + + /*------------------------------------------------------------------------------------------------*/ + /* Input */ + /*------------------------------------------------------------------------------------------------*/ + public function checkPostFieldData($data, &$message, $entry_id = NULL){ $error = self::__OK__; $field_data = $data; + $all_langs = FLang::getAllLangs(); foreach( FLang::getLangs() as $lc ){ @@ -219,7 +223,7 @@ public function checkPostFieldData($data, &$message, $entry_id = NULL){ // if one language fails, all fail if( $status != self::__OK__ ){ - $message .= "
{$lc}: {$file_message}"; + $message .= "
{$all_langs[$lc]}: {$file_message}"; $error = self::__ERROR__; } } @@ -264,7 +268,7 @@ public function processRawFieldData($data, &$status, &$message, $simulate = fals public function appendFormattedElement(XMLElement &$wrapper, $data){ $lang_code = FLang::getLangCode(); - // If value is empty for this language, load value from reference language + // If value is empty for this language, load value from main language if( $this->get('def_ref_lang') == 'yes' && $data['file-'.$lang_code] == '' ){ $lang_code = FLang::getMainLang(); } @@ -295,7 +299,7 @@ public function prepareTableValue($data, XMLElement $link = NULL, $entry_id = nu public function getParameterPoolValue($data){ $lang_code = FLang::getLangCode(); - // If value is empty for this language, load value from reference language + // If value is empty for this language, load value from main language if( $this->get('def_ref_lang') == 'yes' && $data['file-'.$lang_code] == '' ){ $lang_code = FLang::getMainLang(); } @@ -304,18 +308,16 @@ public function getParameterPoolValue($data){ } public function getExampleFormMarkup(){ + $element_name = $this->get('element_name'); - $fieldname = 'fields['.$this->get('element_name').'][value-{$url-language}]'; + $label = Widget::Label($element_name.' + + - $label = Widget::Label($this->get('label').' - - - - '); + '); foreach( FLang::getLangs() as $lc ){ - $fieldname = 'fields['.$this->get('element_name').'][value-'.$lc.']'; - $label->appendChild(Widget::Input($fieldname)); + $label->appendChild(Widget::Input("fields[{$element_name}][value-{$lc}]")); } return $label; @@ -370,6 +372,8 @@ protected function getUniqueFilename($filename, $lang_code = null, $enable = fal * It is possible that data from Symphony won't come as expected associative array. * * @param array $data + * + * @return array */ private function _getData($data){ if( is_string($data) ) return $data; @@ -396,6 +400,7 @@ private function _getData($data){ * @param integer $entry_id */ private function _fakeDefaultFile($lang_code, $entry_id){ + $row = array(); try{ $row = Symphony::Database()->fetchRow(0, sprintf(