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({});
58 aqbooksellers => 'BOOKSELLERS',
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");
68 my @SQLfieldname = ();
69 push @SQLfieldname, {'value' => "", 'text' => '---' . uc($column_map{$table} || $table) . '---'};
70 while (my ($field) = $sth->fetchrow_array) {
72 value => $table . ".$field",
73 text => $table . ".$field"
79 # letter_exists($module, $code)
80 # - return true if a letter with the given $module and $code 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);
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 };
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;
112 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
114 template_name => "tools/letter.tmpl",
117 authnotrequired => 0,
118 flagsrequired => { tools => 'edit_notices' },
124 $template->param($op => 1);
126 $template->param(else => 1);
128 # we show only the TMPL_VAR names $op
131 script_name => $script_name,
132 action => $script_name
134 ################## ADD_FORM ##################################
135 # called by default. Used to create form to add or modify a record
136 if ( $op eq 'add_form' ) {
138 #---- if primkey exists, it's a modify action, so read values to modify...
141 my $sth = $dbh->prepare("SELECT * FROM letter WHERE module=? AND code=?");
142 $sth->execute( $module, $code );
143 $letter = $sth->fetchrow_hashref;
148 foreach (qw(LibrarianFirstname LibrarianSurname LibrarianEmailaddress)) {
149 push @SQLfieldname, {value => $_, text => $_};
151 push @SQLfieldname, column_picks('branches');
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
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($_) };
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');
175 $template->param( modify => 1 );
176 $template->param( code => $letter->{code} );
179 $template->param( adding => 1 );
182 name => $letter->{name},
183 title => $letter->{title},
184 content => ( $content ? $content : $letter->{content} ),
185 ( $module ? $module : $letter->{module} ) => 1,
186 SQLfieldname => \@SQLfieldname,
188 ################## ADD_VALIDATE ##################################
189 # called by add_form, used to insert/modify data in DB
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)) {
201 q{UPDATE letter SET module = ?, code = ?, name = ?, title = ?, content = ? WHERE module = ? AND code = ?},
203 $module, $code, $name, $title, $content,
209 q{INSERT INTO letter (module,code,name,title,content) VALUES (?,?,?,?,?)},
211 $module, $code, $name, $title, $content
214 print $input->redirect("letter.pl");
216 ################## DELETE_CONFIRM ##################################
217 # called by default form, used to confirm deletion of data in DB
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->{$_} );
228 ################## DELETE_CONFIRMED ##################################
229 # called by delete_confirm, used to effectively confirm deletion of data in DB
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");
239 ################## DEFAULT ##################################
242 if ( $searchfield ne '' ) {
243 $template->param( search => 1 );
244 $template->param( searchfield => $searchfield );
246 my ($results) = StringSearch($searchfield);
248 my $protected_letters = protected_letters();
249 foreach my $result (@$results) {
251 foreach my $key (qw(module code name)) {
252 $row_data{$key} = $result->{$key};
253 $row_data{'protected'} = $protected_letters->{$result->{code}};
255 push(@loop_data, \%row_data );
257 $template->param( letter => \@loop_data );
258 } #---- END $OP eq DEFAULT
260 output_html_with_http_headers $input, $cookie, $template->output;