#
# This file is part of Koha.
#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
#
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
#
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
=head1 tools/letter.pl
use strict;
use warnings;
-use CGI;
+use CGI qw ( -utf8 );
use C4::Auth;
use C4::Context;
use C4::Output;
-
-# letter_exists($module, $code)
-# - return true if a letter with the given $module and $code exists
-sub letter_exists {
- my ($module, $code) = @_;
- my $dbh = C4::Context->dbh;
- my $letters = $dbh->selectall_arrayref(q{SELECT name FROM letter WHERE module = ? AND code = ?}, undef, $module, $code);
- return @{$letters};
-}
+use C4::Branch; # GetBranches
+use C4::Letters;
+use C4::Members::Attributes;
# $protected_letters = protected_letters()
# - return a hashref of letter_codes representing letters that should never be deleted
return { map { $_->[0] => 1 } @{$codes} };
}
-my $input = new CGI;
+our $input = new CGI;
my $searchfield = $input->param('searchfield');
my $script_name = '/cgi-bin/koha/tools/letter.pl';
+our $branchcode = $input->param('branchcode');
my $code = $input->param('code');
-my $module = $input->param('module');
+my $module = $input->param('module') || '';
my $content = $input->param('content');
-my $op = $input->param('op');
+my $op = $input->param('op') || '';
my $dbh = C4::Context->dbh;
-if (!defined $module ) {
- $module = q{};
-}
-my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
+our ( $template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
{
- template_name => 'tools/letter.tmpl',
+ template_name => 'tools/letter.tt',
query => $input,
type => 'intranet',
authnotrequired => 0,
}
);
-if (!defined $op) {
- $op = q{}; # silence errors from eq
-}
+our $my_branch = C4::Context->preference("IndependentBranches") && !$staffflags->{'superlibrarian'}
+ ? C4::Context->userenv()->{'branch'}
+ : undef;
# we show only the TMPL_VAR names $op
$template->param(
+ independant_branch => $my_branch,
script_name => $script_name,
+ searchfield => $searchfield,
+ branchcode => $branchcode,
action => $script_name
);
-if ($op eq 'add_form') {
- add_form($module, $code);
-}
-elsif ( $op eq 'add_validate' ) {
+if ( $op eq 'add_validate' or $op eq 'copy_validate' ) {
add_validate();
- $op = q{}; # next operation is to return to default screen
+ $op = q{}; # we return to the default screen for the next operation
+}
+if ($op eq 'copy_form') {
+ my $oldbranchcode = $input->param('oldbranchcode') || q||;
+ my $branchcode = $input->param('branchcode') || q||;
+ add_form($oldbranchcode, $module, $code);
+ $template->param(
+ oldbranchcode => $oldbranchcode,
+ branchcode => $branchcode,
+ branchloop => _branchloop($branchcode),
+ copying => 1,
+ modify => 0,
+ );
+}
+elsif ( $op eq 'add_form' ) {
+ add_form($branchcode, $module, $code);
}
elsif ( $op eq 'delete_confirm' ) {
- delete_confirm($module, $code);
+ delete_confirm($branchcode, $module, $code);
}
elsif ( $op eq 'delete_confirmed' ) {
- delete_confirmed($module, $code);
+ delete_confirmed($branchcode, $module, $code);
$op = q{}; # next operation is to return to default screen
}
else {
- default_display($searchfield);
+ default_display($branchcode,$searchfield);
}
# Do this last as delete_confirmed resets
output_html_with_http_headers $input, $cookie, $template->output;
sub add_form {
- my ($module, $code ) = @_;
+ my ( $branchcode,$module, $code ) = @_;
- my $letter;
+ my $letters;
# if code has been passed we can identify letter and its an update action
if ($code) {
- $letter = $dbh->selectrow_hashref(q{SELECT module, code, name, title, content FROM letter WHERE module=? AND code=?},
- undef, $module, $code);
- $template->param( modify => 1 );
- $template->param( code => $letter->{code} );
+ $letters = C4::Letters::GetLetterTemplates(
+ {
+ branchcode => $branchcode,
+ module => $module,
+ code => $code,
+ }
+ );
+ }
+
+ my $message_transport_types = GetMessageTransportTypes();
+ my @letter_loop;
+ if ($letters) {
+ $template->param(
+ modify => 1,
+ code => $code,
+ branchcode => $branchcode,
+ );
+ my $first_flag = 1;
+ # The letter name is contained into each mtt row.
+ # So we can only sent the first one to the template.
+ for my $mtt ( @$message_transport_types ) {
+ # The letter_name
+ if ( $first_flag and $letters->{$mtt}{name} ) {
+ $template->param(
+ letter_name=> $letters->{$mtt}{name},
+ );
+ $first_flag = 0;
+ }
+
+ push @letter_loop, {
+ message_transport_type => $mtt,
+ is_html => $letters->{$mtt}{is_html},
+ title => $letters->{$mtt}{title},
+ content => $letters->{$mtt}{content}//'',
+ };
+ }
}
else { # initialize the new fields
- $letter = {
- module => $module,
- code => q{},
- name => q{},
- title => q{},
- content => q{},
- };
+ for my $mtt ( @$message_transport_types ) {
+ push @letter_loop, {
+ message_transport_type => $mtt,
+ }
+ }
+ $template->param(
+ branchcode => $branchcode,
+ module => $module,
+ );
$template->param( adding => 1 );
}
+ $template->param(
+ letters => \@letter_loop,
+ );
+
my $field_selection;
push @{$field_selection}, add_fields('branches');
if ($module eq 'reserves') {
push @{$field_selection}, add_fields('borrowers', 'reserves', 'biblio', 'items');
}
+ elsif ( $module eq 'acquisition' ) {
+ push @{$field_selection}, add_fields('aqbooksellers', 'aqorders', 'biblio', 'items');
+ }
elsif ($module eq 'claimacquisition') {
- push @{$field_selection}, add_fields('aqbooksellers', 'aqorders');
+ push @{$field_selection}, add_fields('aqbooksellers', 'aqorders', 'biblio', 'biblioitems');
}
elsif ($module eq 'claimissues') {
push @{$field_selection}, add_fields('aqbooksellers', 'serial', 'subscription');
push @{$field_selection}, {value => "biblio.$_", text => ucfirst $_ };
}
}
+ elsif ($module eq 'serial') {
+ push @{$field_selection}, add_fields('branches', 'biblio', 'biblioitems', 'borrowers', 'subscription', 'serial');
+ }
elsif ($module eq 'suggestions') {
push @{$field_selection}, add_fields('suggestions', 'borrowers', 'biblio');
}
else {
push @{$field_selection}, add_fields('biblio','biblioitems'),
- {value => q{}, text => '---ITEMS---' },
+ add_fields('items'),
{value => 'items.content', text => 'items.content'},
- add_fields('issues','borrowers');
+ {value => 'items.fine', text => 'items.fine'},
+ add_fields('borrowers');
+ if ($module eq 'circulation') {
+ push @{$field_selection}, add_fields('opac_news');
+
+ }
+
+ if ( $module eq 'circulation' and $code and $code eq "CHECKIN" ) {
+ push @{$field_selection}, add_fields('old_issues');
+ } else {
+ push @{$field_selection}, add_fields('issues');
+ }
}
$template->param(
- name => $letter->{name},
- title => $letter->{title},
- content => $letter->{content},
- $module => 1,
- SQLfieldname => $field_selection,
+ module => $module,
+ branchloop => _branchloop($branchcode),
+ SQLfieldnames => $field_selection,
+ branchcode => $branchcode,
);
return;
}
sub add_validate {
- my $dbh = C4::Context->dbh;
- my $module = $input->param('module');
- my $code = $input->param('code');
- my $name = $input->param('name');
- my $title = $input->param('title');
- my $content = $input->param('content');
- if (letter_exists($module, $code)) {
- $dbh->do(
- q{UPDATE letter SET module = ?, code = ?, name = ?, title = ?, content = ? WHERE module = ? AND code = ?},
- undef,
- $module, $code, $name, $title, $content,
- $module, $code
- );
- } else {
- $dbh->do(
- q{INSERT INTO letter (module,code,name,title,content) VALUES (?,?,?,?,?)},
- undef,
- $module, $code, $name, $title, $content
- );
+ my $dbh = C4::Context->dbh;
+ my $branchcode = $input->param('branchcode') || '';
+ my $module = $input->param('module');
+ my $oldmodule = $input->param('oldmodule');
+ my $code = $input->param('code');
+ my $name = $input->param('name');
+ my @mtt = $input->param('message_transport_type');
+ my @title = $input->param('title');
+ my @content = $input->param('content');
+ for my $mtt ( @mtt ) {
+ my $is_html = $input->param("is_html_$mtt");
+ my $title = shift @title;
+ my $content = shift @content;
+ my $letter = C4::Letters::getletter( $oldmodule, $code, $branchcode, $mtt);
+
+ # getletter can return the default letter even if we pass a branchcode
+ # If we got the default one and we needed the specific one, we didn't get the one we needed!
+ if ( $letter and $branchcode ne $letter->{branchcode} ) {
+ $letter = undef;
+ }
+ unless ( $title and $content ) {
+ # Delete this mtt if no title or content given
+ delete_confirmed( $branchcode, $oldmodule, $code, $mtt );
+ next;
+ }
+ elsif ( $letter and $letter->{message_transport_type} eq $mtt ) {
+ $dbh->do(
+ q{
+ UPDATE letter
+ SET branchcode = ?, module = ?, name = ?, is_html = ?, title = ?, content = ?
+ WHERE branchcode = ? AND module = ? AND code = ? AND message_transport_type = ?
+ },
+ undef,
+ $branchcode, $module, $name, $is_html || 0, $title, $content,
+ $branchcode, $oldmodule, $code, $mtt
+ );
+ } else {
+ $dbh->do(
+ q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES (?,?,?,?,?,?,?,?)},
+ undef,
+ $branchcode, $module, $code, $name, $is_html || 0, $title, $content, $mtt
+ );
+ }
}
# set up default display
- default_display();
- return;
+ default_display($branchcode);
+ return 1;
}
sub delete_confirm {
- my ($module, $code) = @_;
+ my ($branchcode, $module, $code) = @_;
my $dbh = C4::Context->dbh;
- my $letter = $dbh->selectrow_hashref(q|SELECT name FROM letter WHERE module = ? AND code = ?|,
- { Slice => {} },
- $module, $code);
- $template->param( code => $code );
- $template->param( module => $module);
- $template->param( name => $letter->{name});
+ my $letter = C4::Letters::getletter($module, $code, $branchcode);
+ my @values = values %$letter;
+ $template->param(
+ letter => $letter,
+ );
return;
}
sub delete_confirmed {
- my ($module, $code) = @_;
- my $dbh = C4::Context->dbh;
- $dbh->do('DELETE FROM letter WHERE module=? AND code=?',{},$module,$code);
+ my ($branchcode, $module, $code, $mtt) = @_;
+ C4::Letters::DelLetter(
+ {
+ branchcode => $branchcode,
+ module => $module,
+ code => $code,
+ mtt => $mtt
+ }
+ );
# setup default display for screen
- default_display();
+ default_display($branchcode);
return;
}
sub retrieve_letters {
- my $searchstring = shift;
+ my ($branchcode, $searchstring) = @_;
+
+ $branchcode = $my_branch if $branchcode && $my_branch;
+
my $dbh = C4::Context->dbh;
- if ($searchstring) {
- if ($searchstring=~m/(\S+)/) {
- $searchstring = $1 . q{%};
- return $dbh->selectall_arrayref('SELECT module, code, name FROM letter WHERE code LIKE ? ORDER BY module, code',
- { Slice => {} }, $searchstring);
- }
+ my ($sql, @where, @args);
+ $sql = "SELECT branchcode, module, code, name, branchname
+ FROM letter
+ LEFT OUTER JOIN branches USING (branchcode)
+ ";
+ if ($searchstring && $searchstring=~m/(\S+)/) {
+ $searchstring = $1 . q{%};
+ push @where, 'code LIKE ?';
+ push @args, $searchstring;
}
- else {
- return $dbh->selectall_arrayref('SELECT module, code, name FROM letter ORDER BY module, code', { Slice => {} });
+ elsif ($branchcode) {
+ push @where, 'branchcode = ?';
+ push @args, $branchcode || '';
}
- return;
+ elsif ($my_branch) {
+ push @where, "(branchcode = ? OR branchcode = '')";
+ push @args, $my_branch;
+ }
+
+ $sql .= " WHERE ".join(" AND ", @where) if @where;
+ $sql .= " GROUP BY branchcode,module,code";
+ $sql .= " ORDER BY module, code, branchcode";
+
+ return $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
}
sub default_display {
- my $searchfield = shift;
- my $results;
+ my ($branchcode, $searchfield) = @_;
+
if ( $searchfield ) {
$template->param( search => 1 );
- $template->param( searchfield => $searchfield );
- $results = retrieve_letters($searchfield);
- } else {
- $results = retrieve_letters();
}
+ my $results = retrieve_letters($branchcode,$searchfield);
+
my $loop_data = [];
my $protected_letters = protected_letters();
foreach my $row (@{$results}) {
- $row->{protected} = $protected_letters->{ $row->{code}};
+ $row->{protected} = !$row->{branchcode} && $protected_letters->{ $row->{code} };
push @{$loop_data}, $row;
}
- $template->param( letter => $loop_data );
- return;
+
+ $template->param(
+ letter => $loop_data,
+ branchloop => _branchloop($branchcode),
+ );
+}
+
+sub _branchloop {
+ my ($branchcode) = @_;
+
+ my $branches = GetBranches();
+ my @branchloop;
+ for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
+ push @branchloop, {
+ value => $thisbranch,
+ selected => $branchcode && $thisbranch eq $branchcode,
+ branchname => $branches->{$thisbranch}->{'branchname'},
+ };
+ }
+
+ return \@branchloop;
}
sub add_fields {
sub get_columns_for {
my $table = shift;
-# FIXME untranslateable
+# FIXME untranslatable
my %column_map = (
aqbooksellers => '---BOOKSELLERS---',
aqorders => '---ORDERS---',
text => $tlabel,
};
}
+
my $sql = "SHOW COLUMNS FROM $table";# TODO not db agnostic
my $table_prefix = $table . q|.|;
my $rows = C4::Context->dbh->selectall_arrayref($sql, { Slice => {} });
text => $table_prefix . $row->{Field},
}
}
+ if ($table eq 'borrowers') {
+ if ( my $attributes = C4::Members::Attributes::GetAttributes() ) {
+ foreach (@$attributes) {
+ push @fields, {
+ value => "borrower-attribute:$_",
+ text => "attribute:$_",
+ }
+ }
+ }
+ }
return @fields;
}