Letters / alert system, continuing...
[koha-ffzg.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::Date;
43 use C4::Auth;
44 use C4::Context;
45 use C4::Output;
46 use C4::Interface::CGI::Output;
47 use C4::Search;
48 use HTML::Template;
49
50 sub StringSearch  {
51         my ($env,$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 letter where (code like ?) order by module,code");
57         $sth->execute("$data[0]%");
58         my @results;
59         my $cnt=0;
60         while (my $data=$sth->fetchrow_hashref){
61         push(@results,$data);
62         $cnt ++;
63         }
64         #  $sth->execute;
65         $sth->finish;
66         return ($cnt,\@results);
67 }
68
69 my $input = new CGI;
70 my $searchfield=$input->param('searchfield');
71 my $offset=$input->param('offset');
72 my $script_name="/cgi-bin/koha/admin/letter.pl";
73 my $code=$input->param('code');
74 my $module = $input->param('module');
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 => "parameters/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 $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
112         $sth2->execute;
113         my %line = ('value' => "", 'text' => '---BIBLIO---');
114         push @SQLfieldname, \%line;
115         while ((my $field) = $sth2->fetchrow_array) {
116                 my %line = ('value' => "biblio.".$field, 'text' => "biblio.".$field);
117                 push @SQLfieldname, \%line;
118         }
119         my $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
120         $sth2->execute;
121         my %line = ('value' => "", 'text' => '---BIBLIOITEMS---');
122         push @SQLfieldname, \%line;
123         while ((my $field) = $sth2->fetchrow_array) {
124                 my %line = ('value' => "biblioitems.".$field, 'text' => "biblioitems.".$field);
125                 push @SQLfieldname, \%line;
126         }
127         my %line = ('value' => "", 'text' => '---ITEMS---');
128         push @SQLfieldname, \%line;
129         my %line = ('value' => "items.content", 'text' => 'items.content');
130         push @SQLfieldname, \%line;
131         
132         my $sth2=$dbh->prepare("SHOW COLUMNS from borrowers");
133         $sth2->execute;
134         my %line = ('value' => "", 'text' => '---BORROWERS---');
135         push @SQLfieldname, \%line;
136         while ((my $field) = $sth2->fetchrow_array) {
137                 my %line = ('value' => "borrowers.".$field, 'text' => "borrowers.".$field);
138                 push @SQLfieldname, \%line;
139         }
140         if ($code) {
141             $template->param(modify => 1);
142             $template->param(code => $letter->{code});
143         } else {
144             $template->param(adding => 1);
145         }
146         $template->param(name => $letter->{name},title => $letter->{title},
147                                         content => $letter->{content},
148                                         $letter->{module} => 1,
149                                         SQLfieldname => \@SQLfieldname,);
150                                                                                                         # END $OP eq ADD_FORM
151 ################## ADD_VALIDATE ##################################
152 # called by add_form, used to insert/modify data in DB
153 } elsif ($op eq 'add_validate') {
154         my $dbh = C4::Context->dbh;
155         my $sth=$dbh->prepare("replace letter (module,code,name,title,content) values (?,?,?,?,?)");
156         $sth->execute($input->param('module'),$input->param('code'),$input->param('name'),$input->param('title'),$input->param('content'));
157         $sth->finish;
158          print $input->redirect("letter.pl");
159          exit;
160 # END $OP eq ADD_VALIDATE
161 ################## DELETE_CONFIRM ##################################
162 # called by default form, used to confirm deletion of data in DB
163 } elsif ($op eq 'delete_confirm') {
164         my $dbh = C4::Context->dbh;
165         my $sth=$dbh->prepare("select * from letter where code=?");
166         $sth->execute($code);
167         my $data=$sth->fetchrow_hashref;
168         $sth->finish;
169         $template->param(module => $data->{module});
170         $template->param(code => $code);
171         $template->param(name => $data->{'name'});
172         $template->param(content => $data->{'content'});
173                                                                                                         # END $OP eq DELETE_CONFIRM
174 ################## DELETE_CONFIRMED ##################################
175 # called by delete_confirm, used to effectively confirm deletion of data in DB
176 } elsif ($op eq 'delete_confirmed') {
177         my $dbh = C4::Context->dbh;
178         my $code=uc($input->param('code'));
179         my $module=$input->param('module');
180         my $sth=$dbh->prepare("delete from letter where module=? and code=?");
181         $sth->execute($module,$code);
182         $sth->finish;
183          print $input->redirect("letter.pl");
184          return;
185                                                                                                         # END $OP eq DELETE_CONFIRMED
186 ################## DEFAULT ##################################
187 } else { # DEFAULT
188         if  ($searchfield ne '') {
189                 $template->param(search => 1);
190                 $template->param(searchfield => $searchfield);
191         }
192         my $env;
193         my ($count,$results)=StringSearch($env,$searchfield,'web');
194         my $toggle="white";
195         my @loop_data =();
196         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
197                 if ($toggle eq 'white'){
198                         $toggle="#ffffcc";
199                 } else {
200                         $toggle="white";
201                 }
202            my %row_data;
203            $row_data{toggle} = $toggle;
204            $row_data{module} = $results->[$i]{'module'};
205            $row_data{code} = $results->[$i]{'code'};
206            $row_data{name} = $results->[$i]{'name'};
207            push(@loop_data, \%row_data);
208         }
209         $template->param(letter => \@loop_data);
210 } #---- END $OP eq DEFAULT
211
212 output_html_with_http_headers $input, $cookie, $template->output;
213