Bug 2801 and other cleanup.
[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     my $sth = $dbh->prepare("SELECT * FROM letter WHERE (code LIKE ?) ORDER BY module, code");
52     $sth->execute("$data[0]%");     # slightly bogus, only searching on first string.
53     return $sth->fetchall_arrayref({});
54 }
55
56 our %column_map = (
57     aqbooksellers => 'BOOKSELLERS',
58     aqorders => 'ORDERS',
59     serial => 'SEREIALS',
60 );
61
62 sub column_picks ($) {
63     # returns @array of values
64     my $table = shift or return ();
65     my $sth = C4::Context->dbh->prepare("SHOW COLUMNS FROM $table");
66     $sth->execute;
67     my @SQLfieldname = ();
68     push @SQLfieldname, {'value' => "", 'text' => '---' . uc($column_map{$table} || $table) . '---'};
69     while (my ($field) = $sth->fetchrow_array) {
70         push @SQLfieldname, {
71             value => $table . ".$field",
72              text => $table . ".$field"
73         };
74     }
75     return @SQLfieldname;
76 }
77
78 my $input       = new CGI;
79 my $searchfield = $input->param('searchfield');
80 # my $offset      = $input->param('offset'); # pagination not implemented
81 my $script_name = "/cgi-bin/koha/tools/letter.pl";
82 my $code        = $input->param('code');
83 my $module      = $input->param('module');
84 my $content     = $input->param('content');
85 my $op          = $input->param('op');
86 $searchfield =~ s/\,//g;
87 my $dbh = C4::Context->dbh;
88
89 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
90     {
91         template_name   => "tools/letter.tmpl",
92         query           => $input,
93         type            => "intranet",
94         authnotrequired => 0,
95         flagsrequired   => { tools => 'edit_notices' },
96         debug           => 1,
97     }
98 );
99
100 if ($op) {
101         $template->param($op  => 1);
102 } else {
103         $template->param(else => 1);
104 }
105 # we show only the TMPL_VAR names $op
106
107 $template->param(
108         script_name => $script_name,
109         action => $script_name
110 );
111 ################## ADD_FORM ##################################
112 # called by default. Used to create form to add or  modify a record
113 if ( $op eq 'add_form' ) {
114
115     #---- if primkey exists, it's a modify action, so read values to modify...
116     my $letter;
117     if ($code) {
118         my $sth = $dbh->prepare("SELECT * FROM letter WHERE module=? AND code=?");
119         $sth->execute( $module, $code );
120         $letter = $sth->fetchrow_hashref;
121     }
122
123     # build field list
124     my @SQLfieldname;
125     foreach (qw(LibrarianFirstname LibrarianSurname LibrarianEmailaddress)) {
126         push @SQLfieldname, {value => $_, text => $_};
127     }
128     push @SQLfieldname, column_picks('branches');
129
130     # add acquisition specific tables
131     if ( index( $module, "acquisition" ) > 0 ) {        # FIXME: imprecise comparison
132         push @SQLfieldname, column_picks('aqbooksellers'), column_picks('aqorders');
133         # add issues specific tables
134     }
135     elsif ( index( $module, "issues" ) > 0 ) {  # FIXME: imprecise comparison
136         push @SQLfieldname, column_picks('aqbooksellers'),
137             column_picks('serial'),
138             column_picks('subscription'),
139             {value => "", text => '---BIBLIO---'};
140                 foreach(qw(title author serial)) {
141                 push @SQLfieldname, {value => "biblio.$_", text => ucfirst($_) };
142                 }
143     }
144     else {
145         push @SQLfieldname, column_picks('biblio'),
146             column_picks('biblioitems'),
147             {value => "",              text => '---ITEMS---'  },
148             {value => "items.content", text => 'items.content'},
149             column_picks('borrowers');
150     }
151     if ($code) {
152         $template->param( modify => 1 );
153         $template->param( code   => $letter->{code} );
154     }
155     else {
156         $template->param( adding => 1 );
157     }
158     $template->param(
159         name    => $letter->{name},
160         title   => $letter->{title},
161         content => ( $content ? $content : $letter->{content} ),
162         ( $module ? $module : $letter->{module} ) => 1,
163         SQLfieldname => \@SQLfieldname,
164     );
165 ################## ADD_VALIDATE ##################################
166     # called by add_form, used to insert/modify data in DB
167 }
168 elsif ( $op eq 'add_validate' ) {
169     my $dbh = C4::Context->dbh;
170     my $sth = $dbh->prepare(
171         "REPLACE letter (module,code,name,title,content) VALUES (?,?,?,?,?)");
172     $sth->execute(
173         $input->param('module'), $input->param('code'),
174         $input->param('name'),   $input->param('title'),
175         $input->param('content')
176     );
177     print $input->redirect("letter.pl");
178     exit;
179 ################## DELETE_CONFIRM ##################################
180     # called by default form, used to confirm deletion of data in DB
181 }
182 elsif ( $op eq 'delete_confirm' ) {
183     my $dbh = C4::Context->dbh;
184     my $sth = $dbh->prepare("SELECT * FROM letter WHERE code=?");
185     $sth->execute($code);
186     my $data = $sth->fetchrow_hashref;
187     $template->param( code => $code );
188         foreach (qw(module name content)) {
189         $template->param( $_ => $data->{$_} );
190         }
191 ################## DELETE_CONFIRMED ##################################
192   # called by delete_confirm, used to effectively confirm deletion of data in DB
193 }
194 elsif ( $op eq 'delete_confirmed' ) {
195     my $dbh    = C4::Context->dbh;
196     my $code   = uc( $input->param('code') );
197     my $module = $input->param('module');
198     my $sth    = $dbh->prepare("DELETE FROM letter WHERE module=? AND code=?");
199     $sth->execute( $module, $code );
200     print $input->redirect("/cgi-bin/koha/tools/letter.pl");
201     exit;
202 ################## DEFAULT ##################################
203 }
204 else {    # DEFAULT
205     if ( $searchfield ne '' ) {
206         $template->param( search      => 1 );
207         $template->param( searchfield => $searchfield );
208     }
209     my ($results) = StringSearch($searchfield);
210     my @loop_data = ();
211     foreach my $result (@$results) {
212         my %row_data;
213                 foreach my $key (qw(module code name)) {
214                 $row_data{$key} = $result->{$key};
215                 }
216         push(@loop_data, \%row_data );
217     }
218     $template->param( letter => \@loop_data );
219 }    #---- END $OP eq DEFAULT
220
221 output_html_with_http_headers $input, $cookie, $template->output;
222