-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Categories LastPost are not updated when a post is deleted.
- Loading branch information
Xeta
committed
May 27, 2015
1 parent
94de4db
commit d0f580e
Showing
3 changed files
with
122 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |