}
sub create {
- #my ($self, $uid, $from_uid, $title, $body, $code, $options) = @_;
my $self = shift;
my $params = {@_};
my $uid = $params->{to};
my $from_uid = $params->{from};
- my $template = $params->{template};
- my $vars = $params->{vars} || {};
+ my $title = $params->{title};
+ my $body = $params->{body};
my $code = $params->{code};
my $type = $params->{type} || "";
- return if $self->check_readonly;
- return if (!$uid || !$template || !$code);
-
- my $render_vars = {%$vars};
my $users = $self->new_instance_of("Users");
- my $from_user = $users->select(uid => $from_uid) || {};
- my $to_user = $users->select(uid => $uid) || {};
- $render_vars->{from_user} = $from_user;
- $render_vars->{to_user} = $to_user;
+ return if $self->check_readonly;
+ return if (!$uid || !$title || !$body || !$code);
+
+ # get message mode
my $message_configs = $users->select_message_config(uid => $uid);
my $mode = -1;
for my $conf (@$message_configs) {
last;
}
}
- return 0 if ($mode == -1);
-
- # first, render title and body
- $render_vars->{format} = "text/html";
- $render_vars->{target} = "title";
- my $title = eval { $self->_render($template, $render_vars); };
- $render_vars->{target} = "body";
- my $body = eval { $self->_render($template, $render_vars); };
+ return if $mode == -1;
- $self->logger->error("messages: no title; template: $template") if !$title;
- $self->logger->error("messages: no body; template: $template") if !$body;
-
- # fist, create message_drop
+ # create message_drop
+ my $dbh = $self->start_transaction;
my $sql = <<"EOSQL";
INSERT INTO message_drop
(user, fuser, code, date, altto, send, message)
(?, ?, ?, NOW(), "", "defer", ?)
EOSQL
- my $dbh = $self->start_transaction;
- my $sth = $dbh->prepare($sql);
- my $rs = $sth->execute($uid, $from_uid, $code, $body);
+ my $rs = $dbh->do($sql, undef, $uid, $from_uid, $code, $body);
if (!$rs) {
$self->set_error($dbh->errstr, $dbh->err);
$self->rollback;
}
my $message_id = $dbh->last_insert_id(undef, undef, undef, undef);
+ # create log
+ $sql = <<"EOSQL";
+INSERT INTO message_log
+ (id, user, fuser, code, mode, date)
+ VALUE
+ (?, ?, ?, ?, ?, NOW())
+EOSQL
+
+ $rs = $dbh->do($sql, undef,
+ $message_id,
+ $uid,
+ $from_uid,
+ $code,
+ $mode);
+ if (!$rs) {
+ $self->set_error($dbh->errstr, $dbh->err);
+ $self->rollback;
+ return;
+ }
+
+ # send message
if ($mode == 0) {
# mode == 0: mail
#my $mail = $self->new_instance_of("mail_messages");