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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
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
50 # letter_exists($module, $code)
51 # - return true if a letter with the given $module and $code 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);
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} };
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 ) {
79 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
81 template_name
=> 'tools/letter.tmpl',
85 flagsrequired
=> { tools
=> 'edit_notices' },
91 $op = q{}; # silence errors from eq
93 # we show only the TMPL_VAR names $op
96 script_name
=> $script_name,
97 action
=> $script_name
100 if ($op eq 'add_form') {
101 add_form
($module, $code);
103 elsif ( $op eq 'add_validate' ) {
105 $op = q{}; # next operation is to return to default screen
107 elsif ( $op eq 'delete_confirm' ) {
108 delete_confirm
($module, $code);
110 elsif ( $op eq 'delete_confirmed' ) {
111 delete_confirmed
($module, $code);
112 $op = q{}; # next operation is to return to default screen
115 default_display
($searchfield);
118 # Do this last as delete_confirmed resets
120 $template->param($op => 1);
122 $template->param(no_op_set
=> 1);
125 output_html_with_http_headers
$input, $cookie, $template->output;
128 my ($module, $code ) = @_;
131 # if code has been passed we can identify letter and its an update action
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
} );
138 else { # initialize the new fields
146 $template->param( adding
=> 1 );
150 push @
{$field_selection}, add_fields
('branches');
151 if ($module eq 'reserves') {
152 push @
{$field_selection}, add_fields
('borrowers', 'reserves', 'biblio', 'items');
154 elsif ($module eq 'claimacquisition') {
155 push @
{$field_selection}, add_fields
('aqbooksellers', 'aqorders');
157 elsif ($module eq 'claimissues') {
158 push @
{$field_selection}, add_fields
('aqbooksellers', 'serial', 'subscription');
159 push @
{$field_selection},
162 text
=> '---BIBLIO---'
164 foreach(qw(title author serial)) {
165 push @
{$field_selection}, {value
=> "biblio.$_", text
=> ucfirst $_ };
168 elsif ($module eq 'suggestions') {
169 push @
{$field_selection}, add_fields
('suggestions', 'borrowers', 'biblio');
172 push @
{$field_selection}, add_fields
('biblio','biblioitems'),
173 {value
=> q{}, text
=> '---ITEMS---' },
174 {value
=> 'items.content', text
=> 'items.content'},
175 add_fields
('borrowers');
179 name
=> $letter->{name
},
180 title
=> $letter->{title
},
181 content
=> $letter->{content
},
183 SQLfieldname
=> $field_selection,
189 my $dbh = C4
::Context
->dbh;
190 my $module = $input->param('module');
191 my $code = $input->param('code');
192 my $name = $input->param('name');
193 my $title = $input->param('title');
194 my $content = $input->param('content');
195 if (letter_exists
($module, $code)) {
197 q{UPDATE letter SET module = ?, code = ?, name = ?, title = ?, content = ? WHERE module = ? AND code = ?},
199 $module, $code, $name, $title, $content,
204 q{INSERT INTO letter (module,code,name,title,content) VALUES (?,?,?,?,?)},
206 $module, $code, $name, $title, $content
209 # set up default display
215 my ($module, $code) = @_;
216 my $dbh = C4
::Context
->dbh;
217 my $letter = $dbh->selectrow_hashref(q
|SELECT name FROM letter WHERE module
= ? AND code
= ?
|,
220 $template->param( code
=> $code );
221 $template->param( module
=> $module);
222 $template->param( name
=> $letter->{name
});
226 sub delete_confirmed
{
227 my ($module, $code) = @_;
228 my $dbh = C4
::Context
->dbh;
229 $dbh->do('DELETE FROM letter WHERE module=? AND code=?',{},$module,$code);
230 # setup default display for screen
235 sub retrieve_letters
{
236 my $searchstring = shift;
237 my $dbh = C4
::Context
->dbh;
239 if ($searchstring=~m/(\S+)/) {
240 $searchstring = $1 . q{%};
241 return $dbh->selectall_arrayref('SELECT module, code, name FROM letter WHERE code LIKE ? ORDER BY module, code',
242 { Slice
=> {} }, $searchstring);
246 return $dbh->selectall_arrayref('SELECT module, code, name FROM letter ORDER BY module, code', { Slice
=> {} });
251 sub default_display
{
252 my $searchfield = shift;
254 if ( $searchfield ) {
255 $template->param( search
=> 1 );
256 $template->param( searchfield
=> $searchfield );
257 $results = retrieve_letters
($searchfield);
259 $results = retrieve_letters
();
262 my $protected_letters = protected_letters
();
263 foreach my $row (@
{$results}) {
264 $row->{protected
} = $protected_letters->{ $row->{code
}};
265 push @
{$loop_data}, $row;
268 $template->param( letter
=> $loop_data );
276 for my $table (@tables) {
277 push @fields, get_columns_for
($table);
283 sub get_columns_for
{
285 # FIXME untranslateable
287 aqbooksellers
=> '---BOOKSELLERS---',
288 aqorders
=> '---ORDERS---',
289 serial
=> '---SERIALS---',
290 reserves
=> '---HOLDS---',
291 suggestions
=> '---SUGGESTIONS---',
294 if (exists $column_map{$table} ) {
297 text
=> $column_map{$table} ,
301 my $tlabel = '---' . uc $table;
308 my $sql = "SHOW COLUMNS FROM $table";# TODO not db agnostic
309 my $table_prefix = $table . q
|.|;
310 my $rows = C4
::Context
->dbh->selectall_arrayref($sql, { Slice
=> {} });
311 for my $row (@
{$rows}) {
313 value
=> $table_prefix . $row->{Field
},
314 text
=> $table_prefix . $row->{Field
},