template modifications => the templates now uses common default theme icon set.
[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 $query="Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield >= $data[0]) order by tagfield";
38         my $sth=$dbh->prepare($query);
39         $sth->execute;
40         my @results;
41         my $cnt=0;
42         while (my $data=$sth->fetchrow_hashref){
43         push(@results,$data);
44         $cnt ++;
45         }
46         #  $sth->execute;
47         $sth->finish;
48         return ($cnt,\@results);
49 }
50
51 my $input = new CGI;
52 my $searchfield=$input->param('searchfield');
53 $searchfield=0 unless $searchfield;
54 my $pkfield="tagfield";
55 my $reqsel="select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where $pkfield='$searchfield'";
56 my $offset=$input->param('offset');
57 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
58
59 my $dbh = C4::Context->dbh;
60
61 my ($template, $loggedinuser, $cookie)
62     = get_template_and_user({template_name => "parameters/marctagstructure.tmpl",
63                              query => $input,
64                              type => "intranet",
65                              authnotrequired => 0,
66                              flagsrequired => {parameters => 1},
67                              debug => 1,
68                              });
69 my $pagesize=20;
70 my $op = $input->param('op');
71 $searchfield=~ s/\,//g;
72
73 if ($op) {
74 $template->param(script_name => $script_name,
75                                                 $op              => 1); # we show only the TMPL_VAR names $op
76 } else {
77 $template->param(script_name => $script_name,
78                                                 else              => 1); # we show only the TMPL_VAR names $op
79 }
80
81 ################## ADD_FORM ##################################
82 # called by default. Used to create form to add or  modify a record
83 if ($op eq 'add_form') {
84         #---- if primkey exists, it's a modify action, so read values to modify...
85         my $data;
86         if ($searchfield) {
87                 my $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where $pkfield='$searchfield'");
88                 $sth->execute;
89                 $data=$sth->fetchrow_hashref;
90                 $sth->finish;
91         }
92         my $sth = $dbh->prepare("select distinct category from authorised_values");
93         $sth->execute;
94         my @authorised_values;
95         push @authorised_values,"";
96         while ((my $category) = $sth->fetchrow_array) {
97                 push @authorised_values, $category;
98         }
99         my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
100                         -values=> \@authorised_values,
101                         -size=>1,
102                         -multiple=>0,
103                         -default => $data->{'authorised_value'},
104                         );
105
106         if ($searchfield) {
107                 $template->param(action => "Modify tag",
108                                                                 searchfield => "<input type=hidden name=tagfield value='$searchfield'>$searchfield");
109         } else {
110                 $template->param(action => "Add tag",
111                                                                 searchfield => "<input type=text name=tagfield size=5 maxlength=3>");
112         }
113         $template->param(liblibrarian => $data->{'liblibrarian'},
114                                                         libopac => $data->{'libopac'},
115                                                         repeatable => CGI::checkbox('repeatable',$data->{'repeatable'}?'checked':'',1,''),
116                                                         mandatory => CGI::checkbox('mandatory',$data->{'mandatory'}?'checked':'',1,''),
117                                                         authorised_value => $authorised_value,
118                                                         );
119                                                                                                         # END $OP eq ADD_FORM
120 ################## ADD_VALIDATE ##################################
121 # called by add_form, used to insert/modify data in DB
122 } elsif ($op eq 'add_validate') {
123         my $dbh = C4::Context->dbh;
124         my $sth=$dbh->prepare("replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value) values (?,?,?,?,?,?)");
125         my $tagfield       =$input->param('tagfield');
126         my $liblibrarian  = $input->param('liblibrarian');
127         my $libopac       =$input->param('libopac');
128         my $repeatable =$input->param('repeatable');
129         my $mandatory =$input->param('mandatory');
130         my $authorised_value =$input->param('authorised_value');
131         $sth->execute($tagfield,
132                                                 $liblibrarian,
133                                                 $libopac,
134                                                 $repeatable?1:0,
135                                                 $mandatory?1:0,
136                                                 $authorised_value
137                                                 );
138         $sth->finish;
139         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl?tagfield=$tagfield\"></html>";
140         exit;
141                                                                                                         # END $OP eq ADD_VALIDATE
142 ################## DELETE_CONFIRM ##################################
143 # called by default form, used to confirm deletion of data in DB
144 } elsif ($op eq 'delete_confirm') {
145         my $dbh = C4::Context->dbh;
146         my $sth=$dbh->prepare($reqsel);
147         $sth->execute;
148         my $data=$sth->fetchrow_hashref;
149         $sth->finish;
150         $template->param(liblibrarian => $data->{'liblibrarian'},
151                                                         searchfield => $searchfield,
152                                                         );
153                                                                                                         # END $OP eq DELETE_CONFIRM
154 ################## DELETE_CONFIRMED ##################################
155 # called by delete_confirm, used to effectively confirm deletion of data in DB
156 } elsif ($op eq 'delete_confirmed') {
157         my $dbh = C4::Context->dbh;
158         $dbh->do("delete from marc_tag_structure where $pkfield='$searchfield'");
159         $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield'");
160                                                                                                         # END $OP eq DELETE_CONFIRMED
161 ################## DEFAULT ##################################
162 } else { # DEFAULT
163         if  ($searchfield ne '') {
164                  $template->param(searchfield => "You Searched for <b>$searchfield<b><p>");
165         }
166         my $env;
167         my ($count,$results)=StringSearch($env,$searchfield,'web');
168         my $toggle="white";
169         my @loop_data = ();
170         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
171                 if ($toggle eq 'white'){
172                         $toggle="#ffffcc";
173                 } else {
174                         $toggle="white";
175                 }
176                 my %row_data;  # get a fresh hash for the row data
177                 $row_data{tagfield} = $results->[$i]{'tagfield'};
178                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
179                 $row_data{repeatable} = $results->[$i]{'repeatable'};
180                 $row_data{mandatory} = $results->[$i]{'mandatory'};
181                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
182                 $row_data{subfield_link} ="marc_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'};
183                 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'};
184                 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'};
185                 $row_data{bgcolor} = $toggle;
186                 push(@loop_data, \%row_data);
187         }
188         $template->param(loop => \@loop_data);
189         if ($offset>0) {
190                 my $prevpage = $offset-$pagesize;
191                 $template->param(previous => "<a href=$script_name?offset=".$prevpage.'>');
192         }
193         if ($offset+$pagesize<$count) {
194                 my $nextpage =$offset+$pagesize;
195                 $template->param(next => "<a href=$script_name?offset=".$nextpage.'>');
196         }
197 } #---- END $OP eq DEFAULT
198
199 $template->param(loggeninuser => $loggedinuser);
200 output_html_with_http_headers $input, $cookie, $template->output;