Adding superlibrarian rights when IndependantBranches is set.
[koha.git] / members / memberentry.pl
1 #!/usr/bin/perl
2 # NOTE: This file uses standard 8-space tabs
3 #       DO NOT SET TAB SIZE TO 4
4
5 # $Id$
6
7 #script to set up screen for modification of borrower details
8 #written 20/12/99 by chris@katipo.co.nz
9
10
11 # Copyright 2000-2002 Katipo Communications
12 #
13 # This file is part of Koha.
14 #
15 # Koha is free software; you can redistribute it and/or modify it under the
16 # terms of the GNU General Public License as published by the Free Software
17 # Foundation; either version 2 of the License, or (at your option) any later
18 # version.
19 #
20 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
21 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License along with
25 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
26 # Suite 330, Boston, MA  02111-1307 USA
27
28 use strict;
29 use C4::Auth;
30 use C4::Context;
31 use C4::Output;
32 use C4::Interface::CGI::Output;
33 use CGI;
34 use C4::Search;
35 use C4::Members;
36 use C4::Koha;
37 use HTML::Template;
38 use Date::Manip;
39 use C4::Date;
40 use C4::Input;
41
42 my $input = new CGI;
43
44 my $dbh = C4::Context->dbh;
45
46 my ($template, $loggedinuser, $cookie)
47     = get_template_and_user({template_name => "members/memberentry.tmpl",
48                              query => $input,
49                              type => "intranet",
50                              authnotrequired => 0,
51                              flagsrequired => {borrowers => 1},
52                              debug => 1,
53                              });
54
55 my $borrowernumber=$input->param('borrowernumber');
56 my $actionType=$input->param('actionType') || '';
57 my $modify=$input->param('modify');
58 my $delete=$input->param('delete');
59 my $op=$input->param('op');
60 my $categorycode=$input->param('categorycode');
61 my $destination=$input->param('destination');
62
63
64
65 my $nok;
66 # if a add or modify is requested => check validity of data.
67 if ($op eq 'add' or $op eq 'modify') {
68         my %data;
69         my @names=$input->param;
70         foreach my $key (@names){
71                 $data{$key}=$input->param($key);
72                 $data{$key}=~ s/\'/\\\'/g;
73                 $data{$key}=~ s/\"/\\\"/g;
74         }
75         my @errors;
76         if ($data{'cardnumber'} eq ''){
77                 push @errors,"ERROR_cardnumber";
78                 $nok=1;
79         } else {
80                 #check cardnumber is valid
81                 my $nounique;
82                 if ( $op eq "add" )    {
83                         $nounique = 0;
84                 } else {
85                         $nounique = 1;
86                 }
87                 my $valid=checkdigit('',$data{'cardnumber'}, $nounique);
88                 if ($valid != 1){
89                         $nok=1;
90                         push @errors, "ERROR_invalid_cardnumber";
91                 }
92         }
93         if ($data{'sex'} eq '' && $categorycode ne "I"){
94                 push @errors, "ERROR_gender";
95                 $nok=1;
96         }
97         if ($data{'firstname'} eq '' && $categorycode ne "I"){
98                 push @errors,"ERROR_firstname";
99                 $nok=1;
100         }
101         if ($data{'surname'} eq ''){
102                 push @errors,"ERROR_surname";
103                 $nok=1;
104         }
105         if ($data{'streetaddress'} eq ''){
106                 push @errors, "ERROR_address";
107                 $nok=1;
108         }
109         if ($data{'city'} eq ''){
110                 push @errors, "ERROR_city";
111                 $nok=1;
112         }
113         if ($nok) {
114                 foreach my $error (@errors) {
115                         $template->param( $error => 1);
116                 }
117                 $template->param(nok => 1);
118         } else {
119                 my $query="Select * from borrowers where borrowernumber=?";
120                 my $sth=$dbh->prepare($query);
121                 $sth->execute($data{'borrowernumber'});
122                 if (my $data2=$sth->fetchrow_hashref){
123                         &modmember(%data);
124                 }else{
125                         $borrowernumber = &newmember(%data);
126                 }
127                 
128         if($destination eq "circ"){
129                 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
130         } else {
131                 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$borrowernumber");
132                 }
133         }
134 }
135 if ($delete){
136         print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
137 } else {  # this else goes down the whole script
138         if ($actionType eq 'Add'){
139                 $template->param( addAction => 1);
140         } else {
141                 $template->param( addAction =>0);
142         }
143         # retrieve previous values : either in DB or in CGI, in case of errors in values
144         my $data;
145         if ($nok) {
146                 my @names=$input->param;
147                 foreach my $key (@names){
148                         $data->{$key}=$input->param($key);
149                 }
150         } else {
151                 $data=borrdata('',$borrowernumber);
152         }
153         if ($actionType eq 'Add'){
154                 $template->param( updtype => 'I');
155         } else {
156                 $template->param( updtype => 'M');
157         }
158         my $cardnumber=C4::Members::fixup_cardnumber($data->{'cardnumber'});
159         if ($data->{'sex'} eq 'F'){
160                 $template->param(female => 1);
161         }
162         my ($categories,$labels)=ethnicitycategories();
163         my $ethnicitycategoriescount=$#{$categories};
164         my $ethcatpopup;
165         if ($ethnicitycategoriescount>=0) {
166                 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
167                                         -id => 'ethnicity',
168                                         -values=>$categories,
169                                         -default=>$data->{'ethnicity'},
170                                         -labels=>$labels);
171                 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
172         }
173
174         ($categories,$labels)=borrowercategories();
175         my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
176                                         -id => 'categorycode',
177                                         -values=>$categories,
178                                         -default=>$data->{'categorycode'},
179                                         -labels=>$labels);
180
181         my @relationships = ('','workplace', 'relative','friend', 'neighbour');
182         my @relshipdata;
183         while (@relationships) {
184                 my $relship = shift @relationships;
185                 my %row = ('relationship' => $relship);
186                 if ($data->{'altrelationship'} eq $relship) {
187                         $row{'selected'}=' selected';
188                 } else {
189                         $row{'selected'}='';
190                 }
191                 push(@relshipdata, \%row);
192         }
193
194         # %flags: keys=$data-keys, datas=[formname, HTML-explanation]
195         my %flags = ('gonenoaddress' => ['gna', 'Gone no address'],
196                                 'lost'          => ['lost', 'Lost'],
197                                 'debarred'      => ['debarred', 'Debarred']);
198
199         my @flagdata;
200         foreach (keys(%flags)) {
201         my $key = $_;
202         my %row =  ('key'   => $key,
203                         'name'  => $flags{$key}[0],
204                         'html'  => $flags{$key}[1]);
205         if ($data->{$key}) {
206                 $row{'yes'}=' checked';
207                 $row{'no'}='';
208         } else {
209                 $row{'yes'}='';
210                 $row{'no'}=' checked';
211         }
212         push(@flagdata, \%row);
213         }
214
215         if ($modify){
216         $template->param( modify => 1 );
217         }
218
219         #Convert dateofbirth to correct format
220         $data->{'dateofbirth'} = format_date($data->{'dateofbirth'});
221
222         my @branches;
223         my @select_branch;
224         my %select_branches;
225         my $branches=getbranches();
226         my $default;
227         foreach my $branch (keys %$branches) {
228                 if (C4::Context->preference("IndependantBranches")) {
229                         my $userenv = C4::Context->userenv;
230                         if ($userenv->{flags} == 1){
231                                 push @select_branch, $branch;
232                                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
233                                 $default = $data->{'branchcode'};
234                         } else {
235                                 push @select_branch, $branch if ($branch eq $userenv->{branch});
236                                 $select_branches{$branch} = $branches->{$branch}->{'branchname'} if ($branch eq $userenv->{branch});
237                                 $default = $userenv->{branch};
238                         }
239                 } else {
240                         push @select_branch, $branch;
241                         $select_branches{$branch} = $branches->{$branch}->{'branchname'};
242                         $default = $data->{'branchcode'};
243                 }
244         }
245         
246         my $CGIbranch=CGI::scrolling_list( -name     => 'branchcode',
247                                 -id => 'branchcode',
248                                 -values   => \@select_branch,
249                                 -default  => $data->{'branchcode'},
250                                 -labels   => \%select_branches,
251                                 -size     => 1,
252                                 -multiple => 0 );
253                                 
254
255         my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data->{'sort1'});
256         if ($CGIsort1) {
257                 $template->param(CGIsort1 => $CGIsort1);
258         } else {
259                 $template->param( sort1 => $data->{'sort1'});
260         }
261
262         my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data->{'sort2'});
263         if ($CGIsort2) {
264                 $template->param(CGIsort2 =>$CGIsort2);
265         } else {
266                 $template->param( sort2 => $data->{'sort2'});
267         }
268         
269         $template->param(       actionType              => $actionType,
270                                 destination => $destination,
271                                 borrowernumber          => $borrowernumber,
272                                 address         => $data->{'streetaddress'},
273                                 firstname       => $data->{'firstname'},
274                                 surname         => $data->{'surname'},
275                                 othernames      => $data->{'othernames'},
276                                 initials        => $data->{'initials'},
277                                 ethcatpopup     => $ethcatpopup,
278                                 catcodepopup    => $catcodepopup,
279                                 streetaddress   => $data->{'streetaddress'},
280                                 physstreet   => $data->{'physstreet'},
281                                 zipcode => $data->{'zipcode'},
282                                 streetcity      => $data->{'streetcity'},
283                                 homezipcode => $data->{'homezipcode'},
284                                 city            => $data->{'city'},
285                                 phone           => $data->{'phone'},
286                                 phoneday        => $data->{'phoneday'},
287                                 faxnumber       => $data->{'faxnumber'},
288                                 emailaddress    => $data->{'emailaddress'},
289                                 textmessaging   => $data->{'textmessaging'},
290                                 contactname     => $data->{'contactname'},
291                                 altphone        => $data->{'altphone'},
292                                 altnotes        => $data->{'altnotes'},
293                                 borrowernotes   => $data->{'borrowernotes'},
294                                 flagloop        => \@flagdata,
295                                 relshiploop     => \@relshipdata,
296                                 "title_".$data->{'title'} => " SELECTED ",
297                                 joining => format_date($data->{'dateenrolled'}),
298                                 expiry          => format_date($data->{'expiry'}),
299                                 cardnumber      => $cardnumber,
300                                 dateofbirth     => $data->{'dateofbirth'},
301                                 dateformat      => display_date_format(),
302                                 modify          => $modify,
303                                 CGIbranch => $CGIbranch);
304         $template->param(Institution => 1) if ($categorycode eq "I");
305         output_html_with_http_headers $input, $cookie, $template->output;
306
307
308 }
309
310 # Local Variables:
311 # tab-width: 8
312 # End: