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