diff --git a/lib/WebGUI/Forum.pm b/lib/WebGUI/Forum.pm index 3bc295e14..a0b51fc0b 100644 --- a/lib/WebGUI/Forum.pm +++ b/lib/WebGUI/Forum.pm @@ -47,7 +47,9 @@ sub set { my ($self, $data) = @_; $data->{forumId} = $self->get("forumId") unless ($data->{forumId}); WebGUI::SQL->setRow("forum","forumId",$data); - $self->{_properties} = $data; + foreach my $key (keys %{$data}) { + $self->{_properties}{$key} = $data->{$key}; + } } 1; diff --git a/lib/WebGUI/Forum/Post.pm b/lib/WebGUI/Forum/Post.pm index dba17a5ac..a9433e0ec 100644 --- a/lib/WebGUI/Forum/Post.pm +++ b/lib/WebGUI/Forum/Post.pm @@ -6,12 +6,6 @@ use WebGUI::Forum::Thread; use WebGUI::Session; use WebGUI::SQL; -sub addView { - my ($self) = @_; - WebGUI::SQL->write("update forumPost set views=views+1 where forumPostId=".$self->get("forumPostId")); - $self->getThread->addView; -} - sub canEdit { my ($self, $userId) = @_; $userId = $session{user}{userId} unless ($userId); @@ -25,8 +19,10 @@ sub create { $data->{forumPostId} = "new"; my $forumPostId = WebGUI::SQL->setRow("forumPost","forumPostId", $data); $self = WebGUI::Forum::Post->new($forumPostId); - if ($data->{parentId} > 0) { - $self->getThread->addReply($self->get("forumPostId"),$forumPostId,$self->get("dateOfPost")); + if ($self->getThread->getForum->get("moderatePosts")) { + $self->setStatusPending; + } else { + $self->setStatusApproved; } return $self; } @@ -71,6 +67,15 @@ sub isMarkedRead { return $isRead; } +sub isReply { + my ($self) = @_; + if ($self->get("parentId") > 0) { + return 1; + } else { + return 0; + } +} + sub markRead { my ($self, $userId) = @_; $userId = $session{user}{userId} unless ($userId); @@ -82,28 +87,42 @@ sub markRead { } sub new { - my ($self, $forumPostId) = @_; + my ($class, $forumPostId) = @_; my $properties = WebGUI::SQL->getRow("forumPost","forumPostId",$forumPostId); - bless {_properties=>$properties}, $self; + if (defined $properties) { + bless {_properties=>$properties}, $class; + } else { + return undef; + } } sub set { my ($self, $data) = @_; $data->{forumPostId} = $self->get("forumPostId") unless ($data->{forumPostId}); WebGUI::SQL->setRow("forumPost","forumPostId",$data); - $self->{_properties} = $data; + foreach my $key (keys %{$data}) { + $self->{_properties}{$key} = $data->{$key}; + } } sub setStatusApproved { my ($self) = @_; $self->set({status=>'approved'}); $self->getThread->setStatusApproved if ($self->getThread->get("rootPostId") == $self->get("forumPostId")); + if ($self->isReply) { + $self->getThread->incrementReplies($self->get("dateOfPost"),$self->get("forumPostId")); + } } sub setStatusDeleted { my ($self) = @_; $self->set({status=>'deleted'}); $self->getThread->setStatusDeleted if ($self->getThread->get("rootPostId") == $self->get("forumPostId")); + if ($self->getThread->get("lastPostId") == $self->get("forumPostId")) { + my ($id, $date) = WebGUI::SQL->quickArray("select forumPostId,dateOfPost from forumPost where forumThreadId=" + .$self->get("forumThreadId")." and status='approved'"); + $self->getThread->setLastPost($id,$date); + } } sub setStatusDenied { diff --git a/lib/WebGUI/Forum/Thread.pm b/lib/WebGUI/Forum/Thread.pm index 93c9d3520..8772aed58 100644 --- a/lib/WebGUI/Forum/Thread.pm +++ b/lib/WebGUI/Forum/Thread.pm @@ -7,18 +7,6 @@ use WebGUI::Forum::Post; use WebGUI::Session; use WebGUI::SQL; -sub addReply { - my ($self, $dateOfReply, $replyId) = @_; - WebGUI::SQL->write("update forumThread set replies=replies+1, lastPostId=$replyId, lastPostDate=$dateOfReply - where forumThreadId=".$self->get("forumThreadId")); - #add method to notify users for subscriptions -} - -sub addView { - my ($self) = @_; - WebGUI::SQL->write("update forumThread set views=views+1 where forumThreadId=".$self->get("forumThreadId")); -} - sub create { my ($self, $data, $postData) = @_; $data->{forumThreadId} = "new"; @@ -82,6 +70,13 @@ sub isLocked { return $self->get("isLocked"); } +sub incrementReplies { + my ($self, $dateOfReply, $replyId) = @_; + WebGUI::SQL->write("update forumThread set replies=replies+1, lastPostId=$replyId, lastPostDate=$dateOfReply + where forumThreadId=".$self->get("forumThreadId")); + #add method to notify users for subscriptions +} + sub incrementViews { my ($self) = @_; WebGUI::SQL->write("update forumThread set views=views+1 where forumThreadId=".$self->get("forumThreadId")); @@ -119,7 +114,17 @@ sub set { my ($self, $data) = @_; $data->{forumThreadId} = $self->get("forumThreadId") unless ($data->{forumThreadId}); WebGUI::SQL->setRow("forumThread","forumThreadId",$data); - $self->{_properties} = $data; + foreach my $key (keys %{$data}) { + $self->{_properties}{$key} = $data->{$key}; + } +} + +sub setLastPost { + my ($self, $postId, $postDate) = @_; + $self->set({ + lastPostId=>$postId, + lastPostDate=>$postDate + }); } sub setStatusApproved {