Bug 2505 - enable Perl warnings in all modules and scripts
[koha.git] / C4 / Members / Messaging.pm
blob2b39f115d24d0fcbd297952143d3071abc3faf2e
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 use warnings;
22 use C4::Context;
24 use vars qw($VERSION);
26 BEGIN {
27 # set the version for version checking
28 $VERSION = 3.00;
31 =head1 NAME
33 C4::Members::Messaging - manage patron messaging preferences
35 =head1 SYNOPSIS
37 use C4::Members::Messaging
39 =head1 DESCRIPTION
41 This module lets you modify a patron's messaging preferences.
43 =head1 FUNCTIONS
45 =head2 GetMessagingPreferences
47 my $preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrower->{'borrowernumber'},
48 message_name => 'DUE' } );
50 my $preferences = C4::Members::Messaging::GetMessagingPreferences( { categorycode => 'LIBRARY',
51 message_name => 'DUE' } );
53 returns: a hashref of messaging preferences for a borrower or patron category for a particlar message_name
55 Requires either a borrowernumber or a categorycode key, but not both.
57 =cut
59 sub GetMessagingPreferences {
60 my $params = shift;
62 return unless exists $params->{message_name};
63 return unless exists $params->{borrowernumber} xor exists $params->{categorycode}; # yes, xor
64 my $sql = <<'END_SQL';
65 SELECT borrower_message_preferences.*,
66 borrower_message_transport_preferences.message_transport_type,
67 message_attributes.message_name,
68 message_attributes.takes_days,
69 message_transports.is_digest,
70 message_transports.letter_module,
71 message_transports.letter_code
72 FROM borrower_message_preferences
73 LEFT JOIN borrower_message_transport_preferences
74 ON borrower_message_transport_preferences.borrower_message_preference_id = borrower_message_preferences.borrower_message_preference_id
75 LEFT JOIN message_attributes
76 ON message_attributes.message_attribute_id = borrower_message_preferences.message_attribute_id
77 JOIN message_transports
78 ON message_transports.message_attribute_id = message_attributes.message_attribute_id
79 AND message_transports.message_transport_type = borrower_message_transport_preferences.message_transport_type
80 WHERE message_attributes.message_name = ?
81 END_SQL
83 my @bind_params = ( $params->{'message_name'} );
84 if ( exists $params->{'borrowernumber'} ) {
85 $sql .= " AND borrower_message_preferences.borrowernumber = ? ";
86 push @bind_params, $params->{borrowernumber};
87 } else {
88 $sql .= " AND borrower_message_preferences.categorycode = ? ";
89 push @bind_params, $params->{categorycode};
92 my $sth = C4::Context->dbh->prepare($sql);
93 $sth->execute(@bind_params);
94 my $return;
95 my %transports; # helps build a list of unique message_transport_types
96 ROW: while ( my $row = $sth->fetchrow_hashref() ) {
97 next ROW unless $row->{'message_attribute_id'};
98 $return->{'days_in_advance'} = $row->{'days_in_advance'} if defined $row->{'days_in_advance'};
99 $return->{'wants_digest'} = $row->{'wants_digest'} if defined $row->{'wants_digest'};
100 $return->{'letter_code'} = $row->{'letter_code'};
101 $transports{$row->{'message_transport_type'}} = 1;
103 @{$return->{'transports'}} = keys %transports;
104 return $return;
107 =head2 SetMessagingPreference
109 This method defines how a user (or a default for a patron category) wants to get a certain
110 message delivered. The list of valid message types can be delivered can be found in the
111 C<message_attributes> table, and the list of valid message transports can be
112 found in the C<message_transport_types> table.
114 C4::Members::Messaging::SetMessagingPreference( { borrowernumber => $borrower->{'borrowernumber'}
115 message_attribute_id => $message_attribute_id,
116 message_transport_types => [ qw( email sms ) ],
117 days_in_advance => 5
118 wants_digest => 1 } )
120 returns nothing useful.
122 =cut
124 sub SetMessagingPreference {
125 my $params = shift;
127 unless (exists $params->{borrowernumber} xor exists $params->{categorycode}) { # yes, xor
128 warn "SetMessagingPreference called without exactly one of borrowernumber or categorycode";
129 return;
131 foreach my $required ( qw( message_attribute_id message_transport_types ) ) {
132 if ( ! exists $params->{ $required } ) {
133 warn "SetMessagingPreference called without required parameter: $required";
134 return;
137 $params->{'days_in_advance'} = undef unless exists ( $params->{'days_in_advance'} );
138 $params->{'wants_digest'} = 0 unless exists ( $params->{'wants_digest'} );
140 my $dbh = C4::Context->dbh();
142 my $delete_sql = <<'END_SQL';
143 DELETE FROM borrower_message_preferences
144 WHERE message_attribute_id = ?
145 END_SQL
146 my @bind_params = ( $params->{'message_attribute_id'} );
147 if ( exists $params->{'borrowernumber'} ) {
148 $delete_sql .= " AND borrowernumber = ? ";
149 push @bind_params, $params->{borrowernumber};
150 } else {
151 $delete_sql .= " AND categorycode = ? ";
152 push @bind_params, $params->{categorycode};
154 my $sth = $dbh->prepare( $delete_sql );
155 my $deleted = $sth->execute( @bind_params );
157 if ( $params->{'message_transport_types'} ) {
158 my $insert_bmp = <<'END_SQL';
159 INSERT INTO borrower_message_preferences
160 (borrower_message_preference_id, borrowernumber, categorycode, message_attribute_id, days_in_advance, wants_digest)
161 VALUES
162 (NULL, ?, ?, ?, ?, ?)
163 END_SQL
165 $sth = C4::Context->dbh()->prepare($insert_bmp);
166 # set up so that we can easily construct the insert SQL
167 $params->{'borrowernumber'} = undef unless exists ( $params->{'borrowernumber'} );
168 $params->{'categorycode'} = undef unless exists ( $params->{'categorycode'} );
169 my $success = $sth->execute( $params->{'borrowernumber'},
170 $params->{'categorycode'},
171 $params->{'message_attribute_id'},
172 $params->{'days_in_advance'},
173 $params->{'wants_digest'} );
174 # my $borrower_message_preference_id = $dbh->last_insert_id();
175 my $borrower_message_preference_id = $dbh->{'mysql_insertid'};
177 my $insert_bmtp = <<'END_SQL';
178 INSERT INTO borrower_message_transport_preferences
179 (borrower_message_preference_id, message_transport_type)
180 VALUES
181 (?, ?)
182 END_SQL
183 $sth = C4::Context->dbh()->prepare($insert_bmtp);
184 foreach my $transport ( @{$params->{'message_transport_types'}}) {
185 my $success = $sth->execute( $borrower_message_preference_id, $transport );
188 return;
191 =head2 GetMessagingOptions
193 my $messaging_options = C4::Members::Messaging::GetMessagingOptions()
195 returns a hashref of messaging options available.
197 =cut
199 sub GetMessagingOptions {
201 my $sql = <<'END_SQL';
202 select message_attributes.message_attribute_id, takes_days, message_name, message_transport_type, is_digest
203 FROM message_attributes
204 LEFT JOIN message_transports
205 ON message_attributes.message_attribute_id = message_transports.message_attribute_id
206 END_SQL
208 my $sth = C4::Context->dbh->prepare($sql);
209 $sth->execute();
210 my $choices;
211 while ( my $row = $sth->fetchrow_hashref() ) {
212 $choices->{ $row->{'message_name'} }->{'message_attribute_id'} = $row->{'message_attribute_id'};
213 $choices->{ $row->{'message_name'} }->{'message_name'} = $row->{'message_name'};
214 $choices->{ $row->{'message_name'} }->{'takes_days'} = $row->{'takes_days'};
215 $choices->{ $row->{'message_name'} }->{'has_digest'} = 1 if $row->{'is_digest'};
216 $choices->{ $row->{'message_name'} }->{'transport_' . $row->{'message_transport_type'}} = ' ';
219 my @return = values %$choices;
220 # warn( Data::Dumper->Dump( [ \@return ], [ 'return' ] ) );
221 return \@return;
224 =head2 SetMessagingPreferencesFromDefaults
226 C4::Members::Messaging::SetMessagingPreferenceFromDefaults( { borrowernumber => $borrower->{'borrowernumber'}
227 categorycode => 'CPL' } );
229 Given a borrowernumber and a patron category code (from the C<borrowernumber> and C<categorycode> keys
230 in the parameter hashref), replace all of the patron's current messaging preferences with
231 whatever defaults are defined for the patron category.
233 =cut
235 sub SetMessagingPreferencesFromDefaults {
236 my $params = shift;
238 foreach my $required ( qw( borrowernumber categorycode ) ) {
239 unless ( exists $params->{ $required } ) {
240 die "SetMessagingPreferencesFromDefaults called without required parameter: $required";
244 my $messaging_options = GetMessagingOptions();
245 OPTION: foreach my $option ( @$messaging_options ) {
246 my $default_pref = GetMessagingPreferences( { categorycode => $params->{categorycode},
247 message_name => $option->{'message_name'} } );
248 # FIXME - except for setting the borrowernumber, it really ought to be possible
249 # to have the output of GetMessagingPreferences be able to be the input
250 # to SetMessagingPreference
251 $default_pref->{message_attribute_id} = $option->{'message_attribute_id'};
252 $default_pref->{message_transport_types} = $default_pref->{transports};
253 $default_pref->{borrowernumber} = $params->{borrowernumber};
254 SetMessagingPreference( $default_pref );
258 =head1 TABLES
260 =head2 message_queue
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?
285 =head1 CONFIG
287 =head2 Adding a New Kind of Message to the System
289 =over 4
291 =item 1.
293 Add a new template to the `letter` table.
295 =item 2.
297 Insert a row into the `message_attributes` table.
299 =item 3.
301 Insert rows into `message_transports` for each message_transport_type.
303 =back
305 =head1 SEE ALSO
307 L<C4::Letters>
309 =head1 AUTHOR
311 Koha Development Team <http://koha-community.org/>
313 Andrew Moore <andrew.moore@liblime.com>
315 =cut