Patron import reform - bug 2287 - expanded error catching and feedback
[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::Dates;
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     $sth->finish;
64     return ( $cnt, \@results );
65 }
66
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;
78
79 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
80     {
81         template_name   => "tools/letter.tmpl",
82         query           => $input,
83         type            => "intranet",
84         authnotrequired => 0,
85         flagsrequired   => { tools => 'edit_notices' },
86         debug           => 1,
87     }
88 );
89
90 if ($op) {
91         $template->param($op  => 1);
92 } else {
93         $template->param(else => 1);
94 }
95 # we show only the TMPL_VAR names $op
96
97 $template->param(
98         script_name => $script_name,
99         action => $script_name
100 );
101 ################## ADD_FORM ##################################
102 # called by default. Used to create form to add or  modify a record
103 if ( $op eq 'add_form' ) {
104
105     #---- if primkey exists, it's a modify action, so read values to modify...
106     my $letter;
107     if ($code) {
108         my $sth = $dbh->prepare("select * from letter where module=? and code=?");
109         $sth->execute( $module, $code );
110         $letter = $sth->fetchrow_hashref;
111         $sth->finish;
112     }
113
114     # build field list
115     my @SQLfieldname;
116     push @SQLfieldname, { 'value' => "LibrarianFirstname",    'text' => 'LibrarianFirstname'    };
117     push @SQLfieldname, { 'value' => "LibrarianSurname",      'text' => 'LibrarianSurname'      };
118     push @SQLfieldname, { 'value' => "LibrarianEmailaddress", 'text' => 'LibrarianEmailaddress' };
119     my $sth2 = $dbh->prepare("SHOW COLUMNS from branches");
120     $sth2->execute;
121     push @SQLfieldname, { 'value' => "", 'text' => '---BRANCHES---' };
122
123     while ( ( my $field ) = $sth2->fetchrow_array ) {
124         push @SQLfieldname, { 'value' => "branches." . $field, 'text' => "branches." . $field };
125     }
126
127     # add acquisition specific tables
128     if ( index( $module, "acquisition" ) > 0 ) {        # FIXME: imprecise comparison
129         $sth2 = $dbh->prepare("SHOW COLUMNS from aqbooksellers");
130         $sth2->execute;
131         push @SQLfieldname, { 'value' => "", 'text' => '---BOOKSELLERS---' };
132         while ( ( my $field ) = $sth2->fetchrow_array ) {
133             push @SQLfieldname, {
134                 'value' => "aqbooksellers." . $field,
135                 'text'  => "aqbooksellers." . $field
136             };
137         }
138         $sth2 = $dbh->prepare("SHOW COLUMNS from aqorders");
139         $sth2->execute;
140         push @SQLfieldname, { 'value' => "", 'text' => '---ORDERS---' };
141         while ( ( my $field ) = $sth2->fetchrow_array ) {
142             push @SQLfieldname, {
143                 'value' => "aqorders." . $field,
144                 'text'  => "aqorders." . $field
145             };
146         }
147
148         # add issues specific tables
149     }
150     elsif ( index( $module, "issues" ) > 0 ) {  # FIXME: imprecise comparison
151         $sth2 = $dbh->prepare("SHOW COLUMNS from aqbooksellers");
152         $sth2->execute;
153         push @SQLfieldname, { 'value' => "", 'text' => '---BOOKSELLERS---' };
154         while ( ( my $field ) = $sth2->fetchrow_array ) {
155             push @SQLfieldname, {
156                 'value' => "aqbooksellers." . $field,
157                 'text'  => "aqbooksellers." . $field
158             };
159         }
160         $sth2 = $dbh->prepare("SHOW COLUMNS from serial");
161         $sth2->execute;
162         push @SQLfieldname, { 'value' => "", 'text' => '---SERIALS---' };
163         while ( ( my $field ) = $sth2->fetchrow_array ) {
164             push @SQLfieldname, { 'value' => "serial." . $field, 'text' => "serial." . $field };
165         }
166         $sth2 = $dbh->prepare("SHOW COLUMNS from subscription");
167         $sth2->execute;
168         push @SQLfieldname, { 'value' => "", 'text' => '---SUBSCRIPTION---' };
169         while ( ( my $field ) = $sth2->fetchrow_array ) {
170             push @SQLfieldname, {
171                 'value' => "subscription." . $field,
172                 'text'  => "subscription." . $field
173             };
174         }
175         push @SQLfieldname, { 'value' => "",             'text' => '---Biblio---' };
176                 foreach(qw(title author serial)) {
177                 push @SQLfieldname, { 'value' => "biblio.$_", 'text' => ucfirst($_) };
178                 }
179     }
180     else {
181         $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
182         $sth2->execute;
183         push @SQLfieldname, { 'value' => "", 'text' => '---BIBLIO---' };
184
185         while ( ( my $field ) = $sth2->fetchrow_array ) {
186             push @SQLfieldname, { 'value' => "biblio." . $field, 'text' => "biblio." . $field };
187         }
188         $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
189         $sth2->execute;
190         push @SQLfieldname, { 'value' => "", 'text' => '---BIBLIOITEMS---' };
191         while ( ( my $field ) = $sth2->fetchrow_array ) {
192             push @SQLfieldname, {
193                 'value' => "biblioitems." . $field,
194                 'text'  => "biblioitems." . $field
195             };
196         }
197         push @SQLfieldname, { 'value' => "", 'text' => '---ITEMS---' };
198         push @SQLfieldname, { 'value' => "items.content", 'text' => 'items.content' };
199
200         $sth2 = $dbh->prepare("SHOW COLUMNS from borrowers");
201         $sth2->execute;
202         push @SQLfieldname, { 'value' => "", 'text' => '---BORROWERS---' };
203         while ( ( my $field ) = $sth2->fetchrow_array ) {
204             push @SQLfieldname, {
205                 'value' => "borrowers." . $field,
206                 'text'  => "borrowers." . $field
207             };
208         }
209     }
210     if ($code) {
211         $template->param( modify => 1 );
212         $template->param( code   => $letter->{code} );
213     }
214     else {
215         $template->param( adding => 1 );
216     }
217     $template->param(
218         name    => $letter->{name},
219         title   => $letter->{title},
220         content => ( $content ? $content : $letter->{content} ),
221         ( $module ? $module : $letter->{module} ) => 1,
222         SQLfieldname => \@SQLfieldname,
223     );
224 ################## ADD_VALIDATE ##################################
225     # called by add_form, used to insert/modify data in DB
226 }
227 elsif ( $op eq 'add_validate' ) {
228     my $dbh = C4::Context->dbh;
229     my $sth =
230       $dbh->prepare(
231         "replace letter (module,code,name,title,content) values (?,?,?,?,?)");
232     $sth->execute(
233         $input->param('module'), $input->param('code'),
234         $input->param('name'),   $input->param('title'),
235         $input->param('content')
236     );
237     $sth->finish;
238     print $input->redirect("letter.pl");
239     exit;
240 ################## DELETE_CONFIRM ##################################
241     # called by default form, used to confirm deletion of data in DB
242 }
243 elsif ( $op eq 'delete_confirm' ) {
244     my $dbh = C4::Context->dbh;
245     my $sth = $dbh->prepare("select * from letter where code=?");
246     $sth->execute($code);
247     my $data = $sth->fetchrow_hashref;
248     $sth->finish;
249     $template->param( code => $code );
250         foreach (qw(module name content)) {
251         $template->param( $_ => $data->{$_} );
252         }
253 ################## DELETE_CONFIRMED ##################################
254   # called by delete_confirm, used to effectively confirm deletion of data in DB
255 }
256 elsif ( $op eq 'delete_confirmed' ) {
257     my $dbh    = C4::Context->dbh;
258     my $code   = uc( $input->param('code') );
259     my $module = $input->param('module');
260     my $sth    = $dbh->prepare("delete from letter where module=? and code=?");
261     $sth->execute( $module, $code );
262     $sth->finish;
263     print $input->redirect("/cgi-bin/koha/tools/letter.pl");
264     return;
265 ################## DEFAULT ##################################
266 }
267 else {    # DEFAULT
268     if ( $searchfield ne '' ) {
269         $template->param( search      => 1 );
270         $template->param( searchfield => $searchfield );
271     }
272     my ( $count, $results ) = StringSearch( $searchfield, 'web' );
273     my $toggle    = 0;
274     my @loop_data = ();
275     for (
276         my $i = $offset ;
277         $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
278         $i++
279       )
280     {
281                 $toggle = ($toggle) ? 0 : 1;
282         my %row_data;
283         $row_data{toggle} = $toggle;
284                 foreach (qw(module code name)) {
285                 $row_data{$_} = $results->[$i]{$_};
286                 }
287         push( @loop_data, \%row_data );
288     }
289     $template->param( letter => \@loop_data );
290 }    #---- END $OP eq DEFAULT
291
292 output_html_with_http_headers $input, $cookie, $template->output;
293