Bug 12032: disable automatic URL conversion by TinyMCE in news
[koha.git] / tools / overduerules.pl
blobcc90d0bf71883141318f54ba1aac2a0019f40dd4
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 use warnings;
22 use CGI;
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Branch;
28 use C4::Letters;
29 use C4::Members;
31 our $input = new CGI;
32 my $dbh = C4::Context->dbh;
34 my @categories = @{$dbh->selectall_arrayref(
35 'SELECT description, categorycode FROM categories WHERE overduenoticerequired > 0',
36 { Slice => {} }
37 )};
38 my @category_codes = map { $_->{categorycode} } @categories;
39 our @rule_params = qw(delay letter debarred);
41 # blank_row($category_code) - return true if the entire row is blank.
42 sub blank_row {
43 my ($category_code) = @_;
44 for my $rp (@rule_params) {
45 for my $n (1 .. 3) {
46 my $key = "${rp}${n}-$category_code";
48 if (utf8::is_utf8($key)) {
49 utf8::encode($key);
52 my $value = $input->param($key);
53 if ($value) {
54 return 0;
58 return 1;
61 my $type=$input->param('type');
62 my $branch = $input->param('branch');
63 $branch ||= q{};
64 my $op = $input->param('op');
65 $op ||= q{};
67 my ($template, $loggedinuser, $cookie)
68 = get_template_and_user({template_name => "tools/overduerules.tmpl",
69 query => $input,
70 type => "intranet",
71 authnotrequired => 0,
72 flagsrequired => { tools => 'edit_notice_status_triggers'},
73 debug => 1,
74 });
75 my $err=0;
77 # save the values entered into tables
78 my %temphash;
79 my $input_saved = 0;
80 if ($op eq 'save') {
81 my @names=$input->param();
82 my $sth_search = $dbh->prepare("SELECT count(*) AS total FROM overduerules WHERE branchcode=? AND categorycode=?");
84 my $sth_insert = $dbh->prepare("INSERT INTO overduerules (branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
85 my $sth_update=$dbh->prepare("UPDATE overduerules SET delay1=?, letter1=?, debarred1=?, delay2=?, letter2=?, debarred2=?, delay3=?, letter3=?, debarred3=? WHERE branchcode=? AND categorycode=?");
86 my $sth_delete=$dbh->prepare("DELETE FROM overduerules WHERE branchcode=? AND categorycode=?");
87 foreach my $key (@names){
88 # ISSUES
89 if ($key =~ /(delay|letter|debarred)([1-3])-(.*)/) {
90 my $type = $1; # data type
91 my $num = $2; # From 1 to 3
92 my $bor = $3; # borrower category
93 my $value = $input->param($key);
94 if ($type eq 'delay') {
95 $temphash{$bor}->{"$type$num"} = ($value =~ /^\d+$/ && int($value) > 0) ? int($value) : '';
96 } else {
97 # type is letter
98 $temphash{$bor}->{"$type$num"} = $value if $value ne '';
103 # figure out which rows need to be deleted
104 my @rows_to_delete = grep { blank_row($_) } @category_codes;
106 foreach my $bor (keys %temphash){
107 # get category name if we need it for an error message
108 my $bor_category = GetBorrowercategory($bor);
109 my $bor_category_name = defined($bor_category) ? $bor_category->{description} : $bor;
111 # Do some Checking here : delay1 < delay2 <delay3 all of them being numbers
112 # Raise error if not true
113 if ($temphash{$bor}->{delay1}=~/[^0-9]/ and $temphash{$bor}->{delay1} ne ""){
114 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay1","BORERR"=>$bor_category_name);
115 $err=1;
116 } elsif ($temphash{$bor}->{delay2}=~/[^0-9]/ and $temphash{$bor}->{delay2} ne ""){
117 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay2","BORERR"=>$bor_category_name);
118 $err=1;
119 } elsif ($temphash{$bor}->{delay3}=~/[^0-9]/ and $temphash{$bor}->{delay3} ne ""){
120 $template->param("ERROR"=>1,"ERRORDELAY"=>"delay3","BORERR"=>$bor_category_name);
121 $err=1;
122 } elsif ($temphash{$bor}->{delay1} and not ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"})) {
123 $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay1","BORERR"=>$bor_category_name);
124 $err=1;
125 } elsif ($temphash{$bor}->{delay2} and not ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"})) {
126 $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay2","BORERR"=>$bor_category_name);
127 $err=1;
128 } elsif ($temphash{$bor}->{delay3} and not ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"})) {
129 $template->param("ERROR"=>1,"ERRORUSELESSDELAY"=>"delay3","BORERR"=>$bor_category_name);
130 $err=1;
131 }elsif ($temphash{$bor}->{delay3} and
132 ($temphash{$bor}->{delay3}<=$temphash{$bor}->{delay2} or $temphash{$bor}->{delay3}<=$temphash{$bor}->{delay1})
133 or $temphash{$bor}->{delay2} and ($temphash{$bor}->{delay2}<=$temphash{$bor}->{delay1})){
134 $template->param("ERROR"=>1,"ERRORORDER"=>1,"BORERR"=>$bor_category_name);
135 $err=1;
137 unless ($err){
138 if (($temphash{$bor}->{delay1} and ($temphash{$bor}->{"letter1"} or $temphash{$bor}->{"debarred1"}))
139 or ($temphash{$bor}->{delay2} and ($temphash{$bor}->{"letter2"} or $temphash{$bor}->{"debarred2"}))
140 or ($temphash{$bor}->{delay3} and ($temphash{$bor}->{"letter3"} or $temphash{$bor}->{"debarred3"}))) {
141 $sth_search->execute($branch,$bor);
142 my $res = $sth_search->fetchrow_hashref();
143 if ($res->{'total'}>0) {
144 $sth_update->execute(
145 ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:undef),
146 ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
147 ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
148 ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:undef),
149 ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
150 ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
151 ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:undef),
152 ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
153 ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0),
154 $branch ,$bor
156 } else {
157 $sth_insert->execute($branch,$bor,
158 ($temphash{$bor}->{"delay1"}?$temphash{$bor}->{"delay1"}:0),
159 ($temphash{$bor}->{"letter1"}?$temphash{$bor}->{"letter1"}:""),
160 ($temphash{$bor}->{"debarred1"}?$temphash{$bor}->{"debarred1"}:0),
161 ($temphash{$bor}->{"delay2"}?$temphash{$bor}->{"delay2"}:0),
162 ($temphash{$bor}->{"letter2"}?$temphash{$bor}->{"letter2"}:""),
163 ($temphash{$bor}->{"debarred2"}?$temphash{$bor}->{"debarred2"}:0),
164 ($temphash{$bor}->{"delay3"}?$temphash{$bor}->{"delay3"}:0),
165 ($temphash{$bor}->{"letter3"}?$temphash{$bor}->{"letter3"}:""),
166 ($temphash{$bor}->{"debarred3"}?$temphash{$bor}->{"debarred3"}:0)
172 unless ($err) {
173 for my $category_code (@rows_to_delete) {
174 $sth_delete->execute($branch, $category_code);
176 $template->param(datasaved => 1);
177 $input_saved = 1;
180 my $branchloop = GetBranchesLoop($branch);
182 my $letters = GetLetters("circulation");
184 my $countletters = keys %{$letters};
186 my @line_loop;
188 for my $data (@categories) {
189 my %row = (
190 overduename => $data->{'categorycode'},
191 line => $data->{'description'}
193 if (%temphash and not $input_saved){
194 # if we managed to save the form submission, don't
195 # reuse %temphash, but take the values from the
196 # database - this makes it easier to identify
197 # bugs where the form submission was not correctly saved
198 for (my $i=1;$i<=3;$i++){
199 $row{"delay$i"}=$temphash{$data->{'categorycode'}}->{"delay$i"};
200 $row{"debarred$i"}=$temphash{$data->{'categorycode'}}->{"debarred$i"};
201 if ($countletters){
202 my @letterloop;
203 foreach my $thisletter (sort { $letters->{$a} cmp $letters->{$b} } keys %$letters) {
204 my $selected;
205 if ( $temphash{$data->{categorycode}}->{"letter$i"} &&
206 $thisletter eq $temphash{$data->{'categorycode'}}->{"letter$i"}) {
207 $selected = 1;
209 my %letterrow =(value => $thisletter,
210 selected => $selected,
211 lettername => $letters->{$thisletter},
213 push @letterloop, \%letterrow;
215 $row{"letterloop$i"}=\@letterloop;
216 } else {
217 $row{"noletter"}=1;
218 $row{"letter$i"}=$temphash{$data->{'categorycode'}}->{"letter$i"};
221 } else {
222 #getting values from table
223 my $sth2=$dbh->prepare("SELECT * from overduerules WHERE branchcode=? AND categorycode=?");
224 $sth2->execute($branch,$data->{'categorycode'});
225 my $dat=$sth2->fetchrow_hashref;
226 for (my $i=1;$i<=3;$i++){
227 if ($countletters){
228 my @letterloop;
229 foreach my $thisletter (sort { $letters->{$a} cmp $letters->{$b} } keys %$letters) {
230 my $selected;
231 if ($dat->{"letter$i"} && $thisletter eq $dat->{"letter$i"}) {
232 $selected = 1;
234 my %letterrow =(value => $thisletter,
235 selected => $selected,
236 lettername => $letters->{$thisletter},
238 push @letterloop, \%letterrow;
240 $row{"letterloop$i"}=\@letterloop;
241 } else {
242 $row{"noletter"}=1;
243 if ($dat->{"letter$i"}){$row{"letter$i"}=$dat->{"letter$i"};}
245 if ($dat->{"delay$i"}){$row{"delay$i"}=$dat->{"delay$i"};}
246 if ($dat->{"debarred$i"}){$row{"debarred$i"}=$dat->{"debarred$i"};}
249 push @line_loop,\%row;
252 $template->param(table=> \@line_loop,
253 branchloop => $branchloop,
254 branch => $branch);
255 output_html_with_http_headers $input, $cookie, $template->output;