Bug 13321: Fix table display in invoice page
[koha.git] / admin / marctagstructure.pl
blob575f4f4bc7155e0bb541d078650aaf5c80129215
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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.
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.
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>.
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;
30 use Koha::Caches;
31 use Koha::BiblioFrameworks;
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;
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;
45 my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl";
47 my $dbh = C4::Context->dbh;
48 my $cache = Koha::Caches->get_instance();
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 authnotrequired => 0,
56 flagsrequired => {parameters => 'parameters_remaining_permissions'},
57 debug => 1,
58 });
60 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
62 # check that framework is defined in marc_tag_structure
63 my $sth=$dbh->prepare("select count(*) from marc_tag_structure where frameworkcode=?");
64 $sth->execute($frameworkcode);
65 my ($frameworkexist) = $sth->fetchrow;
66 unless ($frameworkexist) {
67 # if frameworkcode does not exists, then OP must be changed to "create framework" if we are not on the way to create it
68 # (op = itemtyp_create_confirm)
69 if ($op eq "framework_create_confirm") {
70 duplicate_framework($frameworkcode, $existingframeworkcode);
71 $op = ""; # unset $op to go back to framework list
72 } else {
73 $op = "framework_create";
77 my $framework = $frameworks->search({ frameworkcode => $frameworkcode })->next;
78 $template->param(
79 frameworks => $frameworks,
80 framework => $framework,
81 script_name => $script_name,
82 ( $op || 'else' ) => 1,
86 ################## ADD_FORM ##################################
87 # called by default. Used to create form to add or modify a record
88 if ($op eq 'add_form') {
89 #---- if primkey exists, it's a modify action, so read values to modify...
90 my $data;
91 if ($searchfield) {
92 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
93 $sth->execute($searchfield,$frameworkcode);
94 $data=$sth->fetchrow_hashref;
97 my @authorised_values = @{C4::Koha::GetAuthorisedValueCategories()}; # function returns array ref, dereferencing
98 unshift @authorised_values, ""; # put empty value first
99 my $authorised_value = {
100 values => \@authorised_values,
101 default => $data->{'authorised_value'},
104 if ($searchfield) {
105 $template->param(searchfield => $searchfield);
106 $template->param(action => "Modify tag");
107 $template->param('heading_modify_tag_p' => 1);
108 } else {
109 $template->param(action => "Add tag");
110 $template->param('heading_add_tag_p' => 1);
112 $template->param('use_heading_flags_p' => 1);
113 $template->param(liblibrarian => $data->{'liblibrarian'},
114 libopac => $data->{'libopac'},
115 repeatable => $data->{'repeatable'},
116 mandatory => $data->{'mandatory'},
117 authorised_value => $authorised_value,
118 frameworkcode => $frameworkcode,
119 ); # FIXME: move checkboxes to presentation layer
120 # END $OP eq ADD_FORM
121 ################## ADD_VALIDATE ##################################
122 # called by add_form, used to insert/modify data in DB
123 } elsif ($op eq 'add_validate') {
124 my $tagfield = $input->param('tagfield');
125 my $liblibrarian = $input->param('liblibrarian');
126 my $libopac = $input->param('libopac');
127 my $repeatable = $input->param('repeatable') ? 1 : 0;
128 my $mandatory = $input->param('mandatory') ? 1 : 0;
129 my $authorised_value = $input->param('authorised_value');
130 unless (C4::Context->config('demo')) {
131 if ($input->param('modif')) {
132 $sth = $dbh->prepare(
133 "UPDATE marc_tag_structure SET liblibrarian=? ,libopac=? ,repeatable=? ,mandatory=? ,authorised_value=? WHERE frameworkcode=? AND tagfield=?"
135 $sth->execute( $liblibrarian,
136 $libopac,
137 $repeatable,
138 $mandatory,
139 $authorised_value,
140 $frameworkcode,
141 $tagfield
143 } else {
144 $sth = $dbh->prepare(
145 "INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)"
147 $sth->execute($tagfield,
148 $liblibrarian,
149 $libopac,
150 $repeatable,
151 $mandatory,
152 $authorised_value,
153 $frameworkcode
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-$frameworkcode");
159 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
161 print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
162 exit;
163 # END $OP eq ADD_VALIDATE
164 ################## DELETE_CONFIRM ##################################
165 # called by default form, used to confirm deletion of data in DB
166 } elsif ($op eq 'delete_confirm') {
167 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
168 $sth->execute($searchfield, $frameworkcode);
169 my $data = $sth->fetchrow_hashref;
170 $template->param(
171 liblibrarian => $data->{'liblibrarian'},
172 searchfield => $searchfield,
173 frameworkcode => $frameworkcode,
175 # END $OP eq DELETE_CONFIRM
176 ################## DELETE_CONFIRMED ##################################
177 # called by delete_confirm, used to effectively confirm deletion of data in DB
178 } elsif ($op eq 'delete_confirmed') {
179 unless (C4::Context->config('demo')) {
180 my $sth1 = $dbh->prepare("DELETE FROM marc_tag_structure WHERE tagfield=? AND frameworkcode=?");
181 my $sth2 = $dbh->prepare("DELETE FROM marc_subfield_structure WHERE tagfield=? AND frameworkcode=?");
182 $sth1->execute($searchfield, $frameworkcode);
183 $sth2->execute($searchfield, $frameworkcode);
184 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
185 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
186 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
187 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
189 $template->param(
190 searchfield => $searchfield,
191 frameworkcode => $frameworkcode,
193 # END $OP eq DELETE_CONFIRMED
194 ################## ITEMTYPE_CREATE ##################################
195 # called automatically if an unexisting frameworkis selected
196 } elsif ($op eq 'framework_create') {
197 $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");
198 $sth->execute;
199 my @existingframeworkloop;
200 while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
201 if ($tot>0) {
202 push @existingframeworkloop, {
203 value => $thisframeworkcode,
204 frameworktext => $frameworktext,
208 $template->param(existingframeworkloop => \@existingframeworkloop,
209 frameworkcode => $frameworkcode,
211 ################## DEFAULT ##################################
212 } else { # DEFAULT
213 # here, $op can be unset or set to "framework_create_confirm".
214 if ($searchfield ne '') {
215 $template->param(searchfield => $searchfield);
217 my $cnt=0;
218 if ($dspchoice) {
219 #here, user only wants used tags/subfields displayed
220 $searchfield=~ s/\'/\\\'/g;
221 my @data=split(' ',$searchfield);
222 my $sth=$dbh->prepare("
223 SELECT marc_tag_structure.tagfield AS mts_tagfield,
224 marc_tag_structure.liblibrarian as mts_liblibrarian,
225 marc_tag_structure.libopac as mts_libopac,
226 marc_tag_structure.repeatable as mts_repeatable,
227 marc_tag_structure.mandatory as mts_mandatory,
228 marc_tag_structure.authorised_value as mts_authorized_value,
229 marc_subfield_structure.*
230 FROM marc_tag_structure
231 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");
232 #could be ordoned by tab
233 $sth->execute($data[0], $frameworkcode);
234 my @results = ();
235 while (my $data=$sth->fetchrow_hashref){
236 push(@results,$data);
237 $cnt++;
240 my @loop_data = ();
241 my $j=1;
242 my $i=$offset;
243 while ( $i < $cnt ) {
244 my %row_data; # get a fresh hash for the row data
245 $row_data{tagfield} = $results[$i]->{'mts_tagfield'};
246 $row_data{liblibrarian} = $results[$i]->{'mts_liblibrarian'};
247 $row_data{repeatable} = $results[$i]->{'mts_repeatable'};
248 $row_data{mandatory} = $results[$i]->{'mts_mandatory'};
249 $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
250 $row_data{subfield_link} = "marc_subfields_structure.pl?op=add_form&amp;tagfield=".$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
251 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=" .$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
252 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=" .$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
253 $j=$i;
254 my @internal_loop = ();
255 while ( ( $j < $cnt ) and ( $results[$i]->{'tagfield'} == $results[$j]->{'tagfield'} ) ) {
256 my %subfield_data;
257 $subfield_data{tagsubfield} = $results[$j]->{'tagsubfield'};
258 $subfield_data{liblibrarian} = $results[$j]->{'liblibrarian'};
259 $subfield_data{kohafield} = $results[$j]->{'kohafield'};
260 $subfield_data{repeatable} = $results[$j]->{'repeatable'};
261 $subfield_data{mandatory} = $results[$j]->{'mandatory'};
262 $subfield_data{tab} = $results[$j]->{'tab'};
263 $subfield_data{seealso} = $results[$j]->{'seealso'};
264 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
265 $subfield_data{authtypecode} = $results[$j]->{'authtypecode'};
266 $subfield_data{value_builder} = $results[$j]->{'value_builder'};
267 # warn "tagfield : ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
268 push @internal_loop,\%subfield_data;
269 $j++;
271 $row_data{'subfields'}=\@internal_loop;
272 push(@loop_data, \%row_data);
273 $i=$j;
275 $template->param(select_display => "True",
276 loop => \@loop_data);
277 } else {
278 # Hidden feature: If search was field$subfield, redirect to the subfield edit form
279 my ( $tagfield, $tagsubfield ) = split /\$/, $searchfield;
280 if ( $tagsubfield ) {
281 print $input->redirect('/cgi-bin/koha/admin/marc_subfields_structure.pl?op=add_form&tagfield='.$tagfield.'&frameworkcode='.$frameworkcode.'#sub'.$tagsubfield.'field');
282 exit;
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);
300 $template->param(loop => \@loop_data);
302 if ($offset>0) {
303 $template->param(isprevpage => $offset,
304 prevpage=> $offset-$pagesize,
305 searchfield => $searchfield,
306 script_name => $script_name,
307 frameworkcode => $frameworkcode,
310 if ($offset+$pagesize<$cnt) {
311 $template->param(nextpage =>$offset+$pagesize,
312 searchfield => $searchfield,
313 script_name => $script_name,
314 frameworkcode => $frameworkcode,
317 } #---- END $OP eq DEFAULT
319 output_html_with_http_headers $input, $cookie, $template->output;
322 # the sub used for searches
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
332 $sth->execute($searchstring, $frameworkcode);
333 my $results = $sth->fetchall_arrayref({});
334 return (scalar(@$results), $results);
338 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
340 sub duplicate_framework {
341 my ($newframeworkcode,$oldframeworkcode) = @_;
342 my $dbh = C4::Context->dbh;
343 $dbh->do(q|INSERT INTO marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode)
344 SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value, ? from marc_tag_structure where frameworkcode=?|, undef, $newframeworkcode, $oldframeworkcode );
346 $dbh->do(q|INSERT INTO marc_subfield_structure (frameworkcode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,authtypecode,value_builder,seealso,hidden)
347 SELECT ?,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,authtypecode,value_builder,seealso,hidden from marc_subfield_structure where frameworkcode=?
348 |, undef, $newframeworkcode, $oldframeworkcode );