X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=tools%2Fimport_borrowers.pl;h=d953667fb3d5db21f3813d50091e457b2240d595;hb=e1952c8ad737b8dd731f1c3995967e5ecc315d19;hp=9ac994bc7500b9da88b59b297bbd0573d1a451f4;hpb=9fb2d09ffc0d2de1235356b6f5394b5329c4f8c2;p=koha_gimpoz diff --git a/tools/import_borrowers.pl b/tools/import_borrowers.pl index 9ac994bc75..d953667fb3 100755 --- a/tools/import_borrowers.pl +++ b/tools/import_borrowers.pl @@ -13,18 +13,18 @@ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. See the GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along with -# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, -# Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # Script to take some borrowers data in a known format and load it into Koha # # File format # # cardnumber,surname,firstname,title,othernames,initials,streetnumber,streettype, -# address line , address line 2, city, zipcode, email, phone, mobile, fax, work email, work phone, +# address line , address line 2, city, zipcode, contry, email, phone, mobile, fax, work email, work phone, # alternate streetnumber, alternate streettype, alternate address line 1, alternate city, -# alternate zipcode, alternate email, alternate phone, date of birth, branchcode, +# alternate zipcode, alternate country, alternate email, alternate phone, date of birth, branchcode, # categorycode, enrollment date, expiry date, noaddress, lost, debarred, contact surname, # contact firstname, contact title, borrower notes, contact relationship, ethnicity, ethnicity notes # gender, username, opac note, contact note, password, sort one, sort two @@ -42,8 +42,9 @@ use C4::Dates qw(format_date_in_iso); use C4::Context; use C4::Branch qw(GetBranchName); use C4::Members; -use C4::Members::Attributes; +use C4::Members::Attributes qw(:all); use C4::Members::AttributeTypes; +use C4::Members::Messaging; use Text::CSV; # Text::CSV::Unicode, even in binary mode, fails to parse lines with these diacriticals: @@ -55,6 +56,7 @@ use CGI; my (@errors, @feedback); my $extended = C4::Context->preference('ExtendedPatronAttributes'); +my $set_messaging_prefs = C4::Context->preference('EnhancedMessagingPreferences'); my @columnkeys = C4::Members->columns; if ($extended) { push @columnkeys, 'patron_attributes'; @@ -62,7 +64,7 @@ if ($extended) { my $columnkeystpl = [ map { {'key' => $_} } grep {$_ ne 'borrowernumber' && $_ ne 'cardnumber'} @columnkeys ]; # ref. to array of hashrefs. my $input = CGI->new(); -my $csv = Text::CSV->new({binary => 1}); # binary needed for non-ASCII Unicode +our $csv = Text::CSV->new({binary => 1}); # binary needed for non-ASCII Unicode # push @feedback, {feedback=>1, name=>'backend', value=>$csv->backend, backend=>$csv->backend}; my ( $template, $loggedinuser, $cookie ) = get_template_and_user({ @@ -123,6 +125,7 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { $csvkeycol{$keycol} = $col++; } #warn($borrowerline); + my $ext_preserve = $input->param('ext_preserve') || 0; if ($extended) { $matchpoint_attr_type = C4::Members::AttributeTypes->fetch($matchpoint); } @@ -143,6 +146,12 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { push @missing_criticals, {badparse=>1, line=>$., lineraw=>$borrowerline}; } elsif (@columns == @columnkeys) { @borrower{@columnkeys} = @columns; + # MJR: try to fill blanks gracefully by using default values + foreach my $key (@criticals) { + if ($borrower{$key} !~ /\S/) { + $borrower{$key} = $defaults{$key}; + } + } } else { # MJR: try to recover gracefully by using default values foreach my $key (@columnkeys) { @@ -181,14 +190,10 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { # The first 25 errors are enough. Keeping track of 30,000+ would destroy performance. next LINE; } - my @attrs; if ($extended) { my $attr_str = $borrower{patron_attributes}; - delete $borrower{patron_attributes}; - my $ok = $csv->parse($attr_str); - my @list = $csv->fields(); - # FIXME error handling - $patron_attributes = [ map { map { my @arr = split /:/, $_, 2; { code => $arr[0], value => $arr[1] } } $_ } @list ]; + delete $borrower{patron_attributes}; # not really a field in borrowers, so we don't want to pass it to ModMember. + $patron_attributes = extended_attributes_code_value_arrayref($attr_str); } # Popular spreadsheet applications make it difficult to force date outputs to be zero-padded, but we require it. foreach (qw(dateofbirth dateenrolled dateexpiry)) { @@ -207,7 +212,7 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { my $borrowernumber; my $member; if ( ($matchpoint eq 'cardnumber') && ($borrower{'cardnumber'}) ) { - $member = GetMember( $borrower{'cardnumber'}, 'cardnumber' ); + $member = GetMember( 'cardnumber' => $borrower{'cardnumber'} ); if ($member) { $borrowernumber = $member->{'borrowernumber'}; } @@ -231,20 +236,24 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { next LINE; } $borrower{'borrowernumber'} = $borrowernumber; - for my $col ( keys %borrower) { - # use values from extant patron unless our csv file includes this column or we provided a default. - # FIXME : You cannot update a field with a perl-evaluated false value using the defaults. - unless(exists($csvkeycol{$col}) || $defaults{$col}) { - $borrower{$col} = $member->{$col} if($member->{$col}) ; + for my $col (keys %borrower) { + # use values from extant patron unless our csv file includes this column or we provided a default. + # FIXME : You cannot update a field with a perl-evaluated false value using the defaults. + unless(exists($csvkeycol{$col}) || $defaults{$col}) { + $borrower{$col} = $member->{$col} if($member->{$col}) ; + } } - } unless (ModMember(%borrower)) { $invalid++; $template->param('lastinvalid'=>$borrower{'surname'}.' / '.$borrowernumber); next LINE; } if ($extended) { - C4::Members::Attributes::SetBorrowerAttributes($borrower{'borrowernumber'}, $patron_attributes); + if ($ext_preserve) { + my $old_attributes = GetBorrowerAttributes($borrowernumber); + $patron_attributes = extended_attributes_merge($old_attributes, $patron_attributes); #TODO: expose repeatable options in template + } + SetBorrowerAttributes($borrower{'borrowernumber'}, $patron_attributes); } $overwritten++; $template->param('lastoverwritten'=>$borrower{'surname'}.' / '.$borrowernumber); @@ -252,16 +261,20 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { # FIXME: fixup_cardnumber says to lock table, but the web interface doesn't so this doesn't either. # At least this is closer to AddMember than in members/memberentry.pl if (!$borrower{'cardnumber'}) { - $borrower{'cardnumber'} = fixup_cardnumber(''); + $borrower{'cardnumber'} = fixup_cardnumber(undef); } if ($borrowernumber = AddMember(%borrower)) { if ($extended) { - C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $patron_attributes); + SetBorrowerAttributes($borrowernumber, $patron_attributes); + } + if ($set_messaging_prefs) { + C4::Members::Messaging::SetMessagingPreferencesFromDefaults({ borrowernumber => $borrowernumber, + categorycode => $borrower{categorycode} }); } $imported++; $template->param('lastimported'=>$borrower{'surname'}.' / '.$borrowernumber); } else { - $invalid++; # was just "$invalid", I assume incrementing was the point --atz + $invalid++; $template->param('lastinvalid'=>$borrower{'surname'}.' / AddMember'); } }