templatified:set templatedirectory in koha.conf to use the templates
[koha.git] / memberentry.pl
1 #!/usr/bin/perl
2
3 #script to set up screen for modification of borrower details
4 #written 20/12/99 by chris@katipo.co.nz
5
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use C4::Output;
26 use CGI;
27 use C4::Search;
28 use C4::Database;
29 use C4::Koha;
30 use HTML::Template;
31
32 my $input = new CGI;
33
34 my $theme = $input->param('theme'); # only used if allowthemeoverride is set
35 my %tmpldata = pathtotemplate ( template => 'memberentry.tmpl', theme => $theme );
36 my $template = HTML::Template->new(filename => $tmpldata{'path'}, die_on_bad_params => 0);
37
38 my $member=$input->param('bornum');
39 if ($member eq ''){
40   $member=NewBorrowerNumber();
41 }
42 my $type=$input->param('type') || '';
43 my $modify=$input->param('modify.x'); 
44 my $delete=$input->param('delete.x');
45 if ($delete){
46   print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$member");
47
48 } else {  # this else goes down the whole script
49   if ($type ne 'Add'){
50     $template->param( header => 'Update Member Details'); # bad templating style
51   } else {
52     $template->param( header => 'Add New Member');
53   }
54   
55   my $data=borrdata('',$member);
56   
57   if ($type eq 'Add'){
58     $template->param( updtype => 'I');
59   } else {
60     $template->param( updtype => 'M');
61   }
62   
63   my $cardnumber=$data->{'cardnumber'};
64   my %systemprefs=systemprefs();
65   # FIXME
66   # This logic should probably be moved out of the presentation code.
67   # Not tonight though.
68   #
69   if ($cardnumber eq '' && $systemprefs{'autoMemberNum'} eq '1') {
70     my $dbh=C4Connect;
71     my $query="select max(substring(borrowers.cardnumber,2,7)) from borrowers";
72     my $sth=$dbh->prepare($query);
73     $sth->execute;
74     my $data=$sth->fetchrow_hashref;
75     $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
76     $sth->finish;
77     $dbh->disconnect;
78     # purpose: generate checksum'd member numbers.
79     # We'll assume we just got the max value of digits 2-8 of member #'s from the database and our job is to
80     # increment that by one, determine the 1st and 9th digits and return the full string.
81     my @weightings = (8,4,6,3,5,2,1);
82     my $sum;
83     my $i = 0;
84     if (! $cardnumber) {                        # If DB has no values, start at 1000000
85       $cardnumber = 1000000;
86     } else {
87       $cardnumber = $cardnumber + 1;
88     }
89   
90     while ($i <8) {                     # step from char 1 to 7.
91       my $temp1 = $weightings[$i];      # read weightings, left to right, 1 char at a time
92       my $temp2 = substr($cardnumber,$i,1);     # sequence left to right, 1 char at a time
93   #print "$temp2<br>";
94       $sum = $sum + ($temp1*$temp2);    # mult each char 1-7 by its corresponding weighting
95       $i++;                             # increment counter
96     }
97     my $rem = ($sum%11);                        # remainder of sum/11 (eg. 9999999/11, remainder=2)
98     if ($rem == 10) {                   # if remainder is 10, use X instead
99       $rem = "X";
100     }  
101     $cardnumber="V$cardnumber$rem";
102   } else {
103     $cardnumber=$data->{'cardnumber'};
104   }
105   
106   if ($data->{'sex'} eq 'F'){
107     $template->param(female => 1);
108   } 
109   
110   my @titles = ('Miss', 'Mrs', 'Ms', 'Mr', 'Dr', 'Sir');
111   my @titledata;
112   while (@titles) {
113     my %row;
114     my $title = shift @titles;
115     $row{'title'} = $title;
116     if ($data->{'title'} eq $title) {
117       $row{'selected'}=' selected';
118     } else {
119       $row{'selected'}='';
120     }
121     push(@titledata, \%row);
122   }
123   
124   my ($categories,$labels)=ethnicitycategories();
125   my $ethnicitycategoriescount=$#{$categories};
126   my $ethcatpopup;
127   if ($ethnicitycategoriescount>=0) {
128         $ethcatpopup = popup_menu(-name=>'ethnicity',
129                                 -values=>$categories,
130                                 -default=>$data->{'ethnicity'},
131                                 -labels=>$labels);
132         $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
133   }
134   
135   ($categories,$labels)=borrowercategories();
136   my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
137                                 -values=>$categories,
138                                 -default=>$data->{'categorycode'},
139                                 -labels=>$labels);
140   
141   my @areas = ('L','F','S','H','K','O','X','Z','V');
142   my %arealabels = ('L' => 'Levin',
143                   'F' => 'Foxton',
144                   'S' => 'Shannon',
145                   'H' => 'Horowhenua',
146                   'K' => 'Kapiti',
147                   'O' => 'Out of District',
148                   'X' => 'Temporary Visitor',
149                   'Z' => 'Interloan Libraries',
150                   'V' => 'Village');
151                   
152   my @areadata;
153   while (@areas) {
154     my %row;
155     my $shortcut = shift @areas;
156     $row{'shortcut'} = $shortcut;
157     if ($data->{'area'} eq $shortcut) {
158       $row{'selected'}=' selected';
159     } else {
160       $row{'selected'}='';
161     }
162     $row{'area'}=$arealabels{$shortcut};
163     push(@areadata, \%row);
164   }
165   
166   
167   my @relationships = ('workplace', 'relative','friend', 'neighbour');
168   my @relshipdata;
169   while (@relationships) {
170     my $relship = shift @relationships;
171     my %row = ('relationship' => $relship);
172     if ($data->{'altrelationship'} eq $relship) {
173       $row{'selected'}=' selected';
174     } else {
175       $row{'selected'}='';
176     }
177     push(@relshipdata, \%row);
178   }
179   
180   # %flags: keys=$data-keys, datas=[formname, HTML-explanation]
181   my %flags = ('gonenoaddress' => ['gna', 'Gone no address'],
182                'lost'          => ['lost', 'Lost'],
183                'debarred'      => ['debarred', 'Debarred']);
184   
185   my @flagdata;
186   foreach (keys(%flags)) {
187     my $key = $_;
188     my %row =  ('key'   => $key,
189                 'name'  => $flags{$key}[0],
190                 'html'  => $flags{$key}[1]);
191     if ($data->{$key}) {
192       $row{'yes'}=' checked';
193       $row{'no'}='';
194     } else {
195       $row{'yes'}='';
196       $row{'no'}=' checked';
197     }
198     push(@flagdata, \%row);
199   }
200
201   if ($modify){
202     $template->param( modify => 1 );
203   }
204   
205   $template->param( startmenumember => join ('', startmenu('member')),
206                         endmenumember   => join ('', endmenu('member')),
207                         member          => $member,
208                         firstname       => $data->{'firstname'},
209                         surname         => $data->{'surname'},
210                         othernames      => $data->{'othernames'},
211                         initials        => $data->{'initials'},
212                         ethcatpopup     => $ethcatpopup,
213                         catcodepopup    => $catcodepopup,
214                         streetaddress   => $data->{'physstreet'},
215                         streetcity      => $data->{'streetcity'},
216                         city            => $data->{'city'},
217                         phone           => $data->{'phone'},
218                         phoneday        => $data->{'phoneday'},
219                         faxnumber       => $data->{'faxnumber'},
220                         emailaddress    => $data->{'emailaddress'},
221                         contactname     => $data->{'contactname'},
222                         altphone        => $data->{'altphone'},
223                         altnotes        => $data->{'altnotes'},
224                         borrowernotes   => $data->{'borrowernotes'},
225                         flagloop        => \@flagdata,
226                         relshiploop     => \@relshipdata,
227                         titleloop       => \@titledata,
228                         arealoop        => \@areadata,
229                         dateenrolled    => $data->{'dateenrolled'},
230                         expiry          => $data->{'expiry'},
231                         cardnumber      => $cardnumber,
232                         dateofbirth     => $data->{'dateofbirth'});
233                         
234   print "Content-Type: text/html\n\n", $template->output;
235   
236
237 }