iiplc/II/Render.pm

247 lines
5.0 KiB
Perl
Raw Normal View History

2014-06-11 15:55:00 +04:00
package II::Render;
use II::DB;
use II::T;
use Data::Dumper;
sub new {
my $class = shift;
my $db = II::DB->new();
my $t = II::T->new();
2014-07-30 14:27:48 +04:00
my $c = II::Config->new();
my $config = $c->load();
2014-06-11 15:55:00 +04:00
my $self = {
_db => $db,
_template => $t,
2014-07-30 14:27:48 +04:00
_config => $config,
2014-06-11 15:55:00 +04:00
};
bless $self, $class;
return $self;
}
sub thread {
my ( $self, $subg, $echo ) = @_;
my $db = $self->{_db};
my $t = $self->{_template};
2014-07-30 14:27:48 +04:00
my $config = $self->{_config};
2014-06-11 15:55:00 +04:00
my @post = $db->thread( $subg, $echo );
# Render header
2014-07-30 14:27:48 +04:00
my $render = $t->head("ii ". $config->{name} ." :: $echo");
2014-06-11 15:55:00 +04:00
my $count = 0;
while ( $count < @post ) {
$render .= $t->post( @post[$count] );
$count++;
}
$render .= $t->foot();
return $render;
}
sub out {
my ($self) = @_;
my $db = $self->{_db};
my $t = $self->{_template};
2014-07-30 14:27:48 +04:00
my $config = $self->{_config};
2014-06-11 15:55:00 +04:00
my @post = $db->select_out();
# Render header
my $render
2014-07-30 14:27:48 +04:00
= $t->head('ii '. $config->{name} .' :: неотправленные сообщения');
2014-06-11 15:55:00 +04:00
my $count = 0;
while ( $count < @post ) {
# Render post
$render .= $t->out( @post[$count] );
$count++;
}
$render .= $t->foot();
}
sub echo_mes {
2014-06-15 19:12:03 +04:00
my ( $self, $echo, $view ) = @_;
2014-06-11 15:55:00 +04:00
my $db = $self->{_db};
my $t = $self->{_template};
2014-07-30 14:27:48 +04:00
my $config = $self->{_config};
2014-06-11 15:55:00 +04:00
my @post = $db->echoes($echo);
# Render header
2014-07-30 14:27:48 +04:00
my $render = $t->head("ii ". $config->{name} ." :: $echo");
2014-06-15 19:12:03 +04:00
$render .= $t->echo($echo);
2014-06-11 15:55:00 +04:00
my $count = 0;
2014-06-24 11:31:20 +04:00
if ( $view eq 'thread' ) {
2014-06-15 19:12:03 +04:00
while ( $count < @post ) {
2014-06-11 15:55:00 +04:00
2014-06-15 19:12:03 +04:00
# Render post
if ( !( @post[$count]->{subg} =~ /Re.+/ ) ) {
$render .= $t->tree( @post[$count] );
}
2014-06-11 15:55:00 +04:00
2014-06-15 19:12:03 +04:00
$count++;
}
}
else {
while ( $count < @post ) {
$render .= $t->post( @post[$count] );
$count++;
}
2014-06-11 15:55:00 +04:00
}
$render .= $t->foot();
return $render;
}
sub to_me {
my ( $self, $config ) = @_;
my $db = $self->{_db};
my $t = $self->{_template};
2014-07-30 14:27:48 +04:00
my $config = $self->{_config};
2014-06-11 15:55:00 +04:00
my @post = $db->to_me($config);
my @post_from_me = $db->from_me($config);
# Render header
2014-07-30 14:27:48 +04:00
my $render = $t->head('ii '. $config->{name} .' :: Моя переписка');
2014-06-11 15:55:00 +04:00
my $count = 0;
while ( $count < @post ) {
# Render post
$render .= $t->post( @post[$count] );
$count++;
}
$render .= $t->foot();
return $render;
}
sub tree {
my ( $self, $config ) = @_;
my $db = $self->{_db};
}
# Render index page
sub index {
my ( $self, $config ) = @_;
my $db = $self->{_db};
my $echoareas = $config->{echoareas};
my $t = $self->{_template};
my @hashes = $db->select_index(50);
# Render header
2014-07-30 14:27:48 +04:00
my $render = $t->head('ii '. $config->{name} .' :: Лента');
2014-06-11 15:55:00 +04:00
$render .= $t->index($echoareas);
while (<@hashes>) {
my $message = $_;
my $data = $db->select_new($message);
# Render post
$render .= $t->post($data);
}
$render .= $t->foot();
return $render;
}
2014-06-24 11:31:20 +04:00
# Messages from user
sub user {
my ( $self, $user ) = @_;
my $db = $self->{_db};
my $t = $self->{_template};
2014-07-30 14:27:48 +04:00
my $config = $self->{_config};
2014-06-24 11:31:20 +04:00
# Render header
my $render
2014-07-30 14:27:48 +04:00
= $t->head("ii ". $config->{name} ." :: Сообщения пользователя $user");
2014-06-24 11:31:20 +04:00
my @post = $db->select_user($user);
my $count = 0;
while ( $count < @post ) {
# Render post
$render .= $t->post( @post[$count] );
$count++;
}
$render .= $t->foot();
}
2014-06-11 15:55:00 +04:00
# Render new message form
sub send_new {
my ( $self, $echo ) = @_;
my $t = $self->{_template};
2014-07-30 14:27:48 +04:00
my $config = $self->{_config};
2014-06-11 15:55:00 +04:00
2014-07-30 14:27:48 +04:00
my $render = $t->head("ii ". $config->{name} ." :: Новое сообщение");
2014-06-11 15:55:00 +04:00
$render .= $t->new_mes($echo);
$render .= $t->foot();
return $render;
}
# Render send form
sub send {
my ( $self, $hash ) = @_;
my $db = $self->{_db};
my $t = $self->{_template};
2014-07-30 14:27:48 +04:00
my $config = $self->{_config};
2014-06-11 15:55:00 +04:00
2014-07-30 14:27:48 +04:00
my $render = $t->head("ii". $config->{name} ." :: Ответ на $hash");
2014-06-11 15:55:00 +04:00
# Render post
my $data = $db->select_new($hash);
$render .= $t->send($data);
$render .= $t->foot();
return $render;
}
# Render new messages
sub new_mes {
my ( $self, $msgs ) = @_;
my $db = $self->{_db};
my $t = $self->{_template};
2014-07-30 14:27:48 +04:00
my $config = $self->{_config};
2014-06-11 15:55:00 +04:00
2014-07-30 14:27:48 +04:00
my $render = $t->head('ii '. $config->{name} .' :: Новые сообщения');
2014-06-11 15:55:00 +04:00
# Render posts
if ( defined($msgs) ) {
my @msgs_list = split /\n/, $msgs;
while (<@msgs_list>) {
my $message = $_;
my $data = $db->select_new($message);
# Render post
$render .= $t->post($data);
}
}
# else {
# $render .= "<p>Новых сообщений нет</p>";
# }
$render .= $t->foot();
return $render;
}
1;