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