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
50 use C4::Members::Attributes;
52 # _letter_from_where($branchcode,$module, $code)
53 # - return FROM WHERE clause and bind args for a letter
54 sub _letter_from_where {
55 my ($branchcode, $module, $code) = @_;
56 my $sql = q{FROM letter WHERE branchcode = ? AND module = ? AND code = ?};
57 my @args = ($branchcode || '', $module, $code);
58 # Mysql is retarded. cause branchcode is part of the primary key it cannot be null. How does that
59 # work with foreign key constraint I wonder...
62 # $sql .= " AND branchcode = ?";
63 # push @args, $branchcode;
65 # $sql .= " AND branchcode IS NULL";
68 return ($sql, \@args);
71 # letter_exists($branchcode,$module, $code)
72 # - return true if a letter with the given $branchcode, $module and $code exists
74 my ($sql, $args) = _letter_from_where(@_);
75 my $dbh = C4::Context->dbh;
76 my $letter = $dbh->selectrow_hashref("SELECT * $sql", undef, @$args);
80 # $protected_letters = protected_letters()
81 # - return a hashref of letter_codes representing letters that should never be deleted
82 sub protected_letters {
83 my $dbh = C4::Context->dbh;
84 my $codes = $dbh->selectall_arrayref(q{SELECT DISTINCT letter_code FROM message_transports});
85 return { map { $_->[0] => 1 } @{$codes} };
89 my $searchfield = $input->param('searchfield');
90 my $script_name = '/cgi-bin/koha/tools/letter.pl';
91 our $branchcode = $input->param('branchcode');
92 my $code = $input->param('code');
93 my $module = $input->param('module');
94 my $content = $input->param('content');
95 my $op = $input->param('op') || '';
96 my $dbh = C4::Context->dbh;
98 our ( $template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
100 template_name => 'tools/letter.tmpl',
103 authnotrequired => 0,
104 flagsrequired => { tools => 'edit_notices' },
109 our $my_branch = C4::Context->preference("IndependantBranches") && !$staffflags->{'superlibrarian'}
110 ? C4::Context->userenv()->{'branch'}
112 # we show only the TMPL_VAR names $op
115 independant_branch => $my_branch,
116 script_name => $script_name,
117 searchfield => $searchfield,
118 action => $script_name
126 if ($op eq 'add_form') {
127 add_form($branchcode, $module, $code);
129 elsif ( $op eq 'add_validate' ) {
131 $op = q{}; # next operation is to return to default screen
133 elsif ( $op eq 'delete_confirm' ) {
134 delete_confirm($branchcode, $module, $code);
136 elsif ( $op eq 'delete_confirmed' ) {
137 delete_confirmed($branchcode, $module, $code);
138 $op = q{}; # next operation is to return to default screen
141 default_display($branchcode,$searchfield);
144 # Do this last as delete_confirmed resets
146 $template->param($op => 1);
148 $template->param(no_op_set => 1);
151 output_html_with_http_headers $input, $cookie, $template->output;
154 my ($branchcode,$module, $code ) = @_;
157 # if code has been passed we can identify letter and its an update action
159 $letter = letter_exists($branchcode,$module, $code);
162 $template->param( modify => 1 );
163 $template->param( code => $letter->{code} );
165 else { # initialize the new fields
167 branchcode => $branchcode,
170 $template->param( adding => 1 );
174 push @{$field_selection}, add_fields('branches');
175 if ($module eq 'reserves') {
176 push @{$field_selection}, add_fields('borrowers', 'reserves', 'biblio', 'items');
178 elsif ($module eq 'claimacquisition') {
179 push @{$field_selection}, add_fields('aqbooksellers', 'aqorders', 'biblio', 'biblioitems');
181 elsif ($module eq 'claimissues') {
182 push @{$field_selection}, add_fields('aqbooksellers', 'serial', 'subscription');
183 push @{$field_selection},
186 text => '---BIBLIO---'
188 foreach(qw(title author serial)) {
189 push @{$field_selection}, {value => "biblio.$_", text => ucfirst $_ };
192 elsif ($module eq 'suggestions') {
193 push @{$field_selection}, add_fields('suggestions', 'borrowers', 'biblio');
196 push @{$field_selection}, add_fields('biblio','biblioitems'),
197 {value => q{}, text => '---ITEMS---' },
198 {value => 'items.content', text => 'items.content'},
199 add_fields('issues','borrowers');
200 if ($module eq 'circulation') {
201 push @{$field_selection}, add_fields('opac_news');
206 branchcode => $letter->{branchcode},
207 name => $letter->{name},
208 is_html => $letter->{is_html},
209 title => $letter->{title},
210 content => $letter->{content},
213 branchloop => _branchloop($branchcode),
214 SQLfieldname => $field_selection,
220 my $dbh = C4::Context->dbh;
221 my $oldbranchcode = $input->param('oldbranchcode');
222 my $branchcode = $input->param('branchcode') || '';
223 my $module = $input->param('module');
224 my $oldmodule = $input->param('oldmodule');
225 my $code = $input->param('code');
226 my $name = $input->param('name');
227 my $is_html = $input->param('is_html');
228 my $title = $input->param('title');
229 my $content = $input->param('content');
230 if (letter_exists($oldbranchcode,$oldmodule, $code)) {
232 q{UPDATE letter SET branchcode = ?, module = ?, name = ?, is_html = ?, title = ?, content = ? WHERE branchcode = ? AND module = ? AND code = ?},
234 $branchcode, $module, $name, $is_html || 0, $title, $content,
235 $oldbranchcode, $oldmodule, $code
239 q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content) VALUES (?,?,?,?,?,?,?)},
241 $branchcode, $module, $code, $name, $is_html || 0, $title, $content
244 # set up default display
245 default_display($branchcode);
249 my $dbh = C4::Context->dbh;
250 my $oldbranchcode = $input->param('oldbranchcode');
251 my $branchcode = $input->param('branchcode');
252 my $module = $input->param('module');
253 my $code = $input->param('code');
255 return if letter_exists($branchcode,$module, $code);
257 my $old_letter = letter_exists($oldbranchcode,$module, $code);
260 q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content) VALUES (?,?,?,?,?,?,?)},
262 $branchcode, $module, $code, $old_letter->{name}, $old_letter->{is_html}, $old_letter->{title}, $old_letter->{content}
267 my ($branchcode, $module, $code) = @_;
268 my $dbh = C4::Context->dbh;
269 my $letter = letter_exists($branchcode, $module, $code);
270 $template->param( branchcode => $branchcode, branchname => GetBranchName($branchcode) );
271 $template->param( code => $code );
272 $template->param( module => $module);
273 $template->param( name => $letter->{name});
277 sub delete_confirmed {
278 my ($branchcode, $module, $code) = @_;
279 my ($sql, $args) = _letter_from_where($branchcode, $module, $code);
280 my $dbh = C4::Context->dbh;
281 $dbh->do("DELETE $sql", undef, @$args);
282 # setup default display for screen
283 default_display($branchcode);
287 sub retrieve_letters {
288 my ($branchcode, $searchstring) = @_;
290 $branchcode = $my_branch if $branchcode && $my_branch;
292 my $dbh = C4::Context->dbh;
293 my ($sql, @where, @args);
294 $sql = "SELECT branchcode, module, code, name, branchname
296 LEFT OUTER JOIN branches USING (branchcode)";
297 if ($searchstring && $searchstring=~m/(\S+)/) {
298 $searchstring = $1 . q{%};
299 push @where, 'code LIKE ?';
300 push @args, $searchstring;
302 elsif ($branchcode) {
303 push @where, 'branchcode = ?';
304 push @args, $branchcode || '';
307 push @where, "(branchcode = ? OR branchcode = '')";
308 push @args, $my_branch;
311 $sql .= " WHERE ".join(" AND ", @where) if @where;
312 $sql .= " ORDER BY module, code, branchcode";
313 # use Data::Dumper; die Dumper($sql, \@args);
314 return $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
317 sub default_display {
318 my ($branchcode, $searchfield) = @_;
320 if ( $searchfield ) {
321 $template->param( search => 1 );
323 my $results = retrieve_letters($branchcode,$searchfield);
326 my $protected_letters = protected_letters();
327 foreach my $row (@{$results}) {
328 $row->{protected} = !$row->{branchcode} && $protected_letters->{ $row->{code} };
329 push @{$loop_data}, $row;
334 letter => $loop_data,
335 branchloop => _branchloop($branchcode),
340 my ($branchcode) = @_;
342 my $branches = GetBranches();
344 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
346 value => $thisbranch,
347 selected => $branchcode && $thisbranch eq $branchcode,
348 branchname => $branches->{$thisbranch}->{'branchname'},
359 for my $table (@tables) {
360 push @fields, get_columns_for($table);
366 sub get_columns_for {
368 # FIXME untranslateable
370 aqbooksellers => '---BOOKSELLERS---',
371 aqorders => '---ORDERS---',
372 serial => '---SERIALS---',
373 reserves => '---HOLDS---',
374 suggestions => '---SUGGESTIONS---',
377 if (exists $column_map{$table} ) {
380 text => $column_map{$table} ,
384 my $tlabel = '---' . uc $table;
392 my $sql = "SHOW COLUMNS FROM $table";# TODO not db agnostic
393 my $table_prefix = $table . q|.|;
394 my $rows = C4::Context->dbh->selectall_arrayref($sql, { Slice => {} });
395 for my $row (@{$rows}) {
396 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
398 value => $table_prefix . $row->{Field},
399 text => $table_prefix . $row->{Field},
402 if ($table eq 'borrowers') {
403 if ( my $attributes = C4::Members::Attributes::GetAttributes() ) {
404 foreach (@$attributes) {
406 value => "borrower-attribute:$_",
407 text => "attribute:$_",