adding seealso feature in MARC searches
[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'} eq "Add" )    {
81         $nounique = 0;
82     } else {
83         $nounique = 1;
84     }
85     my $valid=checkdigit(\%env,$data{'cardnumber'}, $nounique);
86     if ($valid != 1){
87         $ok=1;
88         push @errors, "invalid_cardnumber";
89     }
90 }
91 if ($data{'sex'} eq ''){
92     push @errors, "gender";
93     $ok=1;
94 }
95 if ($data{'firstname'} eq ''){
96     push @errors,"firstname";
97     $ok=1;
98 }
99 if ($data{'surname'} eq ''){
100     push @errors,"surname";
101     $ok=1;
102 }
103 if ($data{'address'} eq ''){
104     push @errors, "address";
105     $ok=1;
106 }
107 if ($data{'city'} eq ''){
108     push @errors, "city";
109     $ok=1;
110 }
111
112 # Pass the ok/not ok status and the error message to the template
113 $template->param(       OK=> ($ok==0));
114 foreach my $error (@errors) {
115         $template->param( $error => 1);
116 }
117
118 # If things are ok, display the confirmation page
119 if ($ok == 0) {
120     my $name=$data{'title'}." ";
121     if ($data{'othernames'} ne ''){
122         $name.=$data{'othernames'}." ";
123     } else {
124         $name.=$data{'firstname'}." ";
125     }
126     $name.="$data{'surname'} ( $data{'firstname'}, $data{'initials'})";
127     my $sex;
128     if ($data{'sex'} eq 'M'){
129         $sex=1;
130     } else {
131         $sex=0;
132     }
133     if ($data{'joining'} eq ''){
134         $data{'joining'}=ParseDate('today');
135         $data{'joining'}=format_date($data{'joining'});
136     }
137     if ($data{'expiry'} eq ''){
138         my $get_enrolmentperiod = $dbh->prepare(q{SELECT enrolmentperiod FROM categories WHERE categorycode = ?});
139         $get_enrolmentperiod->execute($data{'categorycode'});
140         my ( $period ) = $get_enrolmentperiod->fetchrow;
141         if ( ($period)  && ($period != 1))
142         {
143                 $data{'expiry'}=ParseDate("in $period years");
144                 $data{'expiry'}=format_date($data{'expiry'});
145         }
146         else
147         {
148                 $data{'expiry'}=ParseDate('in 1 year');
149                 $data{'expiry'}=format_date($data{'expiry'});
150         }
151     }
152     my $ethnic=$data{'ethnicity'}." ".$data{'ethnicnotes'};
153     my $postal=$data{'address'}."<br>".$data{'city'};
154     my $home;
155     if ($data{'streetaddress'} ne ''){
156         $home=$data{'streetaddress'}."<br>".$data{'streetcity'};
157     } else {
158         $home=$postal;
159     }
160     my @inputsloop;
161     while (my ($key, $value) = each %data) {
162         $value=~ s/\"/%22/g;
163         my %line;
164         $line{'key'}=$key;
165         $line{'value'}=$value;
166         push(@inputsloop, \%line);
167     }
168
169     #Get the fee
170     my $sth = $dbh->prepare("SELECT enrolmentfee FROM categories WHERE categorycode = ?");
171     $sth->execute($data{'categorycode'});
172     my ($fee) = $sth->fetchrow;
173     $sth->finish;
174
175     $template->param(name => $name,
176                      bornum => $data{'borrowernumber'},
177                      cardnum => $data{'cardnumber'},
178                      memcat => $data{'categorycode'},
179                      fee => $fee,
180                      joindate => format_date($data{'joining'}),
181                      expdate => format_date($data{'expiry'}),
182                      branchcode => $data{'branchcode'},
183                      ethnic => $ethnic,
184                      dob => format_date($data{'dateofbirth'}),
185                      sex => $sex,
186                      postal => $postal,
187                      home => $home,
188                         zipcode => $data{'zipcode'},
189                         homezipcode => $data{'homezipcode'},
190                      phone => $data{'phone'},
191                      phoneday => $data{'phoneday'},
192                      faxnumber => $data{'faxnumber'},
193                      emailaddress => $data{'emailaddress'},
194                         textmessaging => $data{'textmessaging'},
195                      contactname => $data{'contactname'},
196                      altphone => $data{'altphone'},
197                      altrelationship => $data{'altrelationship'},
198                      altnotes => $data{'altnotes'},
199                      bornotes => $data{'borrowernotes'},
200                      inputsloop => \@inputsloop);
201
202 # If things are not ok, display the error message
203 } else {
204     # Nothing to do; the "OK" and "string" variables have already been set
205     ;
206 }
207
208 output_html_with_http_headers $input, $cookie, $template->output;
209
210