Bug 28130: Manage a patron's subscription alerts
[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 # Copyright 2021 Catalyst IT
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 =head1 NAME
22
23 automatic_item_modification_by_age.pl: Update new status for items.
24
25 =cut
26
27 =head1 DESCRIPTION
28
29 This script allows a user to update the new status for items.
30
31 =cut
32
33 use Modern::Perl;
34
35 use CGI;
36 use JSON qw( to_json );
37
38 use C4::Auth qw( get_template_and_user );
39 use C4::Context;
40 use C4::Output qw( output_html_with_http_headers );
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 @age_fields = $cgi->multi_param("agefield_$unique_id");
70         my $rule = {
71             substitutions => [],
72             conditions => [],
73         };
74         for my $value ( @substitution_values ) {
75             my $field = shift @substitution_fields;
76             last unless $field;
77             push @{ $rule->{substitutions} }, { field => $field, value => $value };
78         }
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 };
85         }
86         push @{ $rule->{conditions} }, {}
87             unless @{ $rule->{conditions} };
88         $rule->{age} = $cgi->param("age_$unique_id");
89
90         for my $age_field ( @age_fields ) {
91             $rule->{agefield} = $age_field ? $age_field : "items.dateaccessioned";
92         }
93         push @rules, $rule;
94     }
95     my $syspref_content = to_json( \@rules );
96     C4::Context->set_preference($syspref_name, $syspref_content);
97
98     $op = 'show';
99 }
100
101 my @messages;
102 my $syspref_content = C4::Context->preference($syspref_name);
103 my $rules;
104 $rules = eval { JSON::from_json( $syspref_content ) }
105     if $syspref_content;
106 if ( $@ ) {
107     push @messages, {
108         type => 'error',
109         code => 'unable_to_load_configuration'
110     };
111     $template->param( messages => \@messages );
112     output_html_with_http_headers $cgi, $cookie, $template->output;
113     exit;
114 }
115
116 my @item_fields = map { "items.$_" } Koha::Items->columns;
117 my @biblioitem_fields = map { "biblioitems.$_" } Koha::Biblioitems->columns;
118 my @age_fields = ('items.dateaccessioned', 'items.replacementpricedate', 'items.datelastborrowed', 'items.datelastseen', 'items.damaged_on', 'items.itemlost_on', 'items.withdrawn_on');
119 $template->param(
120     op => $op,
121     messages => \@messages,
122     agefields => [ @age_fields ],
123     condition_fields => [ @item_fields, @biblioitem_fields ],
124     substitution_fields => \@item_fields,
125     rules => $rules,
126 );
127
128 output_html_with_http_headers $cgi, $cookie, $template->output;