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, $type ) = @_;
48 my $dbh = C4
::Context
->dbh;
49 $searchstring =~ s/\'/\\\'/g;
50 my @data = split( ' ', $searchstring );
54 "Select * from letter where (code like ?) order by module,code");
55 $sth->execute("$data[0]%");
59 while ( my $data = $sth->fetchrow_hashref ) {
60 push( @results, $data );
64 return ( $cnt, \
@results );
68 my $searchfield = $input->param('searchfield');
69 my $offset = $input->param('offset');
70 my $script_name = "/cgi-bin/koha/tools/letter.pl";
71 my $code = $input->param('code');
72 my $module = $input->param('module');
73 my $content = $input->param('content');
75 my $op = $input->param('op');
76 $searchfield =~ s/\,//g;
77 my $dbh = C4
::Context
->dbh;
79 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
81 template_name
=> "tools/letter.tmpl",
85 flagsrequired
=> { tools
=> 'edit_notices' },
91 $template->param($op => 1);
93 $template->param(else => 1);
95 # we show only the TMPL_VAR names $op
98 script_name
=> $script_name,
99 action
=> $script_name
101 ################## ADD_FORM ##################################
102 # called by default. Used to create form to add or modify a record
103 if ( $op eq 'add_form' ) {
105 #---- if primkey exists, it's a modify action, so read values to modify...
108 my $sth = $dbh->prepare("select * from letter where module=? and code=?");
109 $sth->execute( $module, $code );
110 $letter = $sth->fetchrow_hashref;
116 push @SQLfieldname, { 'value' => "LibrarianFirstname", 'text' => 'LibrarianFirstname' };
117 push @SQLfieldname, { 'value' => "LibrarianSurname", 'text' => 'LibrarianSurname' };
118 push @SQLfieldname, { 'value' => "LibrarianEmailaddress", 'text' => 'LibrarianEmailaddress' };
119 my $sth2 = $dbh->prepare("SHOW COLUMNS from branches");
121 push @SQLfieldname, { 'value' => "", 'text' => '---BRANCHES---' };
123 while ( ( my $field ) = $sth2->fetchrow_array ) {
124 push @SQLfieldname, { 'value' => "branches." . $field, 'text' => "branches." . $field };
127 # add acquisition specific tables
128 if ( index( $module, "acquisition" ) > 0 ) { # FIXME: imprecise comparison
129 $sth2 = $dbh->prepare("SHOW COLUMNS from aqbooksellers");
131 push @SQLfieldname, { 'value' => "", 'text' => '---BOOKSELLERS---' };
132 while ( ( my $field ) = $sth2->fetchrow_array ) {
133 push @SQLfieldname, {
134 'value' => "aqbooksellers." . $field,
135 'text' => "aqbooksellers." . $field
138 $sth2 = $dbh->prepare("SHOW COLUMNS from aqorders");
140 push @SQLfieldname, { 'value' => "", 'text' => '---ORDERS---' };
141 while ( ( my $field ) = $sth2->fetchrow_array ) {
142 push @SQLfieldname, {
143 'value' => "aqorders." . $field,
144 'text' => "aqorders." . $field
148 # add issues specific tables
150 elsif ( index( $module, "issues" ) > 0 ) { # FIXME: imprecise comparison
151 $sth2 = $dbh->prepare("SHOW COLUMNS from aqbooksellers");
153 push @SQLfieldname, { 'value' => "", 'text' => '---BOOKSELLERS---' };
154 while ( ( my $field ) = $sth2->fetchrow_array ) {
155 push @SQLfieldname, {
156 'value' => "aqbooksellers." . $field,
157 'text' => "aqbooksellers." . $field
160 $sth2 = $dbh->prepare("SHOW COLUMNS from serial");
162 push @SQLfieldname, { 'value' => "", 'text' => '---SERIALS---' };
163 while ( ( my $field ) = $sth2->fetchrow_array ) {
164 push @SQLfieldname, { 'value' => "serial." . $field, 'text' => "serial." . $field };
166 $sth2 = $dbh->prepare("SHOW COLUMNS from subscription");
168 push @SQLfieldname, { 'value' => "", 'text' => '---SUBSCRIPTION---' };
169 while ( ( my $field ) = $sth2->fetchrow_array ) {
170 push @SQLfieldname, {
171 'value' => "subscription." . $field,
172 'text' => "subscription." . $field
175 push @SQLfieldname, { 'value' => "", 'text' => '---Biblio---' };
176 foreach(qw(title author serial)) {
177 push @SQLfieldname, { 'value' => "biblio.$_", 'text' => ucfirst($_) };
181 $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
183 push @SQLfieldname, { 'value' => "", 'text' => '---BIBLIO---' };
185 while ( ( my $field ) = $sth2->fetchrow_array ) {
186 push @SQLfieldname, { 'value' => "biblio." . $field, 'text' => "biblio." . $field };
188 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
190 push @SQLfieldname, { 'value' => "", 'text' => '---BIBLIOITEMS---' };
191 while ( ( my $field ) = $sth2->fetchrow_array ) {
192 push @SQLfieldname, {
193 'value' => "biblioitems." . $field,
194 'text' => "biblioitems." . $field
197 push @SQLfieldname, { 'value' => "", 'text' => '---ITEMS---' };
198 push @SQLfieldname, { 'value' => "items.content", 'text' => 'items.content' };
200 $sth2 = $dbh->prepare("SHOW COLUMNS from borrowers");
202 push @SQLfieldname, { 'value' => "", 'text' => '---BORROWERS---' };
203 while ( ( my $field ) = $sth2->fetchrow_array ) {
204 push @SQLfieldname, {
205 'value' => "borrowers." . $field,
206 'text' => "borrowers." . $field
211 $template->param( modify
=> 1 );
212 $template->param( code
=> $letter->{code
} );
215 $template->param( adding
=> 1 );
218 name
=> $letter->{name
},
219 title
=> $letter->{title
},
220 content
=> ( $content ?
$content : $letter->{content
} ),
221 ( $module ?
$module : $letter->{module
} ) => 1,
222 SQLfieldname
=> \
@SQLfieldname,
224 ################## ADD_VALIDATE ##################################
225 # called by add_form, used to insert/modify data in DB
227 elsif ( $op eq 'add_validate' ) {
228 my $dbh = C4
::Context
->dbh;
231 "replace letter (module,code,name,title,content) values (?,?,?,?,?)");
233 $input->param('module'), $input->param('code'),
234 $input->param('name'), $input->param('title'),
235 $input->param('content')
238 print $input->redirect("letter.pl");
240 ################## DELETE_CONFIRM ##################################
241 # called by default form, used to confirm deletion of data in DB
243 elsif ( $op eq 'delete_confirm' ) {
244 my $dbh = C4
::Context
->dbh;
245 my $sth = $dbh->prepare("select * from letter where code=?");
246 $sth->execute($code);
247 my $data = $sth->fetchrow_hashref;
249 $template->param( code
=> $code );
250 foreach (qw(module name content)) {
251 $template->param( $_ => $data->{$_} );
253 ################## DELETE_CONFIRMED ##################################
254 # called by delete_confirm, used to effectively confirm deletion of data in DB
256 elsif ( $op eq 'delete_confirmed' ) {
257 my $dbh = C4
::Context
->dbh;
258 my $code = uc( $input->param('code') );
259 my $module = $input->param('module');
260 my $sth = $dbh->prepare("delete from letter where module=? and code=?");
261 $sth->execute( $module, $code );
263 print $input->redirect("/cgi-bin/koha/tools/letter.pl");
265 ################## DEFAULT ##################################
268 if ( $searchfield ne '' ) {
269 $template->param( search
=> 1 );
270 $template->param( searchfield
=> $searchfield );
272 my ( $count, $results ) = StringSearch
( $searchfield, 'web' );
277 $i < ( $offset + $pagesize < $count ?
$offset + $pagesize : $count ) ;
281 $toggle = ($toggle) ?
0 : 1;
283 $row_data{toggle
} = $toggle;
284 foreach (qw(module code name)) {
285 $row_data{$_} = $results->[$i]{$_};
287 push( @loop_data, \
%row_data );
289 $template->param( letter
=> \
@loop_data );
290 } #---- END $OP eq DEFAULT
292 output_html_with_http_headers
$input, $cookie, $template->output;