-
Notifications
You must be signed in to change notification settings - Fork 257
/
Message.pm
416 lines (328 loc) · 10.2 KB
/
Message.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package C4::Message;
# Copyright Liblime 2009
# Copyright Catalyst IT 2012
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY 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, see <http://www.gnu.org/licenses>.
use strict;
use warnings;
use C4::Context;
use C4::Letters qw( GetPreparedLetter EnqueueLetter );
use YAML::XS qw( Dump );
use Encode;
use Carp qw( carp );
=head1 NAME
C4::Message - object for messages in the message_queue table
=head1 SYNOPSIS
How to add a new message to the queue:
use C4::Message;
use C4::Items;
my $patron = Koha::Patron->find({ borrowernumber => 1 });
my $item = Koha::Items->find($itemnumber)->unblessed;
my $letter = C4::Letters::GetPreparedLetter (
module => 'circulation',
letter_code => 'CHECKOUT',
branchcode => $branch,
tables => {
'biblio', $item->{biblionumber},
'biblioitems', $item->{biblionumber},
},
);
C4::Message->enqueue($letter, $patron, 'email');
How to update a borrower's last checkout message:
use C4::Message;
my $borrower = { borrowernumber => 1 };
my $message = C4::Message->find_last_message($borrower, 'CHECKOUT', 'email');
$message->append("you also checked out some other book....");
$message->update;
=head1 DESCRIPTION
This module presents an OO interface to the message_queue. Previously,
you could only add messages to the message_queue via
C<C4::Letters::EnqueueMessage()>. With this module, you can also get
previously inserted messages, manipulate them, and save them back to the
database.
=cut
our $AUTOLOAD;
=head2 Class Methods
=head3 C4::Message->new(\%attributes)
This method creates an in-memory version of a message object.
=cut
# C4::Message->new(\%attributes) -- constructor
sub new {
my ($class, $opts) = @_;
$opts ||= {};
bless {%$opts} => $class;
}
=head3 C4::Message->find($id)
This method searches the message_queue table for a row with the given
C<message_id> and it'll return a C4::Message object if it finds one.
=cut
# C4::Message->find($id) -- find a message by its message_id
sub find {
my ($class, $id) = @_;
my $dbh = C4::Context->dbh;
my $msgs = $dbh->selectall_arrayref(
qq{SELECT * FROM message_queue WHERE message_id = ?},
{ Slice => {} },
$id,
);
if (@$msgs) {
return $class->new($msgs->[0]);
} else {
return;
}
}
=head3 C4::Message->find_last_message($borrower, $letter_code, $transport)
This method is used to get the borrower's most recent, pending, check-in or
checkout message. (This makes it possible to add more information to the
message before it gets sent out.)
=cut
# C4::Message->find_last_message($borrower, $letter_code, $transport)
# -- get the borrower's most recent pending checkin or checkout notification
sub find_last_message {
my ($class, $borrower, $letter_code, $transport) = @_;
# $type is the message_transport_type
$transport ||= 'email';
my $dbh = C4::Context->dbh;
my $msgs = $dbh->selectall_arrayref(
qq{
SELECT *
FROM message_queue
WHERE status = 'pending'
AND borrowernumber = ?
AND letter_code = ?
AND message_transport_type = ?
},
{ Slice => {} },
$borrower->{borrowernumber},
$letter_code,
$transport,
);
if (@$msgs) {
return $class->new($msgs->[0]);
} else {
return;
}
}
=head3 C4::Message->enqueue($letter, $patron, $transport)
This is a front-end for C<C4::Letters::EnqueueLetter()> that adds metadata to
the message.
=cut
# C4::Message->enqueue($letter, $borrower, $transport)
sub enqueue {
my ( $class, $letter, $patron, $transport ) = @_;
my $metadata = _metadata($letter);
my $to_address = _to_address( $patron, $transport );
# Same as render_metadata
my $format ||= sub { $_[0] || "" };
my $body = join( '', map { $format->($_) } @{ $metadata->{body} } );
$letter->{content} = $metadata->{header} . $body . $metadata->{footer};
$letter->{metadata} = Encode::decode_utf8( Dump($metadata) );
C4::Letters::EnqueueLetter(
{
letter => $letter,
borrowernumber => $patron->id,
message_transport_type => $transport,
to_address => $to_address,
}
);
}
# based on message $transport, pick an appropriate address to send to
sub _to_address {
my ( $patron, $transport ) = @_;
my $address;
if ( $transport eq 'email' ) {
$address = $patron->notice_email_address;
}
elsif ( $transport eq 'sms' ) {
$address = $patron->smsalertnumber;
}
else {
warn "'$transport' is an unknown message transport.";
}
if ( not defined $address ) {
warn "An appropriate $transport address "
. "for borrower "
. $patron->userid
. "could not be found.";
}
return $address;
}
# _metadata($letter) -- return the letter split into head/body/footer
sub _metadata {
my ($letter) = @_;
if ($letter->{content} =~ /----/) {
my ($header, $body, $footer) = split(/----\r?\n?/, $letter->{content});
return {
header => $header,
body => [$body],
footer => $footer,
};
} else {
return {
header => '',
body => [$letter->{content}],
footer => '',
};
}
}
=head2 Instance Methods
=head3 $message->update()
This saves the $message object back to the database. It needs to have
already been created via C<enqueue> for this to work.
=cut
# $object->update -- save object to database
sub update {
my ($self) = @_;
my $dbh = C4::Context->dbh;
$dbh->do(
qq{
UPDATE message_queue
SET
borrowernumber = ?,
subject = ?,
content = ?,
metadata = ?,
letter_code = ?,
message_transport_type = ?,
status = ?,
time_queued = ?,
to_address = ?,
from_address = ?,
content_type = ?
WHERE message_id = ?
},
{},
$self->borrowernumber,
$self->subject,
$self->content,
$self->{metadata}, # we want the raw YAML here
$self->letter_code,
$self->message_transport_type,
$self->status,
$self->time_queued,
$self->to_address,
$self->from_address,
$self->content_type,
$self->message_id
);
}
=head3 $message->metadata(\%new_metadata)
This method automatically serializes and deserializes the metadata
attribute. (It is stored in YAML format.)
=cut
# $object->metadata -- this is a YAML serialized column that contains a
# structured representation of $object->content
sub metadata {
my ($self, $data) = @_;
if ($data) {
$data->{header} ||= '';
$data->{body} ||= [];
$data->{footer} ||= '';
$self->{metadata} = Encode::decode_utf8(Dump($data));
$self->content($self->render_metadata);
return $data;
} else {
return YAML::XS::Load(Encode::encode_utf8($self->{metadata}));
}
}
# turn $object->metadata into a string suitable for $object->content
sub render_metadata {
my ($self, $format) = @_;
$format ||= sub { $_[0] || "" };
my $metadata = $self->metadata;
my $body = $metadata->{body};
my $text = join('', map { $format->($_) } @$body);
return $metadata->{header} . $text . $metadata->{footer};
}
=head3 $message->append(\%letter)
If passed a hashref, this method will assume that the hashref is in the form
that C<C4::Letters::getletter()> returns. It will append the body of the
letter to the message.
=head3 $message->append($string)
If passed a string, it'll append the string to the message.
=cut
# $object->append($letter_or_item) -- add a new item to a message's content
sub append {
my ($self, $letter_or_item, $format) = @_;
my ( $item, $header, $footer );
if (ref($letter_or_item)) {
my $letter = $letter_or_item;
my $metadata = _metadata($letter);
$header = $metadata->{header};
$footer = $metadata->{footer};
$item = $metadata->{body}->[0];
} else {
$item = $letter_or_item;
}
if (not $self->metadata) {
carp "Can't append to messages that don't have metadata.";
return;
}
my $metadata = $self->metadata;
push @{$metadata->{body}}, $item;
$metadata->{header} = $header;
$metadata->{footer} = $footer;
$self->metadata($metadata);
my $new_content = $self->render_metadata($format);
return $self->content($new_content);
}
=head2 Attributes Accessors
=head3 $message->message_id
=cut
=head3 $message->borrowernumber
=cut
=head3 $message->subject
=cut
=head3 $message->content
=cut
=head3 $message->metadata
=cut
=head3 $message->letter_code
=cut
=head3 $message->message_transport_type
=cut
=head3 $message->status
=cut
=head3 $message->time_queued
=cut
=head3 $message->to_address
=cut
=head3 $message->from_address
=cut
=head3 $message->content_type
=cut
# $object->$method -- treat keys as methods
sub AUTOLOAD {
my ($self, @args) = @_;
my $attr = $AUTOLOAD;
$attr =~ s/.*://;
if (ref($self->{$attr}) eq 'CODE') {
$self->{$attr}->($self, @args);
} else {
if (@args) {
$self->{$attr} = $args[0];
} else {
$self->{$attr};
}
}
}
sub DESTROY { }
1;
=head1 SEE ALSO
L<C4::Circulation>, L<C4::Letters>, L<C4::Members::Messaging>
=head1 AUTHOR
John Beppu <[email protected]>
=cut