(bug #3053) extract ISBD view generator, and permit to display valuecode in ISBD...
[koha.git] / tools / letter.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 =head1 tools/letter.pl
21
22  ALGO :
23  this script use an $op to know what to do.
24  if $op is empty or none of the above values,
25         - the default screen is build (with all records, or filtered datas).
26         - the   user can clic on add, modify or delete record.
27  if $op=add_form
28         - if primkey exists, this is a modification,so we read the $primkey record
29         - builds the add/modify form
30  if $op=add_validate
31         - the user has just send datas, so we create/modify the record
32  if $op=delete_form
33         - we show the record having primkey=$primkey and ask for deletion validation form
34  if $op=delete_confirm
35         - we delete the record having primkey=$primkey
36
37 =cut
38
39 use strict;
40 use warnings;
41 use CGI;
42 use C4::Auth;
43 use C4::Context;
44 use C4::Output;
45
46 sub StringSearch {
47     my ($searchstring) = @_;
48     my $dbh = C4::Context->dbh;
49     $searchstring =~ s/\'/\\\'/g;
50     my @data = split( ' ', $searchstring );
51     $data[0] = '' unless @data;
52     my $sth = $dbh->prepare("SELECT * FROM letter WHERE (code LIKE ?) ORDER BY module, code");
53     $sth->execute("$data[0]%");     # slightly bogus, only searching on first string.
54     return $sth->fetchall_arrayref({});
55 }
56
57 our %column_map = (
58     aqbooksellers => 'BOOKSELLERS',
59     aqorders => 'ORDERS',
60     serial => 'SERIALS',
61 );
62
63 sub column_picks ($) {
64     # returns @array of values
65     my $table = shift or return ();
66     my $sth = C4::Context->dbh->prepare("SHOW COLUMNS FROM $table");
67     $sth->execute;
68     my @SQLfieldname = ();
69     push @SQLfieldname, {'value' => "", 'text' => '---' . uc($column_map{$table} || $table) . '---'};
70     while (my ($field) = $sth->fetchrow_array) {
71         push @SQLfieldname, {
72             value => $table . ".$field",
73              text => $table . ".$field"
74         };
75     }
76     return @SQLfieldname;
77 }
78
79 # letter_exists($module, $code)
80 # - return true if a letter with the given $module and $code exists
81 sub letter_exists {
82     my ($module, $code) = @_;
83     my $dbh = C4::Context->dbh;
84     my $sql = q{SELECT * FROM letter WHERE module = ? AND code = ?};
85     my $letters = $dbh->selectall_arrayref($sql, { Slice => {} }, $module, $code);
86     return scalar(@$letters);
87 }
88
89 # $protected_letters = protected_letters()
90 # - return a hashref of letter_codes representing letters that should never be deleted
91 sub protected_letters {
92     my $dbh = C4::Context->dbh;
93     my $sql = q{SELECT DISTINCT letter_code FROM message_transports};
94     my $codes = $dbh->selectall_arrayref($sql);
95     return { map { $_->[0] => 1 } @$codes };
96 }
97
98 my $input       = new CGI;
99 my $searchfield = $input->param('searchfield');
100 $searchfield = '' unless defined($searchfield);
101 # my $offset      = $input->param('offset'); # pagination not implemented
102 my $script_name = "/cgi-bin/koha/tools/letter.pl";
103 my $code        = $input->param('code');
104 my $module      = $input->param('module');
105 $module = '' unless defined($module);
106 my $content     = $input->param('content');
107 my $op          = $input->param('op');
108 $op = '' unless defined($op);
109 $searchfield =~ s/\,//g;
110 my $dbh = C4::Context->dbh;
111
112 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
113     {
114         template_name   => "tools/letter.tmpl",
115         query           => $input,
116         type            => "intranet",
117         authnotrequired => 0,
118         flagsrequired   => { tools => 'edit_notices' },
119         debug           => 1,
120     }
121 );
122
123 if ($op) {
124         $template->param($op  => 1);
125 } else {
126         $template->param(else => 1);
127 }
128 # we show only the TMPL_VAR names $op
129
130 $template->param(
131         script_name => $script_name,
132         action => $script_name
133 );
134 ################## ADD_FORM ##################################
135 # called by default. Used to create form to add or  modify a record
136 if ( $op eq 'add_form' ) {
137
138     #---- if primkey exists, it's a modify action, so read values to modify...
139     my $letter;
140     if ($code) {
141         my $sth = $dbh->prepare("SELECT * FROM letter WHERE module=? AND code=?");
142         $sth->execute( $module, $code );
143         $letter = $sth->fetchrow_hashref;
144     }
145
146     # build field list
147     my @SQLfieldname;
148     foreach (qw(LibrarianFirstname LibrarianSurname LibrarianEmailaddress)) {
149         push @SQLfieldname, {value => $_, text => $_};
150     }
151     push @SQLfieldname, column_picks('branches');
152
153     # add acquisition specific tables
154     if ( index( $module, "acquisition" ) > 0 ) {        # FIXME: imprecise comparison
155         push @SQLfieldname, column_picks('aqbooksellers'), column_picks('aqorders');
156         # add issues specific tables
157     }
158     elsif ( index( $module, "issues" ) > 0 ) {  # FIXME: imprecise comparison
159         push @SQLfieldname, column_picks('aqbooksellers'),
160             column_picks('serial'),
161             column_picks('subscription'),
162             {value => "", text => '---BIBLIO---'};
163                 foreach(qw(title author serial)) {
164                 push @SQLfieldname, {value => "biblio.$_", text => ucfirst($_) };
165                 }
166     }
167     else {
168         push @SQLfieldname, column_picks('biblio'),
169             column_picks('biblioitems'),
170             {value => "",              text => '---ITEMS---'  },
171             {value => "items.content", text => 'items.content'},
172             column_picks('borrowers');
173     }
174     if ($code) {
175         $template->param( modify => 1 );
176         $template->param( code   => $letter->{code} );
177     }
178     else {
179         $template->param( adding => 1 );
180     }
181     $template->param(
182         name    => $letter->{name},
183         title   => $letter->{title},
184         content => ( $content ? $content : $letter->{content} ),
185         ( $module ? $module : $letter->{module} ) => 1,
186         SQLfieldname => \@SQLfieldname,
187     );
188 ################## ADD_VALIDATE ##################################
189     # called by add_form, used to insert/modify data in DB
190 }
191 elsif ( $op eq 'add_validate' ) {
192     my $dbh     = C4::Context->dbh;
193     my $module  = $input->param('module');
194     my $code    = $input->param('code');
195     my $name    = $input->param('name');
196     my $title   = $input->param('title');
197     my $content = $input->param('content');
198     if (letter_exists($module, $code)) {
199         # UPDATE
200         $dbh->do(
201             q{UPDATE letter SET module = ?, code = ?, name = ?, title = ?, content = ? WHERE module = ? AND code = ?},
202             undef,
203             $module, $code, $name, $title, $content,
204             $module, $code
205         );
206     } else {
207         # INSERT
208         $dbh->do(
209             q{INSERT INTO letter (module,code,name,title,content) VALUES (?,?,?,?,?)},
210             undef,
211             $module, $code, $name, $title, $content
212         );
213     }
214     print $input->redirect("letter.pl");
215     exit;
216 ################## DELETE_CONFIRM ##################################
217     # called by default form, used to confirm deletion of data in DB
218 }
219 elsif ( $op eq 'delete_confirm' ) {
220     my $dbh = C4::Context->dbh;
221     my $sth = $dbh->prepare("SELECT * FROM letter WHERE code=?");
222     $sth->execute($code);
223     my $data = $sth->fetchrow_hashref;
224     $template->param( code => $code );
225         foreach (qw(module name content)) {
226         $template->param( $_ => $data->{$_} );
227         }
228 ################## DELETE_CONFIRMED ##################################
229   # called by delete_confirm, used to effectively confirm deletion of data in DB
230 }
231 elsif ( $op eq 'delete_confirmed' ) {
232     my $dbh    = C4::Context->dbh;
233     my $code   = uc( $input->param('code') );
234     my $module = $input->param('module');
235     my $sth    = $dbh->prepare("DELETE FROM letter WHERE module=? AND code=?");
236     $sth->execute( $module, $code );
237     print $input->redirect("/cgi-bin/koha/tools/letter.pl");
238     exit;
239 ################## DEFAULT ##################################
240 }
241 else {    # DEFAULT
242     if ( $searchfield ne '' ) {
243         $template->param( search      => 1 );
244         $template->param( searchfield => $searchfield );
245     }
246     my ($results) = StringSearch($searchfield);
247     my @loop_data = ();
248     my $protected_letters = protected_letters();
249     foreach my $result (@$results) {
250         my %row_data;
251         foreach my $key (qw(module code name)) {
252             $row_data{$key} = $result->{$key};
253             $row_data{'protected'} = $protected_letters->{$result->{code}};
254         }
255         push(@loop_data, \%row_data );
256     }
257     $template->param( letter => \@loop_data );
258 }    #---- END $OP eq DEFAULT
259
260 output_html_with_http_headers $input, $cookie, $template->output;
261