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>.
23 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
29 use Koha::AuthorisedValues;
30 use Koha::BiblioFrameworks;
34 my $frameworkcode = $input->param('frameworkcode') || ''; # set to select framework
35 my $existingframeworkcode = $input->param('existingframeworkcode') || '';
36 my $searchfield = $input->param('searchfield') || 0;
37 $searchfield=~ s/\,//g;
39 my $offset = $input->param('offset') || 0;
40 my $op = $input->param('op') || '';
41 my $dspchoice = $input->cookie("marctagstructure_selectdisplay") // $input->param('select_display');
44 my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl";
46 my $dbh = C4::Context->dbh;
47 my $cache = Koha::Caches->get_instance();
50 my ($template, $loggedinuser, $cookie)
51 = get_template_and_user({template_name => "admin/marctagstructure.tt",
54 flagsrequired => { parameters => 'manage_marc_frameworks' },
57 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
59 # check that framework is defined in marc_tag_structure
60 my $sth=$dbh->prepare("select count(*) from marc_tag_structure where frameworkcode=?");
61 $sth->execute($frameworkcode);
62 my ($frameworkexist) = $sth->fetchrow;
63 unless ($frameworkexist) {
64 # if frameworkcode does not exists, then OP must be changed to "create framework" if we are not on the way to create it
65 # (op = itemtyp_create_confirm)
66 if ($op eq "cud-framework_create_confirm") {
67 duplicate_framework($frameworkcode, $existingframeworkcode);
68 $op = ""; # unset $op to go back to framework list
70 $op = "framework_create";
74 my $framework = $frameworks->search({ frameworkcode => $frameworkcode })->next;
76 frameworks => $frameworks,
77 framework => $framework,
78 script_name => $script_name,
79 ( $op || 'else' ) => 1,
83 ################## ADD_FORM ##################################
84 # called by default. Used to create form to add or modify a record
85 if ($op eq 'add_form') {
86 #---- if primkey exists, it's a modify action, so read values to modify...
89 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,important,authorised_value,ind1_defaultvalue,ind2_defaultvalue from marc_tag_structure where tagfield=? and frameworkcode=?");
90 $sth->execute($searchfield,$frameworkcode);
91 $data=$sth->fetchrow_hashref;
95 $template->param(searchfield => $searchfield);
96 $template->param('heading_modify_tag_p' => 1);
98 $template->param('heading_add_tag_p' => 1);
100 $template->param('use_heading_flags_p' => 1);
101 $template->param(liblibrarian => $data->{'liblibrarian'},
102 libopac => $data->{'libopac'},
103 repeatable => $data->{'repeatable'},
104 mandatory => $data->{'mandatory'},
105 important => $data->{'important'},
106 authorised_value => $data->{authorised_value},
107 ind1_defaultvalue => $data->{'ind1_defaultvalue'},
108 ind2_defaultvalue => $data->{'ind2_defaultvalue'}
109 ); # FIXME: move checkboxes to presentation layer
110 # END $OP eq ADD_FORM
111 ################## ADD_VALIDATE ##################################
112 # called by add_form, used to insert/modify data in DB
113 } elsif ($op eq 'cud-add_validate') {
114 my $tagfield = $input->param('tagfield');
115 my $liblibrarian = $input->param('liblibrarian');
116 my $libopac = $input->param('libopac');
117 my $repeatable = $input->param('repeatable') ? 1 : 0;
118 my $mandatory = $input->param('mandatory') ? 1 : 0;
119 my $important = $input->param('important') ? 1 : 0;
120 my $authorised_value = $input->param('authorised_value');
121 my $ind1_defaultvalue = $input->param('ind1_defaultvalue');
122 my $ind2_defaultvalue = $input->param('ind2_defaultvalue');
123 if ($input->param('modif')) {
124 $sth = $dbh->prepare(
125 "UPDATE marc_tag_structure SET liblibrarian=? ,libopac=? ,repeatable=? ,mandatory=? ,important=? ,authorised_value=?, ind1_defaultvalue=?, ind2_defaultvalue=? WHERE frameworkcode=? AND tagfield=?"
127 $sth->execute( $liblibrarian,
139 $sth = $dbh->prepare(
140 "INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,important,authorised_value,ind1_defaultvalue,ind2_defaultvalue,frameworkcode) values (?,?,?,?,?,?,?,?,?,?)"
142 $sth->execute($tagfield,
154 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
155 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
156 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
157 $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
158 print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
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;
168 liblibrarian => $data->{'liblibrarian'},
169 searchfield => $searchfield
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 'cud-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("MarcSubfieldStructure-$frameworkcode");
182 $cache->clear_from_cache("MarcCodedFields-$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 my $frameworks = Koha::BiblioFrameworks->search(
190 'marc_tag_structure.frameworkcode' => { '!=' => undef }
193 join => 'marc_tag_structure',
197 $template->param( existing_frameworks => $frameworks );
199 ################## DEFAULT ##################################
201 # here, $op can be unset or set to "cud-framework_create_confirm".
202 if ($searchfield ne '') {
203 $template->param(searchfield => $searchfield);
207 #here, user only wants used tags/subfields displayed
208 $searchfield=~ s/\'/\\\'/g;
209 my @data=split(' ',$searchfield);
210 my $sth=$dbh->prepare("
211 SELECT marc_tag_structure.tagfield AS mts_tagfield,
212 marc_tag_structure.liblibrarian as mts_liblibrarian,
213 marc_tag_structure.libopac as mts_libopac,
214 marc_tag_structure.repeatable as mts_repeatable,
215 marc_tag_structure.mandatory as mts_mandatory,
216 marc_tag_structure.important as mts_important,
217 marc_tag_structure.authorised_value as mts_authorized_value,
218 marc_tag_structure.ind1_defaultvalue as mts_ind1_defaultvalue,
219 marc_tag_structure.ind1_defaultvalue as mts_ind2_defaultvalue,
220 marc_subfield_structure.*
221 FROM marc_tag_structure
222 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");
223 #could be ordoned by tab
224 $sth->execute($data[0], $frameworkcode);
226 while (my $data=$sth->fetchrow_hashref){
227 push(@results,$data);
234 while ( $i < $cnt ) {
235 my %row_data; # get a fresh hash for the row data
236 $row_data{tagfield} = $results[$i]->{'mts_tagfield'};
237 $row_data{liblibrarian} = $results[$i]->{'mts_liblibrarian'};
238 $row_data{repeatable} = $results[$i]->{'mts_repeatable'};
239 $row_data{mandatory} = $results[$i]->{'mts_mandatory'};
240 $row_data{important} = $results[$i]->{'mts_important'};
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'};
245 my @internal_loop = ();
246 while ( ( $j < $cnt ) and ( $results[$i]->{'tagfield'} == $results[$j]->{'tagfield'} ) ) {
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{important} = $results[$j]->{'important'};
254 $subfield_data{tab} = $results[$j]->{'tab'};
255 $subfield_data{seealso} = $results[$j]->{'seealso'};
256 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
257 $subfield_data{authtypecode} = $results[$j]->{'authtypecode'};
258 $subfield_data{value_builder} = $results[$j]->{'value_builder'};
259 # warn "tagfield : ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
260 push @internal_loop,\%subfield_data;
263 $row_data{'subfields'}=\@internal_loop;
264 push(@loop_data, \%row_data);
267 $template->param(select_display => "True",
268 loop => \@loop_data);
270 # Hidden feature: If search was field$subfield, redirect to the subfield edit form
271 my ( $tagfield, $tagsubfield ) = split /\$/, $searchfield;
272 if ( $tagsubfield ) {
273 print $input->redirect('/cgi-bin/koha/admin/marc_subfields_structure.pl?op=add_form&tagfield='.$tagfield.'&frameworkcode='.$frameworkcode.'#sub'.$tagsubfield.'field');
276 #here, normal old style : display every tags
277 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
280 for ( my $i = $offset ; $i < $count ; $i++ ) {
281 my %row_data; # get a fresh hash for the row data
282 $row_data{tagfield} = $results->[$i]{'tagfield'};
283 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
284 $row_data{repeatable} = $results->[$i]{'repeatable'};
285 $row_data{mandatory} = $results->[$i]{'mandatory'};
286 $row_data{important} = $results->[$i]{'important'};
287 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
288 $row_data{ind1_defaultvalue} = $results->[$i]{'ind1_defaultvalue'};
289 $row_data{ind2_defaultvalue} = $results->[$i]{'ind2_defaultvalue'};
290 push(@loop_data, \%row_data);
292 $template->param(loop => \@loop_data);
295 $template->param(isprevpage => $offset,
296 prevpage=> $offset-$pagesize,
297 searchfield => $searchfield,
298 script_name => $script_name
301 if ($offset+$pagesize<$cnt) {
302 $template->param(nextpage =>$offset+$pagesize,
303 searchfield => $searchfield,
304 script_name => $script_name
307 } #---- END $OP eq DEFAULT
309 output_html_with_http_headers $input, $cookie, $template->output;
312 # the sub used for searches
315 my ($searchstring,$frameworkcode)=@_;
316 my $sth = C4::Context->dbh->prepare("
317 SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,important,authorised_value,ind1_defaultvalue,ind2_defaultvalue
318 FROM marc_tag_structure
319 WHERE (tagfield >= ? and frameworkcode=?)
322 $sth->execute($searchstring, $frameworkcode);
323 my $results = $sth->fetchall_arrayref({});
324 return (scalar(@$results), $results);
328 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
330 sub duplicate_framework {
331 my ($newframeworkcode,$oldframeworkcode) = @_;
332 my $dbh = C4::Context->dbh;
333 $dbh->do(q|INSERT INTO marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, important, authorised_value, ind1_defaultvalue, ind2_defaultvalue, frameworkcode)
334 SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,important,authorised_value, ind1_defaultvalue, ind2_defaultvalue, ? from marc_tag_structure where frameworkcode=?|, undef, $newframeworkcode, $oldframeworkcode );
336 $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)
337 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=?
338 |, undef, $newframeworkcode, $oldframeworkcode );