Koha/svc/localization
Jonathan Druart 0f0aaf82f4 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 <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2015-10-27 12:34:07 -03:00

101 lines
3.7 KiB
Perl
Executable file

#!/usr/bin/perl
use Modern::Perl;
use Encode qw( encode );
use C4::Service;
use Koha::Localizations;
our ( $query, $response ) = C4::Service->init( parameters => 'parameters_remaining_permissions' );
sub get_translations {
my $rs = Koha::Localizations->search({ entity => $query->param('entity'), code => $query->param('code') });
my @translations;
while ( my $s = $rs->next ) {
push @translations, {
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 );
C4::Service->return_success( $response );
}
sub update_translation {
my $id = $query->param('id');
my $translation = $query->param('translation');
my $lang = $query->param('lang');
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 %params;
if ( $localization->is_changed ) {
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(
%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 );
}
sub add_translation {
my $entity = $query->param('entity');
my $code = $query->param('code');
my $lang = $query->param('lang');
my $translation = $query->param('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 );
}
sub delete_translation {
my $id = $query->param('id');
Koha::Localizations->find($id)->delete;
$response->param( id => $id );
C4::Service->return_success( $response );
}
C4::Service->dispatch(
[ 'GET /', [ 'id' ], \&get_translations ],
[ 'PUT /', [ 'id' ], \&update_translation ],
[ 'POST /', [ 'entity', 'code', 'lang', 'translation' ], \&add_translation ],
[ 'DELETE /', ['id'], \&delete_translation ],
);