From 8e5eea5c4e1a8fa500a6912b2d9d9ddff92bad70 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Wed, 20 May 2009 11:35:54 -0500 Subject: [PATCH] bug 3222: set messaging preferences during patron import If the EnhancedMessagingPreferences option is ON, when creating a new patron record via the patron import, set the default messaging preferences to the applicable values the default preferences for the patron category. Messaging preferences are currently changed only when *adding* a patron record via the import; if the import updates an existing record, the patron's existing preferences are not changed. API changes: SetMessagingPreferencesFromDefaults() is a new function in C4::Members::Messaging to unconditionally replace the current messaging preferences of a patron with the default preferences from a specified patron category. Signed-off-by: Daniel Sweeney Signed-off-by: Galen Charlton --- C4/Members/Messaging.pm | 36 +++++++++++++++++++++++++++++++++++- tools/import_borrowers.pl | 6 ++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/C4/Members/Messaging.pm b/C4/Members/Messaging.pm index 899a3fbab9..08d2b0b4ba 100644 --- a/C4/Members/Messaging.pm +++ b/C4/Members/Messaging.pm @@ -191,7 +191,7 @@ END_SQL my $messaging_options = C4::Members::Messaging::GetMessagingOptions() -returns a hashref of messaing options available. +returns a hashref of messaging options available. =cut @@ -220,6 +220,40 @@ END_SQL return \@return; } +=head2 SetMessagingPreferencesFromDefaults + + C4::Members::Messaging::SetMessagingPreferenceFromDefaults( { borrowernumber => $borrower->{'borrowernumber'} + categorycode => 'CPL' } ); + +Given a borrowernumber and a patron category code (from the C and C keys +in the parameter hashref), replace all of the patron's current messaging preferences with +whatever defaults are defined for the patron category. + +=cut + +sub SetMessagingPreferencesFromDefaults { + my $params = shift; + + foreach my $required ( qw( borrowernumber categorycode ) ) { + unless ( exists $params->{ $required } ) { + die "SetMessagingPreferencesFromDefaults called without required parameter: $required"; + } + } + + my $messaging_options = GetMessagingOptions(); + OPTION: foreach my $option ( @$messaging_options ) { + my $default_pref = GetMessagingPreferences( { categorycode => $params->{categorycode}, + message_name => $option->{'message_name'} } ); + # FIXME - except for setting the borrowernumber, it really ought to be possible + # to have the output of GetMessagingPreferences be able to be the input + # to SetMessagingPreference + $default_pref->{message_attribute_id} = $option->{'message_attribute_id'}; + $default_pref->{message_transport_types} = $default_pref->{transports}; + $default_pref->{borrowernumber} = $params->{borrowernumber}; + SetMessagingPreference( $default_pref ); + } +} + =head1 TABLES =head2 message_queue diff --git a/tools/import_borrowers.pl b/tools/import_borrowers.pl index 5d477ef230..038527508f 100755 --- a/tools/import_borrowers.pl +++ b/tools/import_borrowers.pl @@ -44,6 +44,7 @@ use C4::Branch qw(GetBranchName); use C4::Members; use C4::Members::Attributes; 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'; @@ -264,6 +266,10 @@ if ( $uploadborrowers && length($uploadborrowers) > 0 ) { if ($extended) { C4::Members::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 { -- 2.39.5