2 # NOTE: 4-character tabs
4 #script to administer the mediatype table
5 #modified from the itemtype script written 20/02/2002 by paul.poulain@free.fr
6 #This script written by waylon@robertson.net.nz at 2nd June, 2005
8 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
11 # this script use an $op to know what to do.
12 # if $op is empty or none of the above values,
13 # - the default screen is build (with all records, or filtered datas).
14 # - the user can clic on add, modify or delete record.
16 # - if primkey exists, this is a modification,so we read the $primkey record
17 # - builds the add/modify form
19 # - the user has just send datas, so we create/modify the record
21 # - we show the record having primkey=$primkey and ask for deletion validation form
22 # if $op=delete_confirm
23 # - we delete the record having primkey=$primkey
26 # Copyright 2000-2002 Katipo Communications
28 # This file is part of Koha.
30 # Koha is free software; you can redistribute it and/or modify it under the
31 # terms of the GNU General Public License as published by the Free Software
32 # Foundation; either version 2 of the License, or (at your option) any later
35 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
36 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
37 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
39 # You should have received a copy of the GNU General Public License along with
40 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
41 # Suite 330, Boston, MA 02111-1307 USA
48 use C4::Interface::CGI::Output;
52 my ($env,$searchstring,$type)=@_;
53 my $dbh = C4::Context->dbh;
54 $searchstring=~ s/\'/\\\'/g;
55 my @data=split(' ',$searchstring);
57 my $sth=$dbh->prepare("Select * from mediatypetable where (description like ?) order by mediatypecode");
58 $sth->execute("$data[0]%");
60 while (my $data=$sth->fetchrow_hashref){
65 return (scalar(@results),\@results);
69 my $searchfield=$input->param('description');
70 my $offset=$input->param('offset');
71 my $script_name="/cgi-bin/koha/admin/mediatype.pl";
72 my $mediatypecode=$input->param('mediatypecode');
74 my $op = $input->param('op');
75 $searchfield=~ s/\,//g;
76 my ($template, $borrowernumber, $cookie)
77 = get_template_and_user({template_name => "admin/mediatype.tmpl",
81 flagsrequired => {parameters => 1},
86 $template->param(script_name => $script_name,
87 $op => 1); # we show only the TMPL_VAR names $op
89 $template->param(script_name => $script_name,
90 else => 1); # we show only the TMPL_VAR names $op
92 ################## ADD_FORM ##################################
93 # called by default. Used to create form to add or modify a record
94 if ($op eq 'add_form') {
95 #start the page and read in includes
96 #---- if primkey exists, it's a modify action, so read values to modify...
99 my $dbh = C4::Context->dbh;
100 my @itemtypesselected;
101 if ($mediatypecode) {
102 my $sth=$dbh->prepare("select mediatypecode,description,itemtypecodes from mediatypetable where mediatypecode=?");
103 $sth->execute($mediatypecode);
104 $data=$sth->fetchrow_hashref;
106 @itemtypesselected = split ( /\|/, $data->{'itemtypecodes'} );
109 my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
111 while (my ($description,$itemtype) = $sth->fetchrow) {
112 $itemtypes .='<td><input type="checkbox" name="itemtypecodes" value="'.$itemtype.'"';
113 if(grep /$itemtype/,@itemtypesselected){
114 $itemtypes .=' checked';
116 $itemtypes .='>'.$description.'</td>';
119 $template->param(mediatypecode => $mediatypecode,
120 description => $data->{'description'},
121 itemtypes => $itemtypes
124 # END $OP eq ADD_FORM
125 ################## ADD_VALIDATE ##################################
126 # called by add_form, used to insert/modify data in DB
127 } elsif ($op eq 'add_validate') {
128 my $dbh = C4::Context->dbh;
129 my @itemtypecodesarray = $input->param('itemtypecodes');
130 my $itemtypecodes=join('|',@itemtypecodesarray);
131 my $sth=$dbh->prepare("replace mediatypetable (mediatypecode,description,itemtypecodes) values (?,?,?)");
133 $input->param('mediatypecode'),$input->param('description'),
137 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=mediatype.pl\"></html>";
139 # END $OP eq ADD_VALIDATE
140 ################## DELETE_CONFIRM ##################################
141 # called by default form, used to confirm deletion of data in DB
142 } elsif ($op eq 'delete_confirm') {
143 #start the page and read in includes
144 my $dbh = C4::Context->dbh;
145 my $sth=$dbh->prepare("select mediatypecode,description,itemtypecodes from mediatypetable where mediatypecode=?");
146 $sth->execute($mediatypecode);
147 my $data=$sth->fetchrow_hashref;
150 $template->param(mediatypecode => $mediatypecode,
151 description => $data->{'description'},
152 itemtypecodes => $data->{'itemtypecodes'});
153 # END $OP eq DELETE_CONFIRM
154 ################## DELETE_CONFIRMED ##################################
155 # called by delete_confirm, used to effectively confirm deletion of data in DB
156 } elsif ($op eq 'delete_confirmed') {
157 #start the page and read in includes
158 my $dbh = C4::Context->dbh;
159 my $mediatypecode=uc($input->param('mediatypecode'));
160 my $sth=$dbh->prepare("delete from mediatypetable where mediatypecode=?");
161 $sth->execute($mediatypecode);
163 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=mediatype.pl\"></html>";
165 # END $OP eq DELETE_CONFIRMED
166 ################## DEFAULT ##################################
169 my ($count,$results)=StringSearch($env,$searchfield,'web');
172 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
179 $row_data{toggle} = $toggle;
180 $row_data{mediatypecode} = $results->[$i]{mediatypecode};
181 $row_data{description} = $results->[$i]{description};
182 $row_data{itemtypecodes} = $results->[$i]{itemtypecodes};
183 push(@loop_data, \%row_data);
185 $template->param(loop => \@loop_data);
187 my $prevpage = $offset-$pagesize;
188 $template->param(previous => "$script_name?offset=".$prevpage);
190 if ($offset+$pagesize<$count) {
191 my $nextpage =$offset+$pagesize;
192 $template->param(next => "$script_name?offset=".$nextpage);
194 } #---- END $OP eq DEFAULT
195 output_html_with_http_headers $input, $cookie, $template->output;