Removing warn in Auth.pm
[koha.git] / tools / letter.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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
19
20 =head1 tools/letter.pl
21
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
36
37 =cut
38
39 use strict;
40 use CGI;
41 use C4::Date;
42 use C4::Auth;
43 use C4::Context;
44 use C4::Output;
45
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;
58
59     while ( my $data = $sth->fetchrow_hashref ) {
60         push( @results, $data );
61         $cnt++;
62     }
63
64     #  $sth->execute;
65     $sth->finish;
66     return ( $cnt, \@results );
67 }
68
69 my $input       = new CGI;
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');
76 my $pagesize    = 20;
77 my $op          = $input->param('op');
78 $searchfield =~ s/\,//g;
79 my $dbh = C4::Context->dbh;
80
81 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
82     {
83         template_name   => "tools/letter.tmpl",
84         query           => $input,
85         type            => "intranet",
86         authnotrequired => 0,
87         flagsrequired   => { tools => 1 },
88         debug           => 1,
89     }
90 );
91
92 if ($op) {
93     $template->param(
94         script_name => $script_name,
95         $op         => 1
96     );    # we show only the TMPL_VAR names $op
97 }
98 else {
99     $template->param(
100         script_name => $script_name,
101         else        => 1
102     );    # we show only the TMPL_VAR names $op
103 }
104
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' ) {
109
110     #---- if primkey exists, it's a modify action, so read values to modify...
111     my $letter;
112     if ($code) {
113         my $sth =
114           $dbh->prepare("select * from letter where module=? and code=?");
115         $sth->execute( $module, $code );
116         $letter = $sth->fetchrow_hashref;
117         $sth->finish;
118     }
119
120     # build field list
121     my @SQLfieldname;
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");
129     $sth2->execute;
130     my %line = ( 'value' => "", 'text' => '---BRANCHES---' );
131     push @SQLfieldname, \%line;
132
133     while ( ( my $field ) = $sth2->fetchrow_array ) {
134         my %line = ( 'value' => "branches." . $field, 'text' => "branches." . $field );
135         push @SQLfieldname, \%line;
136     }
137
138     # add acquisition specific tables
139     if ( index( $module, "acquisition" ) > 0 ) {
140         my $sth2 = $dbh->prepare("SHOW COLUMNS from aqbooksellers");
141         $sth2->execute;
142         my %line = ( 'value' => "", 'text' => '---BOOKSELLERS---' );
143         push @SQLfieldname, \%line;
144         while ( ( my $field ) = $sth2->fetchrow_array ) {
145             my %line = (
146                 'value' => "aqbooksellers." . $field,
147                 'text'  => "aqbooksellers." . $field
148             );
149             push @SQLfieldname, \%line;
150         }
151         my $sth2 = $dbh->prepare("SHOW COLUMNS from aqorders");
152         $sth2->execute;
153         my %line = ( 'value' => "", 'text' => '---ORDERS---' );
154         push @SQLfieldname, \%line;
155         while ( ( my $field ) = $sth2->fetchrow_array ) {
156             my %line = (
157                 'value' => "aqorders." . $field,
158                 'text'  => "aqorders." . $field
159             );
160             push @SQLfieldname, \%line;
161         }
162
163         # add issues specific tables
164     }
165     elsif ( index( $module, "issues" ) > 0 ) {
166         my $sth2 = $dbh->prepare("SHOW COLUMNS from aqbooksellers");
167         $sth2->execute;
168         my %line = ( 'value' => "", 'text' => '---BOOKSELLERS---' );
169         push @SQLfieldname, \%line;
170         while ( ( my $field ) = $sth2->fetchrow_array ) {
171             my %line = (
172                 'value' => "aqbooksellers." . $field,
173                 'text'  => "aqbooksellers." . $field
174             );
175             push @SQLfieldname, \%line;
176         }
177         my $sth2 = $dbh->prepare("SHOW COLUMNS from serial");
178         $sth2->execute;
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;
184         }
185         my $sth2 = $dbh->prepare("SHOW COLUMNS from subscription");
186         $sth2->execute;
187         my %line = ( 'value' => "", 'text' => '---SUBSCRIPTION---' );
188         push @SQLfieldname, \%line;
189         while ( ( my $field ) = $sth2->fetchrow_array ) {
190             my %line = (
191                 'value' => "subscription." . $field,
192                 'text'  => "subscription." . $field
193             );
194             push @SQLfieldname, \%line;
195         }
196
197         # add biblio specific tables.
198     }
199     else {
200         my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
201         $sth2->execute;
202         my %line = ( 'value' => "", 'text' => '---BIBLIO---' );
203
204         push @SQLfieldname, \%line;
205         while ( ( my $field ) = $sth2->fetchrow_array ) {
206
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;
210         }
211         my $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
212         $sth2->execute;
213         my %line = ( 'value' => "", 'text' => '---BIBLIOITEMS---' );
214         push @SQLfieldname, \%line;
215         while ( ( my $field ) = $sth2->fetchrow_array ) {
216             my %line = (
217                 'value' => "biblioitems." . $field,
218                 'text'  => "biblioitems." . $field
219             );
220             push @SQLfieldname, \%line;
221         }
222         my %line = ( 'value' => "", 'text' => '---ITEMS---' );
223         push @SQLfieldname, \%line;
224         my %line = ( 'value' => "items.content", 'text' => 'items.content' );
225         push @SQLfieldname, \%line;
226
227         my $sth2 = $dbh->prepare("SHOW COLUMNS from borrowers");
228         $sth2->execute;
229         my %line = ( 'value' => "", 'text' => '---BORROWERS---' );
230         push @SQLfieldname, \%line;
231         while ( ( my $field ) = $sth2->fetchrow_array ) {
232             my %line = (
233                 'value' => "borrowers." . $field,
234                 'text'  => "borrowers." . $field
235             );
236             push @SQLfieldname, \%line;
237         }
238     }
239     if ($code) {
240         $template->param( modify => 1 );
241         $template->param( code   => $letter->{code} );
242     }
243     else {
244         $template->param( adding => 1 );
245     }
246     $template->param(
247         name    => $letter->{name},
248         title   => $letter->{title},
249         content => ( $content ? $content : $letter->{content} ),
250         ( $module ? $module : $letter->{module} ) => 1,
251         SQLfieldname => \@SQLfieldname,
252     );
253
254     # END $OP eq ADD_FORM
255 ################## ADD_VALIDATE ##################################
256     # called by add_form, used to insert/modify data in DB
257 }
258 elsif ( $op eq 'add_validate' ) {
259     my $dbh = C4::Context->dbh;
260     my $sth =
261       $dbh->prepare(
262         "replace letter (module,code,name,title,content) values (?,?,?,?,?)");
263     $sth->execute(
264         $input->param('module'), $input->param('code'),
265         $input->param('name'),   $input->param('title'),
266         $input->param('content')
267     );
268     $sth->finish;
269     print $input->redirect("letter.pl");
270     exit;
271
272     # END $OP eq ADD_VALIDATE
273 ################## DELETE_CONFIRM ##################################
274     # called by default form, used to confirm deletion of data in DB
275 }
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;
281     $sth->finish;
282     $template->param( module  => $data->{module} );
283     $template->param( code    => $code );
284     $template->param( name    => $data->{'name'} );
285     $template->param( content => $data->{'content'} );
286
287     # END $OP eq DELETE_CONFIRM
288 ################## DELETE_CONFIRMED ##################################
289   # called by delete_confirm, used to effectively confirm deletion of data in DB
290 }
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 );
297     $sth->finish;
298     print $input->redirect("/cgi-bin/koha/tools/letter.pl");
299     return;
300
301     # END $OP eq DELETE_CONFIRMED
302 ################## DEFAULT ##################################
303 }
304 else {    # DEFAULT
305     if ( $searchfield ne '' ) {
306         $template->param( search      => 1 );
307         $template->param( searchfield => $searchfield );
308     }
309     my ( $count, $results ) = StringSearch( $searchfield, 'web' );
310     my $toggle    = 0;
311     my @loop_data = ();
312     for (
313         my $i = $offset ;
314         $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
315         $i++
316       )
317     {
318         if ( $toggle ) {
319             $toggle = 0;
320         }
321         else {
322             $toggle = 1;
323         }
324         my %row_data;
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 );
330     }
331     $template->param( letter => \@loop_data );
332 }    #---- END $OP eq DEFAULT
333
334 output_html_with_http_headers $input, $cookie, $template->output;
335