Bug 16911: ->get_expiry_date accepts strings
[koha.git] / svc / localization
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Encode qw( encode );
5
6 use C4::Service;
7 use Koha::Localizations;
8
9 our ( $query, $response ) = C4::Service->init( parameters => 'parameters_remaining_permissions' );
10
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,
21         }
22     }
23     $response->param( translations => \@translations );
24     C4::Service->return_success( $response );
25 }
26
27 sub update_translation {
28     my $id = $query->param('id');
29     my $translation = $query->param('translation');
30     my $lang = $query->param('lang');
31
32     my $localization = Koha::Localizations->find( $id );
33     if ( defined $lang and $localization->lang ne $lang ) {
34         $localization->lang( $lang )
35     }
36     if ( defined $translation and $localization->translation ne $translation ) {
37         $localization->translation( $translation )
38     }
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';
48         }
49     }
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,
58     );
59     C4::Service->return_success( $response );
60 }
61
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');
67
68     unless ( Koha::Localizations->search({entity => $entity, code => $code, lang => $lang, })->count ) {
69         my $localization = Koha::Localization->new(
70             {
71                 entity => $entity,
72                 code => $code,
73                 lang => $lang,
74                 translation => $translation,
75             }
76         );
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,
84         );
85
86     } else {
87         $response->param( error => 1, error_code => 'already_exists', );
88     }
89     C4::Service->return_success( $response );
90 }
91
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 );
97 }
98
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 ],
104 );