Bug 15836: Add missing svc script
[koha_ffzg] / svc / split_callnumbers
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use JSON qw( from_json );
5
6 use C4::Service;
7 use C4::ClassSplitRoutine::RegEx;
8 our ( $query, $response ) = C4::Service->init( parameters => 'parameters_remaining_permissions' );
9
10 sub get_split_callnumbers {
11     my $regexs = from_json( $query->param('regexs') );
12     my $c = $query->param('callnumbers');
13     my @callnumbers = split "\n", $c;
14     use Data::Printer colored => 1; warn p $c;
15     my @callnumbers_split;
16     for my $callnumber ( @callnumbers ) {
17         my @lines = C4::ClassSplitRoutine::RegEx::split_callnumber($callnumber, $regexs);
18         push @callnumbers_split, { inline => $callnumber, split => \@lines };
19     }
20     $response->param( split_callnumbers => \@callnumbers_split );
21     C4::Service->return_success( $response );
22 }
23
24 sub update_translation {
25     my $id = $query->param('id');
26     my $translation = $query->param('translation');
27     my $lang = $query->param('lang');
28
29     my $localization = Koha::Localizations->find( $id );
30     if ( defined $lang and $localization->lang ne $lang ) {
31         $localization->lang( $lang )
32     }
33     if ( defined $translation and $localization->translation ne $translation ) {
34         $localization->translation( $translation )
35     }
36     my %params;
37     my $is_changed;
38     if ( $localization->is_changed ) {
39         $is_changed = 1;
40         unless ( Koha::Localizations->search( { entity => $localization->entity, code => $localization->code, lang => $lang, localization_id => { '!=' => $localization->localization_id }, } )->count ) {
41             $localization->store;
42         } else {
43             $params{error} = 1;
44             $params{error_code} = 'already_exists';
45         }
46     }
47     $response->param(
48         %params,
49         id          => $localization->localization_id,
50         entity      => $localization->entity,
51         code        => $localization->code,
52         lang        => $localization->lang,
53         translation => $localization->translation,
54         is_changed  => $is_changed,
55     );
56     C4::Service->return_success( $response );
57 }
58
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');
64
65     unless ( Koha::Localizations->search({entity => $entity, code => $code, lang => $lang, })->count ) {
66         my $localization = Koha::Localization->new(
67             {
68                 entity => $entity,
69                 code => $code,
70                 lang => $lang,
71                 translation => $translation,
72             }
73         );
74         $localization->store;
75         $response->param(
76             id          => $localization->localization_id,
77             entity      => $localization->entity,
78             code        => $localization->code,
79             lang        => $localization->lang,
80             translation => $localization->translation,
81         );
82
83     } else {
84         $response->param( error => 1, error_code => 'already_exists', );
85     }
86     C4::Service->return_success( $response );
87 }
88
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 );
94 }
95
96 C4::Service->dispatch(
97     [ 'GET /', [ 'callnumbers', 'regexs' ], \&get_split_callnumbers ],
98 );