Bug 14695 [QA Followup] - Unit Tests
[koha.git] / svc / localization
blobe0f362d978ece1483f137ce2b66086db66abb19e
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Encode qw( encode );
6 use C4::Service;
7 use Koha::Localizations;
9 our ( $query, $response ) = C4::Service->init( parameters => 'parameters_remaining_permissions' );
11 sub get_translations {
12 my $rs = Koha::Localizations->search({ entity => $query->param('entity'), code => $query->param('code') });
13 my @translations;
14 while ( my $s = $rs->next ) {
15 push @translations, {
16 id => $s->localization_id,
17 entity => $s->entity,
18 code => $s->code,
19 lang => $s->lang,
20 translation => $s->translation,
23 $response->param( translations => \@translations );
24 C4::Service->return_success( $response );
27 sub update_translation {
28 my $id = $query->param('id');
29 my $translation = $query->param('translation');
30 my $lang = $query->param('lang');
32 my $localization = Koha::Localizations->find( $id );
33 if ( defined $lang and $localization->lang ne $lang ) {
34 $localization->lang( $lang )
36 if ( defined $translation and $localization->translation ne $translation ) {
37 $localization->translation( $translation )
39 my %params;
40 my $is_changed;
41 if ( $localization->is_changed ) {
42 $is_changed = 1;
43 unless ( Koha::Localizations->search( { entity => $localization->entity, code => $localization->code, lang => $lang, localization_id => { '!=' => $localization->localization_id }, } )->count ) {
44 $localization->store;
45 } else {
46 $params{error} = 1;
47 $params{error_code} = 'already_exists';
50 $response->param(
51 %params,
52 id => $localization->localization_id,
53 entity => $localization->entity,
54 code => $localization->code,
55 lang => $localization->lang,
56 translation => $localization->translation,
57 is_changed => $is_changed,
59 C4::Service->return_success( $response );
62 sub add_translation {
63 my $entity = $query->param('entity');
64 my $code = $query->param('code');
65 my $lang = $query->param('lang');
66 my $translation = $query->param('translation');
68 unless ( Koha::Localizations->search({entity => $entity, code => $code, lang => $lang, })->count ) {
69 my $localization = Koha::Localization->new(
71 entity => $entity,
72 code => $code,
73 lang => $lang,
74 translation => $translation,
77 $localization->store;
78 $response->param(
79 id => $localization->localization_id,
80 entity => $localization->entity,
81 code => $localization->code,
82 lang => $localization->lang,
83 translation => $localization->translation,
86 } else {
87 $response->param( error => 1, error_code => 'already_exists', );
89 C4::Service->return_success( $response );
92 sub delete_translation {
93 my $id = $query->param('id');
94 Koha::Localizations->find($id)->delete;
95 $response->param( id => $id );
96 C4::Service->return_success( $response );
99 C4::Service->dispatch(
100 [ 'GET /', [ 'id' ], \&get_translations ],
101 [ 'PUT /', [ 'id' ], \&update_translation ],
102 [ 'POST /', [ 'entity', 'code', 'lang', 'translation' ], \&add_translation ],
103 [ 'DELETE /', ['id'], \&delete_translation ],