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