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 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({});
57 aqbooksellers => 'BOOKSELLERS',
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");
67 my @SQLfieldname = ();
68 push @SQLfieldname, {'value' => "", 'text' => '---' . uc($column_map{$table} || $table) . '---'};
69 while (my ($field) = $sth->fetchrow_array) {
71 value => $table . ".$field",
72 text => $table . ".$field"
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;
89 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
91 template_name => "tools/letter.tmpl",
95 flagsrequired => { tools => 'edit_notices' },
101 $template->param($op => 1);
103 $template->param(else => 1);
105 # we show only the TMPL_VAR names $op
108 script_name => $script_name,
109 action => $script_name
111 ################## ADD_FORM ##################################
112 # called by default. Used to create form to add or modify a record
113 if ( $op eq 'add_form' ) {
115 #---- if primkey exists, it's a modify action, so read values to modify...
118 my $sth = $dbh->prepare("SELECT * FROM letter WHERE module=? AND code=?");
119 $sth->execute( $module, $code );
120 $letter = $sth->fetchrow_hashref;
125 foreach (qw(LibrarianFirstname LibrarianSurname LibrarianEmailaddress)) {
126 push @SQLfieldname, {value => $_, text => $_};
128 push @SQLfieldname, column_picks('branches');
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
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($_) };
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');
152 $template->param( modify => 1 );
153 $template->param( code => $letter->{code} );
156 $template->param( adding => 1 );
159 name => $letter->{name},
160 title => $letter->{title},
161 content => ( $content ? $content : $letter->{content} ),
162 ( $module ? $module : $letter->{module} ) => 1,
163 SQLfieldname => \@SQLfieldname,
165 ################## ADD_VALIDATE ##################################
166 # called by add_form, used to insert/modify data in DB
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 (?,?,?,?,?)");
173 $input->param('module'), $input->param('code'),
174 $input->param('name'), $input->param('title'),
175 $input->param('content')
177 print $input->redirect("letter.pl");
179 ################## DELETE_CONFIRM ##################################
180 # called by default form, used to confirm deletion of data in DB
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->{$_} );
191 ################## DELETE_CONFIRMED ##################################
192 # called by delete_confirm, used to effectively confirm deletion of data in DB
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");
202 ################## DEFAULT ##################################
205 if ( $searchfield ne '' ) {
206 $template->param( search => 1 );
207 $template->param( searchfield => $searchfield );
209 my ($results) = StringSearch($searchfield);
211 foreach my $result (@$results) {
213 foreach my $key (qw(module code name)) {
214 $row_data{$key} = $result->{$key};
216 push(@loop_data, \%row_data );
218 $template->param( letter => \@loop_data );
219 } #---- END $OP eq DEFAULT
221 output_html_with_http_headers $input, $cookie, $template->output;