Bug 26595: Add the option to hide the SMTP server column
[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                  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(action => "Modify tag");
99                 $template->param('heading_modify_tag_p' => 1);
100         } else {
101                 $template->param(action => "Add tag");
102                 $template->param('heading_add_tag_p' => 1);
103         }
104         $template->param('use_heading_flags_p' => 1);
105         $template->param(liblibrarian => $data->{'liblibrarian'},
106                         libopac => $data->{'libopac'},
107             repeatable => $data->{'repeatable'},
108             mandatory => $data->{'mandatory'},
109             important => $data->{'important'},
110             authorised_value => $data->{authorised_value},
111             ind1_defaultvalue => $data->{'ind1_defaultvalue'},
112             ind2_defaultvalue => $data->{'ind2_defaultvalue'}
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 $important        = $input->param('important')  ? 1 : 0;
124         my $authorised_value = $input->param('authorised_value');
125     my $ind1_defaultvalue = $input->param('ind1_defaultvalue');
126     my $ind2_defaultvalue = $input->param('ind2_defaultvalue');
127     if ($input->param('modif')) {
128         $sth = $dbh->prepare(
129         "UPDATE marc_tag_structure SET liblibrarian=? ,libopac=? ,repeatable=? ,mandatory=? ,important=? ,authorised_value=?, ind1_defaultvalue=?, ind2_defaultvalue=? WHERE frameworkcode=? AND tagfield=?"
130         );
131         $sth->execute(  $liblibrarian,
132                         $libopac,
133                         $repeatable,
134                         $mandatory,
135                         $important,
136                         $authorised_value,
137                         $ind1_defaultvalue,
138                         $ind2_defaultvalue,
139                         $frameworkcode,
140                         $tagfield
141         );
142     } else {
143         $sth = $dbh->prepare(
144         "INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,important,authorised_value,ind1_defaultvalue,ind2_defaultvalue,frameworkcode) values (?,?,?,?,?,?,?,?,?,?)"
145         );
146         $sth->execute($tagfield,
147                       $liblibrarian,
148                       $libopac,
149                       $repeatable,
150                       $mandatory,
151                       $important,
152                       $authorised_value,
153                       $ind1_defaultvalue,
154                       $ind2_defaultvalue,
155                       $frameworkcode
156         );
157     }
158     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
159     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
160     $cache->clear_from_cache("default_value_for_mod_marc-");
161     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
162     print $input->redirect("/cgi-bin/koha/admin/marctagstructure.pl?searchfield=$tagfield&frameworkcode=$frameworkcode");
163     exit;
164                                                                                                         # END $OP eq ADD_VALIDATE
165 ################## DELETE_CONFIRM ##################################
166 # called by default form, used to confirm deletion of data in DB
167 } elsif ($op eq 'delete_confirm') {
168     $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,ind1_defaultvalue,ind2_defaultvalue from marc_tag_structure where tagfield=? and frameworkcode=?");
169     $sth->execute($searchfield, $frameworkcode);
170     my $data = $sth->fetchrow_hashref;
171         $template->param(
172          liblibrarian => $data->{'liblibrarian'},
173           searchfield => $searchfield
174     );
175                                                                                                         # END $OP eq DELETE_CONFIRM
176 ################## DELETE_CONFIRMED ##################################
177 # called by delete_confirm, used to effectively confirm deletion of data in DB
178 } elsif ($op eq 'delete_confirmed') {
179     my $sth1 = $dbh->prepare("DELETE FROM marc_tag_structure      WHERE tagfield=? AND frameworkcode=?");
180     my $sth2 = $dbh->prepare("DELETE FROM marc_subfield_structure WHERE tagfield=? AND frameworkcode=?");
181     $sth1->execute($searchfield, $frameworkcode);
182     $sth2->execute($searchfield, $frameworkcode);
183     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
184     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
185     $cache->clear_from_cache("default_value_for_mod_marc-");
186     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
187     $template->param( searchfield => $searchfield );
188                                                                                                         # END $OP eq DELETE_CONFIRMED
189 ################## ITEMTYPE_CREATE ##################################
190 # called automatically if an unexisting  frameworkis selected
191 } elsif ($op eq 'framework_create') {
192         $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");
193         $sth->execute;
194         my @existingframeworkloop;
195         while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
196                 if ($tot>0) {
197                         push @existingframeworkloop, {
198                 value => $thisframeworkcode,
199                 frameworktext => $frameworktext,
200             };
201                 }
202         }
203     $template->param( existingframeworkloop => \@existingframeworkloop );
204
205 ################## DEFAULT ##################################
206 } else { # DEFAULT
207         # here, $op can be unset or set to "framework_create_confirm".
208     if ($searchfield ne '') {
209         $template->param(searchfield => $searchfield);
210     }
211         my $cnt=0;
212         if ($dspchoice) {
213                 #here, user only wants used tags/subfields displayed
214                 $searchfield=~ s/\'/\\\'/g;
215                 my @data=split(' ',$searchfield);
216                 my $sth=$dbh->prepare("
217                       SELECT marc_tag_structure.tagfield AS mts_tagfield,
218                               marc_tag_structure.liblibrarian as mts_liblibrarian,
219                               marc_tag_structure.libopac as mts_libopac,
220                               marc_tag_structure.repeatable as mts_repeatable,
221                               marc_tag_structure.mandatory as mts_mandatory,
222                       marc_tag_structure.important as mts_important,
223                               marc_tag_structure.authorised_value as mts_authorized_value,
224                   marc_tag_structure.ind1_defaultvalue as mts_ind1_defaultvalue,
225                   marc_tag_structure.ind1_defaultvalue as mts_ind2_defaultvalue,
226                               marc_subfield_structure.*
227                 FROM marc_tag_structure 
228                 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");
229                 #could be ordoned by tab
230                 $sth->execute($data[0], $frameworkcode);
231                 my @results = ();
232                 while (my $data=$sth->fetchrow_hashref){
233                         push(@results,$data);
234                         $cnt++;
235                 }
236                 
237                 my @loop_data = ();
238                 my $j=1;
239                 my $i=$offset;
240         while ( $i < $cnt ) {
241                         my %row_data;  # get a fresh hash for the row data
242                         $row_data{tagfield}         = $results[$i]->{'mts_tagfield'};
243                         $row_data{liblibrarian}     = $results[$i]->{'mts_liblibrarian'};
244                         $row_data{repeatable}       = $results[$i]->{'mts_repeatable'};
245                         $row_data{mandatory}        = $results[$i]->{'mts_mandatory'};
246             $row_data{important}        = $results[$i]->{'mts_important'};
247                         $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
248             $row_data{ind1_defaultvalue} = $results[$i]->{'mts_ind1_defaultvalue'};
249             $row_data{ind2_defaultvalue} = $results[$i]->{'mts_ind2_defaultvalue'};
250                         $j=$i;
251                         my @internal_loop = ();
252                         while ( ( $j < $cnt ) and ( $results[$i]->{'tagfield'} == $results[$j]->{'tagfield'} ) ) {
253                                 my %subfield_data;
254                                 $subfield_data{tagsubfield}      = $results[$j]->{'tagsubfield'};
255                                 $subfield_data{liblibrarian}     = $results[$j]->{'liblibrarian'};
256                                 $subfield_data{kohafield}        = $results[$j]->{'kohafield'};
257                                 $subfield_data{repeatable}       = $results[$j]->{'repeatable'};
258                                 $subfield_data{mandatory}        = $results[$j]->{'mandatory'};
259                 $subfield_data{important}        = $results[$j]->{'important'};
260                                 $subfield_data{tab}              = $results[$j]->{'tab'};
261                                 $subfield_data{seealso}          = $results[$j]->{'seealso'};
262                                 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
263                                 $subfield_data{authtypecode}     = $results[$j]->{'authtypecode'};
264                                 $subfield_data{value_builder}    = $results[$j]->{'value_builder'};
265 #                               warn "tagfield :  ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
266                                 push @internal_loop,\%subfield_data;
267                                 $j++;
268                         }
269                         $row_data{'subfields'}=\@internal_loop;
270                         push(@loop_data, \%row_data);
271                         $i=$j;
272                 }
273                 $template->param(select_display => "True",
274                                                 loop => \@loop_data);
275         } else {
276         # Hidden feature: If search was field$subfield, redirect to the subfield edit form
277         my ( $tagfield, $tagsubfield ) = split /\$/, $searchfield;
278         if ( $tagsubfield ) {
279             print $input->redirect('/cgi-bin/koha/admin/marc_subfields_structure.pl?op=add_form&tagfield='.$tagfield.'&frameworkcode='.$frameworkcode.'#sub'.$tagsubfield.'field');
280             exit;
281         }
282                 #here, normal old style : display every tags
283                 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
284                 $cnt = $count;
285                 my @loop_data = ();
286         for ( my $i = $offset ; $i < $count ; $i++ ) {
287                         my %row_data;  # get a fresh hash for the row data
288                         $row_data{tagfield}         = $results->[$i]{'tagfield'};
289                         $row_data{liblibrarian}     = $results->[$i]{'liblibrarian'};
290                         $row_data{repeatable}       = $results->[$i]{'repeatable'};
291                         $row_data{mandatory}        = $results->[$i]{'mandatory'};
292             $row_data{important}        = $results->[$i]{'important'};
293                         $row_data{authorised_value} = $results->[$i]{'authorised_value'};
294             $row_data{ind1_defaultvalue} = $results->[$i]{'ind1_defaultvalue'};
295             $row_data{ind2_defaultvalue} = $results->[$i]{'ind2_defaultvalue'};
296                         push(@loop_data, \%row_data);
297                 }
298                 $template->param(loop => \@loop_data);
299         }
300         if ($offset>0) {
301                 $template->param(isprevpage => $offset,
302                                                 prevpage=> $offset-$pagesize,
303                                                 searchfield => $searchfield,
304                         script_name => $script_name
305                 );
306         }
307         if ($offset+$pagesize<$cnt) {
308                 $template->param(nextpage =>$offset+$pagesize,
309                                                 searchfield => $searchfield,
310                         script_name => $script_name
311                 );
312         }
313 } #---- END $OP eq DEFAULT
314
315 output_html_with_http_headers $input, $cookie, $template->output;
316
317 #
318 # the sub used for searches
319 #
320 sub StringSearch  {
321         my ($searchstring,$frameworkcode)=@_;
322         my $sth = C4::Context->dbh->prepare("
323     SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,important,authorised_value,ind1_defaultvalue,ind2_defaultvalue
324      FROM  marc_tag_structure
325      WHERE (tagfield >= ? and frameworkcode=?)
326     ORDER BY tagfield
327     ");
328         $sth->execute($searchstring, $frameworkcode);
329         my $results = $sth->fetchall_arrayref({});
330         return (scalar(@$results), $results);
331 }
332
333 #
334 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
335 #
336 sub duplicate_framework {
337     my ($newframeworkcode,$oldframeworkcode) = @_;
338     my $dbh = C4::Context->dbh;
339     $dbh->do(q|INSERT INTO marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, important, authorised_value, ind1_defaultvalue, ind2_defaultvalue, frameworkcode)
340         SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,important,authorised_value, ind1_defaultvalue, ind2_defaultvalue, ? from marc_tag_structure where frameworkcode=?|, undef, $newframeworkcode, $oldframeworkcode );
341
342     $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)
343         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=?
344     |, undef, $newframeworkcode, $oldframeworkcode );
345 }
346