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