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"
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;
93 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
95 template_name
=> "tools/letter.tmpl",
99 flagsrequired
=> { tools
=> 'edit_notices' },
105 $template->param($op => 1);
107 $template->param(else => 1);
109 # we show only the TMPL_VAR names $op
112 script_name
=> $script_name,
113 action
=> $script_name
115 ################## ADD_FORM ##################################
116 # called by default. Used to create form to add or modify a record
117 if ( $op eq 'add_form' ) {
119 #---- if primkey exists, it's a modify action, so read values to modify...
122 my $sth = $dbh->prepare("SELECT * FROM letter WHERE module=? AND code=?");
123 $sth->execute( $module, $code );
124 $letter = $sth->fetchrow_hashref;
129 foreach (qw(LibrarianFirstname LibrarianSurname LibrarianEmailaddress)) {
130 push @SQLfieldname, {value
=> $_, text
=> $_};
132 push @SQLfieldname, column_picks
('branches');
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
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($_) };
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');
156 $template->param( modify
=> 1 );
157 $template->param( code
=> $letter->{code
} );
160 $template->param( adding
=> 1 );
163 name
=> $letter->{name
},
164 title
=> $letter->{title
},
165 content
=> ( $content ?
$content : $letter->{content
} ),
166 ( $module ?
$module : $letter->{module
} ) => 1,
167 SQLfieldname
=> \
@SQLfieldname,
169 ################## ADD_VALIDATE ##################################
170 # called by add_form, used to insert/modify data in DB
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 (?,?,?,?,?)");
177 $input->param('module'), $input->param('code'),
178 $input->param('name'), $input->param('title'),
179 $input->param('content')
181 print $input->redirect("letter.pl");
183 ################## DELETE_CONFIRM ##################################
184 # called by default form, used to confirm deletion of data in DB
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->{$_} );
195 ################## DELETE_CONFIRMED ##################################
196 # called by delete_confirm, used to effectively confirm deletion of data in DB
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");
206 ################## DEFAULT ##################################
209 if ( $searchfield ne '' ) {
210 $template->param( search
=> 1 );
211 $template->param( searchfield
=> $searchfield );
213 my ($results) = StringSearch
($searchfield);
215 foreach my $result (@
$results) {
217 foreach my $key (qw(module code name)) {
218 $row_data{$key} = $result->{$key};
220 push(@loop_data, \
%row_data );
222 $template->param( letter
=> \
@loop_data );
223 } #---- END $OP eq DEFAULT
225 output_html_with_http_headers
$input, $cookie, $template->output;