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"
82 my $searchfield = $input->param('searchfield');
83 $searchfield = '' unless defined($searchfield);
84 # my $offset = $input->param('offset'); # pagination not implemented
85 my $script_name = "/cgi-bin/koha/tools/letter.pl";
86 my $code = $input->param('code');
87 my $module = $input->param('module');
88 $module = '' unless defined($module);
89 my $content = $input->param('content');
90 my $op = $input->param('op');
91 $op = '' unless defined($op);
92 $searchfield =~ s/\,//g;
93 my $dbh = C4::Context->dbh;
95 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
97 template_name => "tools/letter.tmpl",
100 authnotrequired => 0,
101 flagsrequired => { tools => 'edit_notices' },
107 $template->param($op => 1);
109 $template->param(else => 1);
111 # we show only the TMPL_VAR names $op
114 script_name => $script_name,
115 action => $script_name
117 ################## ADD_FORM ##################################
118 # called by default. Used to create form to add or modify a record
119 if ( $op eq 'add_form' ) {
121 #---- if primkey exists, it's a modify action, so read values to modify...
124 my $sth = $dbh->prepare("SELECT * FROM letter WHERE module=? AND code=?");
125 $sth->execute( $module, $code );
126 $letter = $sth->fetchrow_hashref;
131 foreach (qw(LibrarianFirstname LibrarianSurname LibrarianEmailaddress)) {
132 push @SQLfieldname, {value => $_, text => $_};
134 push @SQLfieldname, column_picks('branches');
136 # add acquisition specific tables
137 if ( $module eq "reserves" ) {
138 push @SQLfieldname, column_picks('borrowers'),
139 column_picks('reserves'),
140 column_picks('biblio'),
141 column_picks('items');
143 elsif ( index( $module, "acquisition" ) > 0 ) { # FIXME: imprecise comparison
144 push @SQLfieldname, column_picks('aqbooksellers'), column_picks('aqorders');
145 # add issues specific tables
147 elsif ( index( $module, "issues" ) > 0 ) { # FIXME: imprecise comparison
148 push @SQLfieldname, column_picks('aqbooksellers'),
149 column_picks('serial'),
150 column_picks('subscription'),
151 {value => "", text => '---BIBLIO---'};
152 foreach(qw(title author serial)) {
153 push @SQLfieldname, {value => "biblio.$_", text => ucfirst($_) };
157 push @SQLfieldname, column_picks('biblio'),
158 column_picks('biblioitems'),
159 {value => "", text => '---ITEMS---' },
160 {value => "items.content", text => 'items.content'},
161 column_picks('borrowers');
164 $template->param( modify => 1 );
165 $template->param( code => $letter->{code} );
168 $template->param( adding => 1 );
171 name => $letter->{name},
172 title => $letter->{title},
173 content => ( $content ? $content : $letter->{content} ),
174 ( $module ? $module : $letter->{module} ) => 1,
175 SQLfieldname => \@SQLfieldname,
177 ################## ADD_VALIDATE ##################################
178 # called by add_form, used to insert/modify data in DB
180 elsif ( $op eq 'add_validate' ) {
181 my $dbh = C4::Context->dbh;
182 my $sth = $dbh->prepare(
183 "REPLACE letter (module,code,name,title,content) VALUES (?,?,?,?,?)");
185 $input->param('module'), $input->param('code'),
186 $input->param('name'), $input->param('title'),
187 $input->param('content')
189 print $input->redirect("letter.pl");
191 ################## DELETE_CONFIRM ##################################
192 # called by default form, used to confirm deletion of data in DB
194 elsif ( $op eq 'delete_confirm' ) {
195 my $dbh = C4::Context->dbh;
196 my $sth = $dbh->prepare("SELECT * FROM letter WHERE code=?");
197 $sth->execute($code);
198 my $data = $sth->fetchrow_hashref;
199 $template->param( code => $code );
200 foreach (qw(module name content)) {
201 $template->param( $_ => $data->{$_} );
203 ################## DELETE_CONFIRMED ##################################
204 # called by delete_confirm, used to effectively confirm deletion of data in DB
206 elsif ( $op eq 'delete_confirmed' ) {
207 my $dbh = C4::Context->dbh;
208 my $code = uc( $input->param('code') );
209 my $module = $input->param('module');
210 my $sth = $dbh->prepare("DELETE FROM letter WHERE module=? AND code=?");
211 $sth->execute( $module, $code );
212 print $input->redirect("/cgi-bin/koha/tools/letter.pl");
214 ################## DEFAULT ##################################
217 if ( $searchfield ne '' ) {
218 $template->param( search => 1 );
219 $template->param( searchfield => $searchfield );
221 my ($results) = StringSearch($searchfield);
223 foreach my $result (@$results) {
225 foreach my $key (qw(module code name)) {
226 $row_data{$key} = $result->{$key};
228 push(@loop_data, \%row_data );
230 $template->param( letter => \@loop_data );
231 } #---- END $OP eq DEFAULT
233 output_html_with_http_headers $input, $cookie, $template->output;