1 package C4
::Members
::Messaging
;
3 # Copyright (C) 2008 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 C4::Members::Messaging - manage patron messaging preferences
32 use C4::Members::Messaging
36 This module lets you modify a patron's messaging preferences.
40 =head2 GetMessagingPreferences
42 my $preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrower->{'borrowernumber'},
43 message_name => 'DUE' } );
45 my $preferences = C4::Members::Messaging::GetMessagingPreferences( { categorycode => 'LIBRARY',
46 message_name => 'DUE' } );
48 returns: a hashref of messaging preferences for a borrower or patron category for a particlar message_name
50 Requires either a borrowernumber or a categorycode key, but not both.
54 sub GetMessagingPreferences
{
57 return unless exists $params->{message_name
};
58 return unless exists $params->{borrowernumber
} xor exists $params->{categorycode
}; # yes, xor
59 my $sql = <<'END_SQL';
60 SELECT borrower_message_preferences.*,
61 borrower_message_transport_preferences.message_transport_type,
62 message_attributes.message_name,
63 message_attributes.takes_days,
64 message_transports.is_digest,
65 message_transports.letter_module,
66 message_transports.letter_code
67 FROM borrower_message_preferences
68 LEFT JOIN borrower_message_transport_preferences
69 ON borrower_message_transport_preferences.borrower_message_preference_id = borrower_message_preferences.borrower_message_preference_id
70 LEFT JOIN message_attributes
71 ON message_attributes.message_attribute_id = borrower_message_preferences.message_attribute_id
72 LEFT JOIN message_transports
73 ON message_transports.message_attribute_id = message_attributes.message_attribute_id
74 AND message_transports.message_transport_type = borrower_message_transport_preferences.message_transport_type
75 AND message_transports.is_digest = borrower_message_preferences.wants_digest
76 WHERE message_attributes.message_name = ?
79 my @bind_params = ( $params->{'message_name'} );
80 if ( exists $params->{'borrowernumber'} ) {
81 $sql .= " AND borrower_message_preferences.borrowernumber = ? ";
82 push @bind_params, $params->{borrowernumber};
84 $sql .= " AND borrower_message_preferences.categorycode = ? ";
85 push @bind_params, $params->{categorycode};
88 my $sth = C4::Context->dbh->prepare($sql);
89 $sth->execute(@bind_params);
91 my %transports; # helps build a list of unique message_transport_types
92 ROW: while ( my $row = $sth->fetchrow_hashref() ) {
93 next ROW unless $row->{'message_attribute_id'};
94 $return->{'days_in_advance'} = $row->{'days_in_advance'} if defined $row->{'days_in_advance'};
95 $return->{'wants_digest'} = $row->{'wants_digest'} if defined $row->{'wants_digest'};
96 $return->{'letter_code'} = $row->{'letter_code'};
97 next unless defined $row->{'message_transport_type'};
98 $return->{'transports'}->{ $row->{'message_transport_type'} } = $row->{'letter_code'};
103 =head2 SetMessagingPreference
105 This method defines how a user (or a default for a patron category) wants to get a certain
106 message delivered. The list of valid message types can be delivered can be found in the
107 C<message_attributes> table, and the list of valid message transports can be
108 found in the C<message_transport_types> table.
110 C4::Members::Messaging::SetMessagingPreference( { borrowernumber => $borrower->{'borrowernumber'}
111 message_attribute_id => $message_attribute_id,
112 message_transport_types => [ qw( email sms ) ],
114 wants_digest
=> 1 } )
116 returns nothing useful
.
120 sub SetMessagingPreference
{
123 unless (exists $params->{borrowernumber
} xor exists $params->{categorycode
}) { # yes, xor
124 warn "SetMessagingPreference called without exactly one of borrowernumber or categorycode";
127 foreach my $required ( qw( message_attribute_id message_transport_types ) ) {
128 if ( ! exists $params->{ $required } ) {
129 warn "SetMessagingPreference called without required parameter: $required";
133 $params->{'days_in_advance'} = undef unless exists ( $params->{'days_in_advance'} );
134 $params->{'wants_digest'} = 0 unless exists ( $params->{'wants_digest'} );
136 my $dbh = C4
::Context
->dbh();
138 my $delete_sql = <<'END_SQL';
139 DELETE FROM borrower_message_preferences
140 WHERE message_attribute_id = ?
142 my @bind_params = ( $params->{'message_attribute_id'} );
143 if ( exists $params->{'borrowernumber'} ) {
144 $delete_sql .= " AND borrowernumber = ? ";
145 push @bind_params, $params->{borrowernumber};
147 $delete_sql .= " AND categorycode = ? ";
148 push @bind_params, $params->{categorycode};
150 my $sth = $dbh->prepare( $delete_sql );
151 my $deleted = $sth->execute( @bind_params );
153 if ( $params->{'message_transport_types'} ) {
154 my $insert_bmp = <<'END_SQL
';
155 INSERT INTO borrower_message_preferences
156 (borrower_message_preference_id, borrowernumber, categorycode, message_attribute_id, days_in_advance, wants_digest)
158 (NULL, ?, ?, ?, ?, ?)
161 $sth = C4::Context->dbh()->prepare($insert_bmp);
162 # set up so that we can easily construct the insert SQL
163 $params->{'borrowernumber
'} = undef unless exists ( $params->{'borrowernumber
'} );
164 $params->{'categorycode
'} = undef unless exists ( $params->{'categorycode
'} );
165 my $success = $sth->execute( $params->{'borrowernumber
'},
166 $params->{'categorycode
'},
167 $params->{'message_attribute_id
'},
168 $params->{'days_in_advance
'},
169 $params->{'wants_digest
'} );
170 # my $borrower_message_preference_id = $dbh->last_insert_id();
171 my $borrower_message_preference_id = $dbh->{'mysql_insertid
'};
173 my $insert_bmtp = <<'END_SQL';
174 INSERT INTO borrower_message_transport_preferences
175 (borrower_message_preference_id
, message_transport_type
)
179 $sth = C4
::Context
->dbh()->prepare($insert_bmtp);
180 foreach my $transport ( @
{$params->{'message_transport_types'}}) {
181 my $success = $sth->execute( $borrower_message_preference_id, $transport );
187 =head2 GetMessagingOptions
189 my $messaging_options = C4::Members::Messaging::GetMessagingOptions()
191 returns a hashref of messaging options available.
195 sub GetMessagingOptions
{
197 my $sql = <<'END_SQL';
198 select message_attributes.message_attribute_id, takes_days, message_name, message_transport_type, is_digest
199 FROM message_attributes
200 LEFT JOIN message_transports
201 ON message_attributes.message_attribute_id = message_transports.message_attribute_id
204 my $sth = C4::Context->dbh->prepare($sql);
207 while ( my $row = $sth->fetchrow_hashref() ) {
208 $choices->{ $row->{'message_name'} }->{'message_attribute_id'} = $row->{'message_attribute_id'};
209 $choices->{ $row->{'message_name'} }->{'message_name'} = $row->{'message_name'};
210 $choices->{ $row->{'message_name'} }->{'takes_days'} = $row->{'takes_days'};
211 $choices->{ $row->{'message_name'} }->{'has_digest'} = 1 if $row->{'is_digest'};
212 $choices->{ $row->{'message_name'} }->{'transport_' . $row->{'message_transport_type'}} = ' ';
215 my @return = values %$choices;
217 @return = sort { $a->{message_attribute_id} <=> $b->{message_attribute_id} } @return;
219 # warn( Data::Dumper->Dump( [ \@return ], [ 'return' ] ) );
223 =head2 SetMessagingPreferencesFromDefaults
225 C4::Members::Messaging::SetMessagingPreferencesFromDefaults( { borrowernumber => $borrower->{'borrowernumber'}
226 categorycode => 'CPL' } );
228 Given a borrowernumber and a patron category code (from the C<borrowernumber> and C<categorycode> keys
229 in the parameter hashref), replace all of the patron's current messaging preferences with
230 whatever defaults are defined for the patron category.
234 sub SetMessagingPreferencesFromDefaults {
237 foreach my $required ( qw( borrowernumber categorycode ) ) {
238 unless ( exists $params->{ $required } ) {
239 die "SetMessagingPreferencesFromDefaults called without required parameter: $required";
243 my $messaging_options = GetMessagingOptions
();
244 OPTION
: foreach my $option ( @
$messaging_options ) {
245 my $default_pref = GetMessagingPreferences
( { categorycode
=> $params->{categorycode
},
246 message_name
=> $option->{'message_name'} } );
247 # FIXME - except for setting the borrowernumber, it really ought to be possible
248 # to have the output of GetMessagingPreferences be able to be the input
249 # to SetMessagingPreference
250 my @message_transport_types = keys %{ $default_pref->{transports
} };
251 $default_pref->{message_attribute_id
} = $option->{'message_attribute_id'};
252 $default_pref->{message_transport_types
} = \
@message_transport_types;
253 $default_pref->{borrowernumber
} = $params->{borrowernumber
};
254 SetMessagingPreference
( $default_pref );
262 The actual messages which will be sent via a cron job running
263 F<misc/cronjobs/process_message_queue.pl>.
265 =head2 message_attributes
267 What kinds of messages can be sent?
269 =head2 message_transport_types
271 What transports can messages be sent vith? (email, sms, etc.)
273 =head2 message_transports
275 How are message_attributes and message_transport_types correlated?
277 =head2 borrower_message_preferences
279 What messages do the borrowers want to receive?
281 =head2 borrower_message_transport_preferences
283 What transport should a message be sent with?
287 =head2 Adding a New Kind of Message to the System
293 Add a new template to the `letter` table.
297 Insert a row into the `message_attributes` table.
301 Insert rows into `message_transports` for each message_transport_type.
311 Koha Development Team <http://koha-community.org/>
313 Andrew Moore <andrew.moore@liblime.com>