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