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::getletter('circulation', 'CHECKOUT');
22 C4::Letters::parseletter($letter, 'biblio', $item->{biblionumber});
23 C4::Letters::parseletter($letter, 'biblioitems', $item->{biblionumber});
24 C4::Message->enqueue($letter, $borrower->{borrowernumber}, 'email');
26 How to update a borrower's last checkout message:
29 my $borrower = { borrowernumber => 1 };
30 my $message = C4::Message->find_last_message($borrower, 'CHECKOUT', 'email');
31 $message->append("you also checked out some other book....");
36 This module presents an OO interface to the message_queue. Previously,
37 you could only add messages to the message_queue via
38 C<C4::Letters::EnqueueMessage()>. With this module, you can also get
39 previously inserted messages, manipulate them, and save them back to the
50 =head3 C4::Message->new(\%attributes)
52 This method creates an in-memory version of a message object.
56 # C4::Message->new(\%attributes) -- constructor
58 my ($class, $opts) = @_;
60 bless {%$opts} => $class;
64 =head3 C4::Message->find($id)
66 This method searches the message_queue table for a row with the given
67 C<message_id> and it'll return a C4::Message object if it finds one.
71 # C4::Message->find($id) -- find a message by its message_id
73 my ($class, $id) = @_;
74 my $dbh = C4
::Context
->dbh;
75 my $msgs = $dbh->selectall_arrayref(
76 qq{SELECT
* FROM message_queue WHERE message_id
= ?
},
81 return $class->new($msgs->[0]);
87 =head3 C4::Message->find_last_message($borrower, $letter_code, $transport)
89 This method is used to get the borrower's most recent, pending, check-in or
90 checkout message. (This makes it possible to add more information to the
91 message before it gets sent out.)
95 # C4::Message->find_last_message($borrower, $letter_code, $transport)
96 # -- get the borrower's most recent pending checkin or checkout notification
97 sub find_last_message
{
98 my ($class, $borrower, $letter_code, $transport) = @_;
99 # $type is the message_transport_type
100 $transport ||= 'email';
101 my $dbh = C4
::Context
->dbh;
102 my $msgs = $dbh->selectall_arrayref(
106 WHERE status
= 'pending'
107 AND borrowernumber
= ?
109 AND message_transport_type
= ?
112 $borrower->{borrowernumber
},
117 return $class->new($msgs->[0]);
124 =head3 C4::Message->enqueue($letter, $borrower, $transport)
126 This is a front-end for C<C4::Letters::EnqueueLetter()> that adds metadata to
131 # C4::Message->enqueue($letter, $borrower, $transport)
133 my ($class, $letter, $borrower, $transport) = @_;
134 my $metadata = _metadata
($letter);
135 my $to_address = _to_address
($borrower, $transport);
136 $letter->{metadata
} = Dump
($metadata);
137 #carp "enqueuing... to $to_address";
138 C4
::Letters
::EnqueueLetter
({
140 borrowernumber
=> $borrower->{borrowernumber
},
141 message_transport_type
=> $transport,
142 to_address
=> $to_address,
146 # based on message $transport, pick an appropriate address to send to
148 my ($borrower, $transport) = @_;
150 if ($transport eq 'email') {
151 $address = $borrower->{email
}
152 || $borrower->{emailpro
}
153 || $borrower->{B_email
};
154 } elsif ($transport eq 'sms') {
155 $address = $borrower->{smsalertnumber
}
156 || $borrower->{phone
}
157 || $borrower->{phonepro
}
158 || $borrower->{B_phone
};
160 warn "'$transport' is an unknown message transport.";
162 if (not defined $address) {
163 warn "An appropriate $transport address "
164 . "for borrower $borrower->{userid} "
165 . "could not be found.";
170 # _metadata($letter) -- return the letter split into head/body/footer
173 if ($letter->{content
} =~ /----/) {
174 my ($header, $body, $footer) = split(/----\r?\n?/, $letter->{content
});
183 body
=> [$letter->{content
}],
189 =head2 Instance Methods
191 =head3 $message->update()
193 This saves the $message object back to the database. It needs to have
194 already been created via C<enqueue> for this to work.
198 # $object->update -- save object to database
201 my $dbh = C4
::Context
->dbh;
211 message_transport_type
= ?
,
220 $self->borrowernumber,
223 $self->{metadata
}, # we want the raw YAML here
225 $self->message_transport_type,
235 =head3 $message->metadata(\%new_metadata)
237 This method automatically serializes and deserializes the metadata
238 attribute. (It is stored in YAML format.)
242 # $object->metadata -- this is a YAML serialized column that contains a
243 # structured representation of $object->content
245 my ($self, $data) = @_;
247 $data->{header
} ||= '';
248 $data->{body
} ||= [];
249 $data->{footer
} ||= '';
250 $self->{metadata
} = Dump
($data);
251 $self->content($self->render_metadata);
254 return Load
($self->{metadata
});
258 # turn $object->metadata into a string suitable for $object->content
259 sub render_metadata
{
260 my ($self, $format) = @_;
261 $format ||= sub { $_[0] || "" };
262 my $metadata = $self->metadata;
263 my $body = $metadata->{body
};
264 my $text = join('', map { $format->($_) } @
$body);
265 return $metadata->{header
} . $text . $metadata->{footer
};
268 =head3 $message->append(\%letter)
270 If passed a hashref, this method will assume that the hashref is in the form
271 that C<C4::Letters::getletter()> returns. It will append the body of the
272 letter to the message.
274 =head3 $message->append($string)
276 If passed a string, it'll append the string to the message.
280 # $object->append($letter_or_item) -- add a new item to a message's content
282 my ($self, $letter_or_item, $format) = @_;
284 if (ref($letter_or_item)) {
285 my $letter = $letter_or_item;
286 my $metadata = _metadata
($letter);
287 $item = $metadata->{body
}->[0];
289 $item = $letter_or_item;
291 if (not $self->metadata) {
292 carp
"Can't append to messages that don't have metadata.";
295 my $metadata = $self->metadata;
296 push @
{$metadata->{body
}}, $item;
297 $self->metadata($metadata);
298 my $new_content = $self->render_metadata($format);
299 return $self->content($new_content);
302 =head2 Attributes Accessors
304 =head3 $message->message_id
308 =head3 $message->borrowernumber
312 =head3 $message->subject
316 =head3 $message->content
320 =head3 $message->metadata
324 =head3 $message->letter_code
328 =head3 $message->message_transport_type
332 =head3 $message->status
336 =head3 $message->time_queued
340 =head3 $message->to_address
344 =head3 $message->from_address
348 =head3 $message->content_type
352 # $object->$method -- treat keys as methods
354 my ($self, @args) = @_;
355 my $attr = $AUTOLOAD;
357 if (ref($self->{$attr}) eq 'CODE') {
358 $self->{$attr}->($self, @args);
361 $self->{$attr} = $args[0];
374 L<C4::Circulation>, L<C4::Letters>, L<C4::Members::Messaging>
378 John Beppu <john.beppu@liblime.com>