Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha.git] / 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     my @callnumbers_split;
15     for my $callnumber ( @callnumbers ) {
16         my @lines = C4::ClassSplitRoutine::RegEx::split_callnumber($callnumber, $regexs);
17         push @callnumbers_split, { inline => $callnumber, split => \@lines };
18     }
19     $response->param( split_callnumbers => \@callnumbers_split );
20     C4::Service->return_success( $response );
21 }
22
23 sub update_translation {
24     my $id = $query->param('id');
25     my $translation = $query->param('translation');
26     my $lang = $query->param('lang');
27
28     my $localization = Koha::Localizations->find( $id );
29     if ( defined $lang and $localization->lang ne $lang ) {
30         $localization->lang( $lang )
31     }
32     if ( defined $translation and $localization->translation ne $translation ) {
33         $localization->translation( $translation )
34     }
35     my %params;
36     my $is_changed;
37     if ( $localization->is_changed ) {
38         $is_changed = 1;
39         unless ( Koha::Localizations->search( { entity => $localization->entity, code => $localization->code, lang => $lang, localization_id => { '!=' => $localization->localization_id }, } )->count ) {
40             $localization->store;
41         } else {
42             $params{error} = 1;
43             $params{error_code} = 'already_exists';
44         }
45     }
46     $response->param(
47         %params,
48         id          => $localization->localization_id,
49         entity      => $localization->entity,
50         code        => $localization->code,
51         lang        => $localization->lang,
52         translation => $localization->translation,
53         is_changed  => $is_changed,
54     );
55     C4::Service->return_success( $response );
56 }
57
58 sub add_translation {
59     my $entity = $query->param('entity');
60     my $code = $query->param('code');
61     my $lang = $query->param('lang');
62     my $translation = $query->param('translation');
63
64     unless ( Koha::Localizations->search({entity => $entity, code => $code, lang => $lang, })->count ) {
65         my $localization = Koha::Localization->new(
66             {
67                 entity => $entity,
68                 code => $code,
69                 lang => $lang,
70                 translation => $translation,
71             }
72         );
73         $localization->store;
74         $response->param(
75             id          => $localization->localization_id,
76             entity      => $localization->entity,
77             code        => $localization->code,
78             lang        => $localization->lang,
79             translation => $localization->translation,
80         );
81
82     } else {
83         $response->param( error => 1, error_code => 'already_exists', );
84     }
85     C4::Service->return_success( $response );
86 }
87
88 sub delete_translation {
89     my $id = $query->param('id');
90     Koha::Localizations->find($id)->delete;
91     $response->param( id => $id );
92     C4::Service->return_success( $response );
93 }
94
95 C4::Service->dispatch(
96     [ 'GET /', [ 'callnumbers', 'regexs' ], \&get_split_callnumbers ],
97 );