Bug 10044: Remove duplicate pagination on the pending orders page.
[koha.git] / tools / letter.pl
blob68c6701f7246755153f55bf6e1fd39bce359d48c
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;
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...
61 # if ($branchcode) {
62 # $sql .= " AND branchcode = ?";
63 # push @args, $branchcode;
64 # } else {
65 # $sql .= " AND branchcode IS NULL";
66 # }
68 return ($sql, \@args);
71 # letter_exists($branchcode,$module, $code)
72 # - return true if a letter with the given $branchcode, $module and $code exists
73 sub letter_exists {
74 my ($sql, $args) = _letter_from_where(@_);
75 my $dbh = C4::Context->dbh;
76 my $letter = $dbh->selectrow_hashref("SELECT * $sql", undef, @$args);
77 return $letter;
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} };
88 our $input = new CGI;
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',
101 query => $input,
102 type => 'intranet',
103 authnotrequired => 0,
104 flagsrequired => { tools => 'edit_notices' },
105 debug => 1,
109 our $my_branch = C4::Context->preference("IndependantBranches") && !$staffflags->{'superlibrarian'}
110 ? C4::Context->userenv()->{'branch'}
111 : undef;
112 # we show only the TMPL_VAR names $op
114 $template->param(
115 independant_branch => $my_branch,
116 script_name => $script_name,
117 searchfield => $searchfield,
118 branchcode => $branchcode,
119 action => $script_name
122 if ($op eq 'copy') {
123 add_copy();
124 $op = 'add_form';
127 if ($op eq 'add_form') {
128 add_form($branchcode, $module, $code);
130 elsif ( $op eq 'add_validate' ) {
131 add_validate();
132 $op = q{}; # next operation is to return to default screen
134 elsif ( $op eq 'delete_confirm' ) {
135 delete_confirm($branchcode, $module, $code);
137 elsif ( $op eq 'delete_confirmed' ) {
138 delete_confirmed($branchcode, $module, $code);
139 $op = q{}; # next operation is to return to default screen
141 else {
142 default_display($branchcode,$searchfield);
145 # Do this last as delete_confirmed resets
146 if ($op) {
147 $template->param($op => 1);
148 } else {
149 $template->param(no_op_set => 1);
152 output_html_with_http_headers $input, $cookie, $template->output;
154 sub add_form {
155 my ($branchcode,$module, $code ) = @_;
157 my $letter;
158 # if code has been passed we can identify letter and its an update action
159 if ($code) {
160 $letter = letter_exists($branchcode,$module, $code);
162 if ($letter) {
163 $template->param( modify => 1 );
164 $template->param( code => $letter->{code} );
166 else { # initialize the new fields
167 $letter = {
168 branchcode => $branchcode,
169 module => $module,
171 $template->param( adding => 1 );
174 my $field_selection;
175 push @{$field_selection}, add_fields('branches');
176 if ($module eq 'reserves') {
177 push @{$field_selection}, add_fields('borrowers', 'reserves', 'biblio', 'items');
179 elsif ($module eq 'claimacquisition') {
180 push @{$field_selection}, add_fields('aqbooksellers', 'aqorders', 'biblio', 'biblioitems');
182 elsif ($module eq 'claimissues') {
183 push @{$field_selection}, add_fields('aqbooksellers', 'serial', 'subscription');
184 push @{$field_selection},
186 value => q{},
187 text => '---BIBLIO---'
189 foreach(qw(title author serial)) {
190 push @{$field_selection}, {value => "biblio.$_", text => ucfirst $_ };
193 elsif ($module eq 'suggestions') {
194 push @{$field_selection}, add_fields('suggestions', 'borrowers', 'biblio');
196 else {
197 push @{$field_selection}, add_fields('biblio','biblioitems'),
198 add_fields('items'),
199 {value => 'items.content', text => 'items.content'},
200 {value => 'items.fine', text => 'items.fine'},
201 add_fields('borrowers');
202 if ($module eq 'circulation') {
203 push @{$field_selection}, add_fields('opac_news');
207 if ( $module eq 'circulation' && $code eq "CHECKIN" ) {
208 push @{$field_selection}, add_fields('old_issues');
209 } else {
210 push @{$field_selection}, add_fields('issues');
214 $template->param(
215 branchcode => $letter->{branchcode},
216 name => $letter->{name},
217 is_html => $letter->{is_html},
218 title => $letter->{title},
219 content => $letter->{content},
220 module => $module,
221 $module => 1,
222 branchloop => _branchloop($branchcode),
223 SQLfieldname => $field_selection,
225 return;
228 sub add_validate {
229 my $dbh = C4::Context->dbh;
230 my $oldbranchcode = $input->param('oldbranchcode');
231 my $branchcode = $input->param('branchcode') || '';
232 my $module = $input->param('module');
233 my $oldmodule = $input->param('oldmodule');
234 my $code = $input->param('code');
235 my $name = $input->param('name');
236 my $is_html = $input->param('is_html');
237 my $title = $input->param('title');
238 my $content = $input->param('content');
239 if (letter_exists($oldbranchcode,$oldmodule, $code)) {
240 $dbh->do(
241 q{UPDATE letter SET branchcode = ?, module = ?, name = ?, is_html = ?, title = ?, content = ? WHERE branchcode = ? AND module = ? AND code = ?},
242 undef,
243 $branchcode, $module, $name, $is_html || 0, $title, $content,
244 $oldbranchcode, $oldmodule, $code
246 } else {
247 $dbh->do(
248 q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content) VALUES (?,?,?,?,?,?,?)},
249 undef,
250 $branchcode, $module, $code, $name, $is_html || 0, $title, $content
253 # set up default display
254 default_display($branchcode);
257 sub add_copy {
258 my $dbh = C4::Context->dbh;
259 my $oldbranchcode = $input->param('oldbranchcode');
260 my $branchcode = $input->param('branchcode');
261 my $module = $input->param('module');
262 my $code = $input->param('code');
264 return if letter_exists($branchcode,$module, $code);
266 my $old_letter = letter_exists($oldbranchcode,$module, $code);
268 $dbh->do(
269 q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content) VALUES (?,?,?,?,?,?,?)},
270 undef,
271 $branchcode, $module, $code, $old_letter->{name}, $old_letter->{is_html}, $old_letter->{title}, $old_letter->{content}
275 sub delete_confirm {
276 my ($branchcode, $module, $code) = @_;
277 my $dbh = C4::Context->dbh;
278 my $letter = letter_exists($branchcode, $module, $code);
279 $template->param( branchcode => $branchcode, branchname => GetBranchName($branchcode) );
280 $template->param( code => $code );
281 $template->param( module => $module);
282 $template->param( name => $letter->{name});
283 return;
286 sub delete_confirmed {
287 my ($branchcode, $module, $code) = @_;
288 my ($sql, $args) = _letter_from_where($branchcode, $module, $code);
289 my $dbh = C4::Context->dbh;
290 $dbh->do("DELETE $sql", undef, @$args);
291 # setup default display for screen
292 default_display($branchcode);
293 return;
296 sub retrieve_letters {
297 my ($branchcode, $searchstring) = @_;
299 $branchcode = $my_branch if $branchcode && $my_branch;
301 my $dbh = C4::Context->dbh;
302 my ($sql, @where, @args);
303 $sql = "SELECT branchcode, module, code, name, branchname
304 FROM letter
305 LEFT OUTER JOIN branches USING (branchcode)";
306 if ($searchstring && $searchstring=~m/(\S+)/) {
307 $searchstring = $1 . q{%};
308 push @where, 'code LIKE ?';
309 push @args, $searchstring;
311 elsif ($branchcode) {
312 push @where, 'branchcode = ?';
313 push @args, $branchcode || '';
315 elsif ($my_branch) {
316 push @where, "(branchcode = ? OR branchcode = '')";
317 push @args, $my_branch;
320 $sql .= " WHERE ".join(" AND ", @where) if @where;
321 $sql .= " ORDER BY module, code, branchcode";
322 # use Data::Dumper; die Dumper($sql, \@args);
323 return $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
326 sub default_display {
327 my ($branchcode, $searchfield) = @_;
329 if ( $searchfield ) {
330 $template->param( search => 1 );
332 my $results = retrieve_letters($branchcode,$searchfield);
334 my $loop_data = [];
335 my $protected_letters = protected_letters();
336 foreach my $row (@{$results}) {
337 $row->{protected} = !$row->{branchcode} && $protected_letters->{ $row->{code} };
338 push @{$loop_data}, $row;
342 $template->param(
343 letter => $loop_data,
344 branchloop => _branchloop($branchcode),
348 sub _branchloop {
349 my ($branchcode) = @_;
351 my $branches = GetBranches();
352 my @branchloop;
353 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
354 push @branchloop, {
355 value => $thisbranch,
356 selected => $branchcode && $thisbranch eq $branchcode,
357 branchname => $branches->{$thisbranch}->{'branchname'},
361 return \@branchloop;
364 sub add_fields {
365 my @tables = @_;
366 my @fields = ();
368 for my $table (@tables) {
369 push @fields, get_columns_for($table);
372 return @fields;
375 sub get_columns_for {
376 my $table = shift;
377 # FIXME untranslateable
378 my %column_map = (
379 aqbooksellers => '---BOOKSELLERS---',
380 aqorders => '---ORDERS---',
381 serial => '---SERIALS---',
382 reserves => '---HOLDS---',
383 suggestions => '---SUGGESTIONS---',
385 my @fields = ();
386 if (exists $column_map{$table} ) {
387 push @fields, {
388 value => q{},
389 text => $column_map{$table} ,
392 else {
393 my $tlabel = '---' . uc $table;
394 $tlabel.= '---';
395 push @fields, {
396 value => q{},
397 text => $tlabel,
401 my $sql = "SHOW COLUMNS FROM $table";# TODO not db agnostic
402 my $table_prefix = $table . q|.|;
403 my $rows = C4::Context->dbh->selectall_arrayref($sql, { Slice => {} });
404 for my $row (@{$rows}) {
405 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
406 push @fields, {
407 value => $table_prefix . $row->{Field},
408 text => $table_prefix . $row->{Field},
411 if ($table eq 'borrowers') {
412 if ( my $attributes = C4::Members::Attributes::GetAttributes() ) {
413 foreach (@$attributes) {
414 push @fields, {
415 value => "borrower-attribute:$_",
416 text => "attribute:$_",
421 return @fields;