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