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 );
66 return ( $cnt, \@results );
70 my $searchfield = $input->param('searchfield');
71 my $offset = $input->param('offset');
72 my $script_name = "/cgi-bin/koha/tools/letter.pl";
73 my $code = $input->param('code');
74 my $module = $input->param('module');
75 my $content = $input->param('content');
77 my $op = $input->param('op');
78 $searchfield =~ s/\,//g;
79 my $dbh = C4::Context->dbh;
81 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
83 template_name => "tools/letter.tmpl",
87 flagsrequired => { tools => 1 },
94 script_name => $script_name,
96 ); # we show only the TMPL_VAR names $op
100 script_name => $script_name,
102 ); # we show only the TMPL_VAR names $op
105 $template->param( action => $script_name );
106 ################## ADD_FORM ##################################
107 # called by default. Used to create form to add or modify a record
108 if ( $op eq 'add_form' ) {
110 #---- if primkey exists, it's a modify action, so read values to modify...
114 $dbh->prepare("select * from letter where module=? and code=?");
115 $sth->execute( $module, $code );
116 $letter = $sth->fetchrow_hashref;
122 my %line = ( 'value' => "LibrarianFirstname", 'text' => 'LibrarianFirstname' );
123 push @SQLfieldname, \%line;
124 my %line = ( 'value' => "LibrarianSurname", 'text' => 'LibrarianSurname' );
125 push @SQLfieldname, \%line;
126 my %line = ( 'value' => "LibrarianEmailaddress", 'text' => 'LibrarianEmailaddress' );
127 push @SQLfieldname, \%line;
128 my $sth2 = $dbh->prepare("SHOW COLUMNS from branches");
130 my %line = ( 'value' => "", 'text' => '---BRANCHES---' );
131 push @SQLfieldname, \%line;
133 while ( ( my $field ) = $sth2->fetchrow_array ) {
134 my %line = ( 'value' => "branches." . $field, 'text' => "branches." . $field );
135 push @SQLfieldname, \%line;
138 # add acquisition specific tables
139 if ( index( $module, "acquisition" ) > 0 ) {
140 my $sth2 = $dbh->prepare("SHOW COLUMNS from aqbooksellers");
142 my %line = ( 'value' => "", 'text' => '---BOOKSELLERS---' );
143 push @SQLfieldname, \%line;
144 while ( ( my $field ) = $sth2->fetchrow_array ) {
146 'value' => "aqbooksellers." . $field,
147 'text' => "aqbooksellers." . $field
149 push @SQLfieldname, \%line;
151 my $sth2 = $dbh->prepare("SHOW COLUMNS from aqorders");
153 my %line = ( 'value' => "", 'text' => '---ORDERS---' );
154 push @SQLfieldname, \%line;
155 while ( ( my $field ) = $sth2->fetchrow_array ) {
157 'value' => "aqorders." . $field,
158 'text' => "aqorders." . $field
160 push @SQLfieldname, \%line;
163 # add issues specific tables
165 elsif ( index( $module, "issues" ) > 0 ) {
166 my $sth2 = $dbh->prepare("SHOW COLUMNS from aqbooksellers");
168 my %line = ( 'value' => "", 'text' => '---BOOKSELLERS---' );
169 push @SQLfieldname, \%line;
170 while ( ( my $field ) = $sth2->fetchrow_array ) {
172 'value' => "aqbooksellers." . $field,
173 'text' => "aqbooksellers." . $field
175 push @SQLfieldname, \%line;
177 my $sth2 = $dbh->prepare("SHOW COLUMNS from serial");
179 my %line = ( 'value' => "", 'text' => '---SERIALS---' );
180 push @SQLfieldname, \%line;
181 while ( ( my $field ) = $sth2->fetchrow_array ) {
182 my %line = ( 'value' => "serial." . $field, 'text' => "serial." . $field );
183 push @SQLfieldname, \%line;
185 my $sth2 = $dbh->prepare("SHOW COLUMNS from subscription");
187 my %line = ( 'value' => "", 'text' => '---SUBSCRIPTION---' );
188 push @SQLfieldname, \%line;
189 while ( ( my $field ) = $sth2->fetchrow_array ) {
191 'value' => "subscription." . $field,
192 'text' => "subscription." . $field
194 push @SQLfieldname, \%line;
197 # add biblio specific tables.
200 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
202 my %line = ( 'value' => "", 'text' => '---BIBLIO---' );
204 push @SQLfieldname, \%line;
205 while ( ( my $field ) = $sth2->fetchrow_array ) {
207 # note : %line is redefined, otherwise \%line contains the same value for every entry of the list
208 my %line = ( 'value' => "biblio." . $field, 'text' => "biblio." . $field );
209 push @SQLfieldname, \%line;
211 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
213 my %line = ( 'value' => "", 'text' => '---BIBLIOITEMS---' );
214 push @SQLfieldname, \%line;
215 while ( ( my $field ) = $sth2->fetchrow_array ) {
217 'value' => "biblioitems." . $field,
218 'text' => "biblioitems." . $field
220 push @SQLfieldname, \%line;
222 my %line = ( 'value' => "", 'text' => '---ITEMS---' );
223 push @SQLfieldname, \%line;
224 my %line = ( 'value' => "items.content", 'text' => 'items.content' );
225 push @SQLfieldname, \%line;
227 my $sth2 = $dbh->prepare("SHOW COLUMNS from borrowers");
229 my %line = ( 'value' => "", 'text' => '---BORROWERS---' );
230 push @SQLfieldname, \%line;
231 while ( ( my $field ) = $sth2->fetchrow_array ) {
233 'value' => "borrowers." . $field,
234 'text' => "borrowers." . $field
236 push @SQLfieldname, \%line;
240 $template->param( modify => 1 );
241 $template->param( code => $letter->{code} );
244 $template->param( adding => 1 );
247 name => $letter->{name},
248 title => $letter->{title},
249 content => ( $content ? $content : $letter->{content} ),
250 ( $module ? $module : $letter->{module} ) => 1,
251 SQLfieldname => \@SQLfieldname,
254 # END $OP eq ADD_FORM
255 ################## ADD_VALIDATE ##################################
256 # called by add_form, used to insert/modify data in DB
258 elsif ( $op eq 'add_validate' ) {
259 my $dbh = C4::Context->dbh;
262 "replace letter (module,code,name,title,content) values (?,?,?,?,?)");
264 $input->param('module'), $input->param('code'),
265 $input->param('name'), $input->param('title'),
266 $input->param('content')
269 print $input->redirect("letter.pl");
272 # END $OP eq ADD_VALIDATE
273 ################## DELETE_CONFIRM ##################################
274 # called by default form, used to confirm deletion of data in DB
276 elsif ( $op eq 'delete_confirm' ) {
277 my $dbh = C4::Context->dbh;
278 my $sth = $dbh->prepare("select * from letter where code=?");
279 $sth->execute($code);
280 my $data = $sth->fetchrow_hashref;
282 $template->param( module => $data->{module} );
283 $template->param( code => $code );
284 $template->param( name => $data->{'name'} );
285 $template->param( content => $data->{'content'} );
287 # END $OP eq DELETE_CONFIRM
288 ################## DELETE_CONFIRMED ##################################
289 # called by delete_confirm, used to effectively confirm deletion of data in DB
291 elsif ( $op eq 'delete_confirmed' ) {
292 my $dbh = C4::Context->dbh;
293 my $code = uc( $input->param('code') );
294 my $module = $input->param('module');
295 my $sth = $dbh->prepare("delete from letter where module=? and code=?");
296 $sth->execute( $module, $code );
298 print $input->redirect("/cgi-bin/koha/tools/letter.pl");
301 # END $OP eq DELETE_CONFIRMED
302 ################## DEFAULT ##################################
305 if ( $searchfield ne '' ) {
306 $template->param( search => 1 );
307 $template->param( searchfield => $searchfield );
309 my ( $count, $results ) = StringSearch( $searchfield, 'web' );
314 $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
325 $row_data{toggle} = $toggle;
326 $row_data{module} = $results->[$i]{'module'};
327 $row_data{code} = $results->[$i]{'code'};
328 $row_data{name} = $results->[$i]{'name'};
329 push( @loop_data, \%row_data );
331 $template->param( letter => \@loop_data );
332 } #---- END $OP eq DEFAULT
334 output_html_with_http_headers $input, $cookie, $template->output;