Bug 31196: Remove 'default_value_for_mod_marc-' clear_from_cache calls
[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 Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Auth qw( get_template_and_user );
24 use C4::Context;
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Context;
27
28 use Koha::Caches;
29 use Koha::AuthorisedValues;
30 use Koha::BiblioFrameworks;
31
32 # retrieve parameters
33 my $input = CGI->new;
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;
38
39 my $offset    = $input->param('offset') || 0;
40 my $op        = $input->param('op')     || '';
41 my $dspchoice = $input->cookie("marctagstructure_selectdisplay") // $input->param('select_display');
42 my $pagesize = 20;
43
44 my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl";
45
46 my $dbh = C4::Context->dbh;
47 my $cache = Koha::Caches->get_instance();
48
49 # open template
50 my ($template, $loggedinuser, $cookie)
51     = get_template_and_user({template_name => "admin/marctagstructure.tt",
52                              query => $input,
53                              type => "intranet",
54                  flagsrequired => { parameters => 'manage_marc_frameworks' },
55                              });
56
57 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
58
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 "framework_create_confirm") {
67                 duplicate_framework($frameworkcode, $existingframeworkcode);
68                 $op = ""; # unset $op to go back to framework list
69         } else {
70                 $op = "framework_create";
71         }
72 }
73
74 my $framework = $frameworks->search({ frameworkcode => $frameworkcode })->next;
75 $template->param(
76     frameworks    => $frameworks,
77     framework     => $framework,
78     script_name   => $script_name,
79     ( $op || 'else' ) => 1,
80 );
81
82
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...
87         my $data;
88         if ($searchfield) {
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;
92         }
93
94         if ($searchfield) {
95         $template->param(searchfield => $searchfield);
96                 $template->param('heading_modify_tag_p' => 1);
97         } else {
98                 $template->param('heading_add_tag_p' => 1);
99         }
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 '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=?"
126         );
127         $sth->execute(  $liblibrarian,
128                         $libopac,
129                         $repeatable,
130                         $mandatory,
131                         $important,
132                         $authorised_value,
133                         $ind1_defaultvalue,
134                         $ind2_defaultvalue,
135                         $frameworkcode,
136                         $tagfield
137         );
138     } else {
139         $sth = $dbh->prepare(
140         "INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,important,authorised_value,ind1_defaultvalue,ind2_defaultvalue,frameworkcode) values (?,?,?,?,?,?,?,?,?,?)"
141         );
142         $sth->execute($tagfield,
143                       $liblibrarian,
144                       $libopac,
145                       $repeatable,
146                       $mandatory,
147                       $important,
148                       $authorised_value,
149                       $ind1_defaultvalue,
150                       $ind2_defaultvalue,
151                       $frameworkcode
152         );
153     }
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");
159     exit;
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;
167         $template->param(
168          liblibrarian => $data->{'liblibrarian'},
169           searchfield => $searchfield
170     );
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 '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(
189         {
190             'marc_tag_structure.frameworkcode' => { '!=' => undef }
191         },
192         {
193             join => 'marc_tag_structure',
194             distinct => 1
195         }
196     );
197     $template->param( existing_frameworks => $frameworks );
198
199 ################## DEFAULT ##################################
200 } else { # DEFAULT
201         # here, $op can be unset or set to "framework_create_confirm".
202     if ($searchfield ne '') {
203         $template->param(searchfield => $searchfield);
204     }
205         my $cnt=0;
206         if ($dspchoice) {
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);
225                 my @results = ();
226                 while (my $data=$sth->fetchrow_hashref){
227                         push(@results,$data);
228                         $cnt++;
229                 }
230                 
231                 my @loop_data = ();
232                 my $j=1;
233                 my $i=$offset;
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'};
244                         $j=$i;
245                         my @internal_loop = ();
246                         while ( ( $j < $cnt ) and ( $results[$i]->{'tagfield'} == $results[$j]->{'tagfield'} ) ) {
247                                 my %subfield_data;
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;
261                                 $j++;
262                         }
263                         $row_data{'subfields'}=\@internal_loop;
264                         push(@loop_data, \%row_data);
265                         $i=$j;
266                 }
267                 $template->param(select_display => "True",
268                                                 loop => \@loop_data);
269         } else {
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');
274             exit;
275         }
276                 #here, normal old style : display every tags
277                 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
278                 $cnt = $count;
279                 my @loop_data = ();
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);
291                 }
292                 $template->param(loop => \@loop_data);
293         }
294         if ($offset>0) {
295                 $template->param(isprevpage => $offset,
296                                                 prevpage=> $offset-$pagesize,
297                                                 searchfield => $searchfield,
298                         script_name => $script_name
299                 );
300         }
301         if ($offset+$pagesize<$cnt) {
302                 $template->param(nextpage =>$offset+$pagesize,
303                                                 searchfield => $searchfield,
304                         script_name => $script_name
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,important,authorised_value,ind1_defaultvalue,ind2_defaultvalue
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, 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 );
335
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 );
339 }
340