diff --git a/lib/WebGUI/Inbox.pm b/lib/WebGUI/Inbox.pm new file mode 100644 index 000000000..a8e850b57 --- /dev/null +++ b/lib/WebGUI/Inbox.pm @@ -0,0 +1,148 @@ +package WebGUI::Inbox; + +=head1 LEGAL + + ------------------------------------------------------------------- + WebGUI is Copyright 2001-2006 Plain Black Corporation. + ------------------------------------------------------------------- + Please read the legal notices (docs/legal.txt) and the license + (docs/license.txt) that came with this distribution before using + this software. + ------------------------------------------------------------------- + http://www.plainblack.com info@plainblack.com + ------------------------------------------------------------------- + +=cut + +use strict; + +=head1 NAME + +Package WebGUI::Inbox; + +=head1 DESCRIPTION + +This class provides a message routing system, which is primarily used by WebGUI's workflow engine. + +=head1 SYNOPSIS + + use WebGUI::Inbox; + +=head1 METHODS + +These methods are available from this class: + +=cut + + +#------------------------------------------------------------------- + +=head2 addMessage ( ) + +=cut + +sub addMessage { + my $self = shift; + return WebGUI::Inbox::Message->create($self); +} + +#------------------------------------------------------------------- + +=head2 DESTROY ( ) + +Deconstructor. + +=cut + +sub DESTROY { + my $self = shift; + undef $self; +} + +#------------------------------------------------------------------- + +=head2 getMessage ( messageId ) + +Returns a WebGUI::Inbox::Message object. + +=head3 messageId + +The id of the message to retrieve. + +=cut + +sub getMessage { + my $self = shift; + return WebGUI::Inbox::Message->new($self->session, shift); +} + + +#------------------------------------------------------------------- + +=head2 getMessagesForUser ( user [ , limit ] ) + +Returns an array reference containing the most recent message objects for a given user. + +=head3 user + +A user object. + +=head3 limit + +An integer indicating the number of messages to fetch. Defaults to 30. + +=cut + +sub getMessagesForUser { + my $self = shift; + my $user = shift; + my $limit = shift; + my @messages = (); + my $counter = 0; + my $rs = $self->session->db->read("select messageId, userId, groupId from inbox order by status='pending', dateStamp"); + while (my ($messageId, $userId, $groupId) = $rs->array) { + if ($user->userId eq $userId || $user->isInGroup($groupId)) { + push(@messages, $self->getMessage($messageId)); + $counter++; + } + last if ($counter >= $limit); + } + $rs->finish; + return \@messages; +} + + +#------------------------------------------------------------------- + +=head2 new ( session ) + +Constructor. + +=head3 session + +A reference to the current session. + +=cut + +sub new { + my $class = shift; + my $session = shift; + bless {_session=>$session}, $class; +} + +#------------------------------------------------------------------- + +=head2 session + +Returns a reference to the current session. + +=cut + +sub session { + my $self = shift; + return $self->{_session}; +} + + +1; + diff --git a/lib/WebGUI/Inbox/Message.pm b/lib/WebGUI/Inbox/Message.pm new file mode 100644 index 000000000..fd9e8ae44 --- /dev/null +++ b/lib/WebGUI/Inbox/Message.pm @@ -0,0 +1,227 @@ +package WebGUI::Inbox::Message; + +=head1 LEGAL + + ------------------------------------------------------------------- + WebGUI is Copyright 2001-2006 Plain Black Corporation. + ------------------------------------------------------------------- + Please read the legal notices (docs/legal.txt) and the license + (docs/license.txt) that came with this distribution before using + this software. + ------------------------------------------------------------------- + http://www.plainblack.com info@plainblack.com + ------------------------------------------------------------------- + +=cut + +use strict; + +=head1 NAME + +Package WebGUI::Inbox::Message; + +=head1 DESCRIPTION + +This package provides an API for working with inbox messages. + +=head1 SYNOPSIS + + use WebGUI::Inbox::Message; + + my $message = WebGUI::Inbox::Message->new($session, $messageId); + +=head1 METHODS + +These methods are available from this class: + +=cut + + +#------------------------------------------------------------------- + +=head2 create ( session, properties ) + +Creates a new message. + +=head2 session + +A reference to the current session. + +=head3 properties + +A hash reference containing the properties to update. + +=head4 message + +The content of this message. + +=head4 subject + +The topic of this message. + +=head4 status + +May be "pending" or "completed". Defaults to "pending". You should set this to completed if this is a message without an action, such as a notification. + +=head4 userId + +A userId of a user attached to this message. + +=head4 groupId + +A groupId of a group attached to this message. + +=head + +=cut + +sub create { + my $class = shift; + my $session = shift; + my $properties = shift; + my $self = {}; + $self->{_properties}{messageId} = "new"; + $self->{_properties}{status} = $properties->{status} || "pending"; + $self->{_properties}{subject} = $properties->{subject} || "No Subject"; + $self->{_properties}{message} = $properties->{message}; + $self->{_properties}{dateStamp} = time(); + $self->{_properties}{userId} = $properties->{userId}; + $self->{_properties}{groupId} = $properties->{groupId}; + if ($self->{_properties}{status} eq "completed") { + $self->{_properties}{completedBy} = $session->user->userId; + $self->{_properties}{completedOn} = time(); + } + $self->{_messageId} = $self->{_properties}{messageId} = $session->setRow("inbox","messageId",$self->{_properties}); + $self->{_session} = $session; + bless $self, $class; +} + +#------------------------------------------------------------------- + +=head2 delete + +Deletes this message from the inbox. + +=cut + +sub delete { + my $self = shift; + my $sth = $self->session->db->prepare("delete from userInbox where messageId=?"); + $sth->execute($self->getId); +} + +#------------------------------------------------------------------- + +=head DESTROY ( ) + +Deconstructor. + +=cut + +sub DESTROY { + my $self = shift; + undef $self; +} + +#------------------------------------------------------------------- + +=head2 get ( property ) + +Returns the value of a property. + +=head3 property + +The name of any property of an inbox message. See create() for details. In addition to those settable by create, you may also retrieve these: + +=head4 dateStamp + +The date the message was created. + +=head4 completedBy + +The userId of the user that completed the action associated with this message. + +=head4 completedOn + +An epoch date representing when the action associated with this message was completed. + +=cut + +sub get { + my $self = shift; + return $self->{_properties}{shift}; +} + + +#------------------------------------------------------------------- + +=head2 getId () + +Returns the ID of this message. + +=cut + +sub getId { + my $self = shift; + return $self->{_messageId}; +} + +#------------------------------------------------------------------- + +=head2 new ( session, messageId ) + +Constructor. + +=head3 session + +A reference to the current session. + +=head3 messageId + +The unique id of a message. + +=cut + +sub new { + my $class = shift; + my $session = shift; + my $messageId = shift; + bless {_properties=>$self->session->db->getRow("userInbox","messageId",$self->getId), _session=>$session, _messageId=>$messageId}, $class; +} + +#------------------------------------------------------------------- + +=head2 session + +Returns a reference to the current session. + +=cut + +sub session { + my $self = shift; + return $self->{_session}; +} + +#------------------------------------------------------------------- + +=head2 setCompleted ( [ userId ] ) + +Marks a message completed. + +=head4 userId + +The id of the user that completed this task. Defaults to the current user. + +=cut + +sub setCompleted { + my $self = shift; + my $userId = shift || $self->session->user->userId; + $self->{_properties}{status} = "completed"; + $self->{_properties}{completedBy} = $userId; + $self->{_properties}{completedOn} = time(); + $self->session->db->setRow("inbox","messageId",$self->{_properties}); +} + +1; +