Bug 13799: Rename t/.../v1/borrowers.t to t/.../v1/patrons.t
[koha.git] / svc / localization
blob2bf3c6901d3e19eca63fb24019c4aa09f192465e
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 => Encode::encode( 'utf-8', $s->localization_id ),
17 entity => Encode::encode( 'utf-8', $s->entity ),
18 code => Encode::encode( 'utf-8', $s->code ),
19 lang => Encode::encode( 'utf-8', $s->lang ),
20 translation => Encode::encode( 'utf-8', $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 if ( $localization->is_changed ) {
41 unless ( Koha::Localizations->search( { entity => $localization->entity, code => $localization->code, lang => $lang, localization_id => { '!=' => $localization->localization_id }, } )->count ) {
42 $localization->store;
43 } else {
44 $params{error} = 1;
45 $params{error_code} = 'already_exists';
48 $response->param(
49 %params,
50 id => Encode::encode('utf-8', $localization->localization_id),
51 entity => Encode::encode('utf-8', $localization->entity),
52 code => Encode::encode('utf-8', $localization->code),
53 lang => Encode::encode('utf-8', $localization->lang),
54 translation => Encode::encode('utf-8', $localization->translation),
56 C4::Service->return_success( $response );
59 sub add_translation {
60 my $entity = $query->param('entity');
61 my $code = $query->param('code');
62 my $lang = $query->param('lang');
63 my $translation = $query->param('translation');
65 unless ( Koha::Localizations->search({entity => $entity, code => $code, lang => $lang, })->count ) {
66 my $localization = Koha::Localization->new(
68 entity => $entity,
69 code => $code,
70 lang => $lang,
71 translation => $translation,
74 $localization->store;
75 $response->param(
76 id => Encode::encode('utf-8', $localization->localization_id),
77 entity => Encode::encode('utf-8', $localization->entity),
78 code => Encode::encode('utf-8', $localization->code),
79 lang => Encode::encode('utf-8', $localization->lang),
80 translation => Encode::encode('utf-8', $localization->translation),
83 } else {
84 $response->param( error => 1, error_code => 'already_exists', );
86 C4::Service->return_success( $response );
89 sub delete_translation {
90 my $id = $query->param('id');
91 Koha::Localizations->find($id)->delete;
92 $response->param( id => $id );
93 C4::Service->return_success( $response );
96 C4::Service->dispatch(
97 [ 'GET /', [ 'id' ], \&get_translations ],
98 [ 'PUT /', [ 'id' ], \&update_translation ],
99 [ 'POST /', [ 'entity', 'code', 'lang', 'translation' ], \&add_translation ],
100 [ 'DELETE /', ['id'], \&delete_translation ],