framework management : 1 MARC framework for each itemtype
[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::Koha;
25 use C4::Context;
26 use C4::Output;
27 use C4::Interface::CGI::Output;
28 use C4::Search;
29 use C4::Context;
30 use HTML::Template;
31
32 # retrieve parameters
33 my $input = new CGI;
34 my $itemtype = $input->param('itemtype'); # set to select framework
35 $itemtype="" unless $itemtype;
36 my $existingitemtype = $input->param('existingitemtype'); # set when we have to create a new framework (in itemtype) by copying an old one (in existingitemtype)
37 $existingitemtype = "" unless $existingitemtype;
38 my $itemtypeinfo = getitemtypeinfo($itemtype);
39 my $searchfield=$input->param('searchfield');
40 $searchfield=0 unless $searchfield;
41 $searchfield=~ s/\,//g;
42
43 my $offset=$input->param('offset');
44 my $op = $input->param('op');
45 my $pagesize=20;
46
47 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
48
49 my $dbh = C4::Context->dbh;
50
51 # open template
52 my ($template, $loggedinuser, $cookie)
53     = get_template_and_user({template_name => "parameters/marctagstructure.tmpl",
54                              query => $input,
55                              type => "intranet",
56                              authnotrequired => 0,
57                              flagsrequired => {parameters => 1},
58                              debug => 1,
59                              });
60
61 # get itemtype list
62 my $itemtypes = getitemtypes;
63 my @itemtypesloop;
64 foreach my $thisitemtype (keys %$itemtypes) {
65         my $selected = 1 if $thisitemtype eq $itemtype;
66         my %row =(value => $thisitemtype,
67                                 selected => $selected,
68                                 description => $itemtypes->{$thisitemtype}->{'description'},
69                         );
70         push @itemtypesloop, \%row;
71 }
72
73 # check that itemtype framework is defined in marc_tag_structure
74 my $sth=$dbh->prepare("select count(*) from marc_tag_structure where itemtype=?");
75 $sth->execute($itemtype);
76 my ($itemtypeexist) = $sth->fetchrow;
77 if ($itemtypeexist) {
78 } else {
79         # if itemtype does not exists, then OP must be changed to "create itemtype" if we are not on the way to create it
80         # (op = itemtyp_create_confirm)
81         if ($op eq "itemtype_create_confirm") {
82                 duplicate_framework($itemtype, $existingitemtype);
83         } else {
84                 $op = "itemtype_create";
85         }
86 }
87 $template->param(itemtypeloop => \@itemtypesloop);
88 if ($op) {
89 $template->param(script_name => $script_name,
90                                                 $op              => 1); # we show only the TMPL_VAR names $op
91 } else {
92 $template->param(script_name => $script_name,
93                                                 else              => 1); # we show only the TMPL_VAR names $op
94 }
95
96
97 ################## ADD_FORM ##################################
98 # called by default. Used to create form to add or  modify a record
99 if ($op eq 'add_form') {
100         #---- if primkey exists, it's a modify action, so read values to modify...
101         my $data;
102         if ($searchfield) {
103                 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and itemtype=?");
104                 $sth->execute($searchfield,$itemtype);
105                 $data=$sth->fetchrow_hashref;
106                 $sth->finish;
107         }
108         my $sth = $dbh->prepare("select distinct category from authorised_values");
109         $sth->execute;
110         my @authorised_values;
111         push @authorised_values,"";
112         while ((my $category) = $sth->fetchrow_array) {
113                 push @authorised_values, $category;
114         }
115         my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
116                         -values=> \@authorised_values,
117                         -size=>1,
118                         -multiple=>0,
119                         -default => $data->{'authorised_value'},
120                         );
121
122         if ($searchfield) {
123                 $template->param(action => "Modify tag",
124                                                                 searchfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$searchfield\" />$searchfield");
125                 $template->param('heading-modify-tag-p' => 1);
126         } else {
127                 $template->param(action => "Add tag",
128                                                                 searchfield => "<input type=\"text\" name=\"tagfield\" size=\"5\" maxlength=\"3\" />");
129                 $template->param('heading-add-tag-p' => 1);
130         }
131         $template->param('use-heading-flags-p' => 1);
132         $template->param(liblibrarian => $data->{'liblibrarian'},
133                                                         libopac => $data->{'libopac'},
134                                                         repeatable => CGI::checkbox('repeatable',$data->{'repeatable'}?'checked':'',1,''),
135                                                         mandatory => CGI::checkbox('mandatory',$data->{'mandatory'}?'checked':'',1,''),
136                                                         authorised_value => $authorised_value,
137                                                         itemtype => $itemtype,
138                                                         );
139                                                                                                         # END $OP eq ADD_FORM
140 ################## ADD_VALIDATE ##################################
141 # called by add_form, used to insert/modify data in DB
142 } elsif ($op eq 'add_validate') {
143         $sth=$dbh->prepare("replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,itemtype) values (?,?,?,?,?,?,?)");
144         my $tagfield       =$input->param('tagfield');
145         my $liblibrarian  = $input->param('liblibrarian');
146         my $libopac       =$input->param('libopac');
147         my $repeatable =$input->param('repeatable');
148         my $mandatory =$input->param('mandatory');
149         my $authorised_value =$input->param('authorised_value');
150         unless (C4::Context->config('demo') eq 1) {
151                 $sth->execute($tagfield,
152                                                         $liblibrarian,
153                                                         $libopac,
154                                                         $repeatable?1:0,
155                                                         $mandatory?1:0,
156                                                         $authorised_value,
157                                                         $itemtype
158                                                         );
159         }
160         $sth->finish;
161         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl?tagfield=$tagfield&itemtype=$itemtype\"></html>";
162         exit;
163                                                                                                         # END $OP eq ADD_VALIDATE
164 ################## DELETE_CONFIRM ##################################
165 # called by default form, used to confirm deletion of data in DB
166 } elsif ($op eq 'delete_confirm') {
167         $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=?");
168         $sth->execute($searchfield);
169         my $data=$sth->fetchrow_hashref;
170         $sth->finish;
171         $template->param(liblibrarian => $data->{'liblibrarian'},
172                                                         searchfield => $searchfield,
173                                                         );
174                                                                                                         # END $OP eq DELETE_CONFIRM
175 ################## DELETE_CONFIRMED ##################################
176 # called by delete_confirm, used to effectively confirm deletion of data in DB
177 } elsif ($op eq 'delete_confirmed') {
178         unless (C4::Context->config('demo') eq 1) {
179                 $dbh->do("delete from marc_tag_structure where tagfield='$searchfield'");
180                 $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield'");
181         }
182                                                                                                         # END $OP eq DELETE_CONFIRMED
183 ################## ITEMTYPE_CREATE ##################################
184 # called automatically if an unexisting itemtype is selected
185 } elsif ($op eq 'itemtype_create') {
186         $sth = $dbh->prepare("select count(*),marc_tag_structure.itemtype,description from marc_tag_structure,itemtypes where itemtypes.itemtype=marc_tag_structure.itemtype group by marc_tag_structure.itemtype");
187         $sth->execute;
188         my @existingitemtypeloop;
189         while (my ($tot,$thisitemtype,$description) = $sth->fetchrow) {
190                 if ($tot>0) {
191                         my %line = ( value => $thisitemtype,
192                                                 description => $description,
193                                         );
194                         push @existingitemtypeloop,\%line;
195                 }
196         }
197         $template->param(existingitemtypeloop => \@existingitemtypeloop,
198                                         itemtype => $itemtype,
199                                         ITdescription => $itemtypeinfo->{description},
200                                         );
201 ################## DEFAULT ##################################
202 } else { # DEFAULT
203         # here, $op can be unset or set to "itemtype_create_confirm".
204         if  ($searchfield ne '') {
205                  $template->param(searchfield => $searchfield);
206         }
207         my $env;
208         my ($count,$results)=StringSearch($env,$searchfield,$itemtype);
209         my $toggle="white";
210         my @loop_data = ();
211         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
212                 if ($toggle eq 'white'){
213                         $toggle="#ffffcc";
214                 } else {
215                         $toggle="white";
216                 }
217                 my %row_data;  # get a fresh hash for the row data
218                 $row_data{tagfield} = $results->[$i]{'tagfield'};
219                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
220                 $row_data{repeatable} = $results->[$i]{'repeatable'};
221                 $row_data{mandatory} = $results->[$i]{'mandatory'};
222                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
223                 $row_data{subfield_link} ="marc_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&itemtype=".$itemtype;
224                 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'tagfield'}."&itemtype=".$itemtype;
225                 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'tagfield'}."&itemtype=".$itemtype;
226                 $row_data{bgcolor} = $toggle;
227                 push(@loop_data, \%row_data);
228         }
229         $template->param(loop => \@loop_data);
230         if ($offset>0) {
231                 my $prevpage = $offset-$pagesize;
232                 $template->param(isprevpage => $offset,
233                                                 prevpage=> $prevpage,
234                                                 searchfield => $searchfield,
235                                                 script_name => $script_name,
236                                                 itemtype => $itemtype,
237                  );
238         }
239         if ($offset+$pagesize<$count) {
240                 my $nextpage =$offset+$pagesize;
241                 $template->param(nextpage =>$nextpage,
242                                                 searchfield => $searchfield,
243                                                 script_name => $script_name,
244                                                 itemtype => $itemtype,
245                 );
246         }
247 } #---- END $OP eq DEFAULT
248
249 $template->param(loggeninuser => $loggedinuser);
250 output_html_with_http_headers $input, $cookie, $template->output;
251
252
253 #
254 # the sub used for searches
255 #
256 sub StringSearch  {
257         my ($env,$searchstring,$itemtype)=@_;
258         my $dbh = C4::Context->dbh;
259         $searchstring=~ s/\'/\\\'/g;
260         my @data=split(' ',$searchstring);
261         my $count=@data;
262         my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield >= ? and itemtype=?) order by tagfield");
263         $sth->execute($data[0], $itemtype);
264         my @results;
265         while (my $data=$sth->fetchrow_hashref){
266         push(@results,$data);
267         }
268         #  $sth->execute;
269         $sth->finish;
270         return (scalar(@results),\@results);
271 }
272
273 #
274 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
275 #
276 sub duplicate_framework {
277         my ($newitemtype,$olditemtype) = @_;
278         my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where itemtype=?");
279         $sth->execute($olditemtype);
280         my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, itemtype) values (?,?,?,?,?,?,?)");
281         while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
282                 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newitemtype);
283         }
284
285         $sth = $dbh->prepare("select itemtype,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,thesaurus_category,value_builder,seealso from marc_subfield_structure where itemtype=?");
286         $sth->execute($olditemtype);
287         $sth_insert = $dbh->prepare("insert into marc_subfield_structure (itemtype,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,thesaurus_category,value_builder,seealso) values (?,?,?,?,?,?,?,?,?,?,?,?,?)");
288         while ( my ($itemtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso) = $sth->fetchrow) {
289                 $sth_insert->execute($newitemtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso);
290         }
291 }
292