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