road to 1.3.1 : marc parameter table management
[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::Context;
24 use C4::Output;
25 use C4::Search;
26 use C4::Context;
27 use HTML::Template;
28
29 sub StringSearch  {
30         my ($env,$searchstring,$type)=@_;
31         my $dbh = C4::Context->dbh;
32         $searchstring=~ s/\'/\\\'/g;
33         my @data=split(' ',$searchstring);
34         my $count=@data;
35         my $query="Select tagfield,liblibrarian,libopac,repeatable,mandatory from marc_tag_structure where (tagfield like \"$data[0]%\") order by tagfield";
36         my $sth=$dbh->prepare($query);
37         $sth->execute;
38         my @results;
39         my $cnt=0;
40         while (my $data=$sth->fetchrow_hashref){
41         push(@results,$data);
42         $cnt ++;
43         }
44         #  $sth->execute;
45         $sth->finish;
46         return ($cnt,\@results);
47 }
48
49 my $input = new CGI;
50 my $searchfield=$input->param('searchfield');
51 my $pkfield="tagfield";
52 my $reqsel="select tagfield,liblibrarian,libopac,repeatable,mandatory from marc_tag_structure where $pkfield='$searchfield'";
53 my $offset=$input->param('offset');
54 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
55
56 my $template = gettemplate("parameters/marctagstructure.tmpl",0);
57 my $pagesize=20;
58 my $op = $input->param('op');
59 $searchfield=~ s/\,//g;
60
61 if ($op) {
62 $template->param(script_name => $script_name,
63                                                 $op              => 1); # we show only the TMPL_VAR names $op
64 } else {
65 $template->param(script_name => $script_name,
66                                                 else              => 1); # we show only the TMPL_VAR names $op
67 }
68
69 ################## ADD_FORM ##################################
70 # called by default. Used to create form to add or  modify a record
71 if ($op eq 'add_form') {
72         #---- if primkey exists, it's a modify action, so read values to modify...
73         my $data;
74         if ($searchfield) {
75                 my $dbh = C4::Context->dbh;
76                 my $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory from marc_tag_structure where $pkfield='$searchfield'");
77                 $sth->execute;
78                 $data=$sth->fetchrow_hashref;
79                 $sth->finish;
80         }
81         if ($searchfield) {
82                 $template->param(action => "Modify tag",
83                                                                 searchfield => "<input type=hidden name=tagfield value='$searchfield'>$searchfield");
84         } else {
85                 $template->param(action => "Add tag",
86                                                                 searchfield => "<input type=text name=tagfield size=5 maxlength=3>");
87         }
88         $template->param(liblibrarian => $data->{'liblibrarian'},
89                                                         libopac => $data->{'libopac'},
90                                                         repeatable => CGI::checkbox('repeatable',$data->{'repeatable'}?'checked':'',1,''),
91                                                         mandatory => CGI::checkbox('mandatory',$data->{'mandatory'}?'checked':'',1,''),
92                                                         );
93                                                                                                         # END $OP eq ADD_FORM
94 ################## ADD_VALIDATE ##################################
95 # called by add_form, used to insert/modify data in DB
96 } elsif ($op eq 'add_validate') {
97         my $dbh = C4::Context->dbh;
98         my $sth=$dbh->prepare("replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory) values (?,?,?,?,?)");
99         my $tagfield       =$input->param('tagfield');
100         my $liblibrarian  = $input->param('liblibrarian');
101         my $libopac       =$input->param('libopac');
102         my $repeatable =$input->param('repeatable');
103         my $mandatory =$input->param('mandatory');
104         $sth->execute($tagfield,
105                                                 $liblibrarian,
106                                                 $libopac,
107                                                 $repeatable?1:0,
108                                                 $mandatory?1:0,
109                                                 );
110         $sth->finish;
111                                                                                                         # END $OP eq ADD_VALIDATE
112 ################## DELETE_CONFIRM ##################################
113 # called by default form, used to confirm deletion of data in DB
114 } elsif ($op eq 'delete_confirm') {
115         my $dbh = C4::Context->dbh;
116         my $sth=$dbh->prepare($reqsel);
117         $sth->execute;
118         my $data=$sth->fetchrow_hashref;
119         $sth->finish;
120         $template->param(liblibrarian => $data->{'liblibrarian'},
121                                                         searchfield => $searchfield,
122                                                         );
123                                                                                                         # END $OP eq DELETE_CONFIRM
124 ################## DELETE_CONFIRMED ##################################
125 # called by delete_confirm, used to effectively confirm deletion of data in DB
126 } elsif ($op eq 'delete_confirmed') {
127         my $dbh = C4::Context->dbh;
128         $dbh->do("delete from marc_tag_structure where $pkfield='$searchfield'");
129         $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield'");
130                                                                                                         # END $OP eq DELETE_CONFIRMED
131 ################## DEFAULT ##################################
132 } else { # DEFAULT
133         if  ($searchfield ne '') {
134                  $template->param(searchfield => "You Searched for <b>$searchfield<b><p>");
135         }
136         my $env;
137         my ($count,$results)=StringSearch($env,$searchfield,'web');
138         my $toggle="white";
139         my @loop_data = ();
140         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
141                 if ($toggle eq 'white'){
142                         $toggle="#ffffcc";
143                 } else {
144                         $toggle="white";
145                 }
146                 my %row_data;  # get a fresh hash for the row data
147                 $row_data{tagfield} = $results->[$i]{'tagfield'};
148                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
149                 $row_data{repeatable} = $results->[$i]{'repeatable'};
150                 $row_data{mandatory} = $results->[$i]{'mandatory'};
151                 $row_data{subfield_link} ="marc_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'};
152                 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'};
153                 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'};
154                 $row_data{bgcolor} = $toggle;
155                 push(@loop_data, \%row_data);
156         }
157         $template->param(loop => \@loop_data);
158         if ($offset>0) {
159                 my $prevpage = $offset-$pagesize;
160                 $template->param(previous => "<a href=$script_name?offset=".$prevpage.'>&lt;&lt; Prev</a>');
161         }
162         if ($offset+$pagesize<$count) {
163                 my $nextpage =$offset+$pagesize;
164                 $template->param(next => "<a href=$script_name?offset=".$nextpage.'>Next &gt;&gt;</a>');
165         }
166 } #---- END $OP eq DEFAULT
167
168 print "Content-Type: text/html\n\n", $template->output;