per row inserts
[koha.git] / admin / biblio_framework.pl
1 #!/usr/bin/perl
2 # NOTE: 4-character tabs
3
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use CGI;
26 use C4::Context;
27 use C4::Output;
28 use C4::Auth;
29 use C4::Interface::CGI::Output;
30
31
32 sub StringSearch  {
33         my ($env,$searchstring,$type)=@_;
34         my $dbh = C4::Context->dbh;
35         $searchstring=~ s/\'/\\\'/g;
36         my @data=split(' ',$searchstring);
37         my $count=@data;
38         my $sth=$dbh->prepare("Select * from biblio_framework where (frameworkcode like ?) order by frameworktext");
39         $sth->execute("$data[0]%");
40         my @results;
41         while (my $data=$sth->fetchrow_hashref){
42         push(@results,$data);
43         }
44         #  $sth->execute;
45         $sth->finish;
46         return (scalar(@results),\@results);
47 }
48
49 my $input = new CGI;
50 my $searchfield=$input->param('frameworkcode');
51 my $offset=$input->param('offset');
52 my $script_name="/cgi-bin/koha/admin/biblio_framework.pl";
53 my $frameworkcode=$input->param('frameworkcode');
54 my $pagesize=20;
55 my $op = $input->param('op');
56 $searchfield=~ s/\,//g;
57 my ($template, $borrowernumber, $cookie)
58     = get_template_and_user({template_name => "admin/biblio_framework.tmpl",
59                              query => $input,
60                              type => "intranet",
61                              authnotrequired => 0,
62                              flagsrequired => {parameters => 1},
63                              debug => 1,
64                              });
65
66 if ($op) {
67 $template->param(script_name => $script_name,
68                                                 $op              => 1); # we show only the TMPL_VAR names $op
69 } else {
70 $template->param(script_name => $script_name,
71                                                 else              => 1); # we show only the TMPL_VAR names $op
72 }
73
74
75
76
77 ################## ADD_FORM ##################################
78 # called by default. Used to create form to add or  modify a record
79 if ($op eq 'add_form') {
80         #start the page and read in includes
81         #---- if primkey exists, it's a modify action, so read values to modify...
82         my $data;
83         if ($frameworkcode) {
84                 my $dbh = C4::Context->dbh;
85                 my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
86                 $sth->execute($frameworkcode);
87                 $data=$sth->fetchrow_hashref;
88                 $sth->finish;
89         }
90         $template->param(frameworkcode => $frameworkcode,
91                                                         frameworktext => $data->{'frameworktext'},
92                                                         );
93 ;
94                                                                                                         # END $OP eq ADD_FORM
95 ################## ADD_VALIDATE ##################################
96 # called by add_form, used to insert/modify data in DB
97 } elsif ($op eq 'add_validate') {
98         my $dbh = C4::Context->dbh;
99         my $sth=$dbh->prepare("replace biblio_framework (frameworkcode,frameworktext) values (?,?)");
100         $sth->execute($input->param('frameworkcode'),$input->param('frameworktext'));
101         $sth->finish;
102         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=biblio_framework.pl\"></html>";
103         exit;
104                                                                                                         # END $OP eq ADD_VALIDATE
105 ################## DELETE_CONFIRM ##################################
106 # called by default form, used to confirm deletion of data in DB
107 } elsif ($op eq 'delete_confirm') {
108         #start the page and read in includes
109         my $dbh = C4::Context->dbh;
110
111         # Check both categoryitem and biblioitems, see Bug 199
112         my $total = 0;
113         for my $table ('marc_tag_structure') {
114            my $sth=$dbh->prepare("select count(*) as total from $table where frameworkcode=?");
115            $sth->execute($frameworkcode);
116            $total += $sth->fetchrow_hashref->{total};
117            $sth->finish;
118         }
119
120         my $sth=$dbh->prepare("select * from biblio_framework where frameworkcode=?");
121         $sth->execute($frameworkcode);
122         my $data=$sth->fetchrow_hashref;
123         $sth->finish;
124
125         $template->param(frameworkcode => $frameworkcode,
126                                                         frameworktext => $data->{'frameworktext'},
127                                                         total => $total);
128                                                                                                         # END $OP eq DELETE_CONFIRM
129 ################## DELETE_CONFIRMED ##################################
130 # called by delete_confirm, used to effectively confirm deletion of data in DB
131 } elsif ($op eq 'delete_confirmed') {
132         #start the page and read in includes
133         my $dbh = C4::Context->dbh;
134         my $frameworkcode=uc($input->param('frameworkcode'));
135         my $sth=$dbh->prepare("delete from marc_tag_structure where frameworkcode=?");
136         $sth->execute($frameworkcode);
137         $sth=$dbh->prepare("delete from marc_subfield_structure where frameworkcode=?");
138         $sth->execute($frameworkcode);
139         $sth=$dbh->prepare("delete from biblio_framework where frameworkcode=?");
140         $sth->execute($frameworkcode);
141         $sth->finish;
142         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=biblio_framework.pl\"></html>";
143         exit;
144                                                                                                         # END $OP eq DELETE_CONFIRMED
145 ################## DEFAULT ##################################
146 } else { # DEFAULT
147         my $env;
148         my ($count,$results)=StringSearch($env,$searchfield,'web');
149         my $toggle="white";
150         my @loop_data;
151         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
152                 my %row_data;
153                 if ($toggle eq 'white'){
154                         $row_data{toggle}="#ffffcc";
155                 } else {
156                         $row_data{toggle}="white";
157                 }
158                 $row_data{frameworkcode} = $results->[$i]{'frameworkcode'};
159                 $row_data{frameworktext} = $results->[$i]{'frameworktext'};
160                 push(@loop_data, \%row_data);
161         }
162         $template->param(loop => \@loop_data);
163         if ($offset>0) {
164                 my $prevpage = $offset-$pagesize;
165                 $template->param(previous => "$script_name?offset=".$prevpage);
166         }
167         if ($offset+$pagesize<$count) {
168                 my $nextpage =$offset+$pagesize;
169                 $template->param(next => "$script_name?offset=".$nextpage);
170         }
171 } #---- END $OP eq DEFAULT
172 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
173                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
174                 IntranetNav => C4::Context->preference("IntranetNav"),
175                 );
176 output_html_with_http_headers $input, $cookie, $template->output;
177
178 # Local Variables:
179 # tab-width: 4
180 # End: