Using warn instead of die
[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 values listed below,
25         - the default screen is built (with all or filtered (if search string is set) records).
26         - the   user can click on add, modify or delete record.
27     - filtering is done on the code field
28  if $op=add_form
29         - if primary key (module + code) exists, this is a modification,so we read the required record
30         - builds the add/modify form
31  if $op=add_validate
32         - the user has just send data, so we create/modify the record
33  if $op=delete_form
34         - we show the record selected and ask for confirmation
35  if $op=delete_confirm
36         - we delete the designated record
37
38 =cut
39 # TODO This script drives the CRUD operations on the letter table
40 # The DB interaction should be handled by calls to C4/Letters.pm
41
42 use strict;
43 use warnings;
44 use CGI;
45 use C4::Auth;
46 use C4::Context;
47 use C4::Output;
48
49 # letter_exists($module, $code)
50 # - return true if a letter with the given $module and $code exists
51 sub letter_exists {
52     my ($module, $code) = @_;
53     my $dbh = C4::Context->dbh;
54     my $letters = $dbh->selectall_arrayref(q{SELECT name FROM letter WHERE module = ? AND code = ?}, undef, $module, $code);
55     return @{$letters};
56 }
57
58 # $protected_letters = protected_letters()
59 # - return a hashref of letter_codes representing letters that should never be deleted
60 sub protected_letters {
61     my $dbh = C4::Context->dbh;
62     my $codes = $dbh->selectall_arrayref(q{SELECT DISTINCT letter_code FROM message_transports});
63     return { map { $_->[0] => 1 } @{$codes} };
64 }
65
66 my $input       = new CGI;
67 my $searchfield = $input->param('searchfield');
68 my $script_name = '/cgi-bin/koha/tools/letter.pl';
69 my $code        = $input->param('code');
70 my $module      = $input->param('module');
71 my $content     = $input->param('content');
72 my $op          = $input->param('op');
73 my $dbh = C4::Context->dbh;
74 if (!defined $module ) {
75     $module = q{};
76 }
77
78 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
79     {
80         template_name   => 'tools/letter.tmpl',
81         query           => $input,
82         type            => 'intranet',
83         authnotrequired => 0,
84         flagsrequired   => { tools => 'edit_notices' },
85         debug           => 1,
86     }
87 );
88
89 if (!defined $op) {
90     $op = q{}; # silence errors from eq
91 }
92 # we show only the TMPL_VAR names $op
93
94 $template->param(
95         script_name => $script_name,
96         action => $script_name
97 );
98
99 if ($op eq 'add_form') {
100     add_form($module, $code);
101 }
102 elsif ( $op eq 'add_validate' ) {
103     add_validate();
104     $op = q{}; # next operation is to return to default screen
105 }
106 elsif ( $op eq 'delete_confirm' ) {
107     delete_confirm($module, $code);
108 }
109 elsif ( $op eq 'delete_confirmed' ) {
110     delete_confirmed($module, $code);
111     $op = q{}; # next operation is to return to default screen
112 }
113 else {
114     default_display($searchfield);
115 }
116
117 # Do this last as delete_confirmed resets
118 if ($op) {
119     $template->param($op  => 1);
120 } else {
121     $template->param(no_op_set => 1);
122 }
123
124 output_html_with_http_headers $input, $cookie, $template->output;
125
126 sub add_form {
127     my ($module, $code ) = @_;
128
129     my $letter;
130     # if code has been passed we can identify letter and its an update action
131     if ($code) {
132         $letter = $dbh->selectrow_hashref(q{SELECT module, code, name, title, content FROM letter WHERE module=? AND code=?},
133             undef, $module, $code);
134         $template->param( modify => 1 );
135         $template->param( code   => $letter->{code} );
136     }
137     else { # initialize the new fields
138         $letter = {
139             module  => $module,
140             code    => q{},
141             name    => q{},
142             title   => q{},
143             content => q{},
144         };
145         $template->param( adding => 1 );
146     }
147
148     # build field list
149     my $field_selection = [
150     {
151         value => 'LibrarianFirstname',
152         text  => 'LibrarianFirstname',
153     },
154     {
155         value => 'LibrarianSurname',
156         text  => 'LibrarianSurname',
157     },
158     {
159         value => 'LibrarianEmailaddress',
160         text  => 'LibrarianEmailaddress',
161     }
162     ];
163     push @{$field_selection}, add_fields('branches');
164     if ($module eq 'reserves') {
165         push @{$field_selection}, add_fields('borrowers', 'reserves', 'biblio', 'items');
166     }
167     elsif ($module eq 'claimacquisition') {
168         push @{$field_selection}, add_fields('aqbooksellers', 'aqorders');
169     }
170     elsif ($module eq 'claimissues') {
171         push @{$field_selection}, add_fields('aqbooksellers', 'serial', 'subscription');
172         push @{$field_selection},
173         {
174             value => q{},
175             text => '---BIBLIO---'
176         };
177         foreach(qw(title author serial)) {
178             push @{$field_selection}, {value => "biblio.$_", text => ucfirst $_ };
179         }
180     }
181     else {
182         push @{$field_selection}, add_fields('biblio','biblioitems'),
183             {value => q{},             text => '---ITEMS---'  },
184             {value => 'items.content', text => 'items.content'},
185             add_fields('borrowers');
186     }
187
188     $template->param(
189         name    => $letter->{name},
190         title   => $letter->{title},
191         content => $letter->{content},
192         $module => 1,
193         SQLfieldname => $field_selection,
194     );
195     return;
196 }
197
198 sub add_validate {
199     my $dbh     = C4::Context->dbh;
200     my $module  = $input->param('module');
201     my $code    = $input->param('code');
202     my $name    = $input->param('name');
203     my $title   = $input->param('title');
204     my $content = $input->param('content');
205     if (letter_exists($module, $code)) {
206         $dbh->do(
207             q{UPDATE letter SET module = ?, code = ?, name = ?, title = ?, content = ? WHERE module = ? AND code = ?},
208             undef,
209             $module, $code, $name, $title, $content,
210             $module, $code
211         );
212     } else {
213         $dbh->do(
214             q{INSERT INTO letter (module,code,name,title,content) VALUES (?,?,?,?,?)},
215             undef,
216             $module, $code, $name, $title, $content
217         );
218     }
219     # set up default display
220     default_display();
221     return;
222 }
223
224 sub delete_confirm {
225     my ($module, $code) = @_;
226     my $dbh = C4::Context->dbh;
227     my $letter = $dbh->selectrow_hashref(q|SELECT  name FROM letter WHERE module = ? AND code = ?|,
228         { Slice => {} },
229         $module, $code);
230     $template->param( code => $code );
231     $template->param( module => $module);
232     $template->param( name => $letter->{name});
233     return;
234 }
235
236 sub delete_confirmed {
237     my ($module, $code) = @_;
238     my $dbh    = C4::Context->dbh;
239     $dbh->do('DELETE FROM letter WHERE module=? AND code=?',{},$module,$code);
240     # setup default display for screen
241     default_display();
242     return;
243 }
244
245 sub retrieve_letters {
246     my $searchstring = shift;
247     my $dbh = C4::Context->dbh;
248     if ($searchstring) {
249         if ($searchstring=~m/(\S+)/) {
250             $searchstring = $1 . q{%};
251             return $dbh->selectall_arrayref('SELECT module, code, name FROM letter WHERE code LIKE ? ORDER BY module, code',
252                 { Slice => {} }, $searchstring);
253         }
254     }
255     else {
256         return $dbh->selectall_arrayref('SELECT module, code, name FROM letter ORDER BY module, code', { Slice => {} });
257     }
258     return;
259 }
260
261 sub default_display {
262     my $searchfield = shift;
263     my $results;
264     if ( $searchfield  ) {
265         $template->param( search      => 1 );
266         $template->param( searchfield => $searchfield );
267         $results = retrieve_letters($searchfield);
268     } else {
269         $results = retrieve_letters();
270     }
271     my $loop_data = [];
272     my $protected_letters = protected_letters();
273     foreach my $row (@{$results}) {
274         $row->{protected} = $protected_letters->{ $row->{code}};
275         push @{$loop_data}, $row;
276
277     }
278     $template->param( letter => $loop_data );
279     return;
280 }
281
282 sub add_fields {
283     my @tables = @_;
284     my @fields = ();
285
286     for my $table (@tables) {
287         push @fields, get_columns_for($table);
288
289     }
290     return @fields;
291 }
292
293 sub get_columns_for {
294     my $table = shift;
295 # FIXME untranslateable
296     my %column_map = (
297         aqbooksellers => '---BOOKSELLERS---',
298         aqorders      => '---ORDERS---',
299         serial        => '---SERIALS---',
300         reserves      => '---HOLDS---',
301     );
302     my @fields = ();
303     if (exists $column_map{$table} ) {
304         push @fields, {
305             value => q{},
306             text  => $column_map{$table} ,
307         };
308     }
309     else {
310         my $tlabel = '---' . uc $table;
311         $tlabel.= '---';
312         push @fields, {
313             value => q{},
314             text  => $tlabel,
315         };
316     }
317     my $sql = "SHOW COLUMNS FROM $table";# TODO not db agnostic
318     my $table_prefix = $table . q|.|;
319     my $rows = C4::Context->dbh->selectall_arrayref($sql, { Slice => {} });
320     for my $row (@{$rows}) {
321         push @fields, {
322             value => $table_prefix . $row->{Field},
323             text  => $table_prefix . $row->{Field},
324         }
325     }
326     return @fields;
327 }