basket header management
[koha.git] / tools / letter.pl
blob94fc2f5879ac5216a6feaa9611ea8b0ca10acb20
1 #!/usr/bin/perl
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
10 # version.
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
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
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
42 use strict;
43 use warnings;
44 use CGI;
45 use C4::Auth;
46 use C4::Context;
47 use C4::Output;
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};
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} };
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{};
78 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
80 template_name => 'tools/letter.tmpl',
81 query => $input,
82 type => 'intranet',
83 authnotrequired => 0,
84 flagsrequired => { tools => 'edit_notices' },
85 debug => 1,
89 if (!defined $op) {
90 $op = q{}; # silence errors from eq
92 # we show only the TMPL_VAR names $op
94 $template->param(
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' ) {
103 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
113 else {
114 default_display($searchfield);
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);
124 output_html_with_http_headers $input, $cookie, $template->output;
126 sub add_form {
127 my ($module, $code ) = @_;
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} );
137 else { # initialize the new fields
138 $letter = {
139 module => $module,
140 code => q{},
141 name => q{},
142 title => q{},
143 content => q{},
145 $template->param( adding => 1 );
148 # build field list
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},
174 value => q{},
175 text => '---BIBLIO---'
177 foreach(qw(title author serial)) {
178 push @{$field_selection}, {value => "biblio.$_", text => ucfirst $_ };
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');
188 $template->param(
189 name => $letter->{name},
190 title => $letter->{title},
191 content => $letter->{content},
192 $module => 1,
193 SQLfieldname => $field_selection,
195 return;
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
212 } else {
213 $dbh->do(
214 q{INSERT INTO letter (module,code,name,title,content) VALUES (?,?,?,?,?)},
215 undef,
216 $module, $code, $name, $title, $content
219 # set up default display
220 default_display();
221 return;
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;
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;
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);
255 else {
256 return $dbh->selectall_arrayref('SELECT module, code, name FROM letter ORDER BY module, code', { Slice => {} });
258 return;
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();
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;
278 $template->param( letter => $loop_data );
279 return;
282 sub add_fields {
283 my @tables = @_;
284 my @fields = ();
286 for my $table (@tables) {
287 push @fields, get_columns_for($table);
290 return @fields;
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---',
302 my @fields = ();
303 if (exists $column_map{$table} ) {
304 push @fields, {
305 value => q{},
306 text => $column_map{$table} ,
309 else {
310 my $tlabel = '---' . uc $table;
311 $tlabel.= '---';
312 push @fields, {
313 value => q{},
314 text => $tlabel,
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},
326 return @fields;