small bugfix to search on >= and not >
[koha.git] / admin / marctagstructure.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use CGI;
23 use C4::Auth;
24 use C4::Context;
25 use C4::Output;
26 use C4::Search;
27 use C4::Context;
28 use HTML::Template;
29
30 sub StringSearch  {
31         my ($env,$searchstring,$type)=@_;
32         my $dbh = C4::Context->dbh;
33         $searchstring=~ s/\'/\\\'/g;
34         my @data=split(' ',$searchstring);
35         my $count=@data;
36         my $query="Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield >= $data[0]) order by tagfield";
37         my $sth=$dbh->prepare($query);
38         $sth->execute;
39         my @results;
40         my $cnt=0;
41         while (my $data=$sth->fetchrow_hashref){
42         push(@results,$data);
43         $cnt ++;
44         }
45         #  $sth->execute;
46         $sth->finish;
47         return ($cnt,\@results);
48 }
49
50 my $input = new CGI;
51 my $searchfield=$input->param('searchfield');
52 $searchfield=0 unless $searchfield;
53 my $pkfield="tagfield";
54 my $reqsel="select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where $pkfield='$searchfield'";
55 my $offset=$input->param('offset');
56 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
57
58 my $dbh = C4::Context->dbh;
59
60 my ($template, $loggedinuser, $cookie)
61     = get_template_and_user({template_name => "parameters/marctagstructure.tmpl",
62                              query => $input,
63                              type => "intranet",
64                              authnotrequired => 0,
65                              flagsrequired => {parameters => 1},
66                              debug => 1,
67                              });
68 my $pagesize=20;
69 my $op = $input->param('op');
70 $searchfield=~ s/\,//g;
71
72 if ($op) {
73 $template->param(script_name => $script_name,
74                                                 $op              => 1); # we show only the TMPL_VAR names $op
75 } else {
76 $template->param(script_name => $script_name,
77                                                 else              => 1); # we show only the TMPL_VAR names $op
78 }
79
80 ################## ADD_FORM ##################################
81 # called by default. Used to create form to add or  modify a record
82 if ($op eq 'add_form') {
83         #---- if primkey exists, it's a modify action, so read values to modify...
84         my $data;
85         if ($searchfield) {
86                 my $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where $pkfield='$searchfield'");
87                 $sth->execute;
88                 $data=$sth->fetchrow_hashref;
89                 $sth->finish;
90         }
91         my $sth = $dbh->prepare("select distinct category from authorised_values");
92         $sth->execute;
93         my @authorised_values;
94         push @authorised_values,"";
95         while ((my $category) = $sth->fetchrow_array) {
96                 push @authorised_values, $category;
97         }
98         my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
99                         -values=> \@authorised_values,
100                         -size=>1,
101                         -multiple=>0,
102                         -default => $data->{'authorised_value'},
103                         );
104
105         if ($searchfield) {
106                 $template->param(action => "Modify tag",
107                                                                 searchfield => "<input type=hidden name=tagfield value='$searchfield'>$searchfield");
108         } else {
109                 $template->param(action => "Add tag",
110                                                                 searchfield => "<input type=text name=tagfield size=5 maxlength=3>");
111         }
112         $template->param(liblibrarian => $data->{'liblibrarian'},
113                                                         libopac => $data->{'libopac'},
114                                                         repeatable => CGI::checkbox('repeatable',$data->{'repeatable'}?'checked':'',1,''),
115                                                         mandatory => CGI::checkbox('mandatory',$data->{'mandatory'}?'checked':'',1,''),
116                                                         authorised_value => $authorised_value,
117                                                         );
118                                                                                                         # END $OP eq ADD_FORM
119 ################## ADD_VALIDATE ##################################
120 # called by add_form, used to insert/modify data in DB
121 } elsif ($op eq 'add_validate') {
122         my $dbh = C4::Context->dbh;
123         my $sth=$dbh->prepare("replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value) values (?,?,?,?,?,?)");
124         my $tagfield       =$input->param('tagfield');
125         my $liblibrarian  = $input->param('liblibrarian');
126         my $libopac       =$input->param('libopac');
127         my $repeatable =$input->param('repeatable');
128         my $mandatory =$input->param('mandatory');
129         my $authorised_value =$input->param('authorised_value');
130         $sth->execute($tagfield,
131                                                 $liblibrarian,
132                                                 $libopac,
133                                                 $repeatable?1:0,
134                                                 $mandatory?1:0,
135                                                 $authorised_value
136                                                 );
137         $sth->finish;
138         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl?tagfield=$tagfield\"></html>";
139         exit;
140                                                                                                         # END $OP eq ADD_VALIDATE
141 ################## DELETE_CONFIRM ##################################
142 # called by default form, used to confirm deletion of data in DB
143 } elsif ($op eq 'delete_confirm') {
144         my $dbh = C4::Context->dbh;
145         my $sth=$dbh->prepare($reqsel);
146         $sth->execute;
147         my $data=$sth->fetchrow_hashref;
148         $sth->finish;
149         $template->param(liblibrarian => $data->{'liblibrarian'},
150                                                         searchfield => $searchfield,
151                                                         );
152                                                                                                         # END $OP eq DELETE_CONFIRM
153 ################## DELETE_CONFIRMED ##################################
154 # called by delete_confirm, used to effectively confirm deletion of data in DB
155 } elsif ($op eq 'delete_confirmed') {
156         my $dbh = C4::Context->dbh;
157         $dbh->do("delete from marc_tag_structure where $pkfield='$searchfield'");
158         $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield'");
159                                                                                                         # END $OP eq DELETE_CONFIRMED
160 ################## DEFAULT ##################################
161 } else { # DEFAULT
162         if  ($searchfield ne '') {
163                  $template->param(searchfield => "You Searched for <b>$searchfield<b><p>");
164         }
165         my $env;
166         my ($count,$results)=StringSearch($env,$searchfield,'web');
167         my $toggle="white";
168         my @loop_data = ();
169         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
170                 if ($toggle eq 'white'){
171                         $toggle="#ffffcc";
172                 } else {
173                         $toggle="white";
174                 }
175                 my %row_data;  # get a fresh hash for the row data
176                 $row_data{tagfield} = $results->[$i]{'tagfield'};
177                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
178                 $row_data{repeatable} = $results->[$i]{'repeatable'};
179                 $row_data{mandatory} = $results->[$i]{'mandatory'};
180                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
181                 $row_data{subfield_link} ="marc_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'};
182                 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'};
183                 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'};
184                 $row_data{bgcolor} = $toggle;
185                 push(@loop_data, \%row_data);
186         }
187         $template->param(loop => \@loop_data);
188         if ($offset>0) {
189                 my $prevpage = $offset-$pagesize;
190                 $template->param(previous => "<a href=$script_name?offset=".$prevpage.'>&lt;&lt; Prev</a>');
191         }
192         if ($offset+$pagesize<$count) {
193                 my $nextpage =$offset+$pagesize;
194                 $template->param(next => "<a href=$script_name?offset=".$nextpage.'>Next &gt;&gt;</a>');
195         }
196 } #---- END $OP eq DEFAULT
197
198 $template->param(loggeninuser => $loggedinuser);
199 print $input->header(-cookie => $cookie), $template->output;