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 => 1 },
92 script_name => $script_name,
94 ); # we show only the TMPL_VAR names $op
98 script_name => $script_name,
100 ); # we show only the TMPL_VAR names $op
103 $template->param( action => $script_name );
104 ################## ADD_FORM ##################################
105 # called by default. Used to create form to add or modify a record
106 if ( $op eq 'add_form' ) {
108 #---- if primkey exists, it's a modify action, so read values to modify...
111 my $sth = $dbh->prepare("select * from letter where module=? and code=?");
112 $sth->execute( $module, $code );
113 $letter = $sth->fetchrow_hashref;
119 my %line = ( 'value' => "LibrarianFirstname", 'text' => 'LibrarianFirstname' );
120 push @SQLfieldname, \%line;
121 %line = ( 'value' => "LibrarianSurname", 'text' => 'LibrarianSurname' );
122 push @SQLfieldname, \%line;
123 %line = ( 'value' => "LibrarianEmailaddress", 'text' => 'LibrarianEmailaddress' );
124 push @SQLfieldname, \%line;
125 my $sth2 = $dbh->prepare("SHOW COLUMNS from branches");
127 %line = ( 'value' => "", 'text' => '---BRANCHES---' );
128 push @SQLfieldname, \%line;
130 while ( ( my $field ) = $sth2->fetchrow_array ) {
131 %line = ( 'value' => "branches." . $field, 'text' => "branches." . $field );
132 push @SQLfieldname, \%line;
135 # add acquisition specific tables
136 if ( index( $module, "acquisition" ) > 0 ) {
137 $sth2 = $dbh->prepare("SHOW COLUMNS from aqbooksellers");
139 %line = ( 'value' => "", 'text' => '---BOOKSELLERS---' );
140 push @SQLfieldname, \%line;
141 while ( ( my $field ) = $sth2->fetchrow_array ) {
143 'value' => "aqbooksellers." . $field,
144 'text' => "aqbooksellers." . $field
146 push @SQLfieldname, \%line;
148 $sth2 = $dbh->prepare("SHOW COLUMNS from aqorders");
150 %line = ( 'value' => "", 'text' => '---ORDERS---' );
151 push @SQLfieldname, \%line;
152 while ( ( my $field ) = $sth2->fetchrow_array ) {
154 'value' => "aqorders." . $field,
155 'text' => "aqorders." . $field
157 push @SQLfieldname, \%line;
160 # add issues specific tables
162 elsif ( index( $module, "issues" ) > 0 ) {
163 $sth2 = $dbh->prepare("SHOW COLUMNS from aqbooksellers");
165 %line = ( 'value' => "", 'text' => '---BOOKSELLERS---' );
166 push @SQLfieldname, \%line;
167 while ( ( my $field ) = $sth2->fetchrow_array ) {
169 'value' => "aqbooksellers." . $field,
170 'text' => "aqbooksellers." . $field
172 push @SQLfieldname, \%line;
174 $sth2 = $dbh->prepare("SHOW COLUMNS from serial");
176 %line = ( 'value' => "", 'text' => '---SERIALS---' );
177 push @SQLfieldname, \%line;
178 while ( ( my $field ) = $sth2->fetchrow_array ) {
179 %line = ( 'value' => "serial." . $field, 'text' => "serial." . $field );
180 push @SQLfieldname, \%line;
182 $sth2 = $dbh->prepare("SHOW COLUMNS from subscription");
184 %line = ( 'value' => "", 'text' => '---SUBSCRIPTION---' );
185 push @SQLfieldname, \%line;
186 while ( ( my $field ) = $sth2->fetchrow_array ) {
188 'value' => "subscription." . $field,
189 'text' => "subscription." . $field
191 push @SQLfieldname, \%line;
193 %line = ('value' => "", 'text' => '---Biblio---' );
194 push @SQLfieldname, \%line;
195 foreach(qw(title author serial)) {
196 %line = ('value' => "biblio.$_", 'text' => ucfirst($_));
197 push @SQLfieldname, \%line;
201 $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
203 %line = ( 'value' => "", 'text' => '---BIBLIO---' );
205 push @SQLfieldname, \%line;
206 while ( ( my $field ) = $sth2->fetchrow_array ) {
208 # note : %line is redefined, otherwise \%line contains the same value for every entry of the list
209 my %line = ( 'value' => "biblio." . $field, 'text' => "biblio." . $field );
210 push @SQLfieldname, \%line;
212 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
214 %line = ( 'value' => "", 'text' => '---BIBLIOITEMS---' );
215 push @SQLfieldname, \%line;
216 while ( ( my $field ) = $sth2->fetchrow_array ) {
218 'value' => "biblioitems." . $field,
219 'text' => "biblioitems." . $field
221 push @SQLfieldname, \%line;
223 %line = ( 'value' => "", 'text' => '---ITEMS---' );
224 push @SQLfieldname, \%line;
225 %line = ( 'value' => "items.content", 'text' => 'items.content' );
226 push @SQLfieldname, \%line;
228 $sth2 = $dbh->prepare("SHOW COLUMNS from borrowers");
230 %line = ( 'value' => "", 'text' => '---BORROWERS---' );
231 push @SQLfieldname, \%line;
232 while ( ( my $field ) = $sth2->fetchrow_array ) {
234 'value' => "borrowers." . $field,
235 'text' => "borrowers." . $field
237 push @SQLfieldname, \%line;
241 $template->param( modify => 1 );
242 $template->param( code => $letter->{code} );
245 $template->param( adding => 1 );
248 name => $letter->{name},
249 title => $letter->{title},
250 content => ( $content ? $content : $letter->{content} ),
251 ( $module ? $module : $letter->{module} ) => 1,
252 SQLfieldname => \@SQLfieldname,
254 ################## ADD_VALIDATE ##################################
255 # called by add_form, used to insert/modify data in DB
257 elsif ( $op eq 'add_validate' ) {
258 my $dbh = C4::Context->dbh;
261 "replace letter (module,code,name,title,content) values (?,?,?,?,?)");
263 $input->param('module'), $input->param('code'),
264 $input->param('name'), $input->param('title'),
265 $input->param('content')
268 print $input->redirect("letter.pl");
270 ################## DELETE_CONFIRM ##################################
271 # called by default form, used to confirm deletion of data in DB
273 elsif ( $op eq 'delete_confirm' ) {
274 my $dbh = C4::Context->dbh;
275 my $sth = $dbh->prepare("select * from letter where code=?");
276 $sth->execute($code);
277 my $data = $sth->fetchrow_hashref;
279 $template->param( code => $code );
280 foreach (qw(module name content)) {
281 $template->param( $_ => $data->{$_} );
283 ################## DELETE_CONFIRMED ##################################
284 # called by delete_confirm, used to effectively confirm deletion of data in DB
286 elsif ( $op eq 'delete_confirmed' ) {
287 my $dbh = C4::Context->dbh;
288 my $code = uc( $input->param('code') );
289 my $module = $input->param('module');
290 my $sth = $dbh->prepare("delete from letter where module=? and code=?");
291 $sth->execute( $module, $code );
293 print $input->redirect("/cgi-bin/koha/tools/letter.pl");
295 ################## DEFAULT ##################################
298 if ( $searchfield ne '' ) {
299 $template->param( search => 1 );
300 $template->param( searchfield => $searchfield );
302 my ( $count, $results ) = StringSearch( $searchfield, 'web' );
307 $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
311 $toggle = ($toggle) ? 0 : 1;
313 $row_data{toggle} = $toggle;
314 foreach (qw(module code name)) {
315 $row_data{$_} = $results->[$i]{$_};
317 push( @loop_data, \%row_data );
319 $template->param( letter => \@loop_data );
320 } #---- END $OP eq DEFAULT
322 output_html_with_http_headers $input, $cookie, $template->output;