Skip to content

Commit

Permalink
Categories LastPost are not updated when a post is deleted.
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeta committed May 27, 2015
1 parent 94de4db commit d0f580e
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 36 deletions.
19 changes: 11 additions & 8 deletions src/Controller/Forum/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace App\Controller\Forum;

use App\Controller\AppController;
use App\Event\Forum\LastPostUpdater;
use App\Event\Forum\Notifications;
use App\Event\Forum\Statistics;
use Cake\Core\Configure;
Expand Down Expand Up @@ -253,6 +254,7 @@ public function delete()
->find()
->select([
'ForumThreads.id',
'ForumThreads.category_id',
'ForumThreads.last_post_id',
'ForumThreads.last_post_date',
'ForumThreads.first_post_id'
Expand All @@ -267,18 +269,19 @@ public function delete()

$this->ForumThreads->save($thread);

//Event.
//Update the last post for all the parent category.
$this->eventManager()->on(new LastPostUpdater());
$event = new Event('LastPostUpdater.delete', $this, [
'thread' => $thread
]);
$this->eventManager()->dispatch($event);

//Event Statistics.
$this->eventManager()->attach(new Statistics());
$event = new Event('Model.ForumPosts.new', $this);
$this->eventManager()->dispatch($event);

$this->Flash->success(__("The post has been deleted successfully !"));

return $this->redirect([
'controller' => 'posts',
'action' => 'go',
$lastPost->id
]);
$post->forum_thread->last_post_id = $lastPost->id;
}

$this->Flash->success(__("The post has been deleted successfully !"));
Expand Down
43 changes: 15 additions & 28 deletions src/Controller/Forum/ThreadsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use App\Controller\AppController;
use App\Event\Badges;
use App\Event\Forum\Followers;
use App\Event\Forum\LastPostUpdater;
use App\Event\Forum\Notifications;
use App\Event\Forum\Statistics;
use Cake\Event\Event;
Expand Down Expand Up @@ -86,20 +87,13 @@ public function create()
$newThread->reply_count = 0;
$this->ForumThreads->save($newThread);

//Update the last post for all the parent category.
$this->loadModel('ForumCategories');
$category = $this->ForumCategories->get($newThread->category_id);
$parents = $this->ForumCategories
->find()
->where([
'lft <=' => $category->lft,
'rght >=' => $category->rght
]);

foreach ($parents as $parent) {
$parent->last_post_id = $newPost->id;
$this->ForumCategories->save($parent);
}
//LastPostUpdater Event.
$this->eventManager()->attach(new LastPostUpdater());
$event = new Event('LastPostUpdater.new', $this, [
'thread' => $newThread,
'post' => $newPost
]);
$this->eventManager()->dispatch($event);

//Statistics Event.
$this->eventManager()->attach(new Statistics());
Expand Down Expand Up @@ -282,20 +276,13 @@ public function reply()
$thread->last_post_id = $newPost->id;
$this->ForumThreads->save($thread);

//Update the last post for all the parent category.
$this->loadModel('ForumCategories');
$category = $this->ForumCategories->get($thread->category_id);
$parents = $this->ForumCategories
->find()
->where([
'lft <=' => $category->lft,
'rght >=' => $category->rght
]);

foreach ($parents as $parent) {
$parent->last_post_id = $newPost->id;
$this->ForumCategories->save($parent);
}
//LastPostUpdater Event.
$this->eventManager()->attach(new LastPostUpdater());
$event = new Event('LastPostUpdater.new', $this, [
'thread' => $thread,
'post' => $newPost
]);
$this->eventManager()->dispatch($event);

//Statistics Event.
$this->eventManager()->attach(new Statistics());
Expand Down
96 changes: 96 additions & 0 deletions src/Event/Forum/LastPostUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
namespace App\Event\Forum;

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
use Cake\ORM\TableRegistry;

class LastPostUpdater implements EventListenerInterface
{
/**
* ImplementedEvents method.
*
* @return array
*/
public function implementedEvents()
{
return [
'LastPostUpdater.new' => 'newPost',
'LastPostUpdater.delete' => 'deletePost'
];
}

/**
* A new post has been posted.
*
* @param \Cake\Event\Event $event The event that was fired.
*
* @return false
*/
public function newPost(Event $event)
{
if (!isset($event->data['thread']) || !isset($event->data['post'])) {
return false;
}
if ($this->_updateCategories($event->data['thread'], $event->data['post'])) {
return true;
}
return false;
}

/**
* A post has been deleted.
*
* @param \Cake\Event\Event $event The event that was fired.
*
* @return false
*/
public function deletePost(Event $event)
{
if (!isset($event->data['thread'])) {
return false;
}
$this->ForumPosts = TableRegistry::get('ForumPosts');

$thread = $event->data['thread'];
$newLastPost = $this->ForumPosts
->find()
->where(['thread_id' => $thread->id])
->order(['created' => 'DESC'])
->first();

if ($this->_updateCategories($thread, $newLastPost)) {
return true;
}

return false;
}

/**
* Update each parent categories related to the thread category.
*
* @param \App\Model\Entity\ForumThread $thread The thread where we must update the categories.
* @param \App\Model\Entity\ForumPost $post The new last post.
*
* @return bool
*/
private function _updateCategories($thread, $post)
{
$this->ForumCategories = TableRegistry::get('ForumCategories');

$category = $this->ForumCategories->get($thread->category_id);
$parents = $this->ForumCategories
->find()
->where([
'lft <=' => $category->lft,
'rght >=' => $category->rght
]);

foreach ($parents as $parent) {
$parent->last_post_id = $post->id;
$this->ForumCategories->save($parent);
}

return true;
}
}

0 comments on commit d0f580e

Please sign in to comment.