Report borrower's home library on reserves library mismatch.
[koha.git] / admin / authorised_values.pl
blob6b76972e0d51a133ff6bff6633e50aa8a89efed4
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use strict;
21 use CGI;
22 use C4::Auth;
23 use C4::Context;
24 use C4::Output;
26 use C4::Context;
29 sub AuthorizedValuesForCategory {
30 my ($searchstring,$type)=@_;
31 my $dbh = C4::Context->dbh;
32 $searchstring=~ s/\'/\\\'/g;
33 my @data=split(' ',$searchstring);
34 my $count=@data;
35 my $sth=$dbh->prepare("Select id,category,authorised_value,lib from authorised_values where (category = ?) order by category,authorised_value");
36 $sth->execute("$data[0]");
37 my @results;
38 my $cnt=0;
39 while (my $data=$sth->fetchrow_hashref){
40 push(@results,$data);
41 $cnt ++;
43 $sth->finish;
44 return ($cnt,\@results);
47 my $input = new CGI;
48 my $searchfield=$input->param('searchfield');
49 $searchfield=~ s/\,//g;
50 my $id = $input->param('id');
51 my $offset=$input->param('offset');
52 my $script_name="/cgi-bin/koha/admin/authorised_values.pl";
53 my $dbh = C4::Context->dbh;
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user({template_name => "admin/authorised_values.tmpl",
57 query => $input,
58 type => "intranet",
59 authnotrequired => 0,
60 flagsrequired => {parameters => 1},
61 debug => 1,
62 });
63 my $pagesize=20;
64 my $op = $input->param('op');
66 if ($op) {
67 $template->param(script_name => $script_name,
68 $op => 1); # we show only the TMPL_VAR names $op
69 } else {
70 $template->param(script_name => $script_name,
71 else => 1); # we show only the TMPL_VAR names $op
73 ################## ADD_FORM ##################################
74 # called by default. Used to create form to add or modify a record
75 if ($op eq 'add_form') {
76 my $data;
77 if ($id) {
78 my $dbh = C4::Context->dbh;
79 my $sth=$dbh->prepare("select id,category,authorised_value,lib from authorised_values where id=?");
80 $sth->execute($id);
81 $data=$sth->fetchrow_hashref;
82 $sth->finish;
83 } else {
84 $data->{'category'} = $input->param('category');
86 if ($id) {
87 $template->param(action_modify => 1);
88 $template->param('heading-modify-authorized-value-p' => 1);
89 } elsif ( ! $data->{'category'} ) {
90 $template->param(action_add_category => 1);
91 $template->param('heading-add-new-category-p' => 1);
92 } else {
93 $template->param(action_add_value => 1);
94 $template->param('heading-add-authorized-value-p' => 1);
96 $template->param('use-heading-flags-p' => 1);
97 $template->param(category => $data->{'category'},
98 authorised_value => $data->{'authorised_value'},
99 lib => $data->{'lib'},
100 id => $data->{'id'}
102 ################## ADD_VALIDATE ##################################
103 # called by add_form, used to insert/modify data in DB
104 } elsif ($op eq 'add_validate') {
105 my $dbh = C4::Context->dbh;
106 my $new_category = $input->param('category');
107 my $new_authorised_value = $input->param('authorised_value');
108 my $duplicate_entry = 0;
110 if ( $id ) { # Update
111 my $sth = $dbh->prepare( "SELECT category, authorised_value FROM authorised_values WHERE id='$id' ");
112 $sth->execute();
113 my ($category, $authorised_value) = $sth->fetchrow_array();
114 $sth->finish;
115 if ( $authorised_value ne $new_authorised_value ) {
116 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
117 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' ");
118 $sth->execute();
119 ($duplicate_entry) = $sth->fetchrow_array();
120 warn "**** duplicate_entry = $duplicate_entry";
122 unless ( $duplicate_entry ) {
123 my $sth=$dbh->prepare("UPDATE authorised_values SET category=?,authorised_value=?,lib=? where id=?");
124 my $lib = $input->param('lib');
125 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
126 $sth->execute($new_category, $new_authorised_value, $lib, $id);
127 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."\"></html>";
128 exit;
131 else { # Insert
132 my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
133 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' ");
134 $sth->execute();
135 ($duplicate_entry) = $sth->fetchrow_array();
136 $sth->finish();
137 unless ( $duplicate_entry ) {
138 my $sth=$dbh->prepare("INSERT INTO authorised_values (id,category,authorised_value,lib) values (?,?,?,?)");
139 my $lib = $input->param('lib');
140 undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
141 $sth->execute($id, $new_category, $new_authorised_value, $lib);
142 $sth->finish;
143 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."\"></html>";
144 exit;
147 if ( $duplicate_entry ) {
148 $template->param(duplicate_category => $new_category,
149 duplicate_value => $new_authorised_value,
150 else => 1);
151 default_form();
154 ################## DELETE_CONFIRM ##################################
155 # called by default form, used to confirm deletion of data in DB
156 } elsif ($op eq 'delete_confirm') {
157 my $dbh = C4::Context->dbh;
158 my $sth=$dbh->prepare("select category,authorised_value,lib from authorised_values where id=?");
159 $sth->execute($id);
160 my $data=$sth->fetchrow_hashref;
161 $sth->finish;
162 $id = $input->param('id') unless $id;
163 $template->param(searchfield => $searchfield,
164 Tlib => $data->{'lib'},
165 Tvalue => $data->{'authorised_value'},
166 id =>$id,
169 # END $OP eq DELETE_CONFIRM
170 ################## DELETE_CONFIRMED ##################################
171 # called by delete_confirm, used to effectively confirm deletion of data in DB
172 } elsif ($op eq 'delete_confirmed') {
173 my $dbh = C4::Context->dbh;
174 my $id = $input->param('id');
175 my $sth=$dbh->prepare("delete from authorised_values where id=?");
176 $sth->execute($id);
177 $sth->finish;
178 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield\"></html>";
179 exit;
181 # END $OP eq DELETE_CONFIRMED
182 ################## DEFAULT ##################################
183 } else { # DEFAULT
184 default_form();
185 } #---- END $OP eq DEFAULT
186 output_html_with_http_headers $input, $cookie, $template->output;
188 exit 0;
190 sub default_form {
191 # build categories list
192 my $sth = $dbh->prepare("select distinct category from authorised_values");
193 $sth->execute;
194 # the list
195 my @category_list;
196 # a hash, to check that some hardcoded categories exist.
197 my %categories;
198 while ( my ($category) = $sth->fetchrow_array) {
199 push(@category_list,$category);
200 $categories{$category} = 1;
202 # push koha system categories
203 push @category_list, 'Asort1' unless $categories{'Asort1'};
204 push @category_list, 'Asort2' unless $categories{'Asort2'};
205 push @category_list, 'Bsort1' unless $categories{'Bsort1'};
206 push @category_list, 'Bsort2' unless $categories{'Bsort2'};
207 push @category_list, 'SUGGEST' unless $categories{'SUGGEST'};
208 push @category_list, 'DAMAGED' unless $categories{'DAMAGED'};
209 push @category_list, 'LOST' unless $categories{'LOST'};
210 #reorder the list
211 @category_list = sort {$a cmp $b} @category_list;
212 my $tab_list = CGI::scrolling_list(-name=>'searchfield',
213 -id=>'searchfield',
214 -values=> \@category_list,
215 -default=>"",
216 -size=>1,
217 -tabindex=>'',
218 -multiple=>0,
220 if (!$searchfield) {
221 $searchfield=$category_list[0];
223 my ($count,$results)=AuthorizedValuesForCategory($searchfield,'web');
224 my $toggle=1;
225 my @loop_data = ();
226 # builds value list
227 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
228 if ($toggle eq 1){
229 $toggle=1;
230 } else {
231 $toggle=0;
233 my %row_data; # get a fresh hash for the row data
234 $row_data{category} = $results->[$i]{'category'};
235 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
236 $row_data{lib} = $results->[$i]{'lib'};
237 $row_data{edit} = "$script_name?op=add_form&amp;id=".$results->[$i]{'id'};
238 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=$searchfield&amp;id=".$results->[$i]{'id'};
239 push(@loop_data, \%row_data);
242 $template->param(loop => \@loop_data,
243 tab_list => $tab_list,
244 category => $searchfield);
246 if ($offset>0) {
247 my $prevpage = $offset-$pagesize;
248 $template->param(isprevpage => $offset,
249 prevpage=> $prevpage,
250 searchfield => $searchfield,
251 script_name => $script_name,
254 if ($offset+$pagesize<$count) {
255 my $nextpage =$offset+$pagesize;
256 $template->param(nextpage =>$nextpage,
257 searchfield => $searchfield,
258 script_name => $script_name,