synch'ing with rel_2_0 version
[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::Interface::CGI::Output;
27 use C4::Search;
28 use C4::Context;
29 use HTML::Template;
30
31 sub StringSearch  {
32         my ($env,$searchstring,$type)=@_;
33         my $dbh = C4::Context->dbh;
34         $searchstring=~ s/\'/\\\'/g;
35         my @data=split(' ',$searchstring);
36         my $count=@data;
37         my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield >= ?) order by tagfield");
38         $sth->execute($data[0]);
39         my @results;
40         while (my $data=$sth->fetchrow_hashref){
41         push(@results,$data);
42         }
43         #  $sth->execute;
44         $sth->finish;
45         return (scalar(@results),\@results);
46 }
47
48 my $input = new CGI;
49 my $searchfield=$input->param('searchfield');
50 $searchfield=0 unless $searchfield;
51 my $offset=$input->param('offset');
52 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
53
54 my $dbh = C4::Context->dbh;
55
56 my ($template, $loggedinuser, $cookie)
57     = get_template_and_user({template_name => "parameters/marctagstructure.tmpl",
58                              query => $input,
59                              type => "intranet",
60                              authnotrequired => 0,
61                              flagsrequired => {parameters => 1},
62                              debug => 1,
63                              });
64 my $pagesize=20;
65 my $op = $input->param('op');
66 $searchfield=~ s/\,//g;
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
76 ################## ADD_FORM ##################################
77 # called by default. Used to create form to add or  modify a record
78 if ($op eq 'add_form') {
79         #---- if primkey exists, it's a modify action, so read values to modify...
80         my $data;
81         if ($searchfield) {
82                 my $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=?");
83                 $sth->execute($searchfield);
84                 $data=$sth->fetchrow_hashref;
85                 $sth->finish;
86         }
87         my $sth = $dbh->prepare("select distinct category from authorised_values");
88         $sth->execute;
89         my @authorised_values;
90         push @authorised_values,"";
91         while ((my $category) = $sth->fetchrow_array) {
92                 push @authorised_values, $category;
93         }
94         my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
95                         -values=> \@authorised_values,
96                         -size=>1,
97                         -multiple=>0,
98                         -default => $data->{'authorised_value'},
99                         );
100
101         if ($searchfield) {
102                 $template->param(action => "Modify tag",
103                                                                 searchfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$searchfield\" />$searchfield");
104         } else {
105                 $template->param(action => "Add tag",
106                                                                 searchfield => "<input type=\"text\" name=\"tagfield\" size=\"5\" maxlength=\"3\" />");
107         }
108         $template->param(liblibrarian => $data->{'liblibrarian'},
109                                                         libopac => $data->{'libopac'},
110                                                         repeatable => CGI::checkbox('repeatable',$data->{'repeatable'}?'checked':'',1,''),
111                                                         mandatory => CGI::checkbox('mandatory',$data->{'mandatory'}?'checked':'',1,''),
112                                                         authorised_value => $authorised_value,
113                                                         );
114                                                                                                         # END $OP eq ADD_FORM
115 ################## ADD_VALIDATE ##################################
116 # called by add_form, used to insert/modify data in DB
117 } elsif ($op eq 'add_validate') {
118         my $dbh = C4::Context->dbh;
119         my $sth=$dbh->prepare("replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value) values (?,?,?,?,?,?)");
120         my $tagfield       =$input->param('tagfield');
121         my $liblibrarian  = $input->param('liblibrarian');
122         my $libopac       =$input->param('libopac');
123         my $repeatable =$input->param('repeatable');
124         my $mandatory =$input->param('mandatory');
125         my $authorised_value =$input->param('authorised_value');
126         unless (C4::Context->config('demo') eq 1) {
127                 $sth->execute($tagfield,
128                                                         $liblibrarian,
129                                                         $libopac,
130                                                         $repeatable?1:0,
131                                                         $mandatory?1:0,
132                                                         $authorised_value
133                                                         );
134         }
135         $sth->finish;
136         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl?tagfield=$tagfield\"></html>";
137         exit;
138                                                                                                         # END $OP eq ADD_VALIDATE
139 ################## DELETE_CONFIRM ##################################
140 # called by default form, used to confirm deletion of data in DB
141 } elsif ($op eq 'delete_confirm') {
142         my $dbh = C4::Context->dbh;
143         my $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=?");
144         $sth->execute($searchfield);
145         my $data=$sth->fetchrow_hashref;
146         $sth->finish;
147         $template->param(liblibrarian => $data->{'liblibrarian'},
148                                                         searchfield => $searchfield,
149                                                         );
150                                                                                                         # END $OP eq DELETE_CONFIRM
151 ################## DELETE_CONFIRMED ##################################
152 # called by delete_confirm, used to effectively confirm deletion of data in DB
153 } elsif ($op eq 'delete_confirmed') {
154         my $dbh = C4::Context->dbh;
155         unless (C4::Context->config('demo') eq 1) {
156                 $dbh->do("delete from marc_tag_structure where tagfield='$searchfield'");
157                 $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield'");
158         }
159                                                                                                         # END $OP eq DELETE_CONFIRMED
160 ################## DEFAULT ##################################
161 } else { # DEFAULT
162         if  ($searchfield ne '') {
163                  $template->param(searchfield => "<p>You Searched for <strong>$searchfield<strong></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&amp;searchfield=".$results->[$i]{'tagfield'};
183                 $row_data{delete} = "$script_name?op=delete_confirm&amp;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(isprevpage => $offset,
191                                                 prevpage=> $prevpage,
192                                                 searchfield => $searchfield,
193                                                 script_name => $script_name,
194                  );
195         }
196         if ($offset+$pagesize<$count) {
197                 my $nextpage =$offset+$pagesize;
198                 $template->param(nextpage =>$nextpage,
199                                                 searchfield => $searchfield,
200                                                 script_name => $script_name,
201                 );
202         }
203 } #---- END $OP eq DEFAULT
204
205 $template->param(loggeninuser => $loggedinuser);
206 output_html_with_http_headers $input, $cookie, $template->output;