Fix itemtype image problem in moremember.pl
[koha.git] / admin / marctagstructure.pl
blob2759a03af9ebd10ccd29e785d759b95c128f4391
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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
21 use strict;
22 use CGI;
23 use C4::Auth;
24 use C4::Koha;
25 use C4::Context;
26 use C4::Output;
27 use C4::Context;
30 # retrieve parameters
31 my $input = new CGI;
32 my $frameworkcode = $input->param('frameworkcode'); # set to select framework
33 $frameworkcode="" unless $frameworkcode;
34 my $existingframeworkcode = $input->param('existingframeworkcode'); # set when we have to create a new framework (in frameworkcode) by copying an old one (in existingframeworkcode)
35 $existingframeworkcode = "" unless $existingframeworkcode;
36 my $frameworkinfo = getframeworkinfo($frameworkcode);
37 my $searchfield=$input->param('searchfield');
38 $searchfield=0 unless $searchfield;
39 $searchfield=~ s/\,//g;
40 my $last_searchfield=$input->param('searchfield');
42 my $offset=$input->param('offset') || 0;
43 my $op = $input->param('op') || '';
44 my $dspchoice = $input->param('select_display');
45 my $pagesize=20;
47 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
49 my $dbh = C4::Context->dbh;
51 # open template
52 my ($template, $loggedinuser, $cookie)
53 = get_template_and_user({template_name => "admin/marctagstructure.tmpl",
54 query => $input,
55 type => "intranet",
56 authnotrequired => 0,
57 flagsrequired => {parameters => 1},
58 debug => 1,
59 });
61 # get framework list
62 my $frameworks = getframeworks();
63 my @frameworkloop;
64 foreach my $thisframeworkcode (keys %$frameworks) {
65 my $selected = 1 if $thisframeworkcode eq $frameworkcode;
66 my %row =(value => $thisframeworkcode,
67 selected => $selected,
68 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
70 push @frameworkloop, \%row;
73 # check that framework is defined in marc_tag_structure
74 my $sth=$dbh->prepare("select count(*) from marc_tag_structure where frameworkcode=?");
75 $sth->execute($frameworkcode);
76 my ($frameworkexist) = $sth->fetchrow;
77 if ($frameworkexist) {
78 } else {
79 # if frameworkcode does not exists, then OP must be changed to "create framework" if we are not on the way to create it
80 # (op = itemtyp_create_confirm)
81 if ($op eq "framework_create_confirm") {
82 duplicate_framework($frameworkcode, $existingframeworkcode);
83 $op=""; # unset $op to go back to framework list
84 } else {
85 $op = "framework_create";
88 $template->param(frameworkloop => \@frameworkloop,
89 frameworkcode => $frameworkcode,
90 frameworktext => $frameworkinfo->{frameworktext});
91 if ($op) {
92 $template->param(script_name => $script_name,
93 $op => 1); # we show only the TMPL_VAR names $op
94 } else {
95 $template->param(script_name => $script_name,
96 else => 1); # we show only the TMPL_VAR names $op
100 ################## ADD_FORM ##################################
101 # called by default. Used to create form to add or modify a record
102 if ($op eq 'add_form') {
103 #---- if primkey exists, it's a modify action, so read values to modify...
104 my $data;
105 if ($searchfield) {
106 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
107 $sth->execute($searchfield,$frameworkcode);
108 $data=$sth->fetchrow_hashref;
109 $sth->finish;
111 my $sth = $dbh->prepare("select distinct category from authorised_values");
112 $sth->execute;
113 my @authorised_values;
114 push @authorised_values,"";
115 while ((my $category) = $sth->fetchrow_array) {
116 push @authorised_values, $category;
118 my $authorised_value = CGI::scrolling_list(-name=>'authorised_value',
119 -values=> \@authorised_values,
120 -size=>1,
121 -tabindex=>'',
122 -id=>"authorised_value",
123 -multiple=>0,
124 -default => $data->{'authorised_value'},
127 $template->param(searchfield => $searchfield) if ($searchfield);
128 if ($searchfield) {
129 $template->param(action => "Modify tag");
130 $template->param('heading-modify-tag-p' => 1);
131 } else {
132 $template->param(action => "Add tag");
133 $template->param('heading-add-tag-p' => 1);
135 $template->param('use-heading-flags-p' => 1);
136 $template->param(liblibrarian => $data->{'liblibrarian'},
137 libopac => $data->{'libopac'},
138 repeatable => CGI::checkbox(-name=>'repeatable',
139 -checked=> $data->{'repeatable'}?'checked':'',
140 -value=> 1,
141 -tabindex=>'',
142 -label => '',
143 -id=> 'repeatable'),
144 mandatory => CGI::checkbox(-name => 'mandatory',
145 -checked => $data->{'mandatory'}?'checked':'',
146 -value => 1,
147 -tabindex=>'',
148 -label => '',
149 -id => 'mandatory'),
150 authorised_value => $authorised_value,
151 frameworkcode => $frameworkcode,
153 # END $OP eq ADD_FORM
154 ################## ADD_VALIDATE ##################################
155 # called by add_form, used to insert/modify data in DB
156 } elsif ($op eq 'add_validate') {
157 my $tagfield =$input->param('tagfield');
158 my $liblibrarian = $input->param('liblibrarian');
159 my $libopac =$input->param('libopac');
160 my $repeatable =$input->param('repeatable');
161 my $mandatory =$input->param('mandatory');
162 my $authorised_value =$input->param('authorised_value');
163 if ($input->param('modif')) {
164 $sth=$dbh->prepare("UPDATE marc_tag_structure SET liblibrarian=? ,libopac=? ,repeatable=? ,mandatory=? ,authorised_value=? WHERE frameworkcode=? AND tagfield=?");
165 unless (C4::Context->config('demo') eq 1) {
166 $sth->execute( $liblibrarian,
167 $libopac,
168 $repeatable?1:0,
169 $mandatory?1:0,
170 $authorised_value,
171 $frameworkcode,
172 $tagfield
175 $sth->finish;
176 } else {
177 $sth=$dbh->prepare("INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)");
178 unless (C4::Context->config('demo') eq 1) {
179 $sth->execute($tagfield,
180 $liblibrarian,
181 $libopac,
182 $repeatable?1:0,
183 $mandatory?1:0,
184 $authorised_value,
185 $frameworkcode
188 $sth->finish;
190 print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
191 exit;
192 # END $OP eq ADD_VALIDATE
193 ################## DELETE_CONFIRM ##################################
194 # called by default form, used to confirm deletion of data in DB
195 } elsif ($op eq 'delete_confirm') {
196 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
197 $sth->execute($searchfield,$frameworkcode);
198 my $data=$sth->fetchrow_hashref;
199 $sth->finish;
200 $template->param(liblibrarian => $data->{'liblibrarian'},
201 searchfield => $searchfield,
202 frameworkcode => $frameworkcode,
204 # END $OP eq DELETE_CONFIRM
205 ################## DELETE_CONFIRMED ##################################
206 # called by delete_confirm, used to effectively confirm deletion of data in DB
207 } elsif ($op eq 'delete_confirmed') {
208 unless (C4::Context->config('demo') eq 1) {
209 $dbh->do("delete from marc_tag_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
210 $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
212 $template->param(searchfield => $searchfield,
213 frameworkcode => $frameworkcode,
215 # END $OP eq DELETE_CONFIRMED
216 ################## ITEMTYPE_CREATE ##################################
217 # called automatically if an unexisting frameworkis selected
218 } elsif ($op eq 'framework_create') {
219 $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");
220 $sth->execute;
221 my @existingframeworkloop;
222 while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
223 if ($tot>0) {
224 my %line = ( value => $thisframeworkcode,
225 frameworktext => $frameworktext,
227 push @existingframeworkloop,\%line;
230 $template->param(existingframeworkloop => \@existingframeworkloop,
231 frameworkcode => $frameworkcode,
232 # FRtext => $frameworkinfo->{frameworktext},
234 ################## DEFAULT ##################################
235 } else { # DEFAULT
236 # here, $op can be unset or set to "framework_create_confirm".
237 if ($searchfield ne '') {
238 $template->param(searchfield => $searchfield);
240 my $cnt=0;
241 if ($dspchoice) {
242 #here, user only wants used tags/subfields displayed
243 $searchfield=~ s/\'/\\\'/g;
244 my @data=split(' ',$searchfield);
245 my $sth=$dbh->prepare("
246 SELECT marc_tag_structure.tagfield AS mts_tagfield,
247 marc_tag_structure.liblibrarian as mts_liblibrarian,
248 marc_tag_structure.libopac as mts_libopac,
249 marc_tag_structure.repeatable as mts_repeatable,
250 marc_tag_structure.mandatory as mts_mandatory,
251 marc_tag_structure.authorised_value as mts_authorized_value,
252 marc_subfield_structure.*
253 FROM marc_tag_structure
254 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");
255 #could be ordoned by tab
256 $sth->execute($data[0], $frameworkcode);
257 my @results = ();
258 while (my $data=$sth->fetchrow_hashref){
259 push(@results,$data);
260 $cnt++;
262 $sth->finish;
264 my $toggle=0;
265 my @loop_data = ();
266 my $j=1;
267 my $i=$offset;
268 while ($i < ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt)) {
269 if ($toggle eq 0){
270 $toggle=1;
271 } else {
272 $toggle=0;
274 my %row_data; # get a fresh hash for the row data
275 $row_data{tagfield} = $results[$i]->{'mts_tagfield'};
276 $row_data{liblibrarian} = $results[$i]->{'mts_liblibrarian'};
277 $row_data{repeatable} = $results[$i]->{'mts_repeatable'};
278 $row_data{mandatory} = $results[$i]->{'mts_mandatory'};
279 $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
280 $row_data{subfield_link} ="marc_subfields_structure.pl?op=add_form&amp;tagfield=".$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
281 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
282 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
283 $row_data{toggle} = $toggle;
284 $j=$i;
285 my @internal_loop = ();
286 while (($results[$i]->{'tagfield'}==$results[$j]->{'tagfield'}) and ($j< ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt))) {
287 my %subfield_data;
288 $subfield_data{tagsubfield} = $results[$j]->{'tagsubfield'};
289 $subfield_data{liblibrarian} = $results[$j]->{'liblibrarian'};
290 $subfield_data{kohafield} = $results[$j]->{'kohafield'};
291 $subfield_data{repeatable} = $results[$j]->{'repeatable'};
292 $subfield_data{mandatory} = $results[$j]->{'mandatory'};
293 $subfield_data{tab} = $results[$j]->{'tab'};
294 $subfield_data{seealso} = $results[$j]->{'seealso'};
295 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
296 $subfield_data{authtypecode}= $results[$j]->{'authtypecode'};
297 $subfield_data{value_builder}= $results[$j]->{'value_builder'};
298 $subfield_data{toggle} = $toggle;
299 # warn "tagfield : ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
300 push @internal_loop,\%subfield_data;
301 $j++;
303 $row_data{'subfields'}=\@internal_loop;
304 push(@loop_data, \%row_data);
305 # undef @internal_loop;
306 $i=$j;
308 $template->param(select_display => "True",
309 loop => \@loop_data);
310 # $sth->execute;
311 $sth->finish;
312 } else {
313 #here, normal old style : display every tags
314 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
315 $cnt = $count;
316 my $toggle=0;
317 my @loop_data = ();
318 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
319 if ($toggle eq 0){
320 $toggle=1;
321 } else {
322 $toggle=0;
324 my %row_data; # get a fresh hash for the row data
325 $row_data{tagfield} = $results->[$i]{'tagfield'};
326 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
327 $row_data{repeatable} = $results->[$i]{'repeatable'};
328 $row_data{mandatory} = $results->[$i]{'mandatory'};
329 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
330 $row_data{subfield_link} ="marc_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
331 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
332 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
333 $row_data{toggle} = $toggle;
334 push(@loop_data, \%row_data);
336 $template->param(loop => \@loop_data);
338 if ($offset>0) {
339 my $prevpage = $offset-$pagesize;
340 $template->param(isprevpage => $offset,
341 prevpage=> $prevpage,
342 searchfield => $searchfield,
343 script_name => $script_name,
344 frameworkcode => $frameworkcode,
347 if ($offset+$pagesize<$cnt) {
348 my $nextpage =$offset+$pagesize;
349 $template->param(nextpage =>$nextpage,
350 searchfield => $searchfield,
351 script_name => $script_name,
352 frameworkcode => $frameworkcode,
355 } #---- END $OP eq DEFAULT
357 $template->param(loggeninuser => $loggedinuser,
359 output_html_with_http_headers $input, $cookie, $template->output;
363 # the sub used for searches
365 sub StringSearch {
366 my ($searchstring,$frameworkcode)=@_;
367 my $dbh = C4::Context->dbh;
368 $searchstring=~ s/\'/\\\'/g;
369 my @data=split(' ',$searchstring);
370 my $count=@data;
371 my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield >= ? and frameworkcode=?) order by tagfield");
372 $sth->execute($data[0], $frameworkcode);
373 my @results;
374 while (my $data=$sth->fetchrow_hashref){
375 push(@results,$data);
377 # $sth->execute;
378 $sth->finish;
379 return (scalar(@results),\@results);
383 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
385 sub duplicate_framework {
386 my ($newframeworkcode,$oldframeworkcode) = @_;
387 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where frameworkcode=?");
388 $sth->execute($oldframeworkcode);
389 my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode) values (?,?,?,?,?,?,?)");
390 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
391 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newframeworkcode);
394 $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=?");
395 $sth->execute($oldframeworkcode);
396 $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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
397 while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso,$hidden) = $sth->fetchrow) {
398 $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso, $hidden);