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