Bug 16889: Remove C4::Items::biblioitems_columns and use Koha::Biblioitems->columns...
[koha.git] / admin / marctagstructure.pl
blob27d9592237f3deda8ea0efba9515e911ada6ba72
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::Cache;
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;
41 my $offset = $input->param('offset') || 0;
42 my $op = $input->param('op') || '';
43 my $dspchoice = $input->cookie("marctagstructure_selectdisplay") // $input->param('select_display');
44 my $pagesize = 20;
46 my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl";
48 my $dbh = C4::Context->dbh;
49 my $cache = Koha::Cache->get_instance();
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 });
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'},
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";
86 $template->param(
87 frameworkloop => \@frameworkloop,
88 frameworkcode => $frameworkcode,
89 frameworktext => $frameworkinfo->{frameworktext},
90 script_name => $script_name,
91 ($op||'else') => 1,
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;
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'},
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);
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=?"
144 $sth->execute( $liblibrarian,
145 $libopac,
146 $repeatable,
147 $mandatory,
148 $authorised_value,
149 $frameworkcode,
150 $tagfield
152 } else {
153 $sth = $dbh->prepare(
154 "INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)"
156 $sth->execute($tagfield,
157 $liblibrarian,
158 $libopac,
159 $repeatable,
160 $mandatory,
161 $authorised_value,
162 $frameworkcode
165 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
166 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
167 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
168 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
170 print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
171 exit;
172 # END $OP eq ADD_VALIDATE
173 ################## DELETE_CONFIRM ##################################
174 # called by default form, used to confirm deletion of data in DB
175 } elsif ($op eq 'delete_confirm') {
176 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
177 $sth->execute($searchfield, $frameworkcode);
178 my $data = $sth->fetchrow_hashref;
179 $template->param(
180 liblibrarian => $data->{'liblibrarian'},
181 searchfield => $searchfield,
182 frameworkcode => $frameworkcode,
184 # END $OP eq DELETE_CONFIRM
185 ################## DELETE_CONFIRMED ##################################
186 # called by delete_confirm, used to effectively confirm deletion of data in DB
187 } elsif ($op eq 'delete_confirmed') {
188 unless (C4::Context->config('demo')) {
189 my $sth1 = $dbh->prepare("DELETE FROM marc_tag_structure WHERE tagfield=? AND frameworkcode=?");
190 my $sth2 = $dbh->prepare("DELETE FROM marc_subfield_structure WHERE tagfield=? AND frameworkcode=?");
191 $sth1->execute($searchfield, $frameworkcode);
192 $sth2->execute($searchfield, $frameworkcode);
193 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
194 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
195 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
196 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
198 $template->param(
199 searchfield => $searchfield,
200 frameworkcode => $frameworkcode,
202 # END $OP eq DELETE_CONFIRMED
203 ################## ITEMTYPE_CREATE ##################################
204 # called automatically if an unexisting frameworkis selected
205 } elsif ($op eq 'framework_create') {
206 $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");
207 $sth->execute;
208 my @existingframeworkloop;
209 while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
210 if ($tot>0) {
211 push @existingframeworkloop, {
212 value => $thisframeworkcode,
213 frameworktext => $frameworktext,
217 $template->param(existingframeworkloop => \@existingframeworkloop,
218 frameworkcode => $frameworkcode,
219 # FRtext => $frameworkinfo->{frameworktext},
221 ################## DEFAULT ##################################
222 } else { # DEFAULT
223 # here, $op can be unset or set to "framework_create_confirm".
224 if ($searchfield ne '') {
225 $template->param(searchfield => $searchfield);
227 my $cnt=0;
228 if ($dspchoice) {
229 #here, user only wants used tags/subfields displayed
230 $searchfield=~ s/\'/\\\'/g;
231 my @data=split(' ',$searchfield);
232 my $sth=$dbh->prepare("
233 SELECT marc_tag_structure.tagfield AS mts_tagfield,
234 marc_tag_structure.liblibrarian as mts_liblibrarian,
235 marc_tag_structure.libopac as mts_libopac,
236 marc_tag_structure.repeatable as mts_repeatable,
237 marc_tag_structure.mandatory as mts_mandatory,
238 marc_tag_structure.authorised_value as mts_authorized_value,
239 marc_subfield_structure.*
240 FROM marc_tag_structure
241 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");
242 #could be ordoned by tab
243 $sth->execute($data[0], $frameworkcode);
244 my @results = ();
245 while (my $data=$sth->fetchrow_hashref){
246 push(@results,$data);
247 $cnt++;
250 my @loop_data = ();
251 my $j=1;
252 my $i=$offset;
253 while ( $i < $cnt ) {
254 my %row_data; # get a fresh hash for the row data
255 $row_data{tagfield} = $results[$i]->{'mts_tagfield'};
256 $row_data{liblibrarian} = $results[$i]->{'mts_liblibrarian'};
257 $row_data{repeatable} = $results[$i]->{'mts_repeatable'};
258 $row_data{mandatory} = $results[$i]->{'mts_mandatory'};
259 $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
260 $row_data{subfield_link} = "marc_subfields_structure.pl?op=add_form&amp;tagfield=".$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
261 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=" .$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
262 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=" .$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
263 $j=$i;
264 my @internal_loop = ();
265 while ( ( $j < $cnt ) and ( $results[$i]->{'tagfield'} == $results[$j]->{'tagfield'} ) ) {
266 my %subfield_data;
267 $subfield_data{tagsubfield} = $results[$j]->{'tagsubfield'};
268 $subfield_data{liblibrarian} = $results[$j]->{'liblibrarian'};
269 $subfield_data{kohafield} = $results[$j]->{'kohafield'};
270 $subfield_data{repeatable} = $results[$j]->{'repeatable'};
271 $subfield_data{mandatory} = $results[$j]->{'mandatory'};
272 $subfield_data{tab} = $results[$j]->{'tab'};
273 $subfield_data{seealso} = $results[$j]->{'seealso'};
274 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
275 $subfield_data{authtypecode} = $results[$j]->{'authtypecode'};
276 $subfield_data{value_builder} = $results[$j]->{'value_builder'};
277 # warn "tagfield : ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
278 push @internal_loop,\%subfield_data;
279 $j++;
281 $row_data{'subfields'}=\@internal_loop;
282 push(@loop_data, \%row_data);
283 $i=$j;
285 $template->param(select_display => "True",
286 loop => \@loop_data);
287 } else {
288 #here, normal old style : display every tags
289 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
290 $cnt = $count;
291 my @loop_data = ();
292 for ( my $i = $offset ; $i < $count ; $i++ ) {
293 my %row_data; # get a fresh hash for the row data
294 $row_data{tagfield} = $results->[$i]{'tagfield'};
295 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
296 $row_data{repeatable} = $results->[$i]{'repeatable'};
297 $row_data{mandatory} = $results->[$i]{'mandatory'};
298 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
299 $row_data{subfield_link} = "marc_subfields_structure.pl?tagfield=" .$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
300 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=" .$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
301 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
302 push(@loop_data, \%row_data);
304 $template->param(loop => \@loop_data);
306 if ($offset>0) {
307 $template->param(isprevpage => $offset,
308 prevpage=> $offset-$pagesize,
309 searchfield => $searchfield,
310 script_name => $script_name,
311 frameworkcode => $frameworkcode,
314 if ($offset+$pagesize<$cnt) {
315 $template->param(nextpage =>$offset+$pagesize,
316 searchfield => $searchfield,
317 script_name => $script_name,
318 frameworkcode => $frameworkcode,
321 } #---- END $OP eq DEFAULT
323 output_html_with_http_headers $input, $cookie, $template->output;
326 # the sub used for searches
328 sub StringSearch {
329 my ($searchstring,$frameworkcode)=@_;
330 my $sth = C4::Context->dbh->prepare("
331 SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value
332 FROM marc_tag_structure
333 WHERE (tagfield >= ? and frameworkcode=?)
334 ORDER BY tagfield
336 $sth->execute($searchstring, $frameworkcode);
337 my $results = $sth->fetchall_arrayref({});
338 return (scalar(@$results), $results);
342 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
344 sub duplicate_framework {
345 my ($newframeworkcode,$oldframeworkcode) = @_;
346 my $dbh = C4::Context->dbh;
347 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where frameworkcode=?");
348 $sth->execute($oldframeworkcode);
349 my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode) values (?,?,?,?,?,?,?)");
350 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
351 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newframeworkcode);
354 $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=?");
355 $sth->execute($oldframeworkcode);
356 $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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
357 while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso,$hidden) = $sth->fetchrow) {
358 $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso, $hidden);
360 $cache->clear_from_cache("MarcStructure-0-$newframeworkcode");
361 $cache->clear_from_cache("MarcStructure-1-$newframeworkcode");
362 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
363 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");