use Mojo::Base 'Mojo::EventEmitter';
use File::Spec;
use Mojo::Util qw(dumper);
+use Data::Dumper;
sub new {
my ($class, $app, $options) = @_;
sub render {
my ($self, $template_name, $vars, $opts) = @_;
$opts ||= {};
- my $output = '';
my $renderer = $self->app->tt2renderer;
- my $rs = $renderer->render($template_name,
- $vars,
- \$output,
- $self->{template_paths}, $opts);
- if (!$rs) {
- warn $renderer->last_error;
+ my $rs = $renderer->render($template_name, $vars);
+ return $rs;
+}
+
+sub create_message {
+ my $self = shift;
+ my $params = {@_};
+
+ my $uid = $params->{to};
+ my $from_uid = $params->{from};
+ my $template = $params->{template};
+ my $vars = $params->{vars} || {};
+ my $code = $params->{code};
+ my $type = $params->{type} || "";
+
+ return if (!$uid || !$template || !$code);
+ my $messages = $self->app->model('messages');
+
+ my $render_vars = {%$vars};
+ my $users = $self->app->model("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;
+
+ # get message mode
+ my $message_configs = $users->select_message_config(uid => $uid);
+ my $mode = -1;
+ for my $conf (@$message_configs) {
+ if ($conf->{code} == $code) {
+ $mode = $conf->{mode};
+ last;
+ }
}
- return $output;
+ return 0 if ($mode == -1);
+
+ # render title and body
+ $render_vars->{format} = "text/html";
+
+ $render_vars->{target} = "title";
+ my $title = eval { $self->render($template, $render_vars); };
+ $self->app->log->error("messages: no title; template: $template; error: $@") if !$title;
+
+ $render_vars->{target} = "body";
+ my $body = eval { $self->render($template, $render_vars); };
+ $self->app->log->error("messages: no body; template: $template; error: $@") if !$body;
+
+ my $rs = $messages->create(to => $uid,
+ from => $from_uid,
+ title => $title,
+ body => $body,
+ code => $code,
+ type => $type,
+ );
+ return $rs;
}
1;