bug 2870, fixes non-populating of guarantor fields
[koha.git] / tools / letter.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 =head1 tools/letter.pl
21
22  ALGO :
23  this script use an $op to know what to do.
24  if $op is empty or none of the above values,
25         - the default screen is build (with all records, or filtered datas).
26         - the   user can clic on add, modify or delete record.
27  if $op=add_form
28         - if primkey exists, this is a modification,so we read the $primkey record
29         - builds the add/modify form
30  if $op=add_validate
31         - the user has just send datas, so we create/modify the record
32  if $op=delete_form
33         - we show the record having primkey=$primkey and ask for deletion validation form
34  if $op=delete_confirm
35         - we delete the record having primkey=$primkey
36
37 =cut
38
39 use strict;
40 use warnings;
41 use CGI;
42 use C4::Auth;
43 use C4::Context;
44 use C4::Output;
45
46 sub StringSearch {
47     my ($searchstring) = @_;
48     my $dbh = C4::Context->dbh;
49     $searchstring =~ s/\'/\\\'/g;
50     my @data = split( ' ', $searchstring );
51     $data[0] = '' unless @data;
52     my $sth = $dbh->prepare("SELECT * FROM letter WHERE (code LIKE ?) ORDER BY module, code");
53     $sth->execute("$data[0]%");     # slightly bogus, only searching on first string.
54     return $sth->fetchall_arrayref({});
55 }
56
57 # FIXME untranslateable
58 our %column_map = (
59     aqbooksellers => 'BOOKSELLERS',
60     aqorders => 'ORDERS',
61     serial => 'SERIALS',
62     reserves => 'HOLDS',
63 );
64
65 sub column_picks ($) {
66     # returns @array of values
67     my $table = shift or return ();
68     my $sth = C4::Context->dbh->prepare("SHOW COLUMNS FROM $table");
69     $sth->execute;
70     my @SQLfieldname = ();
71     push @SQLfieldname, {'value' => "", 'text' => '---' . uc($column_map{$table} || $table) . '---'};
72     while (my ($field) = $sth->fetchrow_array) {
73         push @SQLfieldname, {
74             value => $table . ".$field",
75              text => $table . ".$field"
76         };
77     }
78     return @SQLfieldname;
79 }
80
81 my $input       = new CGI;
82 my $searchfield = $input->param('searchfield');
83 $searchfield = '' unless defined($searchfield);
84 # my $offset      = $input->param('offset'); # pagination not implemented
85 my $script_name = "/cgi-bin/koha/tools/letter.pl";
86 my $code        = $input->param('code');
87 my $module      = $input->param('module');
88 $module = '' unless defined($module);
89 my $content     = $input->param('content');
90 my $op          = $input->param('op');
91 $op = '' unless defined($op);
92 $searchfield =~ s/\,//g;
93 my $dbh = C4::Context->dbh;
94
95 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
96     {
97         template_name   => "tools/letter.tmpl",
98         query           => $input,
99         type            => "intranet",
100         authnotrequired => 0,
101         flagsrequired   => { tools => 'edit_notices' },
102         debug           => 1,
103     }
104 );
105
106 if ($op) {
107         $template->param($op  => 1);
108 } else {
109         $template->param(else => 1);
110 }
111 # we show only the TMPL_VAR names $op
112
113 $template->param(
114         script_name => $script_name,
115         action => $script_name
116 );
117 ################## ADD_FORM ##################################
118 # called by default. Used to create form to add or  modify a record
119 if ( $op eq 'add_form' ) {
120
121     #---- if primkey exists, it's a modify action, so read values to modify...
122     my $letter;
123     if ($code) {
124         my $sth = $dbh->prepare("SELECT * FROM letter WHERE module=? AND code=?");
125         $sth->execute( $module, $code );
126         $letter = $sth->fetchrow_hashref;
127     }
128
129     # build field list
130     my @SQLfieldname;
131     foreach (qw(LibrarianFirstname LibrarianSurname LibrarianEmailaddress)) {
132         push @SQLfieldname, {value => $_, text => $_};
133     }
134     push @SQLfieldname, column_picks('branches');
135
136     # add acquisition specific tables
137     if ( $module eq "reserves" ) {
138         push @SQLfieldname, column_picks('borrowers'),
139                             column_picks('reserves'),
140                             column_picks('biblio'),
141                             column_picks('items');
142     }
143     elsif ( index( $module, "acquisition" ) > 0 ) {     # FIXME: imprecise comparison
144         push @SQLfieldname, column_picks('aqbooksellers'), column_picks('aqorders');
145         # add issues specific tables
146     }
147     elsif ( index( $module, "issues" ) > 0 ) {  # FIXME: imprecise comparison
148         push @SQLfieldname, column_picks('aqbooksellers'),
149             column_picks('serial'),
150             column_picks('subscription'),
151             {value => "", text => '---BIBLIO---'};
152                 foreach(qw(title author serial)) {
153                 push @SQLfieldname, {value => "biblio.$_", text => ucfirst($_) };
154                 }
155     }
156     else {
157         push @SQLfieldname, column_picks('biblio'),
158             column_picks('biblioitems'),
159             {value => "",              text => '---ITEMS---'  },
160             {value => "items.content", text => 'items.content'},
161             column_picks('borrowers');
162     }
163     if ($code) {
164         $template->param( modify => 1 );
165         $template->param( code   => $letter->{code} );
166     }
167     else {
168         $template->param( adding => 1 );
169     }
170     $template->param(
171         name    => $letter->{name},
172         title   => $letter->{title},
173         content => ( $content ? $content : $letter->{content} ),
174         ( $module ? $module : $letter->{module} ) => 1,
175         SQLfieldname => \@SQLfieldname,
176     );
177 ################## ADD_VALIDATE ##################################
178     # called by add_form, used to insert/modify data in DB
179 }
180 elsif ( $op eq 'add_validate' ) {
181     my $dbh = C4::Context->dbh;
182     my $sth = $dbh->prepare(
183         "REPLACE letter (module,code,name,title,content) VALUES (?,?,?,?,?)");
184     $sth->execute(
185         $input->param('module'), $input->param('code'),
186         $input->param('name'),   $input->param('title'),
187         $input->param('content')
188     );
189     print $input->redirect("letter.pl");
190     exit;
191 ################## DELETE_CONFIRM ##################################
192     # called by default form, used to confirm deletion of data in DB
193 }
194 elsif ( $op eq 'delete_confirm' ) {
195     my $dbh = C4::Context->dbh;
196     my $sth = $dbh->prepare("SELECT * FROM letter WHERE code=?");
197     $sth->execute($code);
198     my $data = $sth->fetchrow_hashref;
199     $template->param( code => $code );
200         foreach (qw(module name content)) {
201         $template->param( $_ => $data->{$_} );
202         }
203 ################## DELETE_CONFIRMED ##################################
204   # called by delete_confirm, used to effectively confirm deletion of data in DB
205 }
206 elsif ( $op eq 'delete_confirmed' ) {
207     my $dbh    = C4::Context->dbh;
208     my $code   = uc( $input->param('code') );
209     my $module = $input->param('module');
210     my $sth    = $dbh->prepare("DELETE FROM letter WHERE module=? AND code=?");
211     $sth->execute( $module, $code );
212     print $input->redirect("/cgi-bin/koha/tools/letter.pl");
213     exit;
214 ################## DEFAULT ##################################
215 }
216 else {    # DEFAULT
217     if ( $searchfield ne '' ) {
218         $template->param( search      => 1 );
219         $template->param( searchfield => $searchfield );
220     }
221     my ($results) = StringSearch($searchfield);
222     my @loop_data = ();
223     foreach my $result (@$results) {
224         my %row_data;
225                 foreach my $key (qw(module code name)) {
226                 $row_data{$key} = $result->{$key};
227                 }
228         push(@loop_data, \%row_data );
229     }
230     $template->param( letter => \@loop_data );
231 }    #---- END $OP eq DEFAULT
232
233 output_html_with_http_headers $input, $cookie, $template->output;
234