4 use Encode
qw( encode );
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') });
14 while ( my $s = $rs->next ) {
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 )
40 if ( $localization->is_changed ) {
41 unless ( Koha
::Localizations
->search( { entity
=> $localization->entity, code
=> $localization->code, lang
=> $lang, localization_id
=> { '!=' => $localization->localization_id }, } )->count ) {
45 $params{error_code
} = 'already_exists';
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 );
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(
71 translation
=> $translation,
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),
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
],