Bug 14100: Generic solution for language overlay - Item types
[koha.git] / svc / localization
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Service;
6 use Koha::Localizations;
7
8 our ( $query, $response ) = C4::Service->init( parameters => 'parameters_remaining_permissions' );
9
10 sub get_translations {
11     my $rs = Koha::Localizations->search({ type => $query->param('type'), code => $query->param('code') });
12     my @translations;
13     while ( my $s = $rs->next ) {
14         push @translations, {
15             id => $s->localization_id,
16             type => $s->type,
17             code => $s->code,
18             lang => $s->lang,
19             translation => $s->translation,
20         }
21     }
22     $response->param( translations => \@translations );
23     C4::Service->return_success( $response );
24 }
25
26 sub update_translation {
27     my $id = $query->param('id');
28     my $translation = $query->param('translation');
29     my $lang = $query->param('lang');
30     my $localization = Koha::Localizations->find( $id );
31
32     if ( defined $lang and $localization->lang ne $lang ) {
33         $localization->lang( $lang )
34     }
35     if ( defined $translation and $localization->translation ne $translation ) {
36         $localization->translation( $translation )
37     }
38     my $is_changed;
39     if ( $localization->is_changed ) {
40         $is_changed = 1;
41         $localization->store;
42     }
43     $response->param(
44         id          => $localization->localization_id,
45         entity      => $localization->entity,
46         code        => $localization->code,
47         lang        => $localization->lang,
48         translation => $localization->translation,
49         is_changed  => $is_changed,
50     );
51     C4::Service->return_success( $response );
52 }
53
54 sub add_translation {
55     my $entity = $query->param('entity');
56     my $code = $query->param('code');
57     my $lang = $query->param('lang');
58     my $translation = $query->param('translation');
59     my $localization = Koha::Localization->new(
60         {
61             entity => $entity,
62             code => $code,
63             lang => $lang,
64             translation => $translation,
65         }
66     );
67     $localization->store;
68     $localization->localization_id;
69     $response->param(
70         id          => $localization->localization_id,
71         entity      => $localization->entity,
72         code        => $localization->code,
73         lang        => $localization->lang,
74         translation => $localization->translation,
75     );
76
77     C4::Service->return_success( $response );
78 }
79
80 sub delete_translation {
81     my $id = $query->param('id');
82     Koha::Localizations->find($id)->delete;
83     $response->param( id => $id );
84     C4::Service->return_success( $response );
85 }
86
87 C4::Service->dispatch(
88     [ 'GET /', [ 'id' ], \&get_translations ],
89     [ 'PUT /', [ 'id' ], \&update_translation ],
90     [ 'POST /', [ 'entity', 'code', 'lang', 'translation' ], \&add_translation ],
91     [ 'DELETE /', ['id'],  \&delete_translation ],
92 );