bug 2287: use defaults when needed even if CSV has correct number of columns
[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 # letter_exists($module, $code)
82 # - return true if a letter with the given $module and $code exists
83 sub letter_exists {
84     my ($module, $code) = @_;
85     my $dbh = C4::Context->dbh;
86     my $sql = q{SELECT * FROM letter WHERE module = ? AND code = ?};
87     my $letters = $dbh->selectall_arrayref($sql, { Slice => {} }, $module, $code);
88     return scalar(@$letters);
89 }
90
91 # $protected_letters = protected_letters()
92 # - return a hashref of letter_codes representing letters that should never be deleted
93 sub protected_letters {
94     my $dbh = C4::Context->dbh;
95     my $sql = q{SELECT DISTINCT letter_code FROM message_transports};
96     my $codes = $dbh->selectall_arrayref($sql);
97     return { map { $_->[0] => 1 } @$codes };
98 }
99
100 my $input       = new CGI;
101 my $searchfield = $input->param('searchfield');
102 $searchfield = '' unless defined($searchfield);
103 # my $offset      = $input->param('offset'); # pagination not implemented
104 my $script_name = "/cgi-bin/koha/tools/letter.pl";
105 my $code        = $input->param('code');
106 my $module      = $input->param('module');
107 $module = '' unless defined($module);
108 my $content     = $input->param('content');
109 my $op          = $input->param('op');
110 $op = '' unless defined($op);
111 $searchfield =~ s/\,//g;
112 my $dbh = C4::Context->dbh;
113
114 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
115     {
116         template_name   => "tools/letter.tmpl",
117         query           => $input,
118         type            => "intranet",
119         authnotrequired => 0,
120         flagsrequired   => { tools => 'edit_notices' },
121         debug           => 1,
122     }
123 );
124
125 if ($op) {
126         $template->param($op  => 1);
127 } else {
128         $template->param(else => 1);
129 }
130 # we show only the TMPL_VAR names $op
131
132 $template->param(
133         script_name => $script_name,
134         action => $script_name
135 );
136 ################## ADD_FORM ##################################
137 # called by default. Used to create form to add or  modify a record
138 if ( $op eq 'add_form' ) {
139
140     #---- if primkey exists, it's a modify action, so read values to modify...
141     my $letter;
142     if ($code) {
143         my $sth = $dbh->prepare("SELECT * FROM letter WHERE module=? AND code=?");
144         $sth->execute( $module, $code );
145         $letter = $sth->fetchrow_hashref;
146     }
147
148     # build field list
149     my @SQLfieldname;
150     foreach (qw(LibrarianFirstname LibrarianSurname LibrarianEmailaddress)) {
151         push @SQLfieldname, {value => $_, text => $_};
152     }
153     push @SQLfieldname, column_picks('branches');
154
155     # add acquisition specific tables
156     if ( $module eq "reserves" ) {
157         push @SQLfieldname, column_picks('borrowers'),
158                             column_picks('reserves'),
159                             column_picks('biblio'),
160                             column_picks('items');
161     }
162     elsif ( index( $module, "acquisition" ) > 0 ) {     # FIXME: imprecise comparison
163         push @SQLfieldname, column_picks('aqbooksellers'), column_picks('aqorders');
164         # add issues specific tables
165     }
166     elsif ( index( $module, "issues" ) > 0 ) {  # FIXME: imprecise comparison
167         push @SQLfieldname, column_picks('aqbooksellers'),
168             column_picks('serial'),
169             column_picks('subscription'),
170             {value => "", text => '---BIBLIO---'};
171                 foreach(qw(title author serial)) {
172                 push @SQLfieldname, {value => "biblio.$_", text => ucfirst($_) };
173                 }
174     }
175     else {
176         push @SQLfieldname, column_picks('biblio'),
177             column_picks('biblioitems'),
178             {value => "",              text => '---ITEMS---'  },
179             {value => "items.content", text => 'items.content'},
180             column_picks('borrowers');
181     }
182     if ($code) {
183         $template->param( modify => 1 );
184         $template->param( code   => $letter->{code} );
185     }
186     else {
187         $template->param( adding => 1 );
188     }
189     $template->param(
190         name    => $letter->{name},
191         title   => $letter->{title},
192         content => ( $content ? $content : $letter->{content} ),
193         ( $module ? $module : $letter->{module} ) => 1,
194         SQLfieldname => \@SQLfieldname,
195     );
196 ################## ADD_VALIDATE ##################################
197     # called by add_form, used to insert/modify data in DB
198 }
199 elsif ( $op eq 'add_validate' ) {
200     my $dbh     = C4::Context->dbh;
201     my $module  = $input->param('module');
202     my $code    = $input->param('code');
203     my $name    = $input->param('name');
204     my $title   = $input->param('title');
205     my $content = $input->param('content');
206     if (letter_exists($module, $code)) {
207         # UPDATE
208         $dbh->do(
209             q{UPDATE letter SET module = ?, code = ?, name = ?, title = ?, content = ? WHERE module = ? AND code = ?},
210             undef,
211             $module, $code, $name, $title, $content,
212             $module, $code
213         );
214     } else {
215         # INSERT
216         $dbh->do(
217             q{INSERT INTO letter (module,code,name,title,content) VALUES (?,?,?,?,?)},
218             undef,
219             $module, $code, $name, $title, $content
220         );
221     }
222     print $input->redirect("letter.pl");
223     exit;
224 ################## DELETE_CONFIRM ##################################
225     # called by default form, used to confirm deletion of data in DB
226 }
227 elsif ( $op eq 'delete_confirm' ) {
228     my $dbh = C4::Context->dbh;
229     my $sth = $dbh->prepare("SELECT * FROM letter WHERE code=?");
230     $sth->execute($code);
231     my $data = $sth->fetchrow_hashref;
232     $template->param( code => $code );
233         foreach (qw(module name content)) {
234         $template->param( $_ => $data->{$_} );
235         }
236 ################## DELETE_CONFIRMED ##################################
237   # called by delete_confirm, used to effectively confirm deletion of data in DB
238 }
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     print $input->redirect("/cgi-bin/koha/tools/letter.pl");
246     exit;
247 ################## DEFAULT ##################################
248 }
249 else {    # DEFAULT
250     if ( $searchfield ne '' ) {
251         $template->param( search      => 1 );
252         $template->param( searchfield => $searchfield );
253     }
254     my ($results) = StringSearch($searchfield);
255     my @loop_data = ();
256     my $protected_letters = protected_letters();
257     foreach my $result (@$results) {
258         my %row_data;
259         foreach my $key (qw(module code name)) {
260             $row_data{$key} = $result->{$key};
261             $row_data{'protected'} = $protected_letters->{$result->{code}};
262         }
263         push(@loop_data, \%row_data );
264     }
265     $template->param( letter => \@loop_data );
266 }    #---- END $OP eq DEFAULT
267
268 output_html_with_http_headers $input, $cookie, $template->output;
269