update of code and translation
[koha.git] / newmember.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 #script to print confirmation screen, then if accepted calls itself to insert data
6 # FIXME - Yes, but what does it _do_?
7 # 2002/12/18 hdl@ifrance.com templating
8
9 # 2003/01/20 acli@ada.dhs.org XXX it seems to do the following:
10 # * "insert" seems to do nothing; in 1.2.2 the script just returns a blank
11 #   page (with the headers etc.) if "insert" has anything in it
12 # * $ok has the opposite meaning of what one expects; $ok == 1 means "not ok"
13 # * if ($ok == 0) considers the "ok" case; it displays a confirmation page
14 #   for the user to "click to confirm that everything is entered correctly"
15 # * The "else" case for ($ok == 0) handles the "not ok" case; $string is the
16 #   error message to display
17
18 # FIXME - What is the correct value of "flagsrequired"?
19
20 # Copyright 2000-2002 Katipo Communications
21 #
22 # This file is part of Koha.
23 #
24 # Koha is free software; you can redistribute it and/or modify it under the
25 # terms of the GNU General Public License as published by the Free Software
26 # Foundation; either version 2 of the License, or (at your option) any later
27 # version.
28 #
29 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
30 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
31 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
32 #
33 # You should have received a copy of the GNU General Public License along with
34 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
35 # Suite 330, Boston, MA  02111-1307 USA
36
37 use strict;
38 use C4::Auth;
39 use C4::Input;
40 use C4::Interface::CGI::Output;
41 use CGI;
42 use Date::Manip;
43 use HTML::Template;
44 use C4::Date;
45 my %env;
46 my $input = new CGI;
47
48 #get rest of data
49 my %data;
50 my @names=$input->param;
51 foreach my $key (@names){
52   $data{$key}=$input->param($key);
53 }
54
55 my ($template, $borrowernumber, $cookie)
56     = get_template_and_user({template_name => "newmember.tmpl",
57                              query => $input,
58                              type => "intranet",
59                              authnotrequired => 0,
60                              flagsrequired => {parameters => 1},
61                          });
62
63 #Get the database handle
64 my $dbh = C4::Context->dbh;
65
66 # Check that all compulsary fields are entered
67 # If everything is ok, set $ok = 0
68 # Otherwise set $ok = 1 and $string to the error message to display.
69
70 my $ok=0;
71 my $string = "The following compulsary fields have been left blank. "
72         . "Please push the back button and try again<p>";
73 my @errors;
74 if ($data{'cardnumber'} eq ''){
75         push @errors,"cardnumber";
76     $ok=1;
77 } else {
78     #check cardnumber is valid
79     my $nounique;
80     if ( $data{'type'} ne "Add" )
81     {
82         $nounique = 1;
83     }
84     else
85     {
86         $nounique = 0;
87     }
88     my $valid=checkdigit(\%env,$data{'cardnumber'}, $nounique);
89     if ($valid != 1){
90         $ok=1;
91         push @errors, "invalid_cardnumber";
92     }
93 }
94 if ($data{'sex'} eq ''){
95     push @errors, "gender";
96     $ok=1;
97 }
98 if ($data{'firstname'} eq ''){
99     push @errors,"firstname";
100     $ok=1;
101 }
102 if ($data{'surname'} eq ''){
103     push @errors,"surname";
104     $ok=1;
105 }
106 if ($data{'address'} eq ''){
107     push @errors, "address";
108     $ok=1;
109 }
110 if ($data{'city'} eq ''){
111     push @errors, "city";
112     $ok=1;
113 }
114
115 # Pass the ok/not ok status and the error message to the template
116 $template->param(       OK=> ($ok==0));
117 foreach my $error (@errors) {
118         $template->param( $error => 1);
119 }
120
121 # If things are ok, display the confirmation page
122 if ($ok == 0) {
123     my $name=$data{'title'}." ";
124     if ($data{'othernames'} ne ''){
125         $name.=$data{'othernames'}." ";
126     } else {
127         $name.=$data{'firstname'}." ";
128     }
129     $name.="$data{'surname'} ( $data{'firstname'}, $data{'initials'})";
130     my $sex;
131     if ($data{'sex'} eq 'M'){
132         $sex="Male";
133     } else {
134         $sex="Female";
135     }
136     if ($data{'joining'} eq ''){
137         $data{'joining'}=ParseDate('today');
138         $data{'joining'}=format_date($data{'joining'});
139     }
140     if ($data{'expiry'} eq ''){
141         my $get_enrolmentperiod = $dbh->prepare(q{SELECT enrolmentperiod FROM categories WHERE categorycode = ?});
142         $get_enrolmentperiod->execute($data{'categorycode'});
143         my ( $period ) = $get_enrolmentperiod->fetchrow;
144         if ( ($period)  && ($period != 1))
145         {
146                 $data{'expiry'}=ParseDate("in $period years");
147                 $data{'expiry'}=format_date($data{'expiry'});
148         }
149         else
150         {
151                 $data{'expiry'}=ParseDate('in 1 year');
152                 $data{'expiry'}=format_date($data{'expiry'});
153         }
154     }
155     my $ethnic=$data{'ethnicity'}." ".$data{'ethnicnotes'};
156     my $postal=$data{'address'}."<br>".$data{'city'};
157     my $home;
158     if ($data{'streetaddress'} ne ''){
159         $home=$data{'streetaddress'}."<br>".$data{'streetcity'};
160     } else {
161         $home=$postal;
162     }
163     my @inputsloop;
164     while (my ($key, $value) = each %data) {
165         $value=~ s/\"/%22/g;
166         my %line;
167         $line{'key'}=$key;
168         $line{'value'}=$value;
169         push(@inputsloop, \%line);
170     }
171
172     #Get the fee
173     my $sth = $dbh->prepare("SELECT enrolmentfee FROM categories WHERE categorycode = ?");
174     $sth->execute($data{'categorycode'});
175     my ($fee) = $sth->fetchrow;
176     $sth->finish;
177
178     $template->param(name => $name,
179                      bornum => $data{'borrowernumber'},
180                      cardnum => $data{'cardnumber'},
181                      memcat => $data{'categorycode'},
182                      fee => $fee,
183                      joindate => format_date($data{'joining'}),
184                      expdate => format_date($data{'expiry'}),
185                      branchcode => $data{'branchcode'},
186                      ethnic => $ethnic,
187                      dob => format_date($data{'dateofbirth'}),
188                      sex => $sex,
189                      postal => $postal,
190                      home => $home,
191                         zipcode => $data{'zipcode'},
192                         homezipcode => $data{'homezipcode'},
193                      phone => $data{'phone'},
194                      phoneday => $data{'phoneday'},
195                      faxnumber => $data{'faxnumber'},
196                      emailaddress => $data{'emailaddress'},
197                         textmessaging => $data{'textmessaging'},
198                      contactname => $data{'contactname'},
199                      altphone => $data{'altphone'},
200                      altrelationship => $data{'altrelationship'},
201                      altnotes => $data{'altnotes'},
202                      bornotes => $data{'borrowernotes'},
203                      inputsloop => \@inputsloop);
204
205 # If things are not ok, display the error message
206 } else {
207     # Nothing to do; the "OK" and "string" variables have already been set
208     ;
209 }
210
211 output_html_with_http_headers $input, $cookie, $template->output;
212
213