Use C4::Context->preference, rather than getting all system
[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::Context;
26 use C4::Output;
27 use CGI;
28 use C4::Search;
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 $autonumber_members = C4::Context->preference("autoMemberNum") || 0;
65                 # Find out whether member numbers should be generated
66                 # automatically. Should be either "1" or something else.
67                 # Defaults to "0", which is interpreted as "no".
68   # FIXME
69   # This logic should probably be moved out of the presentation code.
70   # Not tonight though.
71   #
72   if ($cardnumber eq '' && $autonumber_members eq '1') {
73     my $dbh = C4::Context->dbh;
74     my $query="select max(substring(borrowers.cardnumber,2,7)) from borrowers";
75     my $sth=$dbh->prepare($query);
76     $sth->execute;
77     my $data=$sth->fetchrow_hashref;
78     $cardnumber=$data->{'max(substring(borrowers.cardnumber,2,7))'};
79     $sth->finish;
80     # purpose: generate checksum'd member numbers.
81     # We'll assume we just got the max value of digits 2-8 of member #'s from the database and our job is to
82     # increment that by one, determine the 1st and 9th digits and return the full string.
83     my @weightings = (8,4,6,3,5,2,1);
84     my $sum;
85     my $i = 0;
86     if (! $cardnumber) {                        # If DB has no values, start at 1000000
87       $cardnumber = 1000000;
88     } else {
89       $cardnumber = $cardnumber + 1;
90     }
91   
92     while ($i <8) {                     # step from char 1 to 7.
93       my $temp1 = $weightings[$i];      # read weightings, left to right, 1 char at a time
94       my $temp2 = substr($cardnumber,$i,1);     # sequence left to right, 1 char at a time
95   #print "$temp2<br>";
96       $sum = $sum + ($temp1*$temp2);    # mult each char 1-7 by its corresponding weighting
97       $i++;                             # increment counter
98     }
99     my $rem = ($sum%11);                        # remainder of sum/11 (eg. 9999999/11, remainder=2)
100     if ($rem == 10) {                   # if remainder is 10, use X instead
101       $rem = "X";
102     }  
103     $cardnumber="V$cardnumber$rem";
104   } else {
105     $cardnumber=$data->{'cardnumber'};
106   }
107   
108   if ($data->{'sex'} eq 'F'){
109     $template->param(female => 1);
110   } 
111   
112   my @titles = ('Miss', 'Mrs', 'Ms', 'Mr', 'Dr', 'Sir');
113         # FIXME - Assumes English. This ought to be made part of i18n.
114   my @titledata;
115   while (@titles) {
116     my %row;
117     my $title = shift @titles;
118     $row{'title'} = $title;
119     if ($data->{'title'} eq $title) {
120       $row{'selected'}=' selected';
121     } else {
122       $row{'selected'}='';
123     }
124     push(@titledata, \%row);
125   }
126   
127   my ($categories,$labels)=ethnicitycategories();
128   my $ethnicitycategoriescount=$#{$categories};
129   my $ethcatpopup;
130   if ($ethnicitycategoriescount>=0) {
131         $ethcatpopup = popup_menu(-name=>'ethnicity',
132                                 -values=>$categories,
133                                 -default=>$data->{'ethnicity'},
134                                 -labels=>$labels);
135         $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
136   }
137   
138   ($categories,$labels)=borrowercategories();
139   my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
140                                 -values=>$categories,
141                                 -default=>$data->{'categorycode'},
142                                 -labels=>$labels);
143   
144   my @areas = ('L','F','S','H','K','O','X','Z','V');
145   my %arealabels = ('L' => 'Levin',
146                   'F' => 'Foxton',
147                   'S' => 'Shannon',
148                   'H' => 'Horowhenua',
149                   'K' => 'Kapiti',
150                   'O' => 'Out of District',
151                   'X' => 'Temporary Visitor',
152                   'Z' => 'Interloan Libraries',
153                   'V' => 'Village');
154                   
155   my @areadata;
156   while (@areas) {
157     my %row;
158     my $shortcut = shift @areas;
159     $row{'shortcut'} = $shortcut;
160     if ($data->{'area'} eq $shortcut) {
161       $row{'selected'}=' selected';
162     } else {
163       $row{'selected'}='';
164     }
165     $row{'area'}=$arealabels{$shortcut};
166     push(@areadata, \%row);
167   }
168   
169   
170   my @relationships = ('workplace', 'relative','friend', 'neighbour');
171   my @relshipdata;
172   while (@relationships) {
173     my $relship = shift @relationships;
174     my %row = ('relationship' => $relship);
175     if ($data->{'altrelationship'} eq $relship) {
176       $row{'selected'}=' selected';
177     } else {
178       $row{'selected'}='';
179     }
180     push(@relshipdata, \%row);
181   }
182   
183   # %flags: keys=$data-keys, datas=[formname, HTML-explanation]
184   my %flags = ('gonenoaddress' => ['gna', 'Gone no address'],
185                'lost'          => ['lost', 'Lost'],
186                'debarred'      => ['debarred', 'Debarred']);
187   
188   my @flagdata;
189   foreach (keys(%flags)) {
190     my $key = $_;
191     my %row =  ('key'   => $key,
192                 'name'  => $flags{$key}[0],
193                 'html'  => $flags{$key}[1]);
194     if ($data->{$key}) {
195       $row{'yes'}=' checked';
196       $row{'no'}='';
197     } else {
198       $row{'yes'}='';
199       $row{'no'}=' checked';
200     }
201     push(@flagdata, \%row);
202   }
203
204   if ($modify){
205     $template->param( modify => 1 );
206   }
207   
208   $template->param( startmenumember => join ('', startmenu('member')),
209                         endmenumember   => join ('', endmenu('member')),
210                         member          => $member,
211                         firstname       => $data->{'firstname'},
212                         surname         => $data->{'surname'},
213                         othernames      => $data->{'othernames'},
214                         initials        => $data->{'initials'},
215                         ethcatpopup     => $ethcatpopup,
216                         catcodepopup    => $catcodepopup,
217                         streetaddress   => $data->{'physstreet'},
218                         streetcity      => $data->{'streetcity'},
219                         city            => $data->{'city'},
220                         phone           => $data->{'phone'},
221                         phoneday        => $data->{'phoneday'},
222                         faxnumber       => $data->{'faxnumber'},
223                         emailaddress    => $data->{'emailaddress'},
224                         contactname     => $data->{'contactname'},
225                         altphone        => $data->{'altphone'},
226                         altnotes        => $data->{'altnotes'},
227                         borrowernotes   => $data->{'borrowernotes'},
228                         flagloop        => \@flagdata,
229                         relshiploop     => \@relshipdata,
230                         titleloop       => \@titledata,
231                         arealoop        => \@areadata,
232                         dateenrolled    => $data->{'dateenrolled'},
233                         expiry          => $data->{'expiry'},
234                         cardnumber      => $cardnumber,
235                         dateofbirth     => $data->{'dateofbirth'});
236                         
237   print "Content-Type: text/html\n\n", $template->output;
238   
239
240 }