Update Debian Lenny installation procedure
[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::Context;
28
29
30 # retrieve parameters
31 my $input = new CGI;
32 my $frameworkcode         = $input->param('frameworkcode')         || ''; # set to select framework
33 my $existingframeworkcode = $input->param('existingframeworkcode') || '';
34 my $searchfield           = $input->param('searchfield') || 0;
35 # set when we have to create a new framework (in frameworkcode) by copying an old one (in existingframeworkcode)
36 my $frameworkinfo = getframeworkinfo($frameworkcode);
37 $searchfield=~ s/\,//g;
38
39 my $offset    = $input->param('offset') || 0;
40 my $op        = $input->param('op')     || '';
41 my $dspchoice = $input->param('select_display');
42 my $pagesize = 20;
43
44 my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl";
45
46 my $dbh = C4::Context->dbh;
47
48 # open template
49 my ($template, $loggedinuser, $cookie)
50     = get_template_and_user({template_name => "admin/marctagstructure.tmpl",
51                              query => $input,
52                              type => "intranet",
53                              authnotrequired => 0,
54                              flagsrequired => {parameters => 1},
55                              debug => 1,
56                              });
57
58 # get framework list
59 my $frameworks = getframeworks();
60 my @frameworkloop;
61 foreach my $thisframeworkcode (keys %$frameworks) {
62         push @frameworkloop, {
63         value => $thisframeworkcode,
64         selected => ($thisframeworkcode eq $frameworkcode) ? 1 : 0,
65         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
66     };
67 }
68
69 # check that framework is defined in marc_tag_structure
70 my $sth=$dbh->prepare("select count(*) from marc_tag_structure where frameworkcode=?");
71 $sth->execute($frameworkcode);
72 my ($frameworkexist) = $sth->fetchrow;
73 unless ($frameworkexist) {
74         # if frameworkcode does not exists, then OP must be changed to "create framework" if we are not on the way to create it
75         # (op = itemtyp_create_confirm)
76         if ($op eq "framework_create_confirm") {
77                 duplicate_framework($frameworkcode, $existingframeworkcode);
78                 $op = ""; # unset $op to go back to framework list
79         } else {
80                 $op = "framework_create";
81         }
82 }
83 $template->param(
84     frameworkloop => \@frameworkloop,
85     frameworkcode => $frameworkcode,
86     frameworktext => $frameworkinfo->{frameworktext},
87     script_name   => $script_name,
88     ($op||'else') => 1,
89 );
90
91
92 ################## ADD_FORM ##################################
93 # called by default. Used to create form to add or  modify a record
94 if ($op eq 'add_form') {
95         #---- if primkey exists, it's a modify action, so read values to modify...
96         my $data;
97         if ($searchfield) {
98                 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
99                 $sth->execute($searchfield,$frameworkcode);
100                 $data=$sth->fetchrow_hashref;
101         }
102         my $sth = $dbh->prepare("select distinct category from authorised_values");
103         $sth->execute;
104         my @authorised_values;
105         push @authorised_values,"";
106         while ((my $category) = $sth->fetchrow_array) {
107                 push @authorised_values, $category;
108         }
109         my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
110                         -values=> \@authorised_values,
111                         -size=>1,
112                         -id=>"authorised_value",
113                         -multiple=>0,
114                         -default => $data->{'authorised_value'},
115                         );
116
117         if ($searchfield) {
118         $template->param(searchfield => $searchfield);
119                 $template->param(action => "Modify tag");
120                 $template->param('heading-modify-tag-p' => 1);
121         } else {
122                 $template->param(action => "Add tag");
123                 $template->param('heading-add-tag-p' => 1);
124         }
125         $template->param('use-heading-flags-p' => 1);
126         $template->param(liblibrarian => $data->{'liblibrarian'},
127                         libopac => $data->{'libopac'},
128                         repeatable => CGI::checkbox(-name=>'repeatable',
129                                                 -checked=> $data->{'repeatable'}?'checked':'',
130                                                 -value=> 1,
131                                                 -label => '',
132                                                 -id=> 'repeatable'),
133                         mandatory => CGI::checkbox(-name => 'mandatory',
134                                                 -checked => $data->{'mandatory'}?'checked':'',
135                                                 -value => 1,
136                                                 -label => '',
137                                                 -id => 'mandatory'),
138                         authorised_value => $authorised_value,
139                         frameworkcode => $frameworkcode,
140     );  # FIXME: move checkboxes to presentation layer
141                                                                                                         # END $OP eq ADD_FORM
142 ################## ADD_VALIDATE ##################################
143 # called by add_form, used to insert/modify data in DB
144 } elsif ($op eq 'add_validate') {
145         my $tagfield         = $input->param('tagfield');
146         my $liblibrarian     = $input->param('liblibrarian');
147         my $libopac          = $input->param('libopac');
148         my $repeatable       = $input->param('repeatable') ? 1 : 0;
149         my $mandatory        = $input->param('mandatory')  ? 1 : 0;
150         my $authorised_value = $input->param('authorised_value');
151     unless (C4::Context->config('demo') eq 1) {
152         if ($input->param('modif')) {
153             $sth = $dbh->prepare(
154             "UPDATE marc_tag_structure SET liblibrarian=? ,libopac=? ,repeatable=? ,mandatory=? ,authorised_value=? WHERE frameworkcode=? AND tagfield=?"
155             );
156             $sth->execute(  $liblibrarian,
157                             $libopac,
158                             $repeatable,
159                             $mandatory,
160                             $authorised_value,
161                             $frameworkcode,
162                             $tagfield
163             );
164         } else {
165             $sth = $dbh->prepare(
166             "INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)"
167             );
168             $sth->execute($tagfield,
169                           $liblibrarian,
170                           $libopac,
171                           $repeatable,
172                           $mandatory,
173                           $authorised_value,
174                           $frameworkcode
175             );
176         }
177         }
178     print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
179         exit;
180                                                                                                         # END $OP eq ADD_VALIDATE
181 ################## DELETE_CONFIRM ##################################
182 # called by default form, used to confirm deletion of data in DB
183 } elsif ($op eq 'delete_confirm') {
184         $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
185     $sth->execute($searchfield, $frameworkcode);
186     my $data = $sth->fetchrow_hashref;
187         $template->param(
188          liblibrarian => $data->{'liblibrarian'},
189           searchfield => $searchfield,
190         frameworkcode => $frameworkcode,
191     );
192                                                                                                         # END $OP eq DELETE_CONFIRM
193 ################## DELETE_CONFIRMED ##################################
194 # called by delete_confirm, used to effectively confirm deletion of data in DB
195 } elsif ($op eq 'delete_confirmed') {
196         unless (C4::Context->config('demo') eq 1) {
197         my $sth1 = $dbh->prepare("DELETE FROM marc_tag_structure      WHERE tagfield=? AND frameworkcode=?");
198         my $sth2 = $dbh->prepare("DELETE FROM marc_subfield_structure WHERE tagfield=? AND frameworkcode=?");
199         $sth1->execute($searchfield, $frameworkcode);
200         $sth2->execute($searchfield, $frameworkcode);
201         }
202         $template->param(
203           searchfield => $searchfield,
204         frameworkcode => $frameworkcode,
205     );
206                                                                                                         # END $OP eq DELETE_CONFIRMED
207 ################## ITEMTYPE_CREATE ##################################
208 # called automatically if an unexisting  frameworkis selected
209 } elsif ($op eq 'framework_create') {
210         $sth = $dbh->prepare("select count(*),marc_tag_structure.frameworkcode,frameworktext from marc_tag_structure,biblio_framework where biblio_framework.frameworkcode=marc_tag_structure.frameworkcode group by marc_tag_structure.frameworkcode");
211         $sth->execute;
212         my @existingframeworkloop;
213         while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
214                 if ($tot>0) {
215                         push @existingframeworkloop, {
216                 value => $thisframeworkcode,
217                 frameworktext => $frameworktext,
218             };
219                 }
220         }
221         $template->param(existingframeworkloop => \@existingframeworkloop,
222                                         frameworkcode => $frameworkcode,
223 #                                       FRtext => $frameworkinfo->{frameworktext},
224                                         );
225 ################## DEFAULT ##################################
226 } else { # DEFAULT
227         # here, $op can be unset or set to "framework_create_confirm".
228     if ($searchfield ne '') {
229         $template->param(searchfield => $searchfield);
230     }
231         my $cnt=0;
232         if ($dspchoice) {
233                 #here, user only wants used tags/subfields displayed
234                 $searchfield=~ s/\'/\\\'/g;
235                 my @data=split(' ',$searchfield);
236                 my $sth=$dbh->prepare("
237                       SELECT marc_tag_structure.tagfield AS mts_tagfield,
238                               marc_tag_structure.liblibrarian as mts_liblibrarian,
239                               marc_tag_structure.libopac as mts_libopac,
240                               marc_tag_structure.repeatable as mts_repeatable,
241                               marc_tag_structure.mandatory as mts_mandatory,
242                               marc_tag_structure.authorised_value as mts_authorized_value,
243                               marc_subfield_structure.*
244                 FROM marc_tag_structure 
245                 LEFT JOIN marc_subfield_structure ON (marc_tag_structure.tagfield=marc_subfield_structure.tagfield AND marc_tag_structure.frameworkcode=marc_subfield_structure.frameworkcode) WHERE (marc_tag_structure.tagfield >= ? and marc_tag_structure.frameworkcode=?) AND marc_subfield_structure.tab>=0 ORDER BY marc_tag_structure.tagfield,marc_subfield_structure.tagsubfield");
246                 #could be ordoned by tab
247                 $sth->execute($data[0], $frameworkcode);
248                 my @results = ();
249                 while (my $data=$sth->fetchrow_hashref){
250                         push(@results,$data);
251                         $cnt++;
252                 }
253                 
254                 my @loop_data = ();
255                 my $j=1;
256                 my $i=$offset;
257                 while ($i < ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt)) {
258                         my %row_data;  # get a fresh hash for the row data
259                         $row_data{tagfield}         = $results[$i]->{'mts_tagfield'};
260                         $row_data{liblibrarian}     = $results[$i]->{'mts_liblibrarian'};
261                         $row_data{repeatable}       = $results[$i]->{'mts_repeatable'};
262                         $row_data{mandatory}        = $results[$i]->{'mts_mandatory'};
263                         $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
264                         $row_data{subfield_link} = "marc_subfields_structure.pl?op=add_form&amp;tagfield=".$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
265                         $row_data{edit}          = "$script_name?op=add_form&amp;searchfield="            .$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
266                         $row_data{delete}        = "$script_name?op=delete_confirm&amp;searchfield="      .$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
267                         $j=$i;
268                         my @internal_loop = ();
269                         while (($results[$i]->{'tagfield'}==$results[$j]->{'tagfield'}) and ($j< ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt))) {
270                                 my %subfield_data;
271                                 $subfield_data{tagsubfield}      = $results[$j]->{'tagsubfield'};
272                                 $subfield_data{liblibrarian}     = $results[$j]->{'liblibrarian'};
273                                 $subfield_data{kohafield}        = $results[$j]->{'kohafield'};
274                                 $subfield_data{repeatable}       = $results[$j]->{'repeatable'};
275                                 $subfield_data{mandatory}        = $results[$j]->{'mandatory'};
276                                 $subfield_data{tab}              = $results[$j]->{'tab'};
277                                 $subfield_data{seealso}          = $results[$j]->{'seealso'};
278                                 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
279                                 $subfield_data{authtypecode}     = $results[$j]->{'authtypecode'};
280                                 $subfield_data{value_builder}    = $results[$j]->{'value_builder'};
281 #                               warn "tagfield :  ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
282                                 push @internal_loop,\%subfield_data;
283                                 $j++;
284                         }
285                         $row_data{'subfields'}=\@internal_loop;
286                         push(@loop_data, \%row_data);
287                         $i=$j;
288                 }
289                 $template->param(select_display => "True",
290                                                 loop => \@loop_data);
291         } else {
292                 #here, normal old style : display every tags
293                 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
294                 $cnt = $count;
295                 my @loop_data = ();
296                 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
297                         my %row_data;  # get a fresh hash for the row data
298                         $row_data{tagfield}         = $results->[$i]{'tagfield'};
299                         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
300                         $row_data{repeatable}       = $results->[$i]{'repeatable'};
301                         $row_data{mandatory}        = $results->[$i]{'mandatory'};
302                         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
303                         $row_data{subfield_link}    = "marc_subfields_structure.pl?tagfield="          .$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
304                         $row_data{edit}             = "$script_name?op=add_form&amp;searchfield="      .$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
305                         $row_data{delete}           = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
306                         push(@loop_data, \%row_data);
307                 }
308                 $template->param(loop => \@loop_data);
309         }
310         if ($offset>0) {
311                 $template->param(isprevpage => $offset,
312                                                 prevpage=> $offset-$pagesize,
313                                                 searchfield => $searchfield,
314                                                 script_name => $script_name,
315                                                 frameworkcode => $frameworkcode,
316                 );
317         }
318         if ($offset+$pagesize<$cnt) {
319                 $template->param(nextpage =>$offset+$pagesize,
320                                                 searchfield => $searchfield,
321                                                 script_name => $script_name,
322                                                 frameworkcode => $frameworkcode,
323                 );
324         }
325 } #---- END $OP eq DEFAULT
326
327 output_html_with_http_headers $input, $cookie, $template->output;
328
329 #
330 # the sub used for searches
331 #
332 sub StringSearch  {
333         my ($searchstring,$frameworkcode)=@_;
334         my $sth = C4::Context->dbh->prepare("
335     SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value
336      FROM  marc_tag_structure
337      WHERE (tagfield >= ? and frameworkcode=?)
338     ORDER BY tagfield
339     ");
340         $sth->execute($searchstring, $frameworkcode);
341         my $results = $sth->fetchall_arrayref({});
342         return (scalar(@$results), $results);
343 }
344
345 #
346 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
347 #
348 sub duplicate_framework {
349         my ($newframeworkcode,$oldframeworkcode) = @_;
350         my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where frameworkcode=?");
351         $sth->execute($oldframeworkcode);
352         my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode) values (?,?,?,?,?,?,?)");
353         while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
354                 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newframeworkcode);
355         }
356
357         $sth = $dbh->prepare("select frameworkcode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,authtypecode,value_builder,seealso,hidden from marc_subfield_structure where frameworkcode=?");
358         $sth->execute($oldframeworkcode);
359         $sth_insert = $dbh->prepare("insert into marc_subfield_structure (frameworkcode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,authtypecode,value_builder,seealso,hidden) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
360         while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso,$hidden) = $sth->fetchrow) {
361             $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso, $hidden);
362         }
363 }
364