adding 'gettext' as this is requred by the translation utility
[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 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.
12 #
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.
16 #
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
20
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;
28
29
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
41 my $offset=$input->param('offset') || 0;
42 my $op = $input->param('op') || '';
43 my $dspchoice = $input->param('select_display');
44 my $pagesize=20;
45
46 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
47
48 my $dbh = C4::Context->dbh;
49
50 # open template
51 my ($template, $loggedinuser, $cookie)
52     = get_template_and_user({template_name => "admin/marctagstructure.tmpl",
53                              query => $input,
54                              type => "intranet",
55                              authnotrequired => 0,
56                              flagsrequired => {parameters => 1},
57                              debug => 1,
58                              });
59
60 # get framework list
61 my $frameworks = getframeworks();
62 my @frameworkloop;
63 foreach my $thisframeworkcode (keys %$frameworks) {
64         my $selected = 1 if $thisframeworkcode eq $frameworkcode;
65         my %row =(value => $thisframeworkcode,
66                                 selected => $selected,
67                                 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
68                         );
69         push @frameworkloop, \%row;
70 }
71
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 if ($frameworkexist) {
77 } else {
78         # if frameworkcode does not exists, then OP must be changed to "create framework" if we are not on the way to create it
79         # (op = itemtyp_create_confirm)
80         if ($op eq "framework_create_confirm") {
81                 duplicate_framework($frameworkcode, $existingframeworkcode);
82                 $op=""; # unset $op to go back to framework list
83         } else {
84                 $op = "framework_create";
85         }
86 }
87 $template->param(frameworkloop => \@frameworkloop,
88                                 frameworkcode => $frameworkcode,
89                                 frameworktext => $frameworkinfo->{frameworktext});
90 if ($op) {
91 $template->param(script_name => $script_name,
92                                                 $op              => 1); # we show only the TMPL_VAR names $op
93 } else {
94 $template->param(script_name => $script_name,
95                                                 else              => 1); # we show only the TMPL_VAR names $op
96 }
97
98
99 ################## ADD_FORM ##################################
100 # called by default. Used to create form to add or  modify a record
101 if ($op eq 'add_form') {
102         #---- if primkey exists, it's a modify action, so read values to modify...
103         my $data;
104         if ($searchfield) {
105                 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
106                 $sth->execute($searchfield,$frameworkcode);
107                 $data=$sth->fetchrow_hashref;
108                 $sth->finish;
109         }
110         my $sth = $dbh->prepare("select distinct category from authorised_values");
111         $sth->execute;
112         my @authorised_values;
113         push @authorised_values,"";
114         while ((my $category) = $sth->fetchrow_array) {
115                 push @authorised_values, $category;
116         }
117         my $authorised_value  = CGI::scrolling_list(-name=>'authorised_value',
118                         -values=> \@authorised_values,
119                         -size=>1,
120                         -tabindex=>'',
121                         -id=>"authorised_value",
122                         -multiple=>0,
123                         -default => $data->{'authorised_value'},
124                         );
125
126   $template->param(searchfield => $searchfield) if ($searchfield);
127         if ($searchfield) {
128                 $template->param(action => "Modify tag");
129                 $template->param('heading-modify-tag-p' => 1);
130         } else {
131                 $template->param(action => "Add tag");
132                 $template->param('heading-add-tag-p' => 1);
133         }
134         $template->param('use-heading-flags-p' => 1);
135         $template->param(liblibrarian => $data->{'liblibrarian'},
136                         libopac => $data->{'libopac'},
137                         repeatable => CGI::checkbox(-name=>'repeatable',
138                                                 -checked=> $data->{'repeatable'}?'checked':'',
139                                                 -value=> 1,
140                                                 -tabindex=>'',
141                                                 -label => '',
142                                                 -id=> 'repeatable'),
143                         mandatory => CGI::checkbox(-name => 'mandatory',
144                                                 -checked => $data->{'mandatory'}?'checked':'',
145                                                 -value => 1,
146                                                 -tabindex=>'',
147                                                 -label => '',
148                                                 -id => 'mandatory'),
149                         authorised_value => $authorised_value,
150                         frameworkcode => $frameworkcode,
151                         );
152                                                                                                         # END $OP eq ADD_FORM
153 ################## ADD_VALIDATE ##################################
154 # called by add_form, used to insert/modify data in DB
155 } elsif ($op eq 'add_validate') {
156         my $tagfield       =$input->param('tagfield');
157         my $liblibrarian  = $input->param('liblibrarian');
158         my $libopac       =$input->param('libopac');
159         my $repeatable =$input->param('repeatable');
160         my $mandatory =$input->param('mandatory');
161         my $authorised_value =$input->param('authorised_value');
162     if ($input->param('modif')) {
163         $sth=$dbh->prepare("UPDATE marc_tag_structure SET liblibrarian=? ,libopac=? ,repeatable=? ,mandatory=? ,authorised_value=? WHERE frameworkcode=? AND tagfield=?");
164          unless (C4::Context->config('demo') eq 1) {
165             $sth->execute(  $liblibrarian,
166                             $libopac,
167                             $repeatable?1:0,
168                             $mandatory?1:0,
169                             $authorised_value,
170                             $frameworkcode,
171                             $tagfield
172                                 );
173         }
174         $sth->finish;
175         } else {
176         $sth=$dbh->prepare("INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)");
177         unless (C4::Context->config('demo') eq 1) {
178             $sth->execute($tagfield,
179                                 $liblibrarian,
180                                 $libopac,
181                                 $repeatable?1:0,
182                                 $mandatory?1:0,
183                                 $authorised_value,
184                                 $frameworkcode
185                                 );
186         }
187         $sth->finish;
188         }
189         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
190         exit;
191                                                                                                         # END $OP eq ADD_VALIDATE
192 ################## DELETE_CONFIRM ##################################
193 # called by default form, used to confirm deletion of data in DB
194 } elsif ($op eq 'delete_confirm') {
195         $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
196         $sth->execute($searchfield,$frameworkcode);
197         my $data=$sth->fetchrow_hashref;
198         $sth->finish;
199         $template->param(liblibrarian => $data->{'liblibrarian'},
200                                                         searchfield => $searchfield,
201                                                         frameworkcode => $frameworkcode,
202                                                         );
203                                                                                                         # END $OP eq DELETE_CONFIRM
204 ################## DELETE_CONFIRMED ##################################
205 # called by delete_confirm, used to effectively confirm deletion of data in DB
206 } elsif ($op eq 'delete_confirmed') {
207         unless (C4::Context->config('demo') eq 1) {
208                 $dbh->do("delete from marc_tag_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
209                 $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield' and frameworkcode='$frameworkcode'");
210         }
211                                                                                                         # END $OP eq DELETE_CONFIRMED
212 ################## ITEMTYPE_CREATE ##################################
213 # called automatically if an unexisting  frameworkis selected
214 } elsif ($op eq 'framework_create') {
215         $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");
216         $sth->execute;
217         my @existingframeworkloop;
218         while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
219                 if ($tot>0) {
220                         my %line = ( value => $thisframeworkcode,
221                                                 frameworktext => $frameworktext,
222                                         );
223                         push @existingframeworkloop,\%line;
224                 }
225         }
226         $template->param(existingframeworkloop => \@existingframeworkloop,
227                                         frameworkcode => $frameworkcode,
228 #                                       FRtext => $frameworkinfo->{frameworktext},
229                                         );
230 ################## DEFAULT ##################################
231 } else { # DEFAULT
232         # here, $op can be unset or set to "framework_create_confirm".
233         if  ($searchfield ne '') {
234                  $template->param(searchfield => $searchfield);
235         }
236         my $cnt=0;
237         if ($dspchoice) {
238                 #here, user only wants used tags/subfields displayed
239                 $searchfield=~ s/\'/\\\'/g;
240                 my @data=split(' ',$searchfield);
241                 my $sth=$dbh->prepare("
242                       SELECT marc_tag_structure.tagfield AS mts_tagfield,
243                               marc_tag_structure.liblibrarian as mts_liblibrarian,
244                               marc_tag_structure.libopac as mts_libopac,
245                               marc_tag_structure.repeatable as mts_repeatable,
246                               marc_tag_structure.mandatory as mts_mandatory,
247                               marc_tag_structure.authorised_value as mts_authorized_value,
248                               marc_subfield_structure.*
249                 FROM marc_tag_structure 
250                 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");
251                 #could be ordoned by tab
252                 $sth->execute($data[0], $frameworkcode);
253                 my @results = ();
254                 while (my $data=$sth->fetchrow_hashref){
255                         push(@results,$data);
256                         $cnt++;
257                 }
258                 $sth->finish;
259                 
260                 my $toggle=0;
261                 my @loop_data = ();
262                 my $j=1;
263                 my $i=$offset;
264                 while ($i < ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt)) {
265                         if ($toggle eq 0){
266                                 $toggle=1;
267                         } else {
268                                 $toggle=0;
269                         }
270                         my %row_data;  # get a fresh hash for the row data
271                         $row_data{tagfield} = $results[$i]->{'mts_tagfield'};
272                         $row_data{liblibrarian} = $results[$i]->{'mts_liblibrarian'};
273                         $row_data{repeatable} = $results[$i]->{'mts_repeatable'};
274                         $row_data{mandatory} = $results[$i]->{'mts_mandatory'};
275                         $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
276                         $row_data{subfield_link} ="marc_subfields_structure.pl?op=add_form&amp;tagfield=".$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
277                         $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
278                         $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
279                         $row_data{toggle} = $toggle;
280                         $j=$i;
281                         my @internal_loop = ();
282                         while (($results[$i]->{'tagfield'}==$results[$j]->{'tagfield'}) and ($j< ($offset+$pagesize<$cnt?$offset+$pagesize:$cnt))) {
283                                 my %subfield_data;
284                                 $subfield_data{tagsubfield} = $results[$j]->{'tagsubfield'};
285                                 $subfield_data{liblibrarian} = $results[$j]->{'liblibrarian'};
286                                 $subfield_data{kohafield} = $results[$j]->{'kohafield'};
287                                 $subfield_data{repeatable} = $results[$j]->{'repeatable'};
288                                 $subfield_data{mandatory} = $results[$j]->{'mandatory'};
289                                 $subfield_data{tab} = $results[$j]->{'tab'};
290                                 $subfield_data{seealso} = $results[$j]->{'seealso'};
291                                 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
292                                 $subfield_data{authtypecode}= $results[$j]->{'authtypecode'};
293                                 $subfield_data{value_builder}= $results[$j]->{'value_builder'};
294                                 $subfield_data{toggle}  = $toggle;
295 #                               warn "tagfield :  ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
296                                 push @internal_loop,\%subfield_data;
297                                 $j++;
298                         }
299                         $row_data{'subfields'}=\@internal_loop;
300                         push(@loop_data, \%row_data);
301 #                       undef @internal_loop;
302                         $i=$j;
303                 }
304                 $template->param(select_display => "True",
305                                                 loop => \@loop_data);
306                 #  $sth->execute;
307                 $sth->finish;
308         } else {
309                 #here, normal old style : display every tags
310                 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
311                 $cnt = $count;
312                 my $toggle=0;
313                 my @loop_data = ();
314                 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
315                         if ($toggle eq 0){
316                                 $toggle=1;
317                         } else {
318                                 $toggle=0;
319                         }
320                         my %row_data;  # get a fresh hash for the row data
321                         $row_data{tagfield} = $results->[$i]{'tagfield'};
322                         $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
323                         $row_data{repeatable} = $results->[$i]{'repeatable'};
324                         $row_data{mandatory} = $results->[$i]{'mandatory'};
325                         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
326                         $row_data{subfield_link} ="marc_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
327                         $row_data{edit} = "$script_name?op=add_form&amp;searchfield=".$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
328                         $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
329                         $row_data{toggle} = $toggle;
330                         push(@loop_data, \%row_data);
331                 }
332                 $template->param(loop => \@loop_data);
333         }
334         if ($offset>0) {
335                 my $prevpage = $offset-$pagesize;
336                 $template->param(isprevpage => $offset,
337                                                 prevpage=> $prevpage,
338                                                 searchfield => $searchfield,
339                                                 script_name => $script_name,
340                                                 frameworkcode => $frameworkcode,
341                 );
342         }
343         if ($offset+$pagesize<$cnt) {
344                 my $nextpage =$offset+$pagesize;
345                 $template->param(nextpage =>$nextpage,
346                                                 searchfield => $searchfield,
347                                                 script_name => $script_name,
348                                                 frameworkcode => $frameworkcode,
349                 );
350         }
351 } #---- END $OP eq DEFAULT
352
353 $template->param(loggeninuser => $loggedinuser,
354                 );
355 output_html_with_http_headers $input, $cookie, $template->output;
356
357
358 #
359 # the sub used for searches
360 #
361 sub StringSearch  {
362         my ($searchstring,$frameworkcode)=@_;
363         my $dbh = C4::Context->dbh;
364         $searchstring=~ s/\'/\\\'/g;
365         my @data=split(' ',$searchstring);
366         my $count=@data;
367         my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield >= ? and frameworkcode=?) order by tagfield");
368         $sth->execute($data[0], $frameworkcode);
369         my @results;
370         while (my $data=$sth->fetchrow_hashref){
371         push(@results,$data);
372         }
373         #  $sth->execute;
374         $sth->finish;
375         return (scalar(@results),\@results);
376 }
377
378 #
379 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
380 #
381 sub duplicate_framework {
382         my ($newframeworkcode,$oldframeworkcode) = @_;
383         my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where frameworkcode=?");
384         $sth->execute($oldframeworkcode);
385         my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, frameworkcode) values (?,?,?,?,?,?,?)");
386         while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
387                 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newframeworkcode);
388         }
389
390         $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=?");
391         $sth->execute($oldframeworkcode);
392         $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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
393         while ( my ($frameworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso,$hidden) = $sth->fetchrow) {
394             $sth_insert->execute($newframeworkcode, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso, $hidden);
395         }
396 }
397