Adding Biblio::EndNote dep
[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         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;
204     }
205     else {
206         my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
207         $sth2->execute;
208         my %line = ( 'value' => "", 'text' => '---BIBLIO---' );
209
210         push @SQLfieldname, \%line;
211         while ( ( my $field ) = $sth2->fetchrow_array ) {
212
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;
216         }
217         my $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
218         $sth2->execute;
219         my %line = ( 'value' => "", 'text' => '---BIBLIOITEMS---' );
220         push @SQLfieldname, \%line;
221         while ( ( my $field ) = $sth2->fetchrow_array ) {
222             my %line = (
223                 'value' => "biblioitems." . $field,
224                 'text'  => "biblioitems." . $field
225             );
226             push @SQLfieldname, \%line;
227         }
228         my %line = ( 'value' => "", 'text' => '---ITEMS---' );
229         push @SQLfieldname, \%line;
230         my %line = ( 'value' => "items.content", 'text' => 'items.content' );
231         push @SQLfieldname, \%line;
232
233         my $sth2 = $dbh->prepare("SHOW COLUMNS from borrowers");
234         $sth2->execute;
235         my %line = ( 'value' => "", 'text' => '---BORROWERS---' );
236         push @SQLfieldname, \%line;
237         while ( ( my $field ) = $sth2->fetchrow_array ) {
238             my %line = (
239                 'value' => "borrowers." . $field,
240                 'text'  => "borrowers." . $field
241             );
242             push @SQLfieldname, \%line;
243         }
244     }
245     if ($code) {
246         $template->param( modify => 1 );
247         $template->param( code   => $letter->{code} );
248     }
249     else {
250         $template->param( adding => 1 );
251     }
252     $template->param(
253         name    => $letter->{name},
254         title   => $letter->{title},
255         content => ( $content ? $content : $letter->{content} ),
256         ( $module ? $module : $letter->{module} ) => 1,
257         SQLfieldname => \@SQLfieldname,
258     );
259
260     # END $OP eq ADD_FORM
261 ################## ADD_VALIDATE ##################################
262     # called by add_form, used to insert/modify data in DB
263 }
264 elsif ( $op eq 'add_validate' ) {
265     my $dbh = C4::Context->dbh;
266     my $sth =
267       $dbh->prepare(
268         "replace letter (module,code,name,title,content) values (?,?,?,?,?)");
269     $sth->execute(
270         $input->param('module'), $input->param('code'),
271         $input->param('name'),   $input->param('title'),
272         $input->param('content')
273     );
274     $sth->finish;
275     print $input->redirect("letter.pl");
276     exit;
277
278     # END $OP eq ADD_VALIDATE
279 ################## DELETE_CONFIRM ##################################
280     # called by default form, used to confirm deletion of data in DB
281 }
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;
287     $sth->finish;
288     $template->param( module  => $data->{module} );
289     $template->param( code    => $code );
290     $template->param( name    => $data->{'name'} );
291     $template->param( content => $data->{'content'} );
292
293     # END $OP eq DELETE_CONFIRM
294 ################## DELETE_CONFIRMED ##################################
295   # called by delete_confirm, used to effectively confirm deletion of data in DB
296 }
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 );
303     $sth->finish;
304     print $input->redirect("/cgi-bin/koha/tools/letter.pl");
305     return;
306
307     # END $OP eq DELETE_CONFIRMED
308 ################## DEFAULT ##################################
309 }
310 else {    # DEFAULT
311     if ( $searchfield ne '' ) {
312         $template->param( search      => 1 );
313         $template->param( searchfield => $searchfield );
314     }
315     my ( $count, $results ) = StringSearch( $searchfield, 'web' );
316     my $toggle    = 0;
317     my @loop_data = ();
318     for (
319         my $i = $offset ;
320         $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
321         $i++
322       )
323     {
324         if ( $toggle ) {
325             $toggle = 0;
326         }
327         else {
328             $toggle = 1;
329         }
330         my %row_data;
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 );
336     }
337     $template->param( letter => \@loop_data );
338 }    #---- END $OP eq DEFAULT
339
340 output_html_with_http_headers $input, $cookie, $template->output;
341