Creates a syspref & modifies template to show checked out username in OPAC
[koha.git] / admin / authorised_values.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Output;
28
29
30 sub AuthorizedValuesForCategory ($) {
31     my ($searchstring) = shift or return;
32     my $dbh = C4::Context->dbh;
33     $searchstring=~ s/\'/\\\'/g;
34     my @data=split(' ',$searchstring);
35     my $sth=$dbh->prepare('
36           SELECT  id, category, authorised_value, lib, imageurl
37             FROM  authorised_values
38            WHERE  (category = ?)
39         ORDER BY  category, authorised_value
40     ');
41     $sth->execute("$data[0]");
42     return $sth->fetchall_arrayref({});
43 }
44
45 my $input = new CGI;
46 my $id          = $input->param('id');
47 my $offset      = $input->param('offset') || 0;
48 my $searchfield = $input->param('searchfield');
49 $searchfield = '' unless defined $searchfield;
50 $searchfield=~ s/\,//g;
51 my $script_name = "/cgi-bin/koha/admin/authorised_values.pl";
52 my $dbh = C4::Context->dbh;
53
54 my ($template, $borrowernumber, $cookie)= get_template_and_user({
55     template_name => "admin/authorised_values.tmpl",
56     authnotrequired => 0,
57     flagsrequired => {parameters => 1},
58     query => $input,
59     type => "intranet",
60     debug => 1,
61 });
62 my $pagesize = 20;
63 my $op = $input->param('op') || '';
64
65 $template->param(  script_name => $script_name,
66                  ($op||'else') => 1 );
67 ################## ADD_FORM ##################################
68 # called by default. Used to create form to add or  modify a record
69 if ($op eq 'add_form') {
70         my $data;
71         if ($id) {
72                 my $sth=$dbh->prepare("select id, category, authorised_value, lib, imageurl from authorised_values where id=?");
73                 $sth->execute($id);
74                 $data=$sth->fetchrow_hashref;
75         } else {
76                 $data->{'category'} = $input->param('category');
77         }
78         if ($id) {
79                 $template->param(action_modify => 1);
80                 $template->param('heading-modify-authorized-value-p' => 1);
81         } elsif ( ! $data->{'category'} ) {
82                 $template->param(action_add_category => 1);
83                 $template->param('heading-add-new-category-p' => 1);
84         } else {
85                 $template->param(action_add_value => 1);
86                 $template->param('heading-add-authorized-value-p' => 1);
87         }
88         $template->param('use-heading-flags-p' => 1);
89         $template->param( category        => $data->{'category'},
90                          authorised_value => $data->{'authorised_value'},
91                          lib              => $data->{'lib'},
92                          id               => $data->{'id'},
93                          imagesets        => C4::Koha::getImageSets( checked => $data->{'imageurl'} ),
94                          offset           => $offset,
95                      );
96                           
97 ################## ADD_VALIDATE ##################################
98 # called by add_form, used to insert/modify data in DB
99 } elsif ($op eq 'add_validate') {
100     my $new_authorised_value = $input->param('authorised_value');
101     my $new_category = $input->param('category');
102     my $imageurl     = $input->param( 'imageurl' ) || '';
103         $imageurl = '' if $imageurl =~ /removeImage/;
104     my $duplicate_entry = 0;
105
106     if ( $id ) { # Update
107         my $sth = $dbh->prepare( "SELECT category, authorised_value FROM authorised_values WHERE id='$id' ");
108         $sth->execute();
109         my ($category, $authorised_value) = $sth->fetchrow_array();
110         if ( $authorised_value ne $new_authorised_value ) {
111             my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
112                 "WHERE category = '$new_category' AND authorised_value = '$new_authorised_value' and id<>$id");
113             $sth->execute();
114             ($duplicate_entry) = $sth->fetchrow_array();
115             warn "**** duplicate_entry = $duplicate_entry";
116         }
117         unless ( $duplicate_entry ) {
118             my $sth=$dbh->prepare( 'UPDATE authorised_values
119                                       SET category         = ?,
120                                           authorised_value = ?,
121                                           lib              = ?,
122                                           imageurl         = ?
123                                       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, $imageurl, $id);          
127             print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."&offset=$offset\"></html>";
128             exit;
129         }
130     }
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         unless ( $duplicate_entry ) {
137             my $sth=$dbh->prepare( 'INSERT INTO authorised_values
138                                     ( id, category, authorised_value, lib, imageurl )
139                                     values (?, ?, ?, ?, ?)' );
140             my $lib = $input->param('lib');
141             undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
142             $sth->execute($id, $new_category, $new_authorised_value, $lib, $imageurl );
143             print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."&offset=$offset\"></html>";
144             exit;
145         }
146     }
147     if ( $duplicate_entry ) {       
148         $template->param(duplicate_category => $new_category,
149                          duplicate_value =>  $new_authorised_value,
150                          else => 1);
151         default_form();
152      }           
153         
154 ################## DELETE_CONFIRM ##################################
155 # called by default form, used to confirm deletion of data in DB
156 } elsif ($op eq 'delete_confirm') {
157         my $sth=$dbh->prepare("select category,authorised_value,lib from authorised_values where id=?");
158         $sth->execute($id);
159         my $data=$sth->fetchrow_hashref;
160         $id = $input->param('id') unless $id;
161         $template->param(searchfield => $searchfield,
162                                                         Tlib => $data->{'lib'},
163                                                         Tvalue => $data->{'authorised_value'},
164                                                         id =>$id,
165                                                         );
166
167                                                                                                         # END $OP eq DELETE_CONFIRM
168 ################## DELETE_CONFIRMED ##################################
169 # called by delete_confirm, used to effectively confirm deletion of data in DB
170 } elsif ($op eq 'delete_confirmed') {
171         my $id = $input->param('id');
172         my $sth=$dbh->prepare("delete from authorised_values where id=?");
173         $sth->execute($id);
174         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield&offset=$offset\"></html>";
175         exit;
176                                                                                                         # END $OP eq DELETE_CONFIRMED
177 ################## DEFAULT ##################################
178 } else { # DEFAULT
179     default_form();
180 } #---- END $OP eq DEFAULT
181 output_html_with_http_headers $input, $cookie, $template->output;
182
183 exit 0;
184
185 sub default_form {
186         # build categories list
187         my $sth = $dbh->prepare("select distinct category from authorised_values");
188         $sth->execute;
189         my @category_list;
190         my %categories;     # a hash, to check that some hardcoded categories exist.
191         while ( my ($category) = $sth->fetchrow_array) {
192                 push(@category_list,$category);
193                 $categories{$category} = 1;
194         }
195         # push koha system categories
196     foreach (qw(Asort1 Asort2 Bsort1 Bsort2 SUGGEST DAMAGED LOST)) {
197         push @category_list, $_ unless $categories{$_};
198     }
199
200         #reorder the list
201         @category_list = sort {$a cmp $b} @category_list;
202         my $tab_list = CGI::scrolling_list(-name=>'searchfield',
203                 -id=>'searchfield',
204                         -values=> \@category_list,
205                         -default=>"",
206                         -size=>1,
207                         -multiple=>0,
208                         );
209         if (!$searchfield) {
210                 $searchfield=$category_list[0];
211         }
212     my ($results) = AuthorizedValuesForCategory($searchfield);
213     my $count = scalar(@$results);
214         my @loop_data = ();
215         # builds value list
216         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
217                 my %row_data;  # get a fresh hash for the row data
218                 $row_data{category}         = $results->[$i]{'category'};
219                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
220                 $row_data{lib}              = $results->[$i]{'lib'};
221                 $row_data{imageurl}         = getitemtypeimagelocation( 'intranet', $results->[$i]{'imageurl'} );
222                 $row_data{edit}             = "$script_name?op=add_form&amp;id=".$results->[$i]{'id'}."&offset=$offset";
223                 $row_data{delete}           = "$script_name?op=delete_confirm&amp;searchfield=$searchfield&amp;id=".$results->[$i]{'id'}."&offset=$offset";
224                 push(@loop_data, \%row_data);
225         }
226
227         $template->param( loop     => \@loop_data,
228                           tab_list => $tab_list,
229                           category => $searchfield );
230
231         if ($offset>0) {
232                 my $prevpage = $offset-$pagesize;
233                 $template->param(isprevpage => $offset,
234                                                 prevpage=> $prevpage,
235                                                 searchfield => $searchfield,
236                                                 script_name => $script_name,
237                  );
238         }
239         if ($offset+$pagesize<$count) {
240                 my $nextpage =$offset+$pagesize;
241                 $template->param(nextpage =>$nextpage,
242                                                 searchfield => $searchfield,
243                                                 script_name => $script_name,
244                 );
245         }
246 }
247