removing all useless %env / $env
[koha.git] / tools / letter.pl
1 #!/usr/bin/perl
2
3 #script to administer the aqbudget table
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # ALGO :
8 # this script use an $op to know what to do.
9 # if $op is empty or none of the above values,
10 #       - the default screen is build (with all records, or filtered datas).
11 #       - the   user can clic on add, modify or delete record.
12 # if $op=add_form
13 #       - if primkey exists, this is a modification,so we read the $primkey record
14 #       - builds the add/modify form
15 # if $op=add_validate
16 #       - the user has just send datas, so we create/modify the record
17 # if $op=delete_form
18 #       - we show the record having primkey=$primkey and ask for deletion validation form
19 # if $op=delete_confirm
20 #       - we delete the record having primkey=$primkey
21
22
23 # Copyright 2000-2002 Katipo Communications
24 #
25 # This file is part of Koha.
26 #
27 # Koha is free software; you can redistribute it and/or modify it under the
28 # terms of the GNU General Public License as published by the Free Software
29 # Foundation; either version 2 of the License, or (at your option) any later
30 # version.
31 #
32 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
33 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
34 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License along with
37 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
38 # Suite 330, Boston, MA  02111-1307 USA
39
40 use strict;
41 use CGI;
42 use C4::Date;
43 use C4::Auth;
44 use C4::Context;
45 use C4::Output;
46 use C4::Interface::CGI::Output;
47
48
49 sub StringSearch  {
50     my ($searchstring,$type)=@_;
51     my $dbh = C4::Context->dbh;
52     $searchstring=~ s/\'/\\\'/g;
53     my @data=split(' ',$searchstring);
54     my $count=@data;
55     my $sth=$dbh->prepare("Select * from letter where (code like ?) order by module,code");
56     $sth->execute("$data[0]%");
57     my @results;
58     my $cnt=0;
59     while (my $data=$sth->fetchrow_hashref){
60     push(@results,$data);
61     $cnt ++;
62     }
63     #  $sth->execute;
64     $sth->finish;
65     return ($cnt,\@results);
66 }
67
68 my $input = new CGI;
69 my $searchfield=$input->param('searchfield');
70 my $offset=$input->param('offset');
71 my $script_name="/cgi-bin/koha/tools/letter.pl";
72 my $code=$input->param('code');
73 my $module = $input->param('module');
74 my $content = $input->param('content');
75 my $pagesize=20;
76 my $op = $input->param('op');
77 $searchfield=~ s/\,//g;
78 my $dbh = C4::Context->dbh;
79
80 my ($template, $borrowernumber, $cookie)
81     = get_template_and_user({template_name => "tools/letter.tmpl",
82                 query => $input,
83                 type => "intranet",
84                 authnotrequired => 0,
85                 flagsrequired => {tools => 1},
86                 debug => 1,
87                 });
88
89 if ($op) {
90 $template->param(script_name => $script_name,
91                         $op              => 1); # we show only the TMPL_VAR names $op
92 } else {
93 $template->param(script_name => $script_name,
94                         else              => 1); # we show only the TMPL_VAR names $op
95 }
96
97 $template->param(action => $script_name);
98 ################## ADD_FORM ##################################
99 # called by default. Used to create form to add or  modify a record
100 if ($op eq 'add_form') {
101     #---- if primkey exists, it's a modify action, so read values to modify...
102     my $letter;
103     if ($code) {
104         my $sth=$dbh->prepare("select * from letter where module=? and code=?");
105         $sth->execute($module,$code);
106         $letter=$sth->fetchrow_hashref;
107         $sth->finish;
108     }
109     # build field list
110     my @SQLfieldname;
111     my %line = ('value' => "LibrarianFirstname", 'text' => 'LibrarianFirstname');
112     push @SQLfieldname, \%line;
113     my %line = ('value' => "LibrarianSurname", 'text' => 'LibrarianSurname');
114     push @SQLfieldname, \%line;
115     my %line = ('value' => "LibrarianEmailaddress", 'text' => 'LibrarianEmailaddress');
116     push @SQLfieldname, \%line;
117     my $sth2=$dbh->prepare("SHOW COLUMNS from branches");
118     $sth2->execute;
119     my %line = ('value' => "", 'text' => '---BRANCHES---');
120     push @SQLfieldname, \%line;
121     while ((my $field) = $sth2->fetchrow_array) {
122         my %line = ('value' => "branches.".$field, 'text' => "branches.".$field);
123         push @SQLfieldname, \%line;
124     }
125     # add acquisition specific tables
126     if (index($module,"acquisition")>0){
127     my $sth2=$dbh->prepare("SHOW COLUMNS from aqbooksellers");
128     $sth2->execute;
129     my %line = ('value' => "", 'text' => '---BOOKSELLERS---');
130     push @SQLfieldname, \%line;
131     while ((my $field) = $sth2->fetchrow_array) {
132         my %line = ('value' => "aqbooksellers.".$field, 'text' => "aqbooksellers.".$field);
133         push @SQLfieldname, \%line;
134     }
135     my $sth2=$dbh->prepare("SHOW COLUMNS from aqorders");
136     $sth2->execute;
137     my %line = ('value' => "", 'text' => '---ORDERS---');
138     push @SQLfieldname, \%line;
139     while ((my $field) = $sth2->fetchrow_array) {
140         my %line = ('value' => "aqorders.".$field, 'text' => "aqorders.".$field);
141         push @SQLfieldname, \%line;
142     }
143     # add issues specific tables
144     }elsif (index($module,"issues")>0){
145     my $sth2=$dbh->prepare("SHOW COLUMNS from aqbooksellers");
146     $sth2->execute;
147     my %line = ('value' => "", 'text' => '---BOOKSELLERS---');
148     push @SQLfieldname, \%line;
149     while ((my $field) = $sth2->fetchrow_array) {
150         my %line = ('value' => "aqbooksellers.".$field, 'text' => "aqbooksellers.".$field);
151         push @SQLfieldname, \%line;
152     }
153     my $sth2=$dbh->prepare("SHOW COLUMNS from serial");
154     $sth2->execute;
155     my %line = ('value' => "", 'text' => '---SERIALS---');
156     push @SQLfieldname, \%line;
157     while ((my $field) = $sth2->fetchrow_array) {
158         my %line = ('value' => "serial.".$field, 'text' => "serial.".$field);
159         push @SQLfieldname, \%line;
160     }
161     my $sth2=$dbh->prepare("SHOW COLUMNS from subscription");
162     $sth2->execute;
163     my %line = ('value' => "", 'text' => '---SUBSCRIPTION---');
164     push @SQLfieldname, \%line;
165     while ((my $field) = $sth2->fetchrow_array) {
166         my %line = ('value' => "subscription.".$field, 'text' => "subscription.".$field);
167         push @SQLfieldname, \%line;
168     }
169     # add biblio specific tables.
170     } else {
171     my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
172     $sth2->execute;
173     my %line = ('value' => "", 'text' => '---BIBLIO---');
174
175     push @SQLfieldname, \%line;
176     while ((my $field) = $sth2->fetchrow_array) {
177         # note : %line is redefined, otherwise \%line contains the same value for every entry of the list
178         my %line = ('value' => "biblio.".$field, 'text' => "biblio.".$field);
179         push @SQLfieldname, \%line;
180     }
181     my $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
182     $sth2->execute;
183     my %line = ('value' => "", 'text' => '---BIBLIOITEMS---');
184     push @SQLfieldname, \%line;
185     while ((my $field) = $sth2->fetchrow_array) {
186         my %line = ('value' => "biblioitems.".$field, 'text' => "biblioitems.".$field);
187         push @SQLfieldname, \%line;
188     }
189     my %line = ('value' => "", 'text' => '---ITEMS---');
190     push @SQLfieldname, \%line;
191     my %line = ('value' => "items.content", 'text' => 'items.content');
192     push @SQLfieldname, \%line;
193     
194     my $sth2=$dbh->prepare("SHOW COLUMNS from borrowers");
195     $sth2->execute;
196     my %line = ('value' => "", 'text' => '---BORROWERS---');
197     push @SQLfieldname, \%line;
198     while ((my $field) = $sth2->fetchrow_array) {
199         my %line = ('value' => "borrowers.".$field, 'text' => "borrowers.".$field);
200         push @SQLfieldname, \%line;
201     }
202     }
203     if ($code) {
204         $template->param(modify => 1);
205         $template->param(code => $letter->{code});
206     } else {
207         $template->param(adding => 1);
208     }
209     $template->param(name => $letter->{name},title => $letter->{title},
210                     content => ($content?$content:$letter->{content}),
211                     ($module?$module:$letter->{module}) => 1,
212                     SQLfieldname => \@SQLfieldname,);
213                                                     # END $OP eq ADD_FORM
214 ################## ADD_VALIDATE ##################################
215 # called by add_form, used to insert/modify data in DB
216 } elsif ($op eq 'add_validate') {
217     my $dbh = C4::Context->dbh;
218     my $sth=$dbh->prepare("replace letter (module,code,name,title,content) values (?,?,?,?,?)");
219     $sth->execute($input->param('module'),$input->param('code'),$input->param('name'),$input->param('title'),$input->param('content'));
220     $sth->finish;
221     print $input->redirect("letter.pl");
222     exit;
223 # END $OP eq ADD_VALIDATE
224 ################## DELETE_CONFIRM ##################################
225 # called by default form, used to confirm deletion of data in DB
226 } elsif ($op eq 'delete_confirm') {
227     my $dbh = C4::Context->dbh;
228     my $sth=$dbh->prepare("select * from letter where code=?");
229     $sth->execute($code);
230     my $data=$sth->fetchrow_hashref;
231     $sth->finish;
232     $template->param(module => $data->{module});
233     $template->param(code => $code);
234     $template->param(name => $data->{'name'});
235     $template->param(content => $data->{'content'});
236                                                     # END $OP eq DELETE_CONFIRM
237 ################## DELETE_CONFIRMED ##################################
238 # called by delete_confirm, used to effectively confirm deletion of data in DB
239 } elsif ($op eq 'delete_confirmed') {
240     my $dbh = C4::Context->dbh;
241     my $code=uc($input->param('code'));
242     my $module=$input->param('module');
243     my $sth=$dbh->prepare("delete from letter where module=? and code=?");
244     $sth->execute($module,$code);
245     $sth->finish;
246     print $input->redirect("letter.pl");
247     return;
248                                                     # END $OP eq DELETE_CONFIRMED
249 ################## DEFAULT ##################################
250 } else { # DEFAULT
251     if  ($searchfield ne '') {
252             $template->param(search => 1);
253         $template->param(searchfield => $searchfield);
254     }
255     my ($count,$results)=StringSearch($searchfield,'web');
256     my $toggle="white";
257     my @loop_data =();
258     for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
259         if ($toggle eq 'white'){
260                 $toggle="#ffffcc";
261         } else {
262                 $toggle="white";
263         }
264     my %row_data;
265     $row_data{toggle} = $toggle;
266     $row_data{module} = $results->[$i]{'module'};
267     $row_data{code} = $results->[$i]{'code'};
268     $row_data{name} = $results->[$i]{'name'};
269     push(@loop_data, \%row_data);
270     }
271     $template->param(letter => \@loop_data);
272 } #---- END $OP eq DEFAULT
273
274 output_html_with_http_headers $input, $cookie, $template->output;
275