Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Feature #276 ACP: basic forums management #275

Merged
merged 6 commits into from
Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Database/Models/Forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Forum extends Model implements HasPresenter, InheritPermissionInterface, C
*
* @var array
*/
protected $guarded = ['left_id', 'right_id', 'parent_id'];
protected $guarded = [];

/**
* @var array
Expand Down
59 changes: 59 additions & 0 deletions app/Database/Repositories/Decorators/Forum/CachingDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,63 @@ public function moveTopicToForum(Topic $topic, Forum $forum)

return $this->decoratedRepository->moveTopicToForum($topic, $forum);
}

/**
* {@inheritdoc}
*/
public function create(array $details = [])
{
$this->cache->forget('forums.index_tree');
$this->cache->forget('forums.all');

return $this->decoratedRepository->create($details);
}

/**
* {@inheritdoc}
*/
public function getForum(int $id)
{
return $this->decoratedRepository->getForum($id);
}

/**
* {@inheritdoc}
*/
public function isEmpty()
{
return $this->decoratedRepository->isEmpty();
}

/**
* {@inheritdoc}
*/
public function delete(Forum $forum)
{
$this->cache->forget('forums.index_tree');
$this->cache->forget('forums.all');

return $this->decoratedRepository->delete($forum);
}

/**
* {@inheritdoc}
*/
public function update(Forum $forum, $details)
{
$this->cache->forget('forums.index_tree');
$this->cache->forget('forums.all');

return $this->decoratedRepository->update($forum, $details);
}

/**
* {@inheritdoc}
*/
public function changeParent(Forum $forum, int $newParent)
{
$this->decoratedRepository->changeParent($forum, $newParent);
$this->cache->forget('forums.index_tree');
return $this->cache->forget('forums.all');
}
}
72 changes: 67 additions & 5 deletions app/Database/Repositories/Eloquent/ForumRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace MyBB\Core\Database\Repositories\Eloquent;

use Illuminate\Database\DatabaseManager;
use MyBB\Core\Database\Models\Forum;
use MyBB\Core\Database\Models\Post;
use MyBB\Core\Database\Models\Topic;
Expand All @@ -28,16 +29,24 @@ class ForumRepository implements ForumRepositoryInterface
*/
private $permissionChecker;

/**
* @var DatabaseManager
*/
protected $dbManager;

/**
* @param Forum $forumModel The model to use for forums.
* @param PermissionChecker $permissionChecker The permission class
* @param DatabaseManager $dbManager
*/
public function __construct(
Forum $forumModel,
PermissionChecker $permissionChecker
PermissionChecker $permissionChecker,
DatabaseManager $dbManager
) {
$this->forumModel = $forumModel;
$this->permissionChecker = $permissionChecker;
$this->dbManager = $dbManager;
}

/**
Expand Down Expand Up @@ -77,7 +86,7 @@ public function find($id = 0)
->whereBetween('left_id', [$parent->left_id, $parent->right_id])
->get();

return $forums->toTree();
return $forums->toTree()->first();
}

/**
Expand Down Expand Up @@ -107,7 +116,7 @@ public function findBySlug($slug = '')
->whereBetween('left_id', [$parent->left_id, $parent->right_id])
->get();

return $forums->toTree();
return $forums->toTree()->first();
}

/**
Expand Down Expand Up @@ -135,6 +144,14 @@ public function getIndexTree($checkPermissions = true)
return $res->toTree();
}

/**
* {@inheritdoc}
*/
public function getForum(int $id)
{
return $this->forumModel->find($id);
}

/**
* Increment the number of posts in the forum by one.
*
Expand All @@ -144,7 +161,7 @@ public function getIndexTree($checkPermissions = true)
*/
public function incrementPostCount($id = 0)
{
$forum = $this->find($id);
$forum = $this->forumModel->find($id);

if ($forum) {
$forum->increment('num_posts');
Expand All @@ -162,7 +179,7 @@ public function incrementPostCount($id = 0)
*/
public function incrementTopicCount($id = 0)
{
$forum = $this->find($id);
$forum = $this->forumModel->find($id);

if ($forum) {
$forum->increment('num_topics');
Expand Down Expand Up @@ -220,4 +237,49 @@ public function moveTopicToForum(Topic $topic, Forum $forum)

$this->updateLastPost($forum);
}

/**
* {@inheritdoc}
*/
public function create(array $details = [])
{
return $this->dbManager->transaction(function () use ($details) {
$this->forumModel->where('left_id', '>=', $details['left_id'])->increment('left_id', 2);
$this->forumModel->where('right_id', '>=', $details['left_id'])->increment('right_id', 2);
$this->forumModel->create($details);
}, 2);
}

/**
* {@inheritdoc}
*/
public function isEmpty()
{
$result = $this->forumModel->select('id')->first();
return !(bool)$result;
}

/**
* {@inheritdoc}
*/
public function update(Forum $forum, $details)
{
return $forum->update($details);
}

/**
* {@inheritdoc}
*/
public function delete(Forum $forum)
{
//TODO implement function
}

/**
* {@inheritdoc}
*/
public function changeParent(Forum $forum, int $newParent)
{
//TODO implement function
}
}
8 changes: 8 additions & 0 deletions app/Database/Repositories/Eloquent/TopicRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,12 @@ public function movePostToTopic(Post $post, Topic $topic)
'last_post_user_id' => $post->author->id,
]);
}

/**
* {@inheritdoc}
*/
public function countTopicsForForum(int $forumId)
{
return $this->topicModel->where('forum_id', $forumId)->count();
}
}
48 changes: 48 additions & 0 deletions app/Database/Repositories/ForumRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace MyBB\Core\Database\Repositories;

use Doctrine\Common\Collections\Collection;
use MyBB\Core\Database\Models\Forum;
use MyBB\Core\Database\Models\Post;
use MyBB\Core\Database\Models\Topic;
Expand Down Expand Up @@ -83,4 +84,51 @@ public function updateLastPost(Forum $forum, Post $post = null);
* @param Forum $forum
*/
public function moveTopicToForum(Topic $topic, Forum $forum);

/**
* Create new forum
*
* @param array $details
* @return mixed
*/
public function create(array $details = []);

/**
* Return single forum by id (without any relations, just forum)
*
* @param int $id Forum id
* @return Collection
*/
public function getForum(int $id);

/**
* Check if there are already created any forums
*
* @return bool
*/
public function isEmpty();

/**
* Delete forum by id. Removes all related forums/subforums/topics/posts
*
* @param Forum $forum
* @return bool
*/
public function delete(Forum $forum);

/**
* Update forum
*
* @param Forum $forum
* @param $details
* @return mixed
*/
public function update(Forum $forum, $details);

/**
* @param Forum $forum
* @param int $newParent
* @return mixed
*/
public function changeParent(Forum $forum, int $newParent);
}
8 changes: 8 additions & 0 deletions app/Database/Repositories/TopicRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,12 @@ public function updateLastPost(Topic $topic, Post $post = null);
* @param Topic $topic
*/
public function movePostToTopic(Post $post, Topic $topic);

/**
* Count topics for forum
*
* @param int $forumId Forum id
* @return int
*/
public function countTopicsForForum(int $forumId);
}
Loading