From f4c1f566ab8b19cbe04a36f6953bc5d5835dde99 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 13 Oct 2015 09:49:55 +0100 Subject: [PATCH] [PASSED QA] Bug 14100: Better errors handling This patch mainly improves the errors handling. It's now not possible to add several translations for the same entity-code-lang combination. It also: - wording: Translate for other languages => into - fixes encoding issues on add/update (was just display) - add entity and code to the title of the pop-up window Signed-off-by: Jonathan Druart Signed-off-by: Katrin Fischer --- .../Bug_14100-add_table_localization.sql | 3 +- installer/data/mysql/kohastructure.sql | 3 +- .../prog/en/modules/admin/itemtypes.tt | 2 +- .../prog/en/modules/admin/localization.tt | 36 +++++++++-- svc/localization | 75 ++++++++++++---------- 5 files changed, 76 insertions(+), 43 deletions(-) diff --git a/installer/data/mysql/atomicupdate/Bug_14100-add_table_localization.sql b/installer/data/mysql/atomicupdate/Bug_14100-add_table_localization.sql index 83bdb9d..ebe4b7e 100644 --- a/installer/data/mysql/atomicupdate/Bug_14100-add_table_localization.sql +++ b/installer/data/mysql/atomicupdate/Bug_14100-add_table_localization.sql @@ -4,5 +4,6 @@ CREATE TABLE `localization` ( code varchar(64) COLLATE utf8_unicode_ci NOT NULL, lang varchar(25) COLLATE utf8_unicode_ci NOT NULL, translation text COLLATE utf8_unicode_ci, - PRIMARY KEY (localization_id) + PRIMARY KEY (localization_id), + UNIQUE KEY `entity_code_lang` (`entity`,`code`,`lang`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index add8d51..c079d1c 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -3570,7 +3570,8 @@ CREATE TABLE `localization` ( code varchar(64) COLLATE utf8_unicode_ci NOT NULL, lang varchar(25) COLLATE utf8_unicode_ci NOT NULL, --could be a foreign key translation text COLLATE utf8_unicode_ci, - PRIMARY KEY (localization_id) + PRIMARY KEY (localization_id), + UNIQUE KEY `entity_code_lang` (`entity`,`code`,`lang`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt index 8b48ad6..8e2a297 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt @@ -126,7 +126,7 @@ Item types administration [% END %]
  • Required - Translate for other languages + Translate into other languages
  • [% IF ( noItemTypeImages ) %]
  • Image: Item type images are disabled. To enable them, turn off the noItemTypeImages system preference
  • diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/localization.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/localization.tt index ab6ca72..064ef75 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/localization.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/localization.tt @@ -17,7 +17,11 @@ message.text("Entity %s (code %s) for lang %s has correctly been updated with '%s'".format(data.entity, data.code, data.lang, data.translation)); } else if ( type == 'error_on_update' ) { message = $('
    '); - message.text("An error occurred when updating this translation"); + if ( data.error_code == 'already_exists' ) { + message.text("A translation already exists for this language."); + } else { + message.text("An error occurred when updating this translation."); + } } else if ( type == 'success_on_delete' ) { message = $('
    '); message.text("The translation (id %s) has been removed successfully".format(data.id)); @@ -29,7 +33,11 @@ message.text("Translation (id %s) has been added successfully".format(data.id)); } else if ( type == 'error_on_insert' ) { message = $('
    '); - message.text("An error occurred when adding this translation"); + if ( data.error_code == 'already_exists' ) { + message.text("A translation already exists for this language."); + } else { + message.text("An error occurred when adding this translation"); + } } $(messages).append(message); @@ -48,6 +56,8 @@ if ( data.is_changed ) { $(cell).css('background-color', '#00FF00'); show_message({ type: 'success_on_update', data: data }); + } else { + show_message({ type: 'error_on_update', data: data }); } if ( $(cell).hasClass('lang') ) { $(cell).text(data.lang) @@ -91,8 +101,16 @@ var languages_select = $(''); [% FOR language IN languages %] - var option = $(''); - $(languages_select).append(option); + [% FOR sublanguage IN language.sublanguages_loop %] + var option; + [% IF language.plural %] + option = $(''); + $(languages_select).append(option); + [% ELSE %] + option = $(''); + [% END %] + $(languages_select).append(option); + [% END %] [% END %] $("td.translation").on('focus', function(){ @@ -149,9 +167,13 @@ type: 'POST', url: '/cgi-bin/koha/svc/localization', success: function (data) { - // FIXME Should append the delete link - table.row.add( [ data.id, data.entity, data.code, data.lang, data.translation, "" ] ).draw(); - show_message({ type: 'success_on_insert', data: data }); + if ( data.error ) { + show_message({ type: 'error_on_insert', data: data }); + } else { + // FIXME Should append the delete link + table.row.add( [ data.id, data.entity, data.code, data.lang, data.translation, "" ] ).draw(); + show_message({ type: 'success_on_insert', data: data }); + } }, error: function (data) { show_message({ type: 'error_on_insert', data: data }); diff --git a/svc/localization b/svc/localization index ebec0e2..2bf3c69 100755 --- a/svc/localization +++ b/svc/localization @@ -1,6 +1,7 @@ #!/usr/bin/perl use Modern::Perl; +use Encode qw( encode ); use C4::Service; use Koha::Localizations; @@ -8,15 +9,15 @@ use Koha::Localizations; our ( $query, $response ) = C4::Service->init( parameters => 'parameters_remaining_permissions' ); sub get_translations { - my $rs = Koha::Localizations->search({ type => $query->param('type'), code => $query->param('code') }); + my $rs = Koha::Localizations->search({ entity => $query->param('entity'), code => $query->param('code') }); my @translations; while ( my $s = $rs->next ) { push @translations, { - id => $s->localization_id, - type => $s->type, - code => $s->code, - lang => $s->lang, - translation => $s->translation, + id => Encode::encode( 'utf-8', $s->localization_id ), + entity => Encode::encode( 'utf-8', $s->entity ), + code => Encode::encode( 'utf-8', $s->code ), + lang => Encode::encode( 'utf-8', $s->lang ), + translation => Encode::encode( 'utf-8', $s->translation ), } } $response->param( translations => \@translations ); @@ -27,26 +28,30 @@ sub update_translation { my $id = $query->param('id'); my $translation = $query->param('translation'); my $lang = $query->param('lang'); - my $localization = Koha::Localizations->find( $id ); + my $localization = Koha::Localizations->find( $id ); if ( defined $lang and $localization->lang ne $lang ) { $localization->lang( $lang ) } if ( defined $translation and $localization->translation ne $translation ) { $localization->translation( $translation ) } - my $is_changed; + my %params; if ( $localization->is_changed ) { - $is_changed = 1; - $localization->store; + unless ( Koha::Localizations->search( { entity => $localization->entity, code => $localization->code, lang => $lang, localization_id => { '!=' => $localization->localization_id }, } )->count ) { + $localization->store; + } else { + $params{error} = 1; + $params{error_code} = 'already_exists'; + } } $response->param( - id => $localization->localization_id, - entity => $localization->entity, - code => $localization->code, - lang => $localization->lang, - translation => $localization->translation, - is_changed => $is_changed, + %params, + id => Encode::encode('utf-8', $localization->localization_id), + entity => Encode::encode('utf-8', $localization->entity), + code => Encode::encode('utf-8', $localization->code), + lang => Encode::encode('utf-8', $localization->lang), + translation => Encode::encode('utf-8', $localization->translation), ); C4::Service->return_success( $response ); } @@ -56,24 +61,28 @@ sub add_translation { my $code = $query->param('code'); my $lang = $query->param('lang'); my $translation = $query->param('translation'); - my $localization = Koha::Localization->new( - { - entity => $entity, - code => $code, - lang => $lang, - translation => $translation, - } - ); - $localization->store; - $localization->localization_id; - $response->param( - id => $localization->localization_id, - entity => $localization->entity, - code => $localization->code, - lang => $localization->lang, - translation => $localization->translation, - ); + unless ( Koha::Localizations->search({entity => $entity, code => $code, lang => $lang, })->count ) { + my $localization = Koha::Localization->new( + { + entity => $entity, + code => $code, + lang => $lang, + translation => $translation, + } + ); + $localization->store; + $response->param( + id => Encode::encode('utf-8', $localization->localization_id), + entity => Encode::encode('utf-8', $localization->entity), + code => Encode::encode('utf-8', $localization->code), + lang => Encode::encode('utf-8', $localization->lang), + translation => Encode::encode('utf-8', $localization->translation), + ); + + } else { + $response->param( error => 1, error_code => 'already_exists', ); + } C4::Service->return_success( $response ); } -- 1.9.1