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