Bug 11703 [QA Followup]
[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.tmpl",
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         my $sth = $dbh->prepare("select distinct category from authorised_values");
104         $sth->execute;
105         my @authorised_values;
106         push @authorised_values,"";
107         while ((my $category) = $sth->fetchrow_array) {
108                 push @authorised_values, $category;
109         }
110         my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
111                         -values=> \@authorised_values,
112                         -size=>1,
113                         -id=>"authorised_value",
114                         -multiple=>0,
115                         -default => $data->{'authorised_value'},
116                         );
117
118         if ($searchfield) {
119         $template->param(searchfield => $searchfield);
120                 $template->param(action => "Modify tag");
121                 $template->param('heading_modify_tag_p' => 1);
122         } else {
123                 $template->param(action => "Add tag");
124                 $template->param('heading_add_tag_p' => 1);
125         }
126         $template->param('use_heading_flags_p' => 1);
127         $template->param(liblibrarian => $data->{'liblibrarian'},
128                         libopac => $data->{'libopac'},
129             repeatable => $data->{'repeatable'},
130             mandatory => $data->{'mandatory'},
131                         authorised_value => $authorised_value,
132                         frameworkcode => $frameworkcode,
133     );  # FIXME: move checkboxes to presentation layer
134                                                                                                         # END $OP eq ADD_FORM
135 ################## ADD_VALIDATE ##################################
136 # called by add_form, used to insert/modify data in DB
137 } elsif ($op eq 'add_validate') {
138         my $tagfield         = $input->param('tagfield');
139         my $liblibrarian     = $input->param('liblibrarian');
140         my $libopac          = $input->param('libopac');
141         my $repeatable       = $input->param('repeatable') ? 1 : 0;
142         my $mandatory        = $input->param('mandatory')  ? 1 : 0;
143         my $authorised_value = $input->param('authorised_value');
144     unless (C4::Context->config('demo')) {
145         if ($input->param('modif')) {
146             $sth = $dbh->prepare(
147             "UPDATE marc_tag_structure SET liblibrarian=? ,libopac=? ,repeatable=? ,mandatory=? ,authorised_value=? WHERE frameworkcode=? AND tagfield=?"
148             );
149             $sth->execute(  $liblibrarian,
150                             $libopac,
151                             $repeatable,
152                             $mandatory,
153                             $authorised_value,
154                             $frameworkcode,
155                             $tagfield
156             );
157         } else {
158             $sth = $dbh->prepare(
159             "INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)"
160             );
161             $sth->execute($tagfield,
162                           $liblibrarian,
163                           $libopac,
164                           $repeatable,
165                           $mandatory,
166                           $authorised_value,
167                           $frameworkcode
168             );
169         }
170         }
171     print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
172         exit;
173                                                                                                         # END $OP eq ADD_VALIDATE
174 ################## DELETE_CONFIRM ##################################
175 # called by default form, used to confirm deletion of data in DB
176 } elsif ($op eq 'delete_confirm') {
177         $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
178     $sth->execute($searchfield, $frameworkcode);
179     my $data = $sth->fetchrow_hashref;
180         $template->param(
181          liblibrarian => $data->{'liblibrarian'},
182           searchfield => $searchfield,
183         frameworkcode => $frameworkcode,
184     );
185                                                                                                         # END $OP eq DELETE_CONFIRM
186 ################## DELETE_CONFIRMED ##################################
187 # called by delete_confirm, used to effectively confirm deletion of data in DB
188 } elsif ($op eq 'delete_confirmed') {
189         unless (C4::Context->config('demo')) {
190         my $sth1 = $dbh->prepare("DELETE FROM marc_tag_structure      WHERE tagfield=? AND frameworkcode=?");
191         my $sth2 = $dbh->prepare("DELETE FROM marc_subfield_structure WHERE tagfield=? AND frameworkcode=?");
192         $sth1->execute($searchfield, $frameworkcode);
193         $sth2->execute($searchfield, $frameworkcode);
194         }
195         $template->param(
196           searchfield => $searchfield,
197         frameworkcode => $frameworkcode,
198     );
199                                                                                                         # END $OP eq DELETE_CONFIRMED
200 ################## ITEMTYPE_CREATE ##################################
201 # called automatically if an unexisting  frameworkis selected
202 } elsif ($op eq 'framework_create') {
203         $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");
204         $sth->execute;
205         my @existingframeworkloop;
206         while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
207                 if ($tot>0) {
208                         push @existingframeworkloop, {
209                 value => $thisframeworkcode,
210                 frameworktext => $frameworktext,
211             };
212                 }
213         }
214         $template->param(existingframeworkloop => \@existingframeworkloop,
215                                         frameworkcode => $frameworkcode,
216 #                                       FRtext => $frameworkinfo->{frameworktext},
217                                         );
218 ################## DEFAULT ##################################
219 } else { # DEFAULT
220         # here, $op can be unset or set to "framework_create_confirm".
221     if ($searchfield ne '') {
222         $template->param(searchfield => $searchfield);
223     }
224         my $cnt=0;
225         if ($dspchoice) {
226                 #here, user only wants used tags/subfields displayed
227                 $searchfield=~ s/\'/\\\'/g;
228                 my @data=split(' ',$searchfield);
229                 my $sth=$dbh->prepare("
230                       SELECT marc_tag_structure.tagfield AS mts_tagfield,
231                               marc_tag_structure.liblibrarian as mts_liblibrarian,
232                               marc_tag_structure.libopac as mts_libopac,
233                               marc_tag_structure.repeatable as mts_repeatable,
234                               marc_tag_structure.mandatory as mts_mandatory,
235                               marc_tag_structure.authorised_value as mts_authorized_value,
236                               marc_subfield_structure.*
237                 FROM marc_tag_structure 
238                 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");
239                 #could be ordoned by tab
240                 $sth->execute($data[0], $frameworkcode);
241                 my @results = ();
242                 while (my $data=$sth->fetchrow_hashref){
243                         push(@results,$data);
244                         $cnt++;
245                 }
246                 
247                 my @loop_data = ();
248                 my $j=1;
249                 my $i=$offset;
250         while ( $i < $cnt ) {
251                         my %row_data;  # get a fresh hash for the row data
252                         $row_data{tagfield}         = $results[$i]->{'mts_tagfield'};
253                         $row_data{liblibrarian}     = $results[$i]->{'mts_liblibrarian'};
254                         $row_data{repeatable}       = $results[$i]->{'mts_repeatable'};
255                         $row_data{mandatory}        = $results[$i]->{'mts_mandatory'};
256                         $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
257                         $row_data{subfield_link} = "marc_subfields_structure.pl?op=add_form&amp;tagfield=".$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
258                         $row_data{edit}          = "$script_name?op=add_form&amp;searchfield="            .$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
259                         $row_data{delete}        = "$script_name?op=delete_confirm&amp;searchfield="      .$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
260                         $j=$i;
261                         my @internal_loop = ();
262                         while ( ( $j < $cnt ) and ( $results[$i]->{'tagfield'} == $results[$j]->{'tagfield'} ) ) {
263                                 my %subfield_data;
264                                 $subfield_data{tagsubfield}      = $results[$j]->{'tagsubfield'};
265                                 $subfield_data{liblibrarian}     = $results[$j]->{'liblibrarian'};
266                                 $subfield_data{kohafield}        = $results[$j]->{'kohafield'};
267                                 $subfield_data{repeatable}       = $results[$j]->{'repeatable'};
268                                 $subfield_data{mandatory}        = $results[$j]->{'mandatory'};
269                                 $subfield_data{tab}              = $results[$j]->{'tab'};
270                                 $subfield_data{seealso}          = $results[$j]->{'seealso'};
271                                 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
272                                 $subfield_data{authtypecode}     = $results[$j]->{'authtypecode'};
273                                 $subfield_data{value_builder}    = $results[$j]->{'value_builder'};
274 #                               warn "tagfield :  ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
275                                 push @internal_loop,\%subfield_data;
276                                 $j++;
277                         }
278                         $row_data{'subfields'}=\@internal_loop;
279                         push(@loop_data, \%row_data);
280                         $i=$j;
281                 }
282                 $template->param(select_display => "True",
283                                                 loop => \@loop_data);
284         } else {
285                 #here, normal old style : display every tags
286                 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
287                 $cnt = $count;
288                 my @loop_data = ();
289         for ( my $i = $offset ; $i < $count ; $i++ ) {
290                         my %row_data;  # get a fresh hash for the row data
291                         $row_data{tagfield}         = $results->[$i]{'tagfield'};
292                         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
293                         $row_data{repeatable}       = $results->[$i]{'repeatable'};
294                         $row_data{mandatory}        = $results->[$i]{'mandatory'};
295                         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
296                         $row_data{subfield_link}    = "marc_subfields_structure.pl?tagfield="          .$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
297                         $row_data{edit}             = "$script_name?op=add_form&amp;searchfield="      .$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
298                         $row_data{delete}           = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
299                         push(@loop_data, \%row_data);
300                 }
301                 $template->param(loop => \@loop_data);
302         }
303         if ($offset>0) {
304                 $template->param(isprevpage => $offset,
305                                                 prevpage=> $offset-$pagesize,
306                                                 searchfield => $searchfield,
307                                                 script_name => $script_name,
308                                                 frameworkcode => $frameworkcode,
309                 );
310         }
311         if ($offset+$pagesize<$cnt) {
312                 $template->param(nextpage =>$offset+$pagesize,
313                                                 searchfield => $searchfield,
314                                                 script_name => $script_name,
315                                                 frameworkcode => $frameworkcode,
316                 );
317         }
318 } #---- END $OP eq DEFAULT
319
320 output_html_with_http_headers $input, $cookie, $template->output;
321
322 #
323 # the sub used for searches
324 #
325 sub StringSearch  {
326         my ($searchstring,$frameworkcode)=@_;
327         my $sth = C4::Context->dbh->prepare("
328     SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value
329      FROM  marc_tag_structure
330      WHERE (tagfield >= ? and frameworkcode=?)
331     ORDER BY tagfield
332     ");
333         $sth->execute($searchstring, $frameworkcode);
334         my $results = $sth->fetchall_arrayref({});
335         return (scalar(@$results), $results);
336 }
337
338 #
339 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
340 #
341 sub duplicate_framework {
342         my ($newframeworkcode,$oldframeworkcode) = @_;
343         my $dbh = C4::Context->dbh;
344         my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where frameworkcode=?");
345         $sth->execute($oldframeworkcode);
346         my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode) values (?,?,?,?,?,?,?)");
347         while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
348                 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newframeworkcode);
349         }
350
351         $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=?");
352         $sth->execute($oldframeworkcode);
353         $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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
354         while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso,$hidden) = $sth->fetchrow) {
355             $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso, $hidden);
356         }
357 }
358