Bug 20942: OpenAPI spec for /patrons/{patron_id}/account
[koha.git] / admin / marctagstructure.pl
blob43de4fb4ae14f209b83aa5f3c8bfd26ed98cc708
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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.
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.
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>.
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;
29 use Koha::Caches;
30 use Koha::AuthorisedValues;
31 use Koha::BiblioFrameworks;
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;
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;
45 my $script_name = "/cgi-bin/koha/admin/marctagstructure.pl";
47 my $dbh = C4::Context->dbh;
48 my $cache = Koha::Caches->get_instance();
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 });
60 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
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";
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,
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,ind1_defaultvalue,ind2_defaultvalue from marc_tag_structure where tagfield=? and frameworkcode=?");
93 $sth->execute($searchfield,$frameworkcode);
94 $data=$sth->fetchrow_hashref;
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);
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 ind1_defaultvalue => $data->{'ind1_defaultvalue'},
112 ind2_defaultvalue => $data->{'ind2_defaultvalue'},
113 frameworkcode => $frameworkcode,
114 ); # FIXME: move checkboxes to presentation layer
115 # END $OP eq ADD_FORM
116 ################## ADD_VALIDATE ##################################
117 # called by add_form, used to insert/modify data in DB
118 } elsif ($op eq 'add_validate') {
119 my $tagfield = $input->param('tagfield');
120 my $liblibrarian = $input->param('liblibrarian');
121 my $libopac = $input->param('libopac');
122 my $repeatable = $input->param('repeatable') ? 1 : 0;
123 my $mandatory = $input->param('mandatory') ? 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=? ,authorised_value=?, ind1_defaultvalue=?, ind2_defaultvalue=? WHERE frameworkcode=? AND tagfield=?"
131 $sth->execute( $liblibrarian,
132 $libopac,
133 $repeatable,
134 $mandatory,
135 $authorised_value,
136 $ind1_defaultvalue,
137 $ind2_defaultvalue,
138 $frameworkcode,
139 $tagfield
141 } else {
142 $sth = $dbh->prepare(
143 "INSERT INTO marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,ind1_defaultvalue,ind2_defaultvalue,frameworkcode) values (?,?,?,?,?,?,?,?,?)"
145 $sth->execute($tagfield,
146 $liblibrarian,
147 $libopac,
148 $repeatable,
149 $mandatory,
150 $authorised_value,
151 $ind1_defaultvalue,
152 $ind2_defaultvalue,
153 $frameworkcode
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 frameworkcode => $frameworkcode,
174 # END $OP eq DELETE_CONFIRM
175 ################## DELETE_CONFIRMED ##################################
176 # called by delete_confirm, used to effectively confirm deletion of data in DB
177 } elsif ($op eq 'delete_confirmed') {
178 my $sth1 = $dbh->prepare("DELETE FROM marc_tag_structure WHERE tagfield=? AND frameworkcode=?");
179 my $sth2 = $dbh->prepare("DELETE FROM marc_subfield_structure WHERE tagfield=? AND frameworkcode=?");
180 $sth1->execute($searchfield, $frameworkcode);
181 $sth2->execute($searchfield, $frameworkcode);
182 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
183 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
184 $cache->clear_from_cache("default_value_for_mod_marc-");
185 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
186 $template->param(
187 searchfield => $searchfield,
188 frameworkcode => $frameworkcode,
190 # END $OP eq DELETE_CONFIRMED
191 ################## ITEMTYPE_CREATE ##################################
192 # called automatically if an unexisting frameworkis selected
193 } elsif ($op eq 'framework_create') {
194 $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");
195 $sth->execute;
196 my @existingframeworkloop;
197 while (my ($tot,$thisframeworkcode,$frameworktext) = $sth->fetchrow) {
198 if ($tot>0) {
199 push @existingframeworkloop, {
200 value => $thisframeworkcode,
201 frameworktext => $frameworktext,
205 $template->param(existingframeworkloop => \@existingframeworkloop,
206 frameworkcode => $frameworkcode,
208 ################## DEFAULT ##################################
209 } else { # DEFAULT
210 # here, $op can be unset or set to "framework_create_confirm".
211 if ($searchfield ne '') {
212 $template->param(searchfield => $searchfield);
214 my $cnt=0;
215 if ($dspchoice) {
216 #here, user only wants used tags/subfields displayed
217 $searchfield=~ s/\'/\\\'/g;
218 my @data=split(' ',$searchfield);
219 my $sth=$dbh->prepare("
220 SELECT marc_tag_structure.tagfield AS mts_tagfield,
221 marc_tag_structure.liblibrarian as mts_liblibrarian,
222 marc_tag_structure.libopac as mts_libopac,
223 marc_tag_structure.repeatable as mts_repeatable,
224 marc_tag_structure.mandatory as mts_mandatory,
225 marc_tag_structure.authorised_value as mts_authorized_value,
226 marc_tag_structure.ind1_defaultvalue as mts_ind1_defaultvalue,
227 marc_tag_structure.ind1_defaultvalue as mts_ind2_defaultvalue,
228 marc_subfield_structure.*
229 FROM marc_tag_structure
230 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");
231 #could be ordoned by tab
232 $sth->execute($data[0], $frameworkcode);
233 my @results = ();
234 while (my $data=$sth->fetchrow_hashref){
235 push(@results,$data);
236 $cnt++;
239 my @loop_data = ();
240 my $j=1;
241 my $i=$offset;
242 while ( $i < $cnt ) {
243 my %row_data; # get a fresh hash for the row data
244 $row_data{tagfield} = $results[$i]->{'mts_tagfield'};
245 $row_data{liblibrarian} = $results[$i]->{'mts_liblibrarian'};
246 $row_data{repeatable} = $results[$i]->{'mts_repeatable'};
247 $row_data{mandatory} = $results[$i]->{'mts_mandatory'};
248 $row_data{authorised_value} = $results[$i]->{'mts_authorised_value'};
249 $row_data{ind1_defaultvalue} = $results[$i]->{'mts_ind1_defaultvalue'};
250 $row_data{ind2_defaultvalue} = $results[$i]->{'mts_ind2_defaultvalue'};
251 $row_data{subfield_link} = "marc_subfields_structure.pl?op=add_form&amp;tagfield=".$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
252 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=" .$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
253 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=" .$results[$i]->{'mts_tagfield'}."&amp;frameworkcode=".$frameworkcode;
254 $j=$i;
255 my @internal_loop = ();
256 while ( ( $j < $cnt ) and ( $results[$i]->{'tagfield'} == $results[$j]->{'tagfield'} ) ) {
257 my %subfield_data;
258 $subfield_data{tagsubfield} = $results[$j]->{'tagsubfield'};
259 $subfield_data{liblibrarian} = $results[$j]->{'liblibrarian'};
260 $subfield_data{kohafield} = $results[$j]->{'kohafield'};
261 $subfield_data{repeatable} = $results[$j]->{'repeatable'};
262 $subfield_data{mandatory} = $results[$j]->{'mandatory'};
263 $subfield_data{tab} = $results[$j]->{'tab'};
264 $subfield_data{seealso} = $results[$j]->{'seealso'};
265 $subfield_data{authorised_value} = $results[$j]->{'authorised_value'};
266 $subfield_data{authtypecode} = $results[$j]->{'authtypecode'};
267 $subfield_data{value_builder} = $results[$j]->{'value_builder'};
268 # warn "tagfield : ".$results[$j]->{'tagfield'}." tagsubfield :".$results[$j]->{'tagsubfield'};
269 push @internal_loop,\%subfield_data;
270 $j++;
272 $row_data{'subfields'}=\@internal_loop;
273 push(@loop_data, \%row_data);
274 $i=$j;
276 $template->param(select_display => "True",
277 loop => \@loop_data);
278 } else {
279 # Hidden feature: If search was field$subfield, redirect to the subfield edit form
280 my ( $tagfield, $tagsubfield ) = split /\$/, $searchfield;
281 if ( $tagsubfield ) {
282 print $input->redirect('/cgi-bin/koha/admin/marc_subfields_structure.pl?op=add_form&tagfield='.$tagfield.'&frameworkcode='.$frameworkcode.'#sub'.$tagsubfield.'field');
283 exit;
285 #here, normal old style : display every tags
286 my ($count,$results)=StringSearch($searchfield,$frameworkcode);
287 $cnt = $count;
288 my @loop_data = ();
289 for ( my $i = $offset ; $i < $count ; $i++ ) {
290 my %row_data; # get a fresh hash for the row data
291 $row_data{tagfield} = $results->[$i]{'tagfield'};
292 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
293 $row_data{repeatable} = $results->[$i]{'repeatable'};
294 $row_data{mandatory} = $results->[$i]{'mandatory'};
295 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
296 $row_data{ind1_defaultvalue} = $results->[$i]{'ind1_defaultvalue'};
297 $row_data{ind2_defaultvalue} = $results->[$i]{'ind2_defaultvalue'};
298 $row_data{subfield_link} = "marc_subfields_structure.pl?tagfield=" .$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
299 $row_data{edit} = "$script_name?op=add_form&amp;searchfield=" .$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
300 $row_data{delete} = "$script_name?op=delete_confirm&amp;searchfield=".$results->[$i]{'tagfield'}."&amp;frameworkcode=".$frameworkcode;
301 push(@loop_data, \%row_data);
303 $template->param(loop => \@loop_data);
305 if ($offset>0) {
306 $template->param(isprevpage => $offset,
307 prevpage=> $offset-$pagesize,
308 searchfield => $searchfield,
309 script_name => $script_name,
310 frameworkcode => $frameworkcode,
313 if ($offset+$pagesize<$cnt) {
314 $template->param(nextpage =>$offset+$pagesize,
315 searchfield => $searchfield,
316 script_name => $script_name,
317 frameworkcode => $frameworkcode,
320 } #---- END $OP eq DEFAULT
322 output_html_with_http_headers $input, $cookie, $template->output;
325 # the sub used for searches
327 sub StringSearch {
328 my ($searchstring,$frameworkcode)=@_;
329 my $sth = C4::Context->dbh->prepare("
330 SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,ind1_defaultvalue,ind2_defaultvalue
331 FROM marc_tag_structure
332 WHERE (tagfield >= ? and frameworkcode=?)
333 ORDER BY tagfield
335 $sth->execute($searchstring, $frameworkcode);
336 my $results = $sth->fetchall_arrayref({});
337 return (scalar(@$results), $results);
341 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
343 sub duplicate_framework {
344 my ($newframeworkcode,$oldframeworkcode) = @_;
345 my $dbh = C4::Context->dbh;
346 $dbh->do(q|INSERT INTO marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, ind1_defaultvalue, ind2_defaultvalue, frameworkcode)
347 SELECT tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value, ind1_defaultvalue, ind2_defaultvalue, ? from marc_tag_structure where frameworkcode=?|, undef, $newframeworkcode, $oldframeworkcode );
349 $dbh->do(q|INSERT INTO marc_subfield_structure (frameworkcode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,authtypecode,value_builder,seealso,hidden)
350 SELECT ?,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,authtypecode,value_builder,seealso,hidden from marc_subfield_structure where frameworkcode=?
351 |, undef, $newframeworkcode, $oldframeworkcode );