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