MT 2446 : Cancel authority edition for a field goes back to this field
[koha.git] / tools / letter.pl
blobab6039c6d2352fd3b8fd687d671d5954e0b7413d
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
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
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
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
43 use strict;
44 use warnings;
45 use CGI;
46 use C4::Auth;
47 use C4::Context;
48 use C4::Output;
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};
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} };
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{};
79 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
81 template_name => 'tools/letter.tmpl',
82 query => $input,
83 type => 'intranet',
84 authnotrequired => 0,
85 flagsrequired => { tools => 'edit_notices' },
86 debug => 1,
90 if (!defined $op) {
91 $op = q{}; # silence errors from eq
93 # we show only the TMPL_VAR names $op
95 $template->param(
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' ) {
104 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
114 else {
115 default_display($searchfield);
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);
125 output_html_with_http_headers $input, $cookie, $template->output;
127 sub add_form {
128 my ($module, $code ) = @_;
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} );
138 else { # initialize the new fields
139 $letter = {
140 module => $module,
141 code => q{},
142 name => q{},
143 title => q{},
144 content => q{},
146 $template->param( adding => 1 );
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');
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},
161 value => q{},
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');
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('borrowers');
178 $template->param(
179 name => $letter->{name},
180 title => $letter->{title},
181 content => $letter->{content},
182 $module => 1,
183 SQLfieldname => $field_selection,
185 return;
188 sub add_validate {
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)) {
196 $dbh->do(
197 q{UPDATE letter SET module = ?, code = ?, name = ?, title = ?, content = ? WHERE module = ? AND code = ?},
198 undef,
199 $module, $code, $name, $title, $content,
200 $module, $code
202 } else {
203 $dbh->do(
204 q{INSERT INTO letter (module,code,name,title,content) VALUES (?,?,?,?,?)},
205 undef,
206 $module, $code, $name, $title, $content
209 # set up default display
210 default_display();
211 return;
214 sub delete_confirm {
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 = ?|,
218 { Slice => {} },
219 $module, $code);
220 $template->param( code => $code );
221 $template->param( module => $module);
222 $template->param( name => $letter->{name});
223 return;
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
231 default_display();
232 return;
235 sub retrieve_letters {
236 my $searchstring = shift;
237 my $dbh = C4::Context->dbh;
238 if ($searchstring) {
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);
245 else {
246 return $dbh->selectall_arrayref('SELECT module, code, name FROM letter ORDER BY module, code', { Slice => {} });
248 return;
251 sub default_display {
252 my $searchfield = shift;
253 my $results;
254 if ( $searchfield ) {
255 $template->param( search => 1 );
256 $template->param( searchfield => $searchfield );
257 $results = retrieve_letters($searchfield);
258 } else {
259 $results = retrieve_letters();
261 my $loop_data = [];
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 );
269 return;
272 sub add_fields {
273 my @tables = @_;
274 my @fields = ();
276 for my $table (@tables) {
277 push @fields, get_columns_for($table);
280 return @fields;
283 sub get_columns_for {
284 my $table = shift;
285 # FIXME untranslateable
286 my %column_map = (
287 aqbooksellers => '---BOOKSELLERS---',
288 aqorders => '---ORDERS---',
289 serial => '---SERIALS---',
290 reserves => '---HOLDS---',
291 suggestions => '---SUGGESTIONS---',
293 my @fields = ();
294 if (exists $column_map{$table} ) {
295 push @fields, {
296 value => q{},
297 text => $column_map{$table} ,
300 else {
301 my $tlabel = '---' . uc $table;
302 $tlabel.= '---';
303 push @fields, {
304 value => q{},
305 text => $tlabel,
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}) {
312 push @fields, {
313 value => $table_prefix . $row->{Field},
314 text => $table_prefix . $row->{Field},
317 return @fields;