lots of changes

This commit is contained in:
Frank Dillon 2008-11-12 23:03:42 +00:00
parent 93df39d6f6
commit 615e0e3746
28 changed files with 2883 additions and 212 deletions

View file

@ -97,15 +97,43 @@ sub create {
$self->{_properties}{subject} = $properties->{subject} || WebGUI::International->new($session)->get(523);
$self->{_properties}{message} = $properties->{message};
$self->{_properties}{dateStamp} = time();
$self->{_properties}{userId} = $properties->{userId};
$self->{_properties}{userId} = $properties->{userId} || $session->user->userId;
$self->{_properties}{groupId} = $properties->{groupId};
$self->{_properties}{sentBy} = $properties->{sentBy} || 3;
if ($self->{_properties}{status} eq "completed") {
my $status = $self->{_properties}{status};
if ($status eq "completed") {
$self->{_properties}{completedBy} = $session->user->userId;
$self->{_properties}{completedOn} = time();
}
elsif($status ne "pending") {
$self->{_properties}{status} = "active";
}
$self->{_messageId} = $self->{_properties}{messageId} = $session->db->setRow("inbox","messageId",$self->{_properties});
$self->{_userId } = $self->{_properties}{userId};
$self->{_inbox } = $self->{_properties};
#Add the message state row for individual user passed in
if($self->{_properties}{userId}) {
$session->db->write(
q{ REPLACE INTO inbox_messageState (messageId,userId) VALUES (?,?) },
[$self->{_messageId},$self->{_properties}{userId}]
);
}
#Add the message state row for every user in the group
if($self->{_properties}{groupId}) {
my $g = WebGUI::Group->new($session,$self->{_properties}{groupId});
my $users = $g->getAllUsers;
foreach my $userId (@{$users}) {
$session->db->write(
q{ REPLACE INTO inbox_messageState (messageId,userId) VALUES (?,?) },
[$self->{_messageId},$userId]
);
}
}
my $subject = (defined $properties->{emailSubject}) ? $properties->{emailSubject} : $self->{_properties}{subject};
my $mail = WebGUI::Mail::Send->create($session, {
toUser=>$self->{_properties}{userId},
@ -137,16 +165,34 @@ sub create {
#-------------------------------------------------------------------
=head2 delete ( )
=head2 delete ( userId )
Deletes this message from the inbox.
Deletes this message from the inbox for the user passed in
=head3 userId
User to delete message for. If no user is passed in, the current user will be used.
=cut
sub delete {
my $self = shift;
my $sth = $self->session->db->prepare("delete from inbox where messageId=?");
$sth->execute([$self->getId]);
my $self = shift;
my $session = $self->session;
my $db = $session->db;
my $messageId = $self->getId;
my $userId = shift || $self->{_userId};
$self->setDeleted($userId);
my $isActive = $db->quickScalar(
q{ select count(*) from inbox_messageState where messageId=? and deleted=0 },
[$messageId]
);
#Delete the message from the database if everyone who was sent the message has deleted it
unless ($isActive) {
$db->write("delete from inbox where messageId=?",[$messageId]);
$db->write("delete from inbox_messageState where messageId=?",[$messageId]);
}
}
#-------------------------------------------------------------------
@ -158,8 +204,8 @@ Deconstructor.
=cut
sub DESTROY {
my $self = shift;
undef $self;
my $self = shift;
undef $self;
}
#-------------------------------------------------------------------
@ -189,6 +235,17 @@ An epoch date representing when the action associated with this message was comp
sub get {
my $self = shift;
my $name = shift;
if($name eq "status") {
my $status = $self->{_properties}{status};
if($status eq "active") {
return "read" if($self->{_properties}{isRead});
return "replied" if($self->{_properties}{repliedTo});
return "unread";
}
return $status;
}
return $self->{_properties}{$name};
}
@ -206,6 +263,48 @@ sub getId {
return $self->{_messageId};
}
#-------------------------------------------------------------------
=head2 getStatus ( [ userId ] )
Gets the current status of the message for the user passed in
=head3 userId
The id of the user to get the status of the message for. Defaults to the current user.
=cut
sub getStatus {
my $self = shift;
my $userId = shift || $self->{_userId};
my $status = $self->{_properties}{status};
my $statusCodes = $self->statusCodes;
if($status eq "active") {
return $statusCodes->{"replied"} if($self->{_properties}{repliedTo});
return $statusCodes->{"read" } if($self->{_properties}{isRead});
return $statusCodes->{"unread" };
}
return $statusCodes->{$self->get("status")};
}
#-------------------------------------------------------------------
=head2 isRead ( )
Returns whether or not the message has been read.
=cut
sub isRead {
my $self = shift;
return $self->{_properties}{isRead};
}
#-------------------------------------------------------------------
=head2 new ( session, messageId )
@ -224,10 +323,31 @@ The unique id of a message.
=cut
sub new {
my $class = shift;
my $session = shift;
my $class = shift;
my $session = shift;
my $messageId = shift;
bless {_properties=>$session->db->getRow("inbox","messageId",$messageId), _session=>$session, _messageId=>$messageId}, $class;
my $userId = shift || $session->user->userId;
#Don't bother going on if a messageId wasn't passed in
return undef unless $messageId;
my $inbox = $session->db->getRow("inbox","messageId",$messageId);
my $statusValues = $session->db->quickHashRef(
q{ select isRead, repliedTo, deleted from inbox_messageState where messageId=? and userId=? },
[$messageId,$userId]
);
#Don't return messages that don't exist
return undef unless (scalar(keys %{$inbox}));
#Don't return deleted messages
return undef if($statusValues->{deleted});
my $self = {};
my %properties = (%{$inbox},%{$statusValues});
bless {_properties=>\%properties, _inbox=>$inbox, _session=>$session, _messageId=>$messageId, _userId=>$userId}, $class;
}
#-------------------------------------------------------------------
@ -261,9 +381,78 @@ sub setCompleted {
$self->{_properties}{status} = "completed";
$self->{_properties}{completedBy} = $userId;
$self->{_properties}{completedOn} = time();
$self->session->db->setRow("inbox","messageId",$self->{_properties});
$self->session->db->setRow("inbox","messageId",$self->{_inbox});
#Completed messages should also be marked read
$self->setRead($userId);
}
#-------------------------------------------------------------------
=head2 setDeleted ( [ userId ] )
Marks a message deleted.
=head4 userId
The id of the user that deleted this message. Defaults to the current user.
=cut
sub setDeleted {
my $self = shift;
my $userId = shift || $self->session->user->userId;
$self->session->db->write(
q{update inbox_messageState set deleted=1 where messageId=? and userId=?},
[$self->getId,$userId]
);
}
#-------------------------------------------------------------------
=head2 setRead ( [ userId ] )
Marks a message read.
=head4 userId
The id of the user that reads this message. Defaults to the current user.
=cut
sub setRead {
my $self = shift;
my $userId = shift || $self->session->user->userId;
$self->session->db->write(
q{update inbox_messageState set isRead=1 where messageId=? and userId=?},
[$self->getId,$userId]
);
}
#-------------------------------------------------------------------
=head2 setReplied ( [ userId ] )
Marks a message replied.
=head4 userId
The id of the user that replied to this message. Defaults to the current user.
=cut
sub setReplied {
my $self = shift;
my $userId = shift || $self->session->user->userId;
$self->session->db->write(
q{update inbox_messageState set repliedTo=1, isRead=1 where messageId=? and userId=?},
[$self->getId,$userId]
);
}
#-------------------------------------------------------------------
=head2 setStatus ( status,[ userId ] )
@ -281,22 +470,111 @@ The id of the user that completed this task. Defaults to the current user.
=cut
sub setStatus {
my $self = shift;
my $status = shift;
my $userId = shift || $self->session->user->userId;
my $self = shift;
my $status = shift;
my $session = $self->session;
my $userId = shift || $session->user->userId;
unless ($status) {
$self->session->errorHandler->warn("No status passed in for message. Exit without update");
$session->log->warn("No status passed in for message. Exit without update");
return undef;
}
unless($self->isValidStatus($status)) {
$self->session->log->warn("Invalid status $status passed in for message. Exit without update");
return undef;
}
if($status eq "completed") {
$self->setCompleted($userId);
return undef;
}
$self->{_properties}{status} = $status;
$self->session->db->setRow("inbox","messageId",$self->{_properties});
elsif($status eq "read") {
$self->setRead($userId);
}
elsif($status eq "unread") {
$self->setUnread($userId);
}
elsif($status eq "replied") {
$self->setReplied($userId);
}
$self->{_properties}{status} = ( $status ne "pending") ? "active" : "pending";
$self->session->db->setRow("inbox","messageId",$self->{_inbox});
return undef;
}
#-------------------------------------------------------------------
=head2 setUnread ( [ userId ] )
Marks a message unread.
=head4 userId
The id of the user that reads this message. Defaults to the current user.
=cut
sub setUnread {
my $self = shift;
my $userId = shift || $self->session->user->userId;
$self->session->db->write(
q{update inbox_messageState set isRead=0 where messageId=? and userId=?},
[$self->getId,$userId]
);
}
#-------------------------------------------------------------------
=head2 statusCodes ( session )
Returns a hash ref of valid status values. Can be called as a class or instance method
=head4 status
The id of the user that replied to this message. Defaults to the current user.
=cut
sub statusCodes {
my $self = shift;
my $session = shift;
if(ref $self eq "WebGUI::Inbox::Message") {
$session = $self->session;
}
my $i18n = WebGUI::International->new($session);
return {
"active" => $i18n->get("inbox message status active"),
"pending" => $i18n->get(552),
"completed" => $i18n->get(350),
"unread" => $i18n->get("private message status unread"),
"read" => $i18n->get("private message status read"),
"replied" => $i18n->get("private message status replied"),
}
}
#-------------------------------------------------------------------
=head2 isValidStatus ( status )
Returns whether or not the status passed in is valid. Can be called as a class or instance method
=head4 status
The id of the user that replied to this message. Defaults to the current user.
=cut
sub isValidStatus {
my $self = shift;
my $status = shift;
return (exists $self->statusCodes->{$status});
}
1;