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
49 use C4::Interface::CGI::Output;
53 my ($env,$searchstring,$type)=@_;
54 my $dbh = C4::Context->dbh;
55 $searchstring=~ s/\'/\\\'/g;
56 my @data=split(' ',$searchstring);
58 my $sth=$dbh->prepare("Select * from mediatypetable where (description like ?) order by mediatypecode");
59 $sth->execute("$data[0]%");
61 while (my $data=$sth->fetchrow_hashref){
66 return (scalar(@results),\@results);
70 my $searchfield=$input->param('description');
71 my $offset=$input->param('offset');
72 my $script_name="/cgi-bin/koha/admin/mediatype.pl";
73 my $mediatypecode=$input->param('mediatypecode');
75 my $op = $input->param('op');
76 $searchfield=~ s/\,//g;
77 my ($template, $borrowernumber, $cookie)
78 = get_template_and_user({template_name => "parameters/mediatype.tmpl",
82 flagsrequired => {parameters => 1, management => 1},
87 $template->param(script_name => $script_name,
88 $op => 1); # we show only the TMPL_VAR names $op
90 $template->param(script_name => $script_name,
91 else => 1); # we show only the TMPL_VAR names $op
93 ################## ADD_FORM ##################################
94 # called by default. Used to create form to add or modify a record
95 if ($op eq 'add_form') {
96 #start the page and read in includes
97 #---- if primkey exists, it's a modify action, so read values to modify...
100 my $dbh = C4::Context->dbh;
101 my @itemtypesselected;
102 if ($mediatypecode) {
103 my $sth=$dbh->prepare("select mediatypecode,description,itemtypecodes from mediatypetable where mediatypecode=?");
104 $sth->execute($mediatypecode);
105 $data=$sth->fetchrow_hashref;
107 @itemtypesselected = split ( /\|/, $data->{'itemtypecodes'} );
110 my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
112 while (my ($description,$itemtype) = $sth->fetchrow) {
113 $itemtypes .='<td><input type="checkbox" name="itemtypecodes" value="'.$itemtype.'"';
114 if(grep /$itemtype/,@itemtypesselected){
115 $itemtypes .=' checked';
117 $itemtypes .='>'.$description.'</td>';
120 $template->param(mediatypecode => $mediatypecode,
121 description => $data->{'description'},
122 itemtypes => $itemtypes
125 # END $OP eq ADD_FORM
126 ################## ADD_VALIDATE ##################################
127 # called by add_form, used to insert/modify data in DB
128 } elsif ($op eq 'add_validate') {
129 my $dbh = C4::Context->dbh;
130 my @itemtypecodesarray = $input->param('itemtypecodes');
131 my $itemtypecodes=join('|',@itemtypecodesarray);
132 my $sth=$dbh->prepare("replace mediatypetable (mediatypecode,description,itemtypecodes) values (?,?,?)");
134 $input->param('mediatypecode'),$input->param('description'),
138 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=mediatype.pl\"></html>";
140 # END $OP eq ADD_VALIDATE
141 ################## DELETE_CONFIRM ##################################
142 # called by default form, used to confirm deletion of data in DB
143 } elsif ($op eq 'delete_confirm') {
144 #start the page and read in includes
145 my $dbh = C4::Context->dbh;
146 my $sth=$dbh->prepare("select mediatypecode,description,itemtypecodes from mediatypetable where mediatypecode=?");
147 $sth->execute($mediatypecode);
148 my $data=$sth->fetchrow_hashref;
151 $template->param(mediatypecode => $mediatypecode,
152 description => $data->{'description'},
153 itemtypecodes => $data->{'itemtypecodes'});
154 # END $OP eq DELETE_CONFIRM
155 ################## DELETE_CONFIRMED ##################################
156 # called by delete_confirm, used to effectively confirm deletion of data in DB
157 } elsif ($op eq 'delete_confirmed') {
158 #start the page and read in includes
159 my $dbh = C4::Context->dbh;
160 my $mediatypecode=uc($input->param('mediatypecode'));
161 my $sth=$dbh->prepare("delete from mediatypetable where mediatypecode=?");
162 $sth->execute($mediatypecode);
164 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=mediatype.pl\"></html>";
166 # END $OP eq DELETE_CONFIRMED
167 ################## DEFAULT ##################################
170 my ($count,$results)=StringSearch($env,$searchfield,'web');
173 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
180 $row_data{toggle} = $toggle;
181 $row_data{mediatypecode} = $results->[$i]{mediatypecode};
182 $row_data{description} = $results->[$i]{description};
183 $row_data{itemtypecodes} = $results->[$i]{itemtypecodes};
184 push(@loop_data, \%row_data);
186 $template->param(loop => \@loop_data);
188 my $prevpage = $offset-$pagesize;
189 $template->param(previous => "$script_name?offset=".$prevpage);
191 if ($offset+$pagesize<$count) {
192 my $nextpage =$offset+$pagesize;
193 $template->param(next => "$script_name?offset=".$nextpage);
195 } #---- END $OP eq DEFAULT
196 output_html_with_http_headers $input, $cookie, $template->output;