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
51 my ($searchstring) = @_;
52 my $dbh = C4::Context->dbh;
53 $searchstring =~ s/\'/\\\'/g;
54 my @data = split( ' ', $searchstring );
55 $data[0] = '' unless @data;
56 my $sth = $dbh->prepare("SELECT * FROM letter WHERE (code LIKE ?) ORDER BY module, code");
57 $sth->execute("$data[0]%"); # slightly bogus, only searching on first string.
58 return $sth->fetchall_arrayref({});
61 # FIXME untranslateable
63 aqbooksellers => 'BOOKSELLERS',
67 suggestions => 'SUGGESTIONS',
70 sub column_picks ($) {
71 # returns @array of values
72 my $table = shift or return ();
73 my $sth = C4::Context->dbh->prepare("SHOW COLUMNS FROM $table");
75 my @SQLfieldname = ();
76 push @SQLfieldname, {'value' => "", 'text' => '---' . uc($column_map{$table} || $table) . '---'};
77 while (my ($field) = $sth->fetchrow_array) {
79 value => $table . ".$field",
80 text => $table . ".$field"
86 # letter_exists($module, $code)
87 # - return true if a letter with the given $module and $code exists
89 my ($module, $code) = @_;
90 my $dbh = C4::Context->dbh;
91 my $letters = $dbh->selectall_arrayref(q{SELECT name FROM letter WHERE module = ? AND code = ?}, undef, $module, $code);
95 # $protected_letters = protected_letters()
96 # - return a hashref of letter_codes representing letters that should never be deleted
97 sub protected_letters {
98 my $dbh = C4::Context->dbh;
99 my $codes = $dbh->selectall_arrayref(q{SELECT DISTINCT letter_code FROM message_transports});
100 return { map { $_->[0] => 1 } @{$codes} };
104 my $searchfield = $input->param('searchfield');
105 my $script_name = '/cgi-bin/koha/tools/letter.pl';
106 my $code = $input->param('code');
107 my $module = $input->param('module');
108 my $content = $input->param('content');
109 my $op = $input->param('op');
110 my $dbh = C4::Context->dbh;
111 if (!defined $module ) {
115 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
117 template_name => 'tools/letter.tmpl',
120 authnotrequired => 0,
121 flagsrequired => { tools => 'edit_notices' },
127 $op = q{}; # silence errors from eq
129 # we show only the TMPL_VAR names $op
132 script_name => $script_name,
133 action => $script_name
136 if ($op eq 'add_form') {
137 add_form($module, $code);
139 elsif ( $op eq 'add_validate' ) {
141 $op = q{}; # next operation is to return to default screen
143 elsif ( $op eq 'delete_confirm' ) {
144 delete_confirm($module, $code);
146 elsif ( $op eq 'delete_confirmed' ) {
147 delete_confirmed($module, $code);
148 $op = q{}; # next operation is to return to default screen
151 default_display($searchfield);
154 # Do this last as delete_confirmed resets
156 $template->param($op => 1);
158 $template->param(no_op_set => 1);
161 output_html_with_http_headers $input, $cookie, $template->output;
164 my ($module, $code ) = @_;
167 # if code has been passed we can identify letter and its an update action
169 $letter = $dbh->selectrow_hashref(q{SELECT module, code, name, title, content FROM letter WHERE module=? AND code=?},
170 undef, $module, $code);
171 $template->param( modify => 1 );
172 $template->param( code => $letter->{code} );
174 else { # initialize the new fields
182 $template->param( adding => 1 );
185 # add acquisition specific tables
188 if ( $module eq "suggestions" ) {
189 push @SQLfieldname, column_picks('borrowers'),
190 column_picks('suggestions'),
191 column_picks('aqbooksellers'),
192 column_picks('biblio'),
193 column_picks('items');
195 elsif ( $module eq "reserves" ) {
196 push @SQLfieldname, column_picks('borrowers'),
197 column_picks('reserves'),
198 column_picks('biblio'),
199 column_picks('items');
201 elsif ( index( $module, "acquisition" ) > 0 ) { # FIXME: imprecise comparison
202 push @SQLfieldname, column_picks('aqbooksellers'), column_picks('aqorders');
203 # add issues specific tables
205 push @{$field_selection}, add_fields('branches');
206 if ($module eq 'reserves') {
207 push @{$field_selection}, add_fields('borrowers', 'reserves', 'biblio', 'items');
209 elsif ($module eq 'claimacquisition') {
210 push @{$field_selection}, add_fields('aqbooksellers', 'aqorders');
212 elsif ($module eq 'claimissues') {
213 push @{$field_selection}, add_fields('aqbooksellers', 'serial', 'subscription');
214 push @{$field_selection},
217 text => '---BIBLIO---'
219 foreach(qw(title author serial)) {
220 push @{$field_selection}, {value => "biblio.$_", text => ucfirst $_ };
224 push @{$field_selection}, add_fields('biblio','biblioitems'),
225 {value => q{}, text => '---ITEMS---' },
226 {value => 'items.content', text => 'items.content'},
227 add_fields('borrowers');
231 name => $letter->{name},
232 title => $letter->{title},
233 content => $letter->{content},
235 SQLfieldname => $field_selection,
241 my $dbh = C4::Context->dbh;
242 my $module = $input->param('module');
243 my $code = $input->param('code');
244 my $name = $input->param('name');
245 my $title = $input->param('title');
246 my $content = $input->param('content');
247 if (letter_exists($module, $code)) {
249 q{UPDATE letter SET module = ?, code = ?, name = ?, title = ?, content = ? WHERE module = ? AND code = ?},
251 $module, $code, $name, $title, $content,
256 q{INSERT INTO letter (module,code,name,title,content) VALUES (?,?,?,?,?)},
258 $module, $code, $name, $title, $content
261 # set up default display
267 my ($module, $code) = @_;
268 my $dbh = C4::Context->dbh;
269 my $letter = $dbh->selectrow_hashref(q|SELECT name FROM letter WHERE module = ? AND code = ?|,
272 $template->param( code => $code );
273 $template->param( module => $module);
274 $template->param( name => $letter->{name});
278 sub delete_confirmed {
279 my ($module, $code) = @_;
280 my $dbh = C4::Context->dbh;
281 $dbh->do('DELETE FROM letter WHERE module=? AND code=?',{},$module,$code);
282 # setup default display for screen
287 sub retrieve_letters {
288 my $searchstring = shift;
289 my $dbh = C4::Context->dbh;
291 if ($searchstring=~m/(\S+)/) {
292 $searchstring = $1 . q{%};
293 return $dbh->selectall_arrayref('SELECT module, code, name FROM letter WHERE code LIKE ? ORDER BY module, code',
294 { Slice => {} }, $searchstring);
298 return $dbh->selectall_arrayref('SELECT module, code, name FROM letter ORDER BY module, code', { Slice => {} });
303 sub default_display {
304 my $searchfield = shift;
306 if ( $searchfield ) {
307 $template->param( search => 1 );
308 $template->param( searchfield => $searchfield );
309 $results = retrieve_letters($searchfield);
311 $results = retrieve_letters();
314 my $protected_letters = protected_letters();
315 foreach my $row (@{$results}) {
316 $row->{protected} = $protected_letters->{ $row->{code}};
317 push @{$loop_data}, $row;
320 $template->param( letter => $loop_data );
328 for my $table (@tables) {
329 push @fields, get_columns_for($table);
335 sub get_columns_for {
337 # FIXME untranslateable
339 aqbooksellers => '---BOOKSELLERS---',
340 aqorders => '---ORDERS---',
341 serial => '---SERIALS---',
342 reserves => '---HOLDS---',
345 if (exists $column_map{$table} ) {
348 text => $column_map{$table} ,
352 my $tlabel = '---' . uc $table;
359 my $sql = "SHOW COLUMNS FROM $table";# TODO not db agnostic
360 my $table_prefix = $table . q|.|;
361 my $rows = C4::Context->dbh->selectall_arrayref($sql, { Slice => {} });
362 for my $row (@{$rows}) {
364 value => $table_prefix . $row->{Field},
365 text => $table_prefix . $row->{Field},