3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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
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.
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
20 =head1 tools/letter.pl
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
29 - if primary key (module + code) exists, this is a modification,so we read the required record
30 - builds the add/modify form
32 - the user has just send data, so we create/modify the record
34 - we show the record selected and ask for confirmation
36 - we delete the designated record
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
49 # letter_exists($module, $code)
50 # - return true if a letter with the given $module and $code 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);
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} };
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 ) {
78 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
80 template_name
=> 'tools/letter.tmpl',
84 flagsrequired
=> { tools
=> 'edit_notices' },
90 $op = q{}; # silence errors from eq
92 # we show only the TMPL_VAR names $op
95 script_name
=> $script_name,
96 action
=> $script_name
99 if ($op eq 'add_form') {
100 add_form
($module, $code);
102 elsif ( $op eq 'add_validate' ) {
104 $op = q{}; # next operation is to return to default screen
106 elsif ( $op eq 'delete_confirm' ) {
107 delete_confirm
($module, $code);
109 elsif ( $op eq 'delete_confirmed' ) {
110 delete_confirmed
($module, $code);
111 $op = q{}; # next operation is to return to default screen
114 default_display
($searchfield);
117 # Do this last as delete_confirmed resets
119 $template->param($op => 1);
121 $template->param(no_op_set
=> 1);
124 output_html_with_http_headers
$input, $cookie, $template->output;
127 my ($module, $code ) = @_;
130 # if code has been passed we can identify letter and its an update action
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
} );
137 else { # initialize the new fields
145 $template->param( adding
=> 1 );
149 my $field_selection = [
151 value
=> 'LibrarianFirstname',
152 text
=> 'LibrarianFirstname',
155 value
=> 'LibrarianSurname',
156 text
=> 'LibrarianSurname',
159 value
=> 'LibrarianEmailaddress',
160 text
=> 'LibrarianEmailaddress',
163 push @
{$field_selection}, add_fields
('branches');
164 if ($module eq 'reserves') {
165 push @
{$field_selection}, add_fields
('borrowers', 'reserves', 'biblio', 'items');
167 elsif ($module eq 'claimacquisition') {
168 push @
{$field_selection}, add_fields
('aqbooksellers', 'aqorders');
170 elsif ($module eq 'claimissues') {
171 push @
{$field_selection}, add_fields
('aqbooksellers', 'serial', 'subscription');
172 push @
{$field_selection},
175 text
=> '---BIBLIO---'
177 foreach(qw(title author serial)) {
178 push @
{$field_selection}, {value
=> "biblio.$_", text
=> ucfirst $_ };
182 push @
{$field_selection}, add_fields
('biblio','biblioitems'),
183 {value
=> q{}, text
=> '---ITEMS---' },
184 {value
=> 'items.content', text
=> 'items.content'},
185 add_fields
('borrowers');
189 name
=> $letter->{name
},
190 title
=> $letter->{title
},
191 content
=> $letter->{content
},
193 SQLfieldname
=> $field_selection,
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)) {
207 q{UPDATE letter SET module = ?, code = ?, name = ?, title = ?, content = ? WHERE module = ? AND code = ?},
209 $module, $code, $name, $title, $content,
214 q{INSERT INTO letter (module,code,name,title,content) VALUES (?,?,?,?,?)},
216 $module, $code, $name, $title, $content
219 # set up default display
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
= ?
|,
230 $template->param( code
=> $code );
231 $template->param( module
=> $module);
232 $template->param( name
=> $letter->{name
});
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
245 sub retrieve_letters
{
246 my $searchstring = shift;
247 my $dbh = C4
::Context
->dbh;
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);
256 return $dbh->selectall_arrayref('SELECT module, code, name FROM letter ORDER BY module, code', { Slice
=> {} });
261 sub default_display
{
262 my $searchfield = shift;
264 if ( $searchfield ) {
265 $template->param( search
=> 1 );
266 $template->param( searchfield
=> $searchfield );
267 $results = retrieve_letters
($searchfield);
269 $results = retrieve_letters
();
272 my $protected_letters = protected_letters
();
273 foreach my $row (@
{$results}) {
274 $row->{protected
} = $protected_letters->{ $row->{code
}};
275 push @
{$loop_data}, $row;
278 $template->param( letter
=> $loop_data );
286 for my $table (@tables) {
287 push @fields, get_columns_for
($table);
293 sub get_columns_for
{
295 # FIXME untranslateable
297 aqbooksellers
=> '---BOOKSELLERS---',
298 aqorders
=> '---ORDERS---',
299 serial
=> '---SERIALS---',
300 reserves
=> '---HOLDS---',
303 if (exists $column_map{$table} ) {
306 text
=> $column_map{$table} ,
310 my $tlabel = '---' . uc $table;
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}) {
322 value
=> $table_prefix . $row->{Field
},
323 text
=> $table_prefix . $row->{Field
},