synch'ing 2.2 and head
[koha.git] / members / newjmember.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.comTemplating
8
9 # Copyright 2000-2002 Katipo Communications
10 #
11 # This file is part of Koha.
12 #
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
17 #
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along with
23 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24 # Suite 330, Boston, MA  02111-1307 USA
25
26 use strict;
27 use C4::Auth;
28 use C4::Context;
29 use C4::Output;
30 use C4::Input;
31 use C4::Interface::CGI::Output;
32 use C4::Interface::CGI::Template;
33 use C4::Koha;
34 use CGI;
35 use Date::Manip;
36 use HTML::Template;
37
38 my %env;
39 my $input = new CGI;
40 #get varibale that tells us whether to show confirmation page
41 #or insert data
42 my $insert=$input->param('insert');
43
44 my ($template, $loggedinuser, $cookie)
45     = get_template_and_user({template_name => "members/newjmember.tmpl",
46                              query => $input,
47                              type => "intranet",
48                              authnotrequired => 0,
49                              flagsrequired => {borrowers => 1},
50                              debug => 1,
51                              });
52
53 #get rest of data
54 my %data;
55 my @names=$input->param;
56 foreach my $key (@names){
57   $data{$key}=$input->param($key);
58 }
59 my $missing=0;
60
61 for (my $i=0;$i<3;$i++){
62   my $number=$data{"cardnumber_child_$i"};
63   my $firstname=$data{"firstname_child_$i"};
64   my $surname=$data{"surname_child_$i"};
65   my $dob=$data{"dateofbirth_child_$i"};
66   my $sex=$data{"sex_child_$i"};
67   if ($number eq ''){
68     if ($i == 0){
69                 $template->param(cardnumber_missing => 1);
70                 $missing=1;
71                     if ($firstname eq ''){
72                 $template->param(firstname_missing => 1);
73                 $missing=1;
74     }
75     if ($surname eq ''){
76                 $template->param(surname_missing => 1);
77                 $missing=1;
78     }
79     if ($dob eq ''){
80                 $template->param(dob_missing => 1);
81                 $missing=1;
82     }
83     if ($sex eq ''){
84                 $template->param(gender_missing => 1);
85                 $missing=1;
86     }
87     }
88   } else {
89     if ($firstname eq ''){
90                 $template->param(firstname_missing => 1);
91                 $missing=1;
92     }
93     if ($surname eq ''){
94                 $template->param(surname_missing => 1);
95                 $missing=1;
96     }
97     if ($dob eq ''){
98                 $template->param(dob_missing => 1);
99                 $missing=1;
100     }
101     if ($sex eq ''){
102                 $template->param(gender_missing => 1);
103                 $missing=1;
104     }
105     #check cardnumber is valid
106     my $nounique;
107     if ( $data{'type'} ne "Add" )    {
108                 $nounique = 0;
109     } else {
110                 $nounique = 1;
111     }
112     my $valid=checkdigit(\%env,$number, $nounique);
113     if ($valid != 1){
114                 $template->param(missing =>1);
115                 $template->param(invalid_cardnumber => 1);
116                 $missing=1;
117     }
118   }
119 }
120         my @identsloop;
121         for (my $i=0;$i<3;$i++){
122                 my %ident;
123 #               $ident{'main'}=$main;
124 #               $ident{'image'}=$image;
125                 $ident{'cardchild'}=$data{"cardnumber_child_$i"};
126                 if ($data{"cardnumber_child_$i"} ne ''){
127                         my $name=$data{"firstname_child_$i"} . " " . $data{"surname_child_$i"};
128                         $ident{'name'}=$name;
129                         $ident{'bornum'}=$data{"bornumber_child_$i"};
130                         $ident{'dob'}=$data{"dateofbirth_child_$i"};
131                         ($data{"sex_child_$i"} eq 'M') ? ($ident{'sex'}="Male") : ($ident{'sex'}="Female") ;
132                         $ident{'school'}=$data{"school_child_$i"};
133                         $ident{'notes'}=$data{"altnotes_child_$i"};
134                         push(@identsloop, \%ident);
135                 }
136         }
137         my @inputsloop;
138         while (my ($key, $value) = each %data) {
139                 $value=~ s/\"/%22/g;
140                 my %line;
141                 $line{'key'}=$key;
142                 $line{'value'}=$value;
143                 push(@inputsloop, \%line);
144         }
145
146 # FIXME IF main and image are not fetched by HTML::TEMPLATE get them into identsloop
147 $template->param(       NOK => ($missing==1),
148                                                                 identsloop => \@identsloop,
149                                                                 inputsloop => \@inputsloop,
150                                                                 );
151
152 output_html_with_http_headers $input, $cookie, $template->output;