Nomenclature cleanup for subscription adding page
[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::Auth;
47 use C4::Output;
48
49
50 sub StringSearch  {
51         my ($searchstring,$type)=@_;
52         my $dbh = C4::Context->dbh;
53         $searchstring=~ s/\'/\\\'/g;
54         my @data=split(' ',$searchstring);
55         my $count=@data;
56         my $sth=$dbh->prepare("SELECT * FROM mediatypetable WHERE (description LIKE ?) ORDER BY mediatypecode");
57         $sth->execute("$data[0]%");
58         my @results;
59         while (my $data=$sth->fetchrow_hashref){
60         push(@results,$data);
61         }
62         #  $sth->execute;
63         $sth->finish;
64         return (scalar(@results),\@results);
65 }
66
67 my $input = new CGI;
68 my $searchfield=$input->param('description');
69 my $offset=$input->param('offset');
70 my $script_name="/cgi-bin/koha/admin/mediatype.pl";
71 my $mediatypecode=$input->param('mediatypecode');
72 my $pagesize=20;
73 my $op = $input->param('op');
74 $searchfield=~ s/\,//g;
75 my ($template, $borrowernumber, $cookie)
76     = get_template_and_user({template_name => "admin/mediatype.tmpl",
77                              query => $input,
78                              type => "intranet",
79                              authnotrequired => 0,
80                              flagsrequired => {parameters => 1},
81                              debug => 1,
82                              });
83
84 if ($op) {
85 $template->param(script_name => $script_name,
86                                                 $op              => 1); # we show only the TMPL_VAR names $op
87 } else {
88 $template->param(script_name => $script_name,
89                                                 else              => 1); # we show only the TMPL_VAR names $op
90 }
91 ################## ADD_FORM ##################################
92 # called by default. Used to create form to add or  modify a record
93 if ($op eq 'add_form') {
94         #start the page and read in includes
95         #---- if primkey exists, it's a modify action, so read values to modify...
96         my $data;
97     my $itemtypes;
98     my $dbh = C4::Context->dbh;
99     my @itemtypesselected;
100         if ($mediatypecode) {
101         my $sth=$dbh->prepare("SELECT mediatypecode,description,itemtypecodes FROM mediatypetable WHERE mediatypecode=?");
102                 $sth->execute($mediatypecode);
103                 $data=$sth->fetchrow_hashref;
104                 $sth->finish;
105         @itemtypesselected = split ( /\|/, $data->{'itemtypecodes'} );
106         }
107
108     my $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
109     $sth->execute;
110     while (my ($description,$itemtype) = $sth->fetchrow) {
111         $itemtypes .='<td><input type="checkbox" name="itemtypecodes" value="'.$itemtype.'"';
112         if(grep /$itemtype/,@itemtypesselected){
113             $itemtypes .=' checked';
114         }
115         $itemtypes .='>'.$description.'</td>';
116     }
117
118         $template->param(mediatypecode => $mediatypecode,
119                                                         description => $data->{'description'},
120                             itemtypes => $itemtypes
121                                                         );
122 ;
123                                                                                                         # END $OP eq ADD_FORM
124 ################## ADD_VALIDATE ##################################
125 # called by add_form, used to insert/modify data in DB
126 } elsif ($op eq 'add_validate') {
127         my $dbh = C4::Context->dbh;
128     my @itemtypecodesarray = $input->param('itemtypecodes');
129     my $itemtypecodes=join('|',@itemtypecodesarray);
130         my $sth = $dbh->prepaer("SELECT * FROM mediatypetable WHERE mediatypecode = ?");
131         $sth->execute($input->param('mediatypecode'));
132         if (my $data = $sth->fetchrow_hashref()){
133                 # row exists, so its a modify
134                 $sth->finish();
135                 $sth = $dbh->prepare("UPDATE mediatypetable SET description=?, itemtypecodes=? WHERE mediatypecode =? ");
136                 $sth->execute($input->param('description'),$itemtypecodes,$input->param('mediatypecode'));
137                 $sth->finish();
138         }
139         else {
140                 # its an add
141                 $sth->finish();
142                 $sth = $dbh->prepare("INSERT INTO mediattypetable (mediatypecode,description,itemtypecodes) VALUES (?,?,?)");
143                 $sth->execute(
144                 $input->param('mediatypecode'),$input->param('description'),
145                 $itemtypecodes
146         );
147                 $sth->finish;
148         }
149         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=mediatype.pl\"></html>";
150         exit;
151                                                                                                         # END $OP eq ADD_VALIDATE
152 ################## DELETE_CONFIRM ##################################
153 # called by default form, used to confirm deletion of data in DB
154 } elsif ($op eq 'delete_confirm') {
155         #start the page and read in includes
156         my $dbh = C4::Context->dbh;
157         my $sth=$dbh->prepare("select mediatypecode,description,itemtypecodes from mediatypetable where mediatypecode=?");
158         $sth->execute($mediatypecode);
159         my $data=$sth->fetchrow_hashref;
160         $sth->finish;
161
162         $template->param(mediatypecode => $mediatypecode,
163                                                         description => $data->{'description'},
164                                                         itemtypecodes => $data->{'itemtypecodes'});
165                                                                                                         # END $OP eq DELETE_CONFIRM
166 ################## DELETE_CONFIRMED ##################################
167 # called by delete_confirm, used to effectively confirm deletion of data in DB
168 } elsif ($op eq 'delete_confirmed') {
169         #start the page and read in includes
170         my $dbh = C4::Context->dbh;
171         my $mediatypecode=uc($input->param('mediatypecode'));
172         my $sth=$dbh->prepare("delete from mediatypetable where mediatypecode=?");
173         $sth->execute($mediatypecode);
174         $sth->finish;
175         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=mediatype.pl\"></html>";
176         exit;
177                                                                                                         # END $OP eq DELETE_CONFIRMED
178 ################## DEFAULT ##################################
179 } else { # DEFAULT
180         my ($count,$results)=StringSearch($searchfield,'web');
181         my $toggle=0;
182         my @loop_data;
183         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
184                 my %row_data;
185                 if ($toggle eq 0){
186                         $toggle=1;
187                 } else {
188                         $toggle=0;
189                 }
190                 $row_data{toggle} = $toggle;
191                 $row_data{mediatypecode} = $results->[$i]{mediatypecode};
192                 $row_data{description} = $results->[$i]{description};
193                 $row_data{itemtypecodes} = $results->[$i]{itemtypecodes};
194                 push(@loop_data, \%row_data);
195         }
196         $template->param(loop => \@loop_data);
197         if ($offset>0) {
198                 my $prevpage = $offset-$pagesize;
199                 $template->param(previous => "$script_name?offset=".$prevpage);
200         }
201         if ($offset+$pagesize<$count) {
202                 my $nextpage =$offset+$pagesize;
203                 $template->param(next => "$script_name?offset=".$nextpage);
204         }
205 } #---- END $OP eq DEFAULT
206 output_html_with_http_headers $input, $cookie, $template->output;
207
208 # Local Variables:
209 # tab-width: 4
210 # End: