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