Bug 8585 : Add System Preference to specify Holds to Pull List Start Date
[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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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, lib_opac, 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 $op          = $input->param('op')     || '';
48 our $offset      = $input->param('offset') || 0;
49 our $searchfield = $input->param('searchfield');
50 $searchfield = '' unless defined $searchfield;
51 $searchfield =~ s/\,//g;
52 our $script_name = "/cgi-bin/koha/admin/authorised_values.pl";
53 our $dbh = C4::Context->dbh;
54
55 our ($template, $borrowernumber, $cookie)= get_template_and_user({
56     template_name => "admin/authorised_values.tmpl",
57     authnotrequired => 0,
58     flagsrequired => {parameters => 'parameters_remaining_permissions'},
59     query => $input,
60     type => "intranet",
61     debug => 1,
62 });
63
64 $template->param(  script_name => $script_name,
65                  ($op||'else') => 1 );
66 ################## ADD_FORM ##################################
67 # called by default. Used to create form to add or  modify a record
68 if ($op eq 'add_form') {
69         my $data;
70         if ($id) {
71                 my $sth=$dbh->prepare("select id, category, authorised_value, lib, lib_opac, imageurl from authorised_values where id=?");
72                 $sth->execute($id);
73                 $data=$sth->fetchrow_hashref;
74         } else {
75                 $data->{'category'} = $input->param('category');
76         }
77         if ($id) {
78                 $template->param(action_modify => 1);
79                 $template->param('heading_modify_authorized_value_p' => 1);
80         } elsif ( ! $data->{'category'} ) {
81                 $template->param(action_add_category => 1);
82                 $template->param('heading_add_new_category_p' => 1);
83         } else {
84                 $template->param(action_add_value => 1);
85                 $template->param('heading_add_authorized_value_p' => 1);
86         }
87         $template->param('use_heading_flags_p' => 1);
88         $template->param( category        => $data->{'category'},
89                          authorised_value => $data->{'authorised_value'},
90                          lib              => $data->{'lib'},
91                          lib_opac         => $data->{'lib_opac'},
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 = ? ");
108         $sth->execute($id);
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 = ? AND authorised_value = ? and id <> ? ");
113             $sth->execute($new_category, $new_authorised_value, $id);
114             ($duplicate_entry) = $sth->fetchrow_array();
115         }
116         unless ( $duplicate_entry ) {
117             my $sth=$dbh->prepare( 'UPDATE authorised_values
118                                       SET category         = ?,
119                                           authorised_value = ?,
120                                           lib              = ?,
121                                           lib_opac         = ?,
122                                           imageurl         = ?
123                                       WHERE id=?' );
124             my $lib = $input->param('lib');
125             my $lib_opac = $input->param('lib_opac');
126             undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
127             undef $lib_opac if ($lib_opac eq ""); # to insert NULL instead of a blank string
128             $sth->execute($new_category, $new_authorised_value, $lib, $lib_opac, $imageurl, $id);          
129             print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$new_category."&offset=$offset\"></html>";
130             exit;
131         }
132     }
133     else { # Insert
134         my $sth = $dbh->prepare_cached( "SELECT COUNT(*) FROM authorised_values " .
135             "WHERE category = ? AND authorised_value = ? ");
136         $sth->execute($new_category, $new_authorised_value);
137         ($duplicate_entry) = $sth->fetchrow_array();
138         unless ( $duplicate_entry ) {
139             my $sth=$dbh->prepare( 'INSERT INTO authorised_values
140                                     ( id, category, authorised_value, lib, lib_opac, imageurl )
141                                     values (?, ?, ?, ?, ?, ?)' );
142             my $lib = $input->param('lib');
143             my $lib_opac = $input->param('lib_opac');
144             undef $lib if ($lib eq ""); # to insert NULL instead of a blank string
145             undef $lib_opac if ($lib_opac eq ""); # to insert NULL instead of a blank string
146             $sth->execute($id, $new_category, $new_authorised_value, $lib, $lib_opac, $imageurl );
147             print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=".$input->param('category')."&offset=$offset\"></html>";
148             exit;
149         }
150     }
151     if ( $duplicate_entry ) {       
152         $template->param(duplicate_category => $new_category,
153                          duplicate_value =>  $new_authorised_value,
154                          else => 1);
155         default_form();
156      }           
157         
158 ################## DELETE_CONFIRM ##################################
159 # called by default form, used to confirm deletion of data in DB
160 } elsif ($op eq 'delete_confirm') {
161         my $sth=$dbh->prepare("select category,authorised_value,lib,lib_opac from authorised_values where id=?");
162         $sth->execute($id);
163         my $data=$sth->fetchrow_hashref;
164         $id = $input->param('id') unless $id;
165         $template->param(searchfield => $searchfield,
166                                                         Tlib => $data->{'lib'},
167                                                         Tlib_opac => $data->{'lib_opac'},
168                                                         Tvalue => $data->{'authorised_value'},
169                                                         id =>$id,
170                                                         );
171
172                                                                                                         # END $OP eq DELETE_CONFIRM
173 ################## DELETE_CONFIRMED ##################################
174 # called by delete_confirm, used to effectively confirm deletion of data in DB
175 } elsif ($op eq 'delete_confirmed') {
176         my $id = $input->param('id');
177         my $sth=$dbh->prepare("delete from authorised_values where id=?");
178         $sth->execute($id);
179         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=authorised_values.pl?searchfield=$searchfield&offset=$offset\"></html>";
180         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;
187
188 exit 0;
189
190 sub default_form {
191     # build categories list
192     my $sth = $dbh->prepare("select distinct category from authorised_values");
193     $sth->execute;
194     my @category_list;
195     my %categories;    # a hash, to check that some hardcoded categories exist.
196     while ( my ($category) = $sth->fetchrow_array ) {
197         push( @category_list, $category );
198         $categories{$category} = 1;
199     }
200
201     # push koha system categories
202     foreach (qw(Asort1 Asort2 Bsort1 Bsort2 SUGGEST DAMAGED LOST REPORT_GROUP REPORT_SUBGROUP)) {
203         push @category_list, $_ unless $categories{$_};
204     }
205
206         #reorder the list
207         @category_list = sort {$a cmp $b} @category_list;
208         my $tab_list = CGI::scrolling_list(-name=>'searchfield',
209                 -id=>'searchfield',
210                         -values=> \@category_list,
211                         -default=>"",
212                         -size=>1,
213                         -multiple=>0,
214                         );
215         if (!$searchfield) {
216                 $searchfield=$category_list[0];
217         }
218     my ($results) = AuthorizedValuesForCategory($searchfield);
219     my $count = scalar(@$results);
220         my @loop_data = ();
221         # builds value list
222         for (my $i=0; $i < $count; $i++){
223                 my %row_data;  # get a fresh hash for the row data
224                 $row_data{category}              = $results->[$i]{'category'};
225                 $row_data{authorised_value}      = $results->[$i]{'authorised_value'};
226                 $row_data{lib}                   = $results->[$i]{'lib'};
227                 $row_data{lib_opac}              = $results->[$i]{'lib_opac'};
228                 $row_data{imageurl}              = getitemtypeimagelocation( 'intranet', $results->[$i]{'imageurl'} );
229                 $row_data{edit}                  = "$script_name?op=add_form&amp;id=".$results->[$i]{'id'}."&amp;offset=$offset";
230                 $row_data{delete}                = "$script_name?op=delete_confirm&amp;searchfield=$searchfield&amp;id=".$results->[$i]{'id'}."&amp;offset=$offset";
231                 push(@loop_data, \%row_data);
232         }
233
234         $template->param( loop     => \@loop_data,
235                           tab_list => $tab_list,
236                           category => $searchfield );
237 }
238