11 C4::Message - object for messages in the message_queue table
15 How to add a new message to the queue:
19 my $borrower = { borrowernumber => 1 };
20 my $item = C4::Items::GetItem(1);
21 my $letter = C4::Letters::GetPreparedLetter (
22 module => 'circulation',
23 letter_code => 'CHECKOUT',
24 branchcode => $branch,
26 'biblio', $item->{biblionumber},
27 'biblioitems', $item->{biblionumber},
30 C4::Message->enqueue($letter, $borrower->{borrowernumber}, 'email');
32 How to update a borrower's last checkout message:
35 my $borrower = { borrowernumber => 1 };
36 my $message = C4::Message->find_last_message($borrower, 'CHECKOUT', 'email');
37 $message->append("you also checked out some other book....");
42 This module presents an OO interface to the message_queue. Previously,
43 you could only add messages to the message_queue via
44 C<C4::Letters::EnqueueMessage()>. With this module, you can also get
45 previously inserted messages, manipulate them, and save them back to the
56 =head3 C4::Message->new(\%attributes)
58 This method creates an in-memory version of a message object.
62 # C4::Message->new(\%attributes) -- constructor
64 my ($class, $opts) = @_;
66 bless {%$opts} => $class;
70 =head3 C4::Message->find($id)
72 This method searches the message_queue table for a row with the given
73 C<message_id> and it'll return a C4::Message object if it finds one.
77 # C4::Message->find($id) -- find a message by its message_id
79 my ($class, $id) = @_;
80 my $dbh = C4
::Context
->dbh;
81 my $msgs = $dbh->selectall_arrayref(
82 qq{SELECT
* FROM message_queue WHERE message_id
= ?
},
87 return $class->new($msgs->[0]);
93 =head3 C4::Message->find_last_message($borrower, $letter_code, $transport)
95 This method is used to get the borrower's most recent, pending, check-in or
96 checkout message. (This makes it possible to add more information to the
97 message before it gets sent out.)
101 # C4::Message->find_last_message($borrower, $letter_code, $transport)
102 # -- get the borrower's most recent pending checkin or checkout notification
103 sub find_last_message
{
104 my ($class, $borrower, $letter_code, $transport) = @_;
105 # $type is the message_transport_type
106 $transport ||= 'email';
107 my $dbh = C4
::Context
->dbh;
108 my $msgs = $dbh->selectall_arrayref(
112 WHERE status
= 'pending'
113 AND borrowernumber
= ?
115 AND message_transport_type
= ?
118 $borrower->{borrowernumber
},
123 return $class->new($msgs->[0]);
130 =head3 C4::Message->enqueue($letter, $borrower, $transport)
132 This is a front-end for C<C4::Letters::EnqueueLetter()> that adds metadata to
137 # C4::Message->enqueue($letter, $borrower, $transport)
139 my ($class, $letter, $borrower, $transport) = @_;
140 my $metadata = _metadata
($letter);
141 my $to_address = _to_address
($borrower, $transport);
142 $letter->{metadata
} = Dump
($metadata);
143 #carp "enqueuing... to $to_address";
144 C4
::Letters
::EnqueueLetter
({
146 borrowernumber
=> $borrower->{borrowernumber
},
147 message_transport_type
=> $transport,
148 to_address
=> $to_address,
152 # based on message $transport, pick an appropriate address to send to
154 my ($borrower, $transport) = @_;
156 if ($transport eq 'email') {
157 $address = $borrower->{email
}
158 || $borrower->{emailpro
}
159 || $borrower->{B_email
};
160 } elsif ($transport eq 'sms') {
161 $address = $borrower->{smsalertnumber
}
162 || $borrower->{phone
}
163 || $borrower->{phonepro
}
164 || $borrower->{B_phone
};
166 warn "'$transport' is an unknown message transport.";
168 if (not defined $address) {
169 warn "An appropriate $transport address "
170 . "for borrower $borrower->{userid} "
171 . "could not be found.";
176 # _metadata($letter) -- return the letter split into head/body/footer
179 if ($letter->{content
} =~ /----/) {
180 my ($header, $body, $footer) = split(/----\r?\n?/, $letter->{content
});
189 body
=> [$letter->{content
}],
195 =head2 Instance Methods
197 =head3 $message->update()
199 This saves the $message object back to the database. It needs to have
200 already been created via C<enqueue> for this to work.
204 # $object->update -- save object to database
207 my $dbh = C4
::Context
->dbh;
217 message_transport_type
= ?
,
226 $self->borrowernumber,
229 $self->{metadata
}, # we want the raw YAML here
231 $self->message_transport_type,
241 =head3 $message->metadata(\%new_metadata)
243 This method automatically serializes and deserializes the metadata
244 attribute. (It is stored in YAML format.)
248 # $object->metadata -- this is a YAML serialized column that contains a
249 # structured representation of $object->content
251 my ($self, $data) = @_;
253 $data->{header
} ||= '';
254 $data->{body
} ||= [];
255 $data->{footer
} ||= '';
256 $self->{metadata
} = Dump
($data);
257 $self->content($self->render_metadata);
260 return Load
($self->{metadata
});
264 # turn $object->metadata into a string suitable for $object->content
265 sub render_metadata
{
266 my ($self, $format) = @_;
267 $format ||= sub { $_[0] || "" };
268 my $metadata = $self->metadata;
269 my $body = $metadata->{body
};
270 my $text = join('', map { $format->($_) } @
$body);
271 return $metadata->{header
} . $text . $metadata->{footer
};
274 =head3 $message->append(\%letter)
276 If passed a hashref, this method will assume that the hashref is in the form
277 that C<C4::Letters::getletter()> returns. It will append the body of the
278 letter to the message.
280 =head3 $message->append($string)
282 If passed a string, it'll append the string to the message.
286 # $object->append($letter_or_item) -- add a new item to a message's content
288 my ($self, $letter_or_item, $format) = @_;
290 if (ref($letter_or_item)) {
291 my $letter = $letter_or_item;
292 my $metadata = _metadata
($letter);
293 $item = $metadata->{body
}->[0];
295 $item = $letter_or_item;
297 if (not $self->metadata) {
298 carp
"Can't append to messages that don't have metadata.";
301 my $metadata = $self->metadata;
302 push @
{$metadata->{body
}}, $item;
303 $self->metadata($metadata);
304 my $new_content = $self->render_metadata($format);
305 return $self->content($new_content);
308 =head2 Attributes Accessors
310 =head3 $message->message_id
314 =head3 $message->borrowernumber
318 =head3 $message->subject
322 =head3 $message->content
326 =head3 $message->metadata
330 =head3 $message->letter_code
334 =head3 $message->message_transport_type
338 =head3 $message->status
342 =head3 $message->time_queued
346 =head3 $message->to_address
350 =head3 $message->from_address
354 =head3 $message->content_type
358 # $object->$method -- treat keys as methods
360 my ($self, @args) = @_;
361 my $attr = $AUTOLOAD;
363 if (ref($self->{$attr}) eq 'CODE') {
364 $self->{$attr}->($self, @args);
367 $self->{$attr} = $args[0];
380 L<C4::Circulation>, L<C4::Letters>, L<C4::Members::Messaging>
384 John Beppu <john.beppu@liblime.com>