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