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