updates to acqui - first of several commits
[koha.git] / tools / letter.pl
blobabf85fb406d3b01003d44dc7cbf26d6051e5483b
1 #!/usr/bin/perl
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
10 # version.
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
22 ALGO :
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.
27 if $op=add_form
28 - if primkey exists, this is a modification,so we read the $primkey record
29 - builds the add/modify form
30 if $op=add_validate
31 - the user has just send datas, so we create/modify the record
32 if $op=delete_form
33 - we show the record having primkey=$primkey and ask for deletion validation form
34 if $op=delete_confirm
35 - we delete the record having primkey=$primkey
37 =cut
39 use strict;
40 use CGI;
41 use C4::Dates;
42 use C4::Auth;
43 use C4::Context;
44 use C4::Output;
46 sub StringSearch {
47 my ( $searchstring, $type ) = @_;
48 my $dbh = C4::Context->dbh;
49 $searchstring =~ s/\'/\\\'/g;
50 my @data = split( ' ', $searchstring );
51 my $count = @data;
52 my $sth =
53 $dbh->prepare(
54 "Select * from letter where (code like ?) order by module,code");
55 $sth->execute("$data[0]%");
56 my @results;
57 my $cnt = 0;
59 while ( my $data = $sth->fetchrow_hashref ) {
60 push( @results, $data );
61 $cnt++;
63 $sth->finish;
64 return ( $cnt, \@results );
67 my $input = new CGI;
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');
74 my $pagesize = 20;
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",
82 query => $input,
83 type => "intranet",
84 authnotrequired => 0,
85 flagsrequired => { tools => 1 },
86 debug => 1,
90 if ($op) {
91 $template->param(
92 script_name => $script_name,
93 $op => 1
94 ); # we show only the TMPL_VAR names $op
96 else {
97 $template->param(
98 script_name => $script_name,
99 else => 1
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...
109 my $letter;
110 if ($code) {
111 my $sth = $dbh->prepare("select * from letter where module=? and code=?");
112 $sth->execute( $module, $code );
113 $letter = $sth->fetchrow_hashref;
114 $sth->finish;
117 # build field list
118 my @SQLfieldname;
119 my %line = ( 'value' => "LibrarianFirstname", 'text' => 'LibrarianFirstname' );
120 push @SQLfieldname, \%line;
121 my %line = ( 'value' => "LibrarianSurname", 'text' => 'LibrarianSurname' );
122 push @SQLfieldname, \%line;
123 my %line = ( 'value' => "LibrarianEmailaddress", 'text' => 'LibrarianEmailaddress' );
124 push @SQLfieldname, \%line;
125 my $sth2 = $dbh->prepare("SHOW COLUMNS from branches");
126 $sth2->execute;
127 my %line = ( 'value' => "", 'text' => '---BRANCHES---' );
128 push @SQLfieldname, \%line;
130 while ( ( my $field ) = $sth2->fetchrow_array ) {
131 my %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");
138 $sth2->execute;
139 my %line = ( 'value' => "", 'text' => '---BOOKSELLERS---' );
140 push @SQLfieldname, \%line;
141 while ( ( my $field ) = $sth2->fetchrow_array ) {
142 my %line = (
143 'value' => "aqbooksellers." . $field,
144 'text' => "aqbooksellers." . $field
146 push @SQLfieldname, \%line;
148 $sth2 = $dbh->prepare("SHOW COLUMNS from aqorders");
149 $sth2->execute;
150 my %line = ( 'value' => "", 'text' => '---ORDERS---' );
151 push @SQLfieldname, \%line;
152 while ( ( my $field ) = $sth2->fetchrow_array ) {
153 my %line = (
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");
164 $sth2->execute;
165 my %line = ( 'value' => "", 'text' => '---BOOKSELLERS---' );
166 push @SQLfieldname, \%line;
167 while ( ( my $field ) = $sth2->fetchrow_array ) {
168 my %line = (
169 'value' => "aqbooksellers." . $field,
170 'text' => "aqbooksellers." . $field
172 push @SQLfieldname, \%line;
174 $sth2 = $dbh->prepare("SHOW COLUMNS from serial");
175 $sth2->execute;
176 my %line = ( 'value' => "", 'text' => '---SERIALS---' );
177 push @SQLfieldname, \%line;
178 while ( ( my $field ) = $sth2->fetchrow_array ) {
179 my %line = ( 'value' => "serial." . $field, 'text' => "serial." . $field );
180 push @SQLfieldname, \%line;
182 $sth2 = $dbh->prepare("SHOW COLUMNS from subscription");
183 $sth2->execute;
184 my %line = ( 'value' => "", 'text' => '---SUBSCRIPTION---' );
185 push @SQLfieldname, \%line;
186 while ( ( my $field ) = $sth2->fetchrow_array ) {
187 my %line = (
188 'value' => "subscription." . $field,
189 'text' => "subscription." . $field
191 push @SQLfieldname, \%line;
193 my %line = ('value' => "", 'text' => '---Biblio---' );
194 push @SQLfieldname, \%line;
195 foreach(qw(title author serial)) {
196 my %line = ('value' => "biblio.$_", 'text' => ucfirst($_));
197 push @SQLfieldname, \%line;
200 else {
201 $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
202 $sth2->execute;
203 my %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");
213 $sth2->execute;
214 my %line = ( 'value' => "", 'text' => '---BIBLIOITEMS---' );
215 push @SQLfieldname, \%line;
216 while ( ( my $field ) = $sth2->fetchrow_array ) {
217 my %line = (
218 'value' => "biblioitems." . $field,
219 'text' => "biblioitems." . $field
221 push @SQLfieldname, \%line;
223 my %line = ( 'value' => "", 'text' => '---ITEMS---' );
224 push @SQLfieldname, \%line;
225 my %line = ( 'value' => "items.content", 'text' => 'items.content' );
226 push @SQLfieldname, \%line;
228 $sth2 = $dbh->prepare("SHOW COLUMNS from borrowers");
229 $sth2->execute;
230 my %line = ( 'value' => "", 'text' => '---BORROWERS---' );
231 push @SQLfieldname, \%line;
232 while ( ( my $field ) = $sth2->fetchrow_array ) {
233 my %line = (
234 'value' => "borrowers." . $field,
235 'text' => "borrowers." . $field
237 push @SQLfieldname, \%line;
240 if ($code) {
241 $template->param( modify => 1 );
242 $template->param( code => $letter->{code} );
244 else {
245 $template->param( adding => 1 );
247 $template->param(
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;
259 my $sth =
260 $dbh->prepare(
261 "replace letter (module,code,name,title,content) values (?,?,?,?,?)");
262 $sth->execute(
263 $input->param('module'), $input->param('code'),
264 $input->param('name'), $input->param('title'),
265 $input->param('content')
267 $sth->finish;
268 print $input->redirect("letter.pl");
269 exit;
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;
278 $sth->finish;
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 );
292 $sth->finish;
293 print $input->redirect("/cgi-bin/koha/tools/letter.pl");
294 return;
295 ################## DEFAULT ##################################
297 else { # DEFAULT
298 if ( $searchfield ne '' ) {
299 $template->param( search => 1 );
300 $template->param( searchfield => $searchfield );
302 my ( $count, $results ) = StringSearch( $searchfield, 'web' );
303 my $toggle = 0;
304 my @loop_data = ();
305 for (
306 my $i = $offset ;
307 $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
308 $i++
311 $toggle = ($toggle) ? 0 : 1;
312 my %row_data;
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;