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
49 use C4::Branch; # GetBranches
51 use C4::Members::Attributes;
53 # _letter_from_where($branchcode,$module, $code, $mtt)
54 # - return FROM WHERE clause and bind args for a letter
55 sub _letter_from_where {
56 my ($branchcode, $module, $code, $mtt) = @_;
57 my $sql = q{FROM letter WHERE branchcode = ? AND module = ? AND code = ?};
58 $sql .= q{ AND message_transport_type = ?} if $mtt ne '*';
59 my @args = ( $branchcode || '', $module, $code, ($mtt ne '*' ? $mtt : ()) );
60 # Mysql is retarded. cause branchcode is part of the primary key it cannot be null. How does that
61 # work with foreign key constraint I wonder...
64 # $sql .= " AND branchcode = ?";
65 # push @args, $branchcode;
67 # $sql .= " AND branchcode IS NULL";
70 return ($sql, \@args);
73 # get_letters($branchcode,$module, $code, $mtt)
74 # - return letters with the given $branchcode, $module, $code and $mtt exists
76 my ($sql, $args) = _letter_from_where(@_);
77 my $dbh = C4::Context->dbh;
78 my $letter = $dbh->selectall_hashref("SELECT * $sql", 'message_transport_type', undef, @$args);
82 # $protected_letters = protected_letters()
83 # - return a hashref of letter_codes representing letters that should never be deleted
84 sub protected_letters {
85 my $dbh = C4::Context->dbh;
86 my $codes = $dbh->selectall_arrayref(q{SELECT DISTINCT letter_code FROM message_transports});
87 return { map { $_->[0] => 1 } @{$codes} };
91 my $searchfield = $input->param('searchfield');
92 my $script_name = '/cgi-bin/koha/tools/letter.pl';
93 our $branchcode = $input->param('branchcode');
94 my $code = $input->param('code');
95 my $module = $input->param('module') || '';
96 my $content = $input->param('content');
97 my $op = $input->param('op') || '';
98 my $dbh = C4::Context->dbh;
100 our ( $template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
102 template_name => 'tools/letter.tmpl',
105 authnotrequired => 0,
106 flagsrequired => { tools => 'edit_notices' },
111 our $my_branch = C4::Context->preference("IndependentBranches") && !$staffflags->{'superlibrarian'}
112 ? C4::Context->userenv()->{'branch'}
114 # we show only the TMPL_VAR names $op
117 independant_branch => $my_branch,
118 script_name => $script_name,
119 searchfield => $searchfield,
120 branchcode => $branchcode,
121 action => $script_name
129 if ($op eq 'add_form') {
130 add_form($branchcode, $module, $code);
132 elsif ( $op eq 'add_validate' ) {
134 $op = q{}; # next operation is to return to default screen
136 elsif ( $op eq 'delete_confirm' ) {
137 delete_confirm($branchcode, $module, $code);
139 elsif ( $op eq 'delete_confirmed' ) {
140 my $mtt = $input->param('message_transport_type');
141 delete_confirmed($branchcode, $module, $code, $mtt);
142 $op = q{}; # next operation is to return to default screen
145 default_display($branchcode,$searchfield);
148 # Do this last as delete_confirmed resets
150 $template->param($op => 1);
152 $template->param(no_op_set => 1);
155 output_html_with_http_headers $input, $cookie, $template->output;
158 my ( $branchcode,$module, $code ) = @_;
161 # if code has been passed we can identify letter and its an update action
163 $letters = get_letters($branchcode,$module, $code, '*');
166 my $message_transport_types = GetMessageTransportTypes();
170 for my $mtt ( @$message_transport_types ) {
175 branchcode => $branchcode,
176 name => $letters->{$mtt}{name},
182 message_transport_type => $mtt,
183 is_html => $letters->{$mtt}{is_html},
184 title => $letters->{$mtt}{title},
185 content => $letters->{$mtt}{content},
189 else { # initialize the new fields
190 for my $mtt ( @$message_transport_types ) {
191 $mtt = $mtt->{message_transport_type};
193 message_transport_type => $mtt,
197 branchcode => $branchcode,
200 $template->param( adding => 1 );
204 letters => \@letter_loop,
208 push @{$field_selection}, add_fields('branches');
209 if ($module eq 'reserves') {
210 push @{$field_selection}, add_fields('borrowers', 'reserves', 'biblio', 'items');
212 elsif ($module eq 'claimacquisition') {
213 push @{$field_selection}, add_fields('aqbooksellers', 'aqorders', 'biblio', 'biblioitems');
215 elsif ($module eq 'claimissues') {
216 push @{$field_selection}, add_fields('aqbooksellers', 'serial', 'subscription');
217 push @{$field_selection},
220 text => '---BIBLIO---'
222 foreach(qw(title author serial)) {
223 push @{$field_selection}, {value => "biblio.$_", text => ucfirst $_ };
226 elsif ($module eq 'suggestions') {
227 push @{$field_selection}, add_fields('suggestions', 'borrowers', 'biblio');
230 push @{$field_selection}, add_fields('biblio','biblioitems'),
232 {value => 'items.content', text => 'items.content'},
233 {value => 'items.fine', text => 'items.fine'},
234 add_fields('borrowers');
235 if ($module eq 'circulation') {
236 push @{$field_selection}, add_fields('opac_news');
240 if ( $module eq 'circulation' && $code eq "CHECKIN" ) {
241 push @{$field_selection}, add_fields('old_issues');
243 push @{$field_selection}, add_fields('issues');
249 branchloop => _branchloop($branchcode),
250 SQLfieldname => $field_selection,
256 my $dbh = C4::Context->dbh;
257 my $oldbranchcode = $input->param('oldbranchcode');
258 my $branchcode = $input->param('branchcode') || '';
259 my $module = $input->param('module');
260 my $oldmodule = $input->param('oldmodule');
261 my $code = $input->param('code');
262 my $name = $input->param('name');
263 my @mtt = $input->param('message_transport_type');
264 my @title = $input->param('title');
265 my @content = $input->param('content');
266 for my $mtt ( @mtt ) {
267 my $is_html = $input->param("is_html_$mtt");
268 my $title = shift @title;
269 my $content = shift @content;
270 my $letter = get_letters($oldbranchcode,$oldmodule, $code, $mtt);
271 unless ( $title and $content ) {
272 delete_confirmed( $oldbranchcode, $oldmodule, $code, $mtt );
275 if ( exists $letter->{$mtt} ) {
279 SET branchcode = ?, module = ?, name = ?, is_html = ?, title = ?, content = ?
280 WHERE branchcode = ? AND module = ? AND code = ? AND message_transport_type = ?
283 $branchcode, $module, $name, $is_html || 0, $title, $content,
284 $oldbranchcode, $oldmodule, $code, $mtt
288 q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES (?,?,?,?,?,?,?,?)},
290 $branchcode, $module, $code, $name, $is_html || 0, $title, $content, $mtt
294 # set up default display
295 default_display($branchcode);
299 my $dbh = C4::Context->dbh;
300 my $oldbranchcode = $input->param('oldbranchcode');
301 my $branchcode = $input->param('branchcode');
302 my $module = $input->param('module');
303 my $code = $input->param('code');
305 return if keys %{ get_letters($branchcode,$module, $code, '*') };
307 my $old_letters = get_letters($oldbranchcode,$module, $code, '*');
309 my $message_transport_types = GetMessageTransportTypes();
310 for my $mtt ( @$message_transport_types ) {
311 next unless exists $old_letters->{$mtt};
312 my $old_letter = $old_letters->{$mtt};
315 q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES (?,?,?,?,?,?,?,?)},
317 $branchcode, $module, $code, $old_letter->{name}, $old_letter->{is_html}, $old_letter->{title}, $old_letter->{content}, $mtt
323 my ($branchcode, $module, $code) = @_;
324 my $dbh = C4::Context->dbh;
325 my $letter = get_letters($branchcode, $module, $code, '*');
326 my @values = values %$letter;
328 branchcode => $branchcode,
329 branchname => GetBranchName($branchcode),
332 name => $values[0]->{name},
337 sub delete_confirmed {
338 my ($branchcode, $module, $code, $mtt) = @_;
339 my ($sql, $args) = _letter_from_where($branchcode, $module, $code, $mtt);
340 my $dbh = C4::Context->dbh;
341 $dbh->do("DELETE $sql", undef, @$args);
342 # setup default display for screen
343 default_display($branchcode);
347 sub retrieve_letters {
348 my ($branchcode, $searchstring) = @_;
350 $branchcode = $my_branch if $branchcode && $my_branch;
352 my $dbh = C4::Context->dbh;
353 my ($sql, @where, @args);
354 $sql = "SELECT branchcode, module, code, name, branchname
356 LEFT OUTER JOIN branches USING (branchcode)
358 if ($searchstring && $searchstring=~m/(\S+)/) {
359 $searchstring = $1 . q{%};
360 push @where, 'code LIKE ?';
361 push @args, $searchstring;
363 elsif ($branchcode) {
364 push @where, 'branchcode = ?';
365 push @args, $branchcode || '';
368 push @where, "(branchcode = ? OR branchcode = '')";
369 push @args, $my_branch;
372 $sql .= " WHERE ".join(" AND ", @where) if @where;
373 $sql .= " GROUP BY branchcode,module,code";
374 $sql .= " ORDER BY module, code, branchcode";
376 return $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
379 sub default_display {
380 my ($branchcode, $searchfield) = @_;
382 if ( $searchfield ) {
383 $template->param( search => 1 );
385 my $results = retrieve_letters($branchcode,$searchfield);
388 my $protected_letters = protected_letters();
389 foreach my $row (@{$results}) {
390 $row->{protected} = !$row->{branchcode} && $protected_letters->{ $row->{code} };
391 push @{$loop_data}, $row;
396 letter => $loop_data,
397 branchloop => _branchloop($branchcode),
402 my ($branchcode) = @_;
404 my $branches = GetBranches();
406 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
408 value => $thisbranch,
409 selected => $branchcode && $thisbranch eq $branchcode,
410 branchname => $branches->{$thisbranch}->{'branchname'},
421 for my $table (@tables) {
422 push @fields, get_columns_for($table);
428 sub get_columns_for {
430 # FIXME untranslateable
432 aqbooksellers => '---BOOKSELLERS---',
433 aqorders => '---ORDERS---',
434 serial => '---SERIALS---',
435 reserves => '---HOLDS---',
436 suggestions => '---SUGGESTIONS---',
439 if (exists $column_map{$table} ) {
442 text => $column_map{$table} ,
446 my $tlabel = '---' . uc $table;
454 my $sql = "SHOW COLUMNS FROM $table";# TODO not db agnostic
455 my $table_prefix = $table . q|.|;
456 my $rows = C4::Context->dbh->selectall_arrayref($sql, { Slice => {} });
457 for my $row (@{$rows}) {
458 next if $row->{'Field'} eq 'timestamp'; # this is really an irrelevant field and there may be other common fields that should be excluded from the list
460 value => $table_prefix . $row->{Field},
461 text => $table_prefix . $row->{Field},
464 if ($table eq 'borrowers') {
465 if ( my $attributes = C4::Members::Attributes::GetAttributes() ) {
466 foreach (@$attributes) {
468 value => "borrower-attribute:$_",
469 text => "attribute:$_",