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