Merge git://git.koha.org/pub/scm/koha
[koha.git] / admin / 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::Dates;
43 use C4::Auth;
44 use C4::Context;
45 use C4::Output;
46
47
48 sub StringSearch  {
49         my ($searchstring,$type)=@_;
50         my $dbh = C4::Context->dbh;
51         $searchstring=~ s/\'/\\\'/g;
52         my @data=split(' ',$searchstring);
53         my $count=@data;
54         my $sth=$dbh->prepare("Select * from letter where (code like ?) order by module,code");
55         $sth->execute("$data[0]%");
56         my @results;
57         my $cnt=0;
58         while (my $data=$sth->fetchrow_hashref){
59                 push(@results,$data);
60                 $cnt++;
61         }
62         $sth->finish;
63         return ($cnt,\@results);
64 }
65
66 my $input = new CGI;
67 my $searchfield=$input->param('searchfield');
68 my $offset=$input->param('offset');
69 my $script_name="/cgi-bin/koha/admin/letter.pl";
70 my $code=$input->param('code');
71 my $module = $input->param('module');
72 my $pagesize=20;
73 my $op = $input->param('op');
74 $searchfield=~ s/\,//g;
75 my $dbh = C4::Context->dbh;
76
77 my ($template, $borrowernumber, $cookie)
78     = get_template_and_user({template_name => "tools/letter.tmpl",
79                              query => $input,
80                              type => "intranet",
81                              authnotrequired => 0,
82                              flagsrequired => {tools => 1},
83                              debug => 1,
84                              });
85
86 if ($op) {
87         $template->param(script_name => $script_name, $op  => 1); # we show only the TMPL_VAR names $op
88 } else {
89         $template->param(script_name => $script_name, else => 1); # we show only the TMPL_VAR names $op
90 }
91
92 $template->param(action => $script_name);
93 ################## ADD_FORM ##################################
94 # called by default. Used to create form to add or  modify a record
95 if ($op eq 'add_form') {
96         #---- if primkey exists, it's a modify action, so read values to modify...
97         my $letter;
98         if ($code) {
99                 my $sth=$dbh->prepare("select * from letter where module=? and code=?");
100                 $sth->execute($module,$code);
101                 $letter=$sth->fetchrow_hashref;
102                 $sth->finish;
103         }
104         # build field list
105         my @SQLfieldname;
106         foreach(qw(LibrarianFirstname LibrarianSurname LibrarianEmailaddress)) {
107                 my %line = ('value' => $_, 'text' => $_);
108                 push @SQLfieldname, \%line;
109         }
110
111         foreach(qw(branches biblio biblioitems)) {
112                 my $sth2=$dbh->prepare("SHOW COLUMNS from $_");
113                 $sth2->execute;
114                 my %line = ('value' => "", 'text' => '---' . uc($_) . '---');
115                 push @SQLfieldname, \%line;
116                 while ((my $field) = $sth2->fetchrow_array) {
117                         %line = ('value' => "$_.".$field, 'text' => "$_.".$field);
118                         push @SQLfieldname, \%line;
119                 }
120         }
121
122         my %line = ('value' => "",              'text' => '---ITEMS---');
123         push @SQLfieldname, \%line;
124         %line = ('value' => "items.content", 'text' => 'items.content');
125         push @SQLfieldname, \%line;
126         
127         my $sth2=$dbh->prepare("SHOW COLUMNS from borrowers");
128         $sth2->execute;
129         %line = ('value' => "",              'text' => '---BORROWERS---');
130         push @SQLfieldname, \%line;
131         while ((my $field) = $sth2->fetchrow_array) {
132                 %line = ('value' => "borrowers.".$field, 'text' => "borrowers.".$field);
133                 push @SQLfieldname, \%line;
134         }
135         if ($code) {
136             $template->param(modify => 1);
137             $template->param(code => $letter->{code});
138         } else {
139             $template->param(adding => 1);
140         }
141         $template->param(name => $letter->{name},
142                                         title => $letter->{title},
143                                         content => $letter->{content},
144                                         $letter->{module} => 1,
145                                         SQLfieldname => \@SQLfieldname,);
146                                                                                                         # END $OP eq ADD_FORM
147 ################## ADD_VALIDATE ##################################
148 # called by add_form, used to insert/modify data in DB
149 } elsif ($op eq 'add_validate') {
150         my $dbh = C4::Context->dbh;
151         my $sth=$dbh->prepare("replace letter (module,code,name,title,content) values (?,?,?,?,?)");
152         $sth->execute(map {$input->param('module')} qw(code name title content));
153         $sth->finish;
154         print $input->redirect("letter.pl");
155         exit;
156 ################## DELETE_CONFIRM ##################################
157 # called by default form, used to confirm deletion of data in DB
158 } elsif ($op eq 'delete_confirm') {
159         my $dbh = C4::Context->dbh;
160         my $sth=$dbh->prepare("select * from letter where code=?");
161         $sth->execute($code);
162         my $data=$sth->fetchrow_hashref;
163         $sth->finish;
164         $template->param(code => $code);
165         foreach(qw(module name content)) {
166                 $template->param($_ => $data->{$_});
167         }
168 ################## DELETE_CONFIRMED ##################################
169 # called by delete_confirm, used to effectively confirm deletion of data in DB
170 } elsif ($op eq 'delete_confirmed') {
171         my $dbh = C4::Context->dbh;
172         my $code=uc($input->param('code'));
173         my $module=$input->param('module');
174         my $sth=$dbh->prepare("delete from letter where module=? and code=?");
175         $sth->execute($module,$code);
176         $sth->finish;
177         print $input->redirect("letter.pl");
178         return;
179 ################## DEFAULT ##################################
180 } else { # DEFAULT
181         if  ($searchfield ne '') {
182                 $template->param(search => 1);
183                 $template->param(searchfield => $searchfield);
184         }
185         my ($count,$results)=StringSearch($searchfield,'web');
186         my $toggle="white";
187         my @loop_data =();
188         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
189                 $toggle = ($toggle eq 'white') ? "#ffffcc" : "white" ;
190                 my %row_data;
191                 $row_data{toggle} = $toggle;
192                 foreach (qw(module code name)) {
193                         $row_data{$_} = $results->[$i]{$_};
194                 }
195                 push(@loop_data, \%row_data);
196         }
197         $template->param(letter => \@loop_data);
198 } #---- END $OP eq DEFAULT
199
200 output_html_with_http_headers $input, $cookie, $template->output;
201