fix for bug 1369: sql-datas dir, pick a naming convention, remove the rest
[koha.git] / tools / letter.pl
1 #!/usr/bin/perl
2
3 #script to administer the aqbudget table
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # ALGO :
8 # this script use an $op to know what to do.
9 # if $op is empty or none of the above values,
10 #       - the default screen is build (with all records, or filtered datas).
11 #       - the   user can clic on add, modify or delete record.
12 # if $op=add_form
13 #       - if primkey exists, this is a modification,so we read the $primkey record
14 #       - builds the add/modify form
15 # if $op=add_validate
16 #       - the user has just send datas, so we create/modify the record
17 # if $op=delete_form
18 #       - we show the record having primkey=$primkey and ask for deletion validation form
19 # if $op=delete_confirm
20 #       - we delete the record having primkey=$primkey
21
22
23 # Copyright 2000-2002 Katipo Communications
24 #
25 # This file is part of Koha.
26 #
27 # Koha is free software; you can redistribute it and/or modify it under the
28 # terms of the GNU General Public License as published by the Free Software
29 # Foundation; either version 2 of the License, or (at your option) any later
30 # version.
31 #
32 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
33 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
34 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License along with
37 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
38 # Suite 330, Boston, MA  02111-1307 USA
39
40 use strict;
41 use CGI;
42 use C4::Date;
43 use C4::Auth;
44 use C4::Context;
45 use C4::Output;
46
47
48 sub StringSearch  {
49     my ($searchstring,$type)=@_;
50     my $dbh = C4::Context->dbh;
51     $searchstring=~ s/\'/\\\'/g;
52     my @data=split(' ',$searchstring);
53     my $count=@data;
54     my $sth=$dbh->prepare("Select * from letter where (code like ?) order by module,code");
55     $sth->execute("$data[0]%");
56     my @results;
57     my $cnt=0;
58     while (my $data=$sth->fetchrow_hashref){
59     push(@results,$data);
60     $cnt ++;
61     }
62     #  $sth->execute;
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)
80     = get_template_and_user({template_name => "tools/letter.tmpl",
81                 query => $input,
82                 type => "intranet",
83                 authnotrequired => 0,
84                 flagsrequired => {tools => 1},
85                 debug => 1,
86                 });
87
88 if ($op) {
89 $template->param(script_name => $script_name,
90                         $op              => 1); # we show only the TMPL_VAR names $op
91 } else {
92 $template->param(script_name => $script_name,
93                         else              => 1); # we show only the TMPL_VAR names $op
94 }
95
96 $template->param(action => $script_name);
97 ################## ADD_FORM ##################################
98 # called by default. Used to create form to add or  modify a record
99 if ($op eq 'add_form') {
100     #---- if primkey exists, it's a modify action, so read values to modify...
101     my $letter;
102     if ($code) {
103         my $sth=$dbh->prepare("select * from letter where module=? and code=?");
104         $sth->execute($module,$code);
105         $letter=$sth->fetchrow_hashref;
106         $sth->finish;
107     }
108     # build field list
109     my @SQLfieldname;
110     my %line = ('value' => "LibrarianFirstname", 'text' => 'LibrarianFirstname');
111     push @SQLfieldname, \%line;
112     my %line = ('value' => "LibrarianSurname", 'text' => 'LibrarianSurname');
113     push @SQLfieldname, \%line;
114     my %line = ('value' => "LibrarianEmailaddress", 'text' => 'LibrarianEmailaddress');
115     push @SQLfieldname, \%line;
116     my $sth2=$dbh->prepare("SHOW COLUMNS from branches");
117     $sth2->execute;
118     my %line = ('value' => "", 'text' => '---BRANCHES---');
119     push @SQLfieldname, \%line;
120     while ((my $field) = $sth2->fetchrow_array) {
121         my %line = ('value' => "branches.".$field, 'text' => "branches.".$field);
122         push @SQLfieldname, \%line;
123     }
124     # add acquisition specific tables
125     if (index($module,"acquisition")>0){
126     my $sth2=$dbh->prepare("SHOW COLUMNS from aqbooksellers");
127     $sth2->execute;
128     my %line = ('value' => "", 'text' => '---BOOKSELLERS---');
129     push @SQLfieldname, \%line;
130     while ((my $field) = $sth2->fetchrow_array) {
131         my %line = ('value' => "aqbooksellers.".$field, 'text' => "aqbooksellers.".$field);
132         push @SQLfieldname, \%line;
133     }
134     my $sth2=$dbh->prepare("SHOW COLUMNS from aqorders");
135     $sth2->execute;
136     my %line = ('value' => "", 'text' => '---ORDERS---');
137     push @SQLfieldname, \%line;
138     while ((my $field) = $sth2->fetchrow_array) {
139         my %line = ('value' => "aqorders.".$field, 'text' => "aqorders.".$field);
140         push @SQLfieldname, \%line;
141     }
142     # add issues specific tables
143     }elsif (index($module,"issues")>0){
144     my $sth2=$dbh->prepare("SHOW COLUMNS from aqbooksellers");
145     $sth2->execute;
146     my %line = ('value' => "", 'text' => '---BOOKSELLERS---');
147     push @SQLfieldname, \%line;
148     while ((my $field) = $sth2->fetchrow_array) {
149         my %line = ('value' => "aqbooksellers.".$field, 'text' => "aqbooksellers.".$field);
150         push @SQLfieldname, \%line;
151     }
152     my $sth2=$dbh->prepare("SHOW COLUMNS from serial");
153     $sth2->execute;
154     my %line = ('value' => "", 'text' => '---SERIALS---');
155     push @SQLfieldname, \%line;
156     while ((my $field) = $sth2->fetchrow_array) {
157         my %line = ('value' => "serial.".$field, 'text' => "serial.".$field);
158         push @SQLfieldname, \%line;
159     }
160     my $sth2=$dbh->prepare("SHOW COLUMNS from subscription");
161     $sth2->execute;
162     my %line = ('value' => "", 'text' => '---SUBSCRIPTION---');
163     push @SQLfieldname, \%line;
164     while ((my $field) = $sth2->fetchrow_array) {
165         my %line = ('value' => "subscription.".$field, 'text' => "subscription.".$field);
166         push @SQLfieldname, \%line;
167     }
168     # add biblio specific tables.
169     } else {
170     my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
171     $sth2->execute;
172     my %line = ('value' => "", 'text' => '---BIBLIO---');
173
174     push @SQLfieldname, \%line;
175     while ((my $field) = $sth2->fetchrow_array) {
176         # note : %line is redefined, otherwise \%line contains the same value for every entry of the list
177         my %line = ('value' => "biblio.".$field, 'text' => "biblio.".$field);
178         push @SQLfieldname, \%line;
179     }
180     my $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
181     $sth2->execute;
182     my %line = ('value' => "", 'text' => '---BIBLIOITEMS---');
183     push @SQLfieldname, \%line;
184     while ((my $field) = $sth2->fetchrow_array) {
185         my %line = ('value' => "biblioitems.".$field, 'text' => "biblioitems.".$field);
186         push @SQLfieldname, \%line;
187     }
188     my %line = ('value' => "", 'text' => '---ITEMS---');
189     push @SQLfieldname, \%line;
190     my %line = ('value' => "items.content", 'text' => 'items.content');
191     push @SQLfieldname, \%line;
192     
193     my $sth2=$dbh->prepare("SHOW COLUMNS from borrowers");
194     $sth2->execute;
195     my %line = ('value' => "", 'text' => '---BORROWERS---');
196     push @SQLfieldname, \%line;
197     while ((my $field) = $sth2->fetchrow_array) {
198         my %line = ('value' => "borrowers.".$field, 'text' => "borrowers.".$field);
199         push @SQLfieldname, \%line;
200     }
201     }
202     if ($code) {
203         $template->param(modify => 1);
204         $template->param(code => $letter->{code});
205     } else {
206         $template->param(adding => 1);
207     }
208     $template->param(name => $letter->{name},title => $letter->{title},
209                     content => ($content?$content:$letter->{content}),
210                     ($module?$module:$letter->{module}) => 1,
211                     SQLfieldname => \@SQLfieldname,);
212                                                     # END $OP eq ADD_FORM
213 ################## ADD_VALIDATE ##################################
214 # called by add_form, used to insert/modify data in DB
215 } elsif ($op eq 'add_validate') {
216     my $dbh = C4::Context->dbh;
217     my $sth=$dbh->prepare("replace letter (module,code,name,title,content) values (?,?,?,?,?)");
218     $sth->execute($input->param('module'),$input->param('code'),$input->param('name'),$input->param('title'),$input->param('content'));
219     $sth->finish;
220     print $input->redirect("letter.pl");
221     exit;
222 # END $OP eq ADD_VALIDATE
223 ################## DELETE_CONFIRM ##################################
224 # called by default form, used to confirm deletion of data in DB
225 } elsif ($op eq 'delete_confirm') {
226     my $dbh = C4::Context->dbh;
227     my $sth=$dbh->prepare("select * from letter where code=?");
228     $sth->execute($code);
229     my $data=$sth->fetchrow_hashref;
230     $sth->finish;
231     $template->param(module => $data->{module});
232     $template->param(code => $code);
233     $template->param(name => $data->{'name'});
234     $template->param(content => $data->{'content'});
235                                                     # END $OP eq DELETE_CONFIRM
236 ################## DELETE_CONFIRMED ##################################
237 # called by delete_confirm, used to effectively confirm deletion of data in DB
238 } elsif ($op eq 'delete_confirmed') {
239     my $dbh = C4::Context->dbh;
240     my $code=uc($input->param('code'));
241     my $module=$input->param('module');
242     my $sth=$dbh->prepare("delete from letter where module=? and code=?");
243     $sth->execute($module,$code);
244     $sth->finish;
245     print $input->redirect("letter.pl");
246     return;
247                                                     # END $OP eq DELETE_CONFIRMED
248 ################## DEFAULT ##################################
249 } else { # DEFAULT
250     if  ($searchfield ne '') {
251             $template->param(search => 1);
252         $template->param(searchfield => $searchfield);
253     }
254     my ($count,$results)=StringSearch($searchfield,'web');
255     my $toggle="white";
256     my @loop_data =();
257     for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
258         if ($toggle eq 'white'){
259                 $toggle="#ffffcc";
260         } else {
261                 $toggle="white";
262         }
263     my %row_data;
264     $row_data{toggle} = $toggle;
265     $row_data{module} = $results->[$i]{'module'};
266     $row_data{code} = $results->[$i]{'code'};
267     $row_data{name} = $results->[$i]{'name'};
268     push(@loop_data, \%row_data);
269     }
270     $template->param(letter => \@loop_data);
271 } #---- END $OP eq DEFAULT
272
273 output_html_with_http_headers $input, $cookie, $template->output;
274