big commit, still breaking things...
[koha.git] / admin / mediatype.pl
1 #!/usr/bin/perl
2 # NOTE: 4-character tabs
3
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
7
8 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
9
10 # ALGO :
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.
15 # if $op=add_form
16 #       - if primkey exists, this is a modification,so we read the $primkey record
17 #       - builds the add/modify form
18 # if $op=add_validate
19 #       - the user has just send datas, so we create/modify the record
20 # if $op=delete_form
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
24
25
26 # Copyright 2000-2002 Katipo Communications
27 #
28 # This file is part of Koha.
29 #
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
33 # version.
34 #
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.
38 #
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
42
43 use strict;
44 use CGI;
45 use C4::Context;
46 use C4::Output;
47 use C4::Search;
48 use C4::Auth;
49 use C4::Interface::CGI::Output;
50 use HTML::Template;
51
52 sub StringSearch  {
53         my ($env,$searchstring,$type)=@_;
54         my $dbh = C4::Context->dbh;
55         $searchstring=~ s/\'/\\\'/g;
56         my @data=split(' ',$searchstring);
57         my $count=@data;
58         my $sth=$dbh->prepare("Select * from mediatypetable where (description like ?) order by mediatypecode");
59         $sth->execute("$data[0]%");
60         my @results;
61         while (my $data=$sth->fetchrow_hashref){
62         push(@results,$data);
63         }
64         #  $sth->execute;
65         $sth->finish;
66         return (scalar(@results),\@results);
67 }
68
69 my $input = new CGI;
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');
74 my $pagesize=20;
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",
79                              query => $input,
80                              type => "intranet",
81                              authnotrequired => 0,
82                              flagsrequired => {parameters => 1, management => 1},
83                              debug => 1,
84                              });
85
86 if ($op) {
87 $template->param(script_name => $script_name,
88                                                 $op              => 1); # we show only the TMPL_VAR names $op
89 } else {
90 $template->param(script_name => $script_name,
91                                                 else              => 1); # we show only the TMPL_VAR names $op
92 }
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...
98         my $data;
99     my $itemtypes;
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;
106                 $sth->finish;
107         @itemtypesselected = split ( /\|/, $data->{'itemtypecodes'} );
108         }
109
110     my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
111     $sth->execute;
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';
116         }
117         $itemtypes .='>'.$description.'</td>';
118     }
119
120         $template->param(mediatypecode => $mediatypecode,
121                                                         description => $data->{'description'},
122                             itemtypes => $itemtypes
123                                                         );
124 ;
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 (?,?,?)");
133         $sth->execute(
134                 $input->param('mediatypecode'),$input->param('description'),
135                 $itemtypecodes
136         );
137         $sth->finish;
138         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=mediatype.pl\"></html>";
139         exit;
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;
149         $sth->finish;
150
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);
163         $sth->finish;
164         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=mediatype.pl\"></html>";
165         exit;
166                                                                                                         # END $OP eq DELETE_CONFIRMED
167 ################## DEFAULT ##################################
168 } else { # DEFAULT
169         my $env;
170         my ($count,$results)=StringSearch($env,$searchfield,'web');
171         my $toggle=0;
172         my @loop_data;
173         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
174                 my %row_data;
175                 if ($toggle eq 0){
176                         $toggle=1;
177                 } else {
178                         $toggle=0;
179                 }
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);
185         }
186         $template->param(loop => \@loop_data);
187         if ($offset>0) {
188                 my $prevpage = $offset-$pagesize;
189                 $template->param(previous => "$script_name?offset=".$prevpage);
190         }
191         if ($offset+$pagesize<$count) {
192                 my $nextpage =$offset+$pagesize;
193                 $template->param(next => "$script_name?offset=".$nextpage);
194         }
195 } #---- END $OP eq DEFAULT
196 output_html_with_http_headers $input, $cookie, $template->output;
197
198 # Local Variables:
199 # tab-width: 4
200 # End: