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;
196 my %line = ( 'value' => "", 'text' => '---Biblio---' );
197 push @SQLfieldname, \%line;
198 my %line = ('value' => "biblio.title",'text' => "Title");
199 push @SQLfieldname, \%line;
200 my %line = ('value' => "biblio.author",'text' => "Author");
201 push @SQLfieldname, \%line;
202 my %line = ('value' => "biblio.serial",'text' => "Serial");
203 push @SQLfieldname, \%line;
206 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
208 my %line = ( 'value' => "", 'text' => '---BIBLIO---' );
210 push @SQLfieldname, \%line;
211 while ( ( my $field ) = $sth2->fetchrow_array ) {
213 # note : %line is redefined, otherwise \%line contains the same value for every entry of the list
214 my %line = ( 'value' => "biblio." . $field, 'text' => "biblio." . $field );
215 push @SQLfieldname, \%line;
217 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
219 my %line = ( 'value' => "", 'text' => '---BIBLIOITEMS---' );
220 push @SQLfieldname, \%line;
221 while ( ( my $field ) = $sth2->fetchrow_array ) {
223 'value' => "biblioitems." . $field,
224 'text' => "biblioitems." . $field
226 push @SQLfieldname, \%line;
228 my %line = ( 'value' => "", 'text' => '---ITEMS---' );
229 push @SQLfieldname, \%line;
230 my %line = ( 'value' => "items.content", 'text' => 'items.content' );
231 push @SQLfieldname, \%line;
233 my $sth2 = $dbh->prepare("SHOW COLUMNS from borrowers");
235 my %line = ( 'value' => "", 'text' => '---BORROWERS---' );
236 push @SQLfieldname, \%line;
237 while ( ( my $field ) = $sth2->fetchrow_array ) {
239 'value' => "borrowers." . $field,
240 'text' => "borrowers." . $field
242 push @SQLfieldname, \%line;
246 $template->param( modify => 1 );
247 $template->param( code => $letter->{code} );
250 $template->param( adding => 1 );
253 name => $letter->{name},
254 title => $letter->{title},
255 content => ( $content ? $content : $letter->{content} ),
256 ( $module ? $module : $letter->{module} ) => 1,
257 SQLfieldname => \@SQLfieldname,
260 # END $OP eq ADD_FORM
261 ################## ADD_VALIDATE ##################################
262 # called by add_form, used to insert/modify data in DB
264 elsif ( $op eq 'add_validate' ) {
265 my $dbh = C4::Context->dbh;
268 "replace letter (module,code,name,title,content) values (?,?,?,?,?)");
270 $input->param('module'), $input->param('code'),
271 $input->param('name'), $input->param('title'),
272 $input->param('content')
275 print $input->redirect("letter.pl");
278 # END $OP eq ADD_VALIDATE
279 ################## DELETE_CONFIRM ##################################
280 # called by default form, used to confirm deletion of data in DB
282 elsif ( $op eq 'delete_confirm' ) {
283 my $dbh = C4::Context->dbh;
284 my $sth = $dbh->prepare("select * from letter where code=?");
285 $sth->execute($code);
286 my $data = $sth->fetchrow_hashref;
288 $template->param( module => $data->{module} );
289 $template->param( code => $code );
290 $template->param( name => $data->{'name'} );
291 $template->param( content => $data->{'content'} );
293 # END $OP eq DELETE_CONFIRM
294 ################## DELETE_CONFIRMED ##################################
295 # called by delete_confirm, used to effectively confirm deletion of data in DB
297 elsif ( $op eq 'delete_confirmed' ) {
298 my $dbh = C4::Context->dbh;
299 my $code = uc( $input->param('code') );
300 my $module = $input->param('module');
301 my $sth = $dbh->prepare("delete from letter where module=? and code=?");
302 $sth->execute( $module, $code );
304 print $input->redirect("/cgi-bin/koha/tools/letter.pl");
307 # END $OP eq DELETE_CONFIRMED
308 ################## DEFAULT ##################################
311 if ( $searchfield ne '' ) {
312 $template->param( search => 1 );
313 $template->param( searchfield => $searchfield );
315 my ( $count, $results ) = StringSearch( $searchfield, 'web' );
320 $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
331 $row_data{toggle} = $toggle;
332 $row_data{module} = $results->[$i]{'module'};
333 $row_data{code} = $results->[$i]{'code'};
334 $row_data{name} = $results->[$i]{'name'};
335 push( @loop_data, \%row_data );
337 $template->param( letter => \@loop_data );
338 } #---- END $OP eq DEFAULT
340 output_html_with_http_headers $input, $cookie, $template->output;