Bug 16782: Use uri filter instead of html
[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
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.
12 #
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.
17 #
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>.
20
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;
29
30 use Koha::Caches;
31 use Koha::AuthorisedValues;
32 use Koha::BiblioFrameworks;
33
34 # retrieve parameters
35 my $input = new CGI;
36 my $frameworkcode         = $input->param('frameworkcode')         || ''; # set to select framework
37 my $existingframeworkcode = $input->param('existingframeworkcode') || '';
38 my $searchfield           = $input->param('searchfield') || 0;
39 $searchfield=~ s/\,//g;
40
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;
45
46 my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl";
47
48 my $dbh = C4::Context->dbh;
49 my $cache = Koha::Caches->get_instance();
50
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                              });
60
61 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
62
63 # check that framework is defined in marc_tag_structure
64 my $sth=$dbh->prepare("select count(*) from marc_tag_structure where frameworkcode=?");
65 $sth->execute($frameworkcode);
66 my ($frameworkexist) = $sth->fetchrow;
67 unless ($frameworkexist) {
68         # if frameworkcode does not exists, then OP must be changed to "create framework" if we are not on the way to create it
69         # (op = itemtyp_create_confirm)
70         if ($op eq "framework_create_confirm") {
71                 duplicate_framework($frameworkcode, $existingframeworkcode);
72                 $op = ""; # unset $op to go back to framework list
73         } else {
74                 $op = "framework_create";
75         }
76 }
77
78 my $framework = $frameworks->search({ frameworkcode => $frameworkcode })->next;
79 $template->param(
80     frameworks    => $frameworks,
81     framework     => $framework,
82     script_name   => $script_name,
83     ( $op || 'else' ) => 1,
84 );
85
86
87 ################## ADD_FORM ##################################
88 # called by default. Used to create form to add or  modify a record
89 if ($op eq 'add_form') {
90         #---- if primkey exists, it's a modify action, so read values to modify...
91         my $data;
92         if ($searchfield) {
93                 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
94                 $sth->execute($searchfield,$frameworkcode);
95                 $data=$sth->fetchrow_hashref;
96         }
97
98         if ($searchfield) {
99         $template->param(searchfield => $searchfield);
100                 $template->param(action => "Modify tag");
101                 $template->param('heading_modify_tag_p' => 1);
102         } else {
103                 $template->param(action => "Add tag");
104                 $template->param('heading_add_tag_p' => 1);
105         }
106         $template->param('use_heading_flags_p' => 1);
107         $template->param(liblibrarian => $data->{'liblibrarian'},
108                         libopac => $data->{'libopac'},
109             repeatable => $data->{'repeatable'},
110             mandatory => $data->{'mandatory'},
111             authorised_value => $data->{authorised_value},
112                         frameworkcode => $frameworkcode,
113     );  # FIXME: move checkboxes to presentation layer
114                                                                                                         # END $OP eq ADD_FORM
115 ################## ADD_VALIDATE ##################################
116 # called by add_form, used to insert/modify data in DB
117 } elsif ($op eq 'add_validate') {
118         my $tagfield         = $input->param('tagfield');
119         my $liblibrarian     = $input->param('liblibrarian');
120         my $libopac          = $input->param('libopac');
121         my $repeatable       = $input->param('repeatable') ? 1 : 0;
122         my $mandatory        = $input->param('mandatory')  ? 1 : 0;
123         my $authorised_value = $input->param('authorised_value');
124     if ($input->param('modif')) {
125         $sth = $dbh->prepare(
126         "UPDATE marc_tag_structure SET liblibrarian=? ,libopac=? ,repeatable=? ,mandatory=? ,authorised_value=? WHERE frameworkcode=? AND tagfield=?"
127         );
128         $sth->execute(  $liblibrarian,
129                         $libopac,
130                         $repeatable,
131                         $mandatory,
132                         $authorised_value,
133                         $frameworkcode,
134                         $tagfield
135         );
136     } else {
137         $sth = $dbh->prepare(
138         "INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,frameworkcode) values (?,?,?,?,?,?,?)"
139         );
140         $sth->execute($tagfield,
141                       $liblibrarian,
142                       $libopac,
143                       $repeatable,
144                       $mandatory,
145                       $authorised_value,
146                       $frameworkcode
147         );
148     }
149     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
150     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
151     $cache->clear_from_cache("default_value_for_mod_marc-");
152     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
153     print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
154     exit;
155                                                                                                         # END $OP eq ADD_VALIDATE
156 ################## DELETE_CONFIRM ##################################
157 # called by default form, used to confirm deletion of data in DB
158 } elsif ($op eq 'delete_confirm') {
159         $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and frameworkcode=?");
160     $sth->execute($searchfield, $frameworkcode);
161     my $data = $sth->fetchrow_hashref;
162         $template->param(
163          liblibrarian => $data->{'liblibrarian'},
164           searchfield => $searchfield,
165         frameworkcode => $frameworkcode,
166     );
167                                                                                                         # END $OP eq DELETE_CONFIRM
168 ################## DELETE_CONFIRMED ##################################
169 # called by delete_confirm, used to effectively confirm deletion of data in DB
170 } elsif ($op eq 'delete_confirmed') {
171     my $sth1 = $dbh->prepare("DELETE FROM marc_tag_structure      WHERE tagfield=? AND frameworkcode=?");
172     my $sth2 = $dbh->prepare("DELETE FROM marc_subfield_structure WHERE tagfield=? AND frameworkcode=?");
173     $sth1->execute($searchfield, $frameworkcode);
174     $sth2->execute($searchfield, $frameworkcode);
175     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
176     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
177     $cache->clear_from_cache("default_value_for_mod_marc-");
178     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
179         $template->param(
180           searchfield => $searchfield,
181         frameworkcode => $frameworkcode,
182     );
183                                                                                                         # END $OP eq DELETE_CONFIRMED
184 ################## ITEMTYPE_CREATE ##################################
185 # called automatically if an unexisting  frameworkis selected
186 } elsif ($op eq 'framework_create') {
187         $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");
188         $sth->execute;
189         my @existingframeworkloop;
190         while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
191                 if ($tot>0) {
192                         push @existingframeworkloop, {
193                 value => $thisframeworkcode,
194                 frameworktext => $frameworktext,
195             };
196                 }
197         }
198         $template->param(existingframeworkloop => \@existingframeworkloop,
199                                         frameworkcode => $frameworkcode,
200                                         );
201 ################## DEFAULT ##################################
202 } else { # DEFAULT
203         # here, $op can be unset or set to "framework_create_confirm".
204     if ($searchfield ne '') {
205         $template->param(searchfield => $searchfield);
206     }
207         my $cnt=0;
208         if ($dspchoice) {
209                 #here, user only wants used tags/subfields displayed
210                 $searchfield=~ s/\'/\\\'/g;
211                 my @data=split(' ',$searchfield);
212                 my $sth=$dbh->prepare("
213                       SELECT marc_tag_structure.tagfield AS mts_tagfield,
214                               marc_tag_structure.liblibrarian as mts_liblibrarian,
215                               marc_tag_structure.libopac as mts_libopac,
216                               marc_tag_structure.repeatable as mts_repeatable,
217                               marc_tag_structure.mandatory as mts_mandatory,
218                               marc_tag_structure.authorised_value as mts_authorized_value,
219                               marc_subfield_structure.*
220                 FROM marc_tag_structure 
221                 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");
222                 #could be ordoned by tab
223                 $sth->execute($data[0], $frameworkcode);
224                 my @results = ();
225                 while (my $data=$sth->fetchrow_hashref){
226                         push(@results,$data);
227                         $cnt++;
228                 }
229                 
230                 my @loop_data = ();
231                 my $j=1;
232                 my $i=$offset;
233         while ( $i < $cnt ) {
234                         my %row_data;  # get a fresh hash for the row data
235                         $row_data{tagfield}         = $results[$i]->{'mts_tagfield'};
236                         $row_data{liblibrarian}     = $results[$i]->{'mts_liblibrarian'};
237                         $row_data{repeatable}       = $results[$i]->{'mts_repeatable'};
238                         $row_data{mandatory}        = $results[$i]->{'mts_mandatory'};
239                         $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
240                         $row_data{subfield_link} = "marc_subfields_structure.pl?op=add_form&amp;tagfield=".$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
241                         $row_data{edit}          = "$script_name?op=add_form&amp;searchfield="            .$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
242                         $row_data{delete}        = "$script_name?op=delete_confirm&amp;searchfield="      .$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
243                         $j=$i;
244                         my @internal_loop = ();
245                         while ( ( $j < $cnt ) and ( $results[$i]->{'tagfield'} == $results[$j]->{'tagfield'} ) ) {
246                                 my %subfield_data;
247                                 $subfield_data{tagsubfield}      = $results[$j]->{'tagsubfield'};
248                                 $subfield_data{liblibrarian}     = $results[$j]->{'liblibrarian'};
249                                 $subfield_data{kohafield}        = $results[$j]->{'kohafield'};
250                                 $subfield_data{repeatable}       = $results[$j]->{'repeatable'};
251                                 $subfield_data{mandatory}        = $results[$j]->{'mandatory'};
252                                 $subfield_data{tab}              = $results[$j]->{'tab'};
253                                 $subfield_data{seealso}          = $results[$j]->{'seealso'};
254                                 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
255                                 $subfield_data{authtypecode}     = $results[$j]->{'authtypecode'};
256                                 $subfield_data{value_builder}    = $results[$j]->{'value_builder'};
257 #                               warn "tagfield :  ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
258                                 push @internal_loop,\%subfield_data;
259                                 $j++;
260                         }
261                         $row_data{'subfields'}=\@internal_loop;
262                         push(@loop_data, \%row_data);
263                         $i=$j;
264                 }
265                 $template->param(select_display => "True",
266                                                 loop => \@loop_data);
267         } else {
268         # Hidden feature: If search was field$subfield, redirect to the subfield edit form
269         my ( $tagfield, $tagsubfield ) = split /\$/, $searchfield;
270         if ( $tagsubfield ) {
271             print $input->redirect('/cgi-bin/koha/admin/marc_subfields_structure.pl?op=add_form&tagfield='.$tagfield.'&frameworkcode='.$frameworkcode.'#sub'.$tagsubfield.'field');
272             exit;
273         }
274                 #here, normal old style : display every tags
275                 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
276                 $cnt = $count;
277                 my @loop_data = ();
278         for ( my $i = $offset ; $i < $count ; $i++ ) {
279                         my %row_data;  # get a fresh hash for the row data
280                         $row_data{tagfield}         = $results->[$i]{'tagfield'};
281                         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
282                         $row_data{repeatable}       = $results->[$i]{'repeatable'};
283                         $row_data{mandatory}        = $results->[$i]{'mandatory'};
284                         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
285                         $row_data{subfield_link}    = "marc_subfields_structure.pl?tagfield="          .$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
286                         $row_data{edit}             = "$script_name?op=add_form&amp;searchfield="      .$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
287                         $row_data{delete}           = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
288                         push(@loop_data, \%row_data);
289                 }
290                 $template->param(loop => \@loop_data);
291         }
292         if ($offset>0) {
293                 $template->param(isprevpage => $offset,
294                                                 prevpage=> $offset-$pagesize,
295                                                 searchfield => $searchfield,
296                                                 script_name => $script_name,
297                                                 frameworkcode => $frameworkcode,
298                 );
299         }
300         if ($offset+$pagesize<$cnt) {
301                 $template->param(nextpage =>$offset+$pagesize,
302                                                 searchfield => $searchfield,
303                                                 script_name => $script_name,
304                                                 frameworkcode => $frameworkcode,
305                 );
306         }
307 } #---- END $OP eq DEFAULT
308
309 output_html_with_http_headers $input, $cookie, $template->output;
310
311 #
312 # the sub used for searches
313 #
314 sub StringSearch  {
315         my ($searchstring,$frameworkcode)=@_;
316         my $sth = C4::Context->dbh->prepare("
317     SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value
318      FROM  marc_tag_structure
319      WHERE (tagfield >= ? and frameworkcode=?)
320     ORDER BY tagfield
321     ");
322         $sth->execute($searchstring, $frameworkcode);
323         my $results = $sth->fetchall_arrayref({});
324         return (scalar(@$results), $results);
325 }
326
327 #
328 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
329 #
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, authorised_value, frameworkcode)
334         SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value, ? from marc_tag_structure where frameworkcode=?|, undef, $newframeworkcode, $oldframeworkcode );
335
336     $dbh->do(q|INSERT INTO marc_subfield_structure (frameworkcode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,authtypecode,value_builder,seealso,hidden)
337         SELECT ?,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,authtypecode,value_builder,seealso,hidden from marc_subfield_structure where frameworkcode=?
338     |, undef, $newframeworkcode, $oldframeworkcode );
339 }
340