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