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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 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 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.
28 - if primkey exists, this is a modification,so we read the $primkey record
29 - builds the add/modify form
31 - the user has just send datas, so we create/modify the record
33 - we show the record having primkey=$primkey and ask for deletion validation form
35 - we delete the record having primkey=$primkey
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({});
57 # FIXME untranslateable
59 aqbooksellers
=> 'BOOKSELLERS',
65 sub column_picks
($) {
66 # returns @array of values
67 my $table = shift or return ();
68 my $sth = C4
::Context
->dbh->prepare("SHOW COLUMNS FROM $table");
70 my @SQLfieldname = ();
71 push @SQLfieldname, {'value' => "", 'text' => '---' . uc($column_map{$table} || $table) . '---'};
72 while (my ($field) = $sth->fetchrow_array) {
74 value
=> $table . ".$field",
75 text
=> $table . ".$field"
81 # letter_exists($module, $code)
82 # - return true if a letter with the given $module and $code exists
84 my ($module, $code) = @_;
85 my $dbh = C4
::Context
->dbh;
86 my $sql = q{SELECT * FROM letter WHERE module = ? AND code = ?};
87 my $letters = $dbh->selectall_arrayref($sql, { Slice
=> {} }, $module, $code);
88 return scalar(@
$letters);
91 # $protected_letters = protected_letters()
92 # - return a hashref of letter_codes representing letters that should never be deleted
93 sub protected_letters
{
94 my $dbh = C4
::Context
->dbh;
95 my $sql = q{SELECT DISTINCT letter_code FROM message_transports};
96 my $codes = $dbh->selectall_arrayref($sql);
97 return { map { $_->[0] => 1 } @
$codes };
101 my $searchfield = $input->param('searchfield');
102 $searchfield = '' unless defined($searchfield);
103 # my $offset = $input->param('offset'); # pagination not implemented
104 my $script_name = "/cgi-bin/koha/tools/letter.pl";
105 my $code = $input->param('code');
106 my $module = $input->param('module');
107 $module = '' unless defined($module);
108 my $content = $input->param('content');
109 my $op = $input->param('op');
110 $op = '' unless defined($op);
111 $searchfield =~ s/\,//g;
112 my $dbh = C4
::Context
->dbh;
114 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
116 template_name
=> "tools/letter.tmpl",
119 authnotrequired
=> 0,
120 flagsrequired
=> { tools
=> 'edit_notices' },
126 $template->param($op => 1);
128 $template->param(else => 1);
130 # we show only the TMPL_VAR names $op
133 script_name
=> $script_name,
134 action
=> $script_name
136 ################## ADD_FORM ##################################
137 # called by default. Used to create form to add or modify a record
138 if ( $op eq 'add_form' ) {
140 #---- if primkey exists, it's a modify action, so read values to modify...
143 my $sth = $dbh->prepare("SELECT * FROM letter WHERE module=? AND code=?");
144 $sth->execute( $module, $code );
145 $letter = $sth->fetchrow_hashref;
150 foreach (qw(LibrarianFirstname LibrarianSurname LibrarianEmailaddress)) {
151 push @SQLfieldname, {value
=> $_, text
=> $_};
153 push @SQLfieldname, column_picks
('branches');
155 # add acquisition specific tables
156 if ( $module eq "reserves" ) {
157 push @SQLfieldname, column_picks
('borrowers'),
158 column_picks
('reserves'),
159 column_picks
('biblio'),
160 column_picks
('items');
162 elsif ( index( $module, "acquisition" ) > 0 ) { # FIXME: imprecise comparison
163 push @SQLfieldname, column_picks
('aqbooksellers'), column_picks
('aqorders');
164 # add issues specific tables
166 elsif ( index( $module, "issues" ) > 0 ) { # FIXME: imprecise comparison
167 push @SQLfieldname, column_picks
('aqbooksellers'),
168 column_picks
('serial'),
169 column_picks
('subscription'),
170 {value
=> "", text
=> '---BIBLIO---'};
171 foreach(qw(title author serial)) {
172 push @SQLfieldname, {value
=> "biblio.$_", text
=> ucfirst($_) };
176 push @SQLfieldname, column_picks
('biblio'),
177 column_picks
('biblioitems'),
178 {value
=> "", text
=> '---ITEMS---' },
179 {value
=> "items.content", text
=> 'items.content'},
180 column_picks
('borrowers');
183 $template->param( modify
=> 1 );
184 $template->param( code
=> $letter->{code
} );
187 $template->param( adding
=> 1 );
190 name
=> $letter->{name
},
191 title
=> $letter->{title
},
192 content
=> ( $content ?
$content : $letter->{content
} ),
193 ( $module ?
$module : $letter->{module
} ) => 1,
194 SQLfieldname
=> \
@SQLfieldname,
196 ################## ADD_VALIDATE ##################################
197 # called by add_form, used to insert/modify data in DB
199 elsif ( $op eq 'add_validate' ) {
200 my $dbh = C4
::Context
->dbh;
201 my $module = $input->param('module');
202 my $code = $input->param('code');
203 my $name = $input->param('name');
204 my $title = $input->param('title');
205 my $content = $input->param('content');
206 if (letter_exists
($module, $code)) {
209 q{UPDATE letter SET module = ?, code = ?, name = ?, title = ?, content = ? WHERE module = ? AND code = ?},
211 $module, $code, $name, $title, $content,
217 q{INSERT INTO letter (module,code,name,title,content) VALUES (?,?,?,?,?)},
219 $module, $code, $name, $title, $content
222 print $input->redirect("letter.pl");
224 ################## DELETE_CONFIRM ##################################
225 # called by default form, used to confirm deletion of data in DB
227 elsif ( $op eq 'delete_confirm' ) {
228 my $dbh = C4
::Context
->dbh;
229 my $sth = $dbh->prepare("SELECT * FROM letter WHERE code=?");
230 $sth->execute($code);
231 my $data = $sth->fetchrow_hashref;
232 $template->param( code
=> $code );
233 foreach (qw(module name content)) {
234 $template->param( $_ => $data->{$_} );
236 ################## DELETE_CONFIRMED ##################################
237 # called by delete_confirm, used to effectively confirm deletion of data in DB
239 elsif ( $op eq 'delete_confirmed' ) {
240 my $dbh = C4
::Context
->dbh;
241 my $code = uc( $input->param('code') );
242 my $module = $input->param('module');
243 my $sth = $dbh->prepare("DELETE FROM letter WHERE module=? AND code=?");
244 $sth->execute( $module, $code );
245 print $input->redirect("/cgi-bin/koha/tools/letter.pl");
247 ################## DEFAULT ##################################
250 if ( $searchfield ne '' ) {
251 $template->param( search
=> 1 );
252 $template->param( searchfield
=> $searchfield );
254 my ($results) = StringSearch
($searchfield);
256 my $protected_letters = protected_letters
();
257 foreach my $result (@
$results) {
259 foreach my $key (qw(module code name)) {
260 $row_data{$key} = $result->{$key};
261 $row_data{'protected'} = $protected_letters->{$result->{code
}};
263 push(@loop_data, \
%row_data );
265 $template->param( letter
=> \
@loop_data );
266 } #---- END $OP eq DEFAULT
268 output_html_with_http_headers
$input, $cookie, $template->output;