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