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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
27 use C4::Interface::CGI::Output;
34 my $itemtype = $input->param('itemtype'); # set to select framework
35 $itemtype="" unless $itemtype;
36 my $existingitemtype = $input->param('existingitemtype'); # set when we have to create a new framework (in itemtype) by copying an old one (in existingitemtype)
37 $existingitemtype = "" unless $existingitemtype;
38 my $itemtypeinfo = getitemtypeinfo($itemtype);
39 my $searchfield=$input->param('searchfield');
40 $searchfield=0 unless $searchfield;
41 $searchfield=~ s/\,//g;
43 my $offset=$input->param('offset');
44 my $op = $input->param('op');
47 my $script_name="/cgi-bin/koha/admin/marctagstructure.pl";
49 my $dbh = C4::Context->dbh;
52 my ($template, $loggedinuser, $cookie)
53 = get_template_and_user({template_name => "parameters/marctagstructure.tmpl",
57 flagsrequired => {parameters => 1},
62 my $itemtypes = getitemtypes;
64 foreach my $thisitemtype (keys %$itemtypes) {
65 my $selected = 1 if $thisitemtype eq $itemtype;
66 my %row =(value => $thisitemtype,
67 selected => $selected,
68 description => $itemtypes->{$thisitemtype}->{'description'},
70 push @itemtypesloop, \%row;
73 # check that itemtype framework is defined in marc_tag_structure
74 my $sth=$dbh->prepare("select count(*) from marc_tag_structure where itemtype=?");
75 $sth->execute($itemtype);
76 my ($itemtypeexist) = $sth->fetchrow;
79 # if itemtype does not exists, then OP must be changed to "create itemtype" if we are not on the way to create it
80 # (op = itemtyp_create_confirm)
81 if ($op eq "itemtype_create_confirm") {
82 duplicate_framework($itemtype, $existingitemtype);
84 $op = "itemtype_create";
87 $template->param(itemtypeloop => \@itemtypesloop);
89 $template->param(script_name => $script_name,
90 $op => 1); # we show only the TMPL_VAR names $op
92 $template->param(script_name => $script_name,
93 else => 1); # we show only the TMPL_VAR names $op
97 ################## ADD_FORM ##################################
98 # called by default. Used to create form to add or modify a record
99 if ($op eq 'add_form') {
100 #---- if primkey exists, it's a modify action, so read values to modify...
103 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=? and itemtype=?");
104 $sth->execute($searchfield,$itemtype);
105 $data=$sth->fetchrow_hashref;
108 my $sth = $dbh->prepare("select distinct category from authorised_values");
110 my @authorised_values;
111 push @authorised_values,"";
112 while ((my $category) = $sth->fetchrow_array) {
113 push @authorised_values, $category;
115 my $authorised_value = CGI::scrolling_list(-name=>'authorised_value',
116 -values=> \@authorised_values,
119 -default => $data->{'authorised_value'},
123 $template->param(action => "Modify tag",
124 searchfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$searchfield\" />$searchfield");
125 $template->param('heading-modify-tag-p' => 1);
127 $template->param(action => "Add tag",
128 searchfield => "<input type=\"text\" name=\"tagfield\" size=\"5\" maxlength=\"3\" />");
129 $template->param('heading-add-tag-p' => 1);
131 $template->param('use-heading-flags-p' => 1);
132 $template->param(liblibrarian => $data->{'liblibrarian'},
133 libopac => $data->{'libopac'},
134 repeatable => CGI::checkbox('repeatable',$data->{'repeatable'}?'checked':'',1,''),
135 mandatory => CGI::checkbox('mandatory',$data->{'mandatory'}?'checked':'',1,''),
136 authorised_value => $authorised_value,
137 itemtype => $itemtype,
139 # END $OP eq ADD_FORM
140 ################## ADD_VALIDATE ##################################
141 # called by add_form, used to insert/modify data in DB
142 } elsif ($op eq 'add_validate') {
143 $sth=$dbh->prepare("replace marc_tag_structure (tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value,itemtype) values (?,?,?,?,?,?,?)");
144 my $tagfield =$input->param('tagfield');
145 my $liblibrarian = $input->param('liblibrarian');
146 my $libopac =$input->param('libopac');
147 my $repeatable =$input->param('repeatable');
148 my $mandatory =$input->param('mandatory');
149 my $authorised_value =$input->param('authorised_value');
150 unless (C4::Context->config('demo') eq 1) {
151 $sth->execute($tagfield,
161 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marctagstructure.pl?tagfield=$tagfield&itemtype=$itemtype\"></html>";
163 # END $OP eq ADD_VALIDATE
164 ################## DELETE_CONFIRM ##################################
165 # called by default form, used to confirm deletion of data in DB
166 } elsif ($op eq 'delete_confirm') {
167 $sth=$dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where tagfield=?");
168 $sth->execute($searchfield);
169 my $data=$sth->fetchrow_hashref;
171 $template->param(liblibrarian => $data->{'liblibrarian'},
172 searchfield => $searchfield,
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 unless (C4::Context->config('demo') eq 1) {
179 $dbh->do("delete from marc_tag_structure where tagfield='$searchfield'");
180 $dbh->do("delete from marc_subfield_structure where tagfield='$searchfield'");
182 # END $OP eq DELETE_CONFIRMED
183 ################## ITEMTYPE_CREATE ##################################
184 # called automatically if an unexisting itemtype is selected
185 } elsif ($op eq 'itemtype_create') {
186 $sth = $dbh->prepare("select count(*),marc_tag_structure.itemtype,description from marc_tag_structure,itemtypes where itemtypes.itemtype=marc_tag_structure.itemtype group by marc_tag_structure.itemtype");
188 my @existingitemtypeloop;
189 while (my ($tot,$thisitemtype,$description) = $sth->fetchrow) {
191 my %line = ( value => $thisitemtype,
192 description => $description,
194 push @existingitemtypeloop,\%line;
197 $template->param(existingitemtypeloop => \@existingitemtypeloop,
198 itemtype => $itemtype,
199 ITdescription => $itemtypeinfo->{description},
201 ################## DEFAULT ##################################
203 # here, $op can be unset or set to "itemtype_create_confirm".
204 if ($searchfield ne '') {
205 $template->param(searchfield => $searchfield);
208 my ($count,$results)=StringSearch($env,$searchfield,$itemtype);
211 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
212 if ($toggle eq 'white'){
217 my %row_data; # get a fresh hash for the row data
218 $row_data{tagfield} = $results->[$i]{'tagfield'};
219 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
220 $row_data{repeatable} = $results->[$i]{'repeatable'};
221 $row_data{mandatory} = $results->[$i]{'mandatory'};
222 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
223 $row_data{subfield_link} ="marc_subfields_structure.pl?tagfield=".$results->[$i]{'tagfield'}."&itemtype=".$itemtype;
224 $row_data{edit} = "$script_name?op=add_form&searchfield=".$results->[$i]{'tagfield'}."&itemtype=".$itemtype;
225 $row_data{delete} = "$script_name?op=delete_confirm&searchfield=".$results->[$i]{'tagfield'}."&itemtype=".$itemtype;
226 $row_data{bgcolor} = $toggle;
227 push(@loop_data, \%row_data);
229 $template->param(loop => \@loop_data);
231 my $prevpage = $offset-$pagesize;
232 $template->param(isprevpage => $offset,
233 prevpage=> $prevpage,
234 searchfield => $searchfield,
235 script_name => $script_name,
236 itemtype => $itemtype,
239 if ($offset+$pagesize<$count) {
240 my $nextpage =$offset+$pagesize;
241 $template->param(nextpage =>$nextpage,
242 searchfield => $searchfield,
243 script_name => $script_name,
244 itemtype => $itemtype,
247 } #---- END $OP eq DEFAULT
249 $template->param(loggeninuser => $loggedinuser);
250 output_html_with_http_headers $input, $cookie, $template->output;
254 # the sub used for searches
257 my ($env,$searchstring,$itemtype)=@_;
258 my $dbh = C4::Context->dbh;
259 $searchstring=~ s/\'/\\\'/g;
260 my @data=split(' ',$searchstring);
262 my $sth=$dbh->prepare("Select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where (tagfield >= ? and itemtype=?) order by tagfield");
263 $sth->execute($data[0], $itemtype);
265 while (my $data=$sth->fetchrow_hashref){
266 push(@results,$data);
270 return (scalar(@results),\@results);
274 # the sub used to duplicate a framework from an existing one in MARC parameters tables.
276 sub duplicate_framework {
277 my ($newitemtype,$olditemtype) = @_;
278 my $sth = $dbh->prepare("select tagfield,liblibrarian,libopac,repeatable,mandatory,authorised_value from marc_tag_structure where itemtype=?");
279 $sth->execute($olditemtype);
280 my $sth_insert = $dbh->prepare("insert into marc_tag_structure (tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value, itemtype) values (?,?,?,?,?,?,?)");
281 while ( my ($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value) = $sth->fetchrow) {
282 $sth_insert->execute($tagfield,$liblibrarian,$libopac,$repeatable,$mandatory,$authorised_value,$newitemtype);
285 $sth = $dbh->prepare("select itemtype,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,thesaurus_category,value_builder,seealso from marc_subfield_structure where itemtype=?");
286 $sth->execute($olditemtype);
287 $sth_insert = $dbh->prepare("insert into marc_subfield_structure (itemtype,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,authorised_value,thesaurus_category,value_builder,seealso) values (?,?,?,?,?,?,?,?,?,?,?,?,?)");
288 while ( my ($itemtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso) = $sth->fetchrow) {
289 $sth_insert->execute($newitemtype, $tagfield, $tagsubfield, $liblibrarian, $libopac, $repeatable, $mandatory, $kohafield, $tab, $authorised_value, $thesaurus_category, $value_builder, $seealso);