ae9bcec192bcb380b5dff9c1ae1b8f7567841ec0
[koha.git] / tools / automatic_item_modification_by_age.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2013 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 =head1 NAME
21
22 automatic_item_modification_by_age.pl: Update new status for items.
23
24 =cut
25
26 =head1 DESCRIPTION
27
28 This script allows a user to update the new status for items.
29
30 =cut
31
32 use Modern::Perl;
33
34 use CGI;
35 use JSON qw( to_json from_json );
36
37 use C4::Auth;
38 use C4::Context;
39 use C4::Items;
40 use C4::Output;
41 use C4::Koha;
42
43 use Koha::Items;
44 use Koha::Biblioitems;
45
46 my $cgi = CGI->new;
47
48 # open template
49 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
50     {
51         template_name   => "tools/automatic_item_modification_by_age.tt",
52         query           => $cgi,
53         type            => "intranet",
54         flagsrequired   => { tools => 'items_batchmod' },
55     }
56 );
57
58 my $op = $cgi->param('op') // 'show';
59
60 my $syspref_name = q|automatic_item_modification_by_age_configuration|;
61 if ( $op eq 'update' ) {
62     my @rules;
63     my @unique_ids = $cgi->multi_param('unique_id');
64     for my $unique_id ( @unique_ids ) {
65         my @substitution_fields = $cgi->multi_param("substitution_field_$unique_id");
66         my @substitution_values = $cgi->multi_param("substitution_value_$unique_id");
67         my @condition_fields = $cgi->multi_param("condition_field_$unique_id");
68         my @condition_values = $cgi->multi_param("condition_value_$unique_id");
69         my $rule = {
70             substitutions => [],
71             conditions => [],
72         };
73         for my $value ( @substitution_values ) {
74             my $field = shift @substitution_fields;
75             last unless $field;
76             push @{ $rule->{substitutions} }, { field => $field, value => $value };
77         }
78         push @{ $rule->{substitutions} }, {}
79             unless @{ $rule->{substitutions} };
80         for my $value ( @condition_values ) {
81             my $field = shift @condition_fields;
82             last unless $field;
83             push @{ $rule->{conditions} }, { field => $field, value => $value };
84         }
85         push @{ $rule->{conditions} }, {}
86             unless @{ $rule->{conditions} };
87         $rule->{age} = $cgi->param("age_$unique_id");
88         push @rules, $rule;
89     }
90     my $syspref_content = to_json( \@rules );
91     C4::Context->set_preference($syspref_name, $syspref_content);
92
93     $op = 'show';
94 }
95
96 my @messages;
97 my $syspref_content = C4::Context->preference($syspref_name);
98 my $rules;
99 $rules = eval { JSON::from_json( $syspref_content ) }
100     if $syspref_content;
101 if ( $@ ) {
102     push @messages, {
103         type => 'error',
104         code => 'unable_to_load_configuration'
105     };
106     $template->param( messages => \@messages );
107     output_html_with_http_headers $cgi, $cookie, $template->output;
108     exit;
109 }
110
111 my @item_fields = map { "items.$_" } Koha::Items->columns;
112 my @biblioitem_fields = map { "biblioitems.$_" } Koha::Biblioitems->columns;
113 $template->param(
114     op => $op,
115     messages => \@messages,
116     condition_fields => [ @item_fields, @biblioitem_fields ],
117     substitution_fields => \@item_fields,
118     rules => $rules,
119 );
120
121 output_html_with_http_headers $cgi, $cookie, $template->output;