Bug 25723: Drop DateTime::Set from cpanfile
[koha.git] / tools / automatic_item_modification_by_age.pl
blob12a1c006e1d2a1de9c44bfeaef7dd1ba271414b8
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright 2013 BibLibre
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.
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.
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>.
20 =head1 NAME
22 automatic_item_modification_by_age.pl: Update new status for items.
24 =cut
26 =head1 DESCRIPTION
28 This script allows a user to update the new status for items.
30 =cut
32 use Modern::Perl;
34 use CGI;
35 use JSON qw( to_json from_json );
37 use C4::Auth;
38 use C4::Context;
39 use C4::Items;
40 use C4::Output;
41 use C4::Koha;
43 use Koha::Items;
44 use Koha::Biblioitems;
46 my $cgi = new CGI;
48 # open template
49 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
51 template_name => "tools/automatic_item_modification_by_age.tt",
52 query => $cgi,
53 type => "intranet",
54 authnotrequired => 0,
55 flagsrequired => { tools => 'items_batchmod' },
59 my $op = $cgi->param('op') // 'show';
61 my $syspref_name = q|automatic_item_modification_by_age_configuration|;
62 if ( $op eq 'update' ) {
63 my @rules;
64 my @unique_ids = $cgi->multi_param('unique_id');
65 for my $unique_id ( @unique_ids ) {
66 my @substitution_fields = $cgi->multi_param("substitution_field_$unique_id");
67 my @substitution_values = $cgi->multi_param("substitution_value_$unique_id");
68 my @condition_fields = $cgi->multi_param("condition_field_$unique_id");
69 my @condition_values = $cgi->multi_param("condition_value_$unique_id");
70 my $rule = {
71 substitutions => [],
72 conditions => [],
74 for my $value ( @substitution_values ) {
75 my $field = shift @substitution_fields;
76 last unless $field;
77 push @{ $rule->{substitutions} }, { field => $field, value => $value };
79 push @{ $rule->{substitutions} }, {}
80 unless @{ $rule->{substitutions} };
81 for my $value ( @condition_values ) {
82 my $field = shift @condition_fields;
83 last unless $field;
84 push @{ $rule->{conditions} }, { field => $field, value => $value };
86 push @{ $rule->{conditions} }, {}
87 unless @{ $rule->{conditions} };
88 $rule->{age} = $cgi->param("age_$unique_id");
89 push @rules, $rule;
91 my $syspref_content = to_json( \@rules );
92 C4::Context->set_preference($syspref_name, $syspref_content);
94 $op = 'show';
97 my @messages;
98 my $syspref_content = C4::Context->preference($syspref_name);
99 my $rules;
100 $rules = eval { JSON::from_json( $syspref_content ) }
101 if $syspref_content;
102 if ( $@ ) {
103 push @messages, {
104 type => 'error',
105 code => 'unable_to_load_configuration'
107 $template->param( messages => \@messages );
108 output_html_with_http_headers $cgi, $cookie, $template->output;
109 exit;
112 my @item_fields = map { "items.$_" } Koha::Items->columns;
113 my @biblioitem_fields = map { "biblioitems.$_" } Koha::Biblioitems->columns;
114 $template->param(
115 op => $op,
116 messages => \@messages,
117 condition_fields => [ @item_fields, @biblioitem_fields ],
118 substitution_fields => \@item_fields,
119 rules => $rules,
122 output_html_with_http_headers $cgi, $cookie, $template->output;