Skip to content

Commit

Permalink
Merge pull request #45 from XetaIO/issue-43
Browse files Browse the repository at this point in the history
Discussions
  • Loading branch information
Emeric authored Mar 13, 2018
2 parents fc310e1 + 4eb3853 commit ff9c1c7
Show file tree
Hide file tree
Showing 30 changed files with 1,002 additions and 27 deletions.
10 changes: 10 additions & 0 deletions app/Http/Controllers/Admin/Blog/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@

class ArticleController extends Controller
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();

$this->breadcrumbs->addCrumb('Blog', route('admin.blog.article.index'));
}

/**
* Show all articles.
*
Expand Down
10 changes: 10 additions & 0 deletions app/Http/Controllers/Admin/Blog/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

class CategoryController extends Controller
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();

$this->breadcrumbs->addCrumb('Blog', route('admin.blog.article.index'));
}

/**
* Show all categories.
*
Expand Down
138 changes: 138 additions & 0 deletions app/Http/Controllers/Admin/Discuss/CategoryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
namespace Xetaravel\Http\Controllers\Admin\Discuss;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Xetaravel\Http\Controllers\Admin\Controller;
use Xetaravel\Models\DiscussCategory;
use Xetaravel\Models\Repositories\DiscussCategoryRepository;
use Xetaravel\Models\Validators\DiscussCategoryValidator;

class CategoryController extends Controller
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();

$this->breadcrumbs->addCrumb('Discuss', route('admin.discuss.category.index'));
}

/**
* Show all categories.
*
* @return \Illuminate\View\View
*/
public function index(): View
{
$categories = DiscussCategory::paginate(config('xetaravel.pagination.discuss.conversation_per_page'));

$this->breadcrumbs->addCrumb('Manage Categories', route('admin.discuss.category.index'));

return view(
'Admin::Discuss.category.index',
['categories' => $categories, 'breadcrumbs' => $this->breadcrumbs]
);
}

/**
* Show the caterory create form.
*
* @return \Illuminate\View\View
*/
public function showCreateForm(): View
{
$breadcrumbs = $this->breadcrumbs
->addCrumb('Manage Categories', route('admin.discuss.category.index'))
->addCrumb("Create", route('admin.discuss.category.create'));

return view('Admin::Discuss.category.create', ['breadcrumbs' => $this->breadcrumbs]);
}

/**
* Handle a category create request for the application.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function create(Request $request): RedirectResponse
{
DiscussCategoryValidator::create($request->all())->validate();
DiscussCategoryRepository::create($request->all());

return redirect()
->route('admin.discuss.category.index')
->with('success', 'Your category has been created successfully !');
}

/**
* Show the category update form.
*
* @param string $slug The slug of the category.
* @param int $id The id of the category.
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
*/
public function showUpdateForm(string $slug, int $id)
{
$category = DiscussCategory::findOrFail($id);

$breadcrumbs = $this->breadcrumbs
->addCrumb('Manage Categories', route('admin.discuss.category.index'))
->addCrumb(
"Update : " . e(str_limit($category->title, 30)),
route(
'admin.discuss.category.index',
['slug' => $category->slug, 'id' => $category->id]
)
);

return view('Admin::Discuss.category.update', compact('category', 'breadcrumbs'));
}

/**
* Handle an category update request for the application.
*
* @param \Illuminate\Http\Request $request
* @param int $id The id of the category.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request, int $id): RedirectResponse
{
$category = DiscussCategory::findOrFail($id);

DiscussCategoryValidator::update($request->all(), $id)->validate();
DiscussCategoryRepository::update($request->all(), $category);

return redirect()
->route('admin.discuss.category.index')
->with('success', 'Your category has been updated successfully !');
}

/**
* Handle the delete request for the category.
*
* @param int $id The id of the category to delete.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function delete(int $id): RedirectResponse
{
$category = DiscussCategory::findOrFail($id);

if ($category->delete()) {
return redirect()
->route('admin.discuss.category.index')
->with('success', 'This category has been deleted successfully !');
}

return redirect()
->route('admin.discuss.category.index')
->with('danger', 'An error occurred while deleting this category !');
}
}
31 changes: 28 additions & 3 deletions app/Http/Controllers/Discuss/ConversationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Xetaravel\Http\Controllers\Discuss;

use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use Xetaio\Mentions\Parser\MentionParser;
use Xetaravel\Models\DiscussCategory;
Expand Down Expand Up @@ -89,8 +90,8 @@ public function create(Request $request)
* Handle a conversation update request for the application.
*
* @param \Illuminate\Http\Request $request
* @param string $slug
* @param int $id
* @param string $slug The slug of the conversation to update.
* @param int $id The id of the conversation to update.
*
* @return \Illuminate\Http\RedirectResponse
*/
Expand All @@ -105,7 +106,31 @@ public function update(Request $request, string $slug, int $id)

return redirect()
->route('discuss.conversation.show', ['slug' => $conversation->slug, 'id' => $conversation->getKey()])
->with('success', 'Your conversation has been updated successfully !');
->with('success', 'Your discussion has been updated successfully !');
}

/**
* Handle the delete request for a conversation.
*
* @param string $slug The slug of the conversation to delete.
* @param int $id The id of the conversation to delete.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function delete(string $slug, int $id) : RedirectResponse
{
$conversation = DiscussConversation::findOrFail($id);

$this->authorize('delete', $conversation);

if ($conversation->delete()) {
return redirect()
->route('discuss.index')
->with('success', 'This discussion has been deleted successfully !');
}

return back()
->with('danger', 'An error occurred while deleting this discussion !');
}

/**
Expand Down
10 changes: 7 additions & 3 deletions app/Http/Controllers/Discuss/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public function create(Request $request): RedirectResponse
{
$conversation = DiscussConversation::findOrFail($request->conversation_id);

if (DiscussPost::isFlooding('xetaravel.flood.discuss.post')) {
// Use that have the permission "manage.discuss" can bypass this rule. (Default to Administrator)
if (DiscussPost::isFlooding('xetaravel.flood.discuss.post') && !Auth::user()->hasPermission('manage.discuss')) {
return back()
->withInput()
->with('danger', 'Wow, keep calm bro, and try to not flood !');
Expand Down Expand Up @@ -94,18 +95,21 @@ public function show(Request $request, int $id): RedirectResponse
public function delete(int $id): RedirectResponse
{
$post = DiscussPost::findOrFail($id);

$this->authorize('delete', $post);

$conversation = $post->conversation;

if ($conversation->first_post_id == $post->getKey()) {
return redirect()
->route('discuss.post.show', ['id' => $post->getKey()])
->with('danger', 'You can not deete the first post of a conversation !');
->with('danger', 'You can not delete the first post of a discussion !');
}

if ($conversation->last_post_id == $post->getKey()) {
$previousPost = DiscussPostRepository::findPreviousPost($post);

$conversation->last_post_id = $previousPost->getKey();
$conversation->last_post_id = !is_null($previousPost) ? $previousPost->getKey() : null;
}

if ($conversation->solved_post_id == $post->getKey()) {
Expand Down
10 changes: 10 additions & 0 deletions app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ class Category extends Model
use Sluggable,
CategoryPresenter;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'description'
];

/**
* The accessors to append to the model's array form.
*
Expand Down
24 changes: 23 additions & 1 deletion app/Models/DiscussCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ class DiscussCategory extends Model
use Sluggable,
DiscussCategoryPresenter;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'color',
'is_locked',
'description'
];

/**
* The accessors to append to the model's array form.
*
Expand All @@ -33,6 +45,16 @@ protected static function boot()
static::updating(function ($model) {
$model->generateSlug();
});

// Reset the last_conversation_id field and handle the conversations deleting.
static::deleting(function ($model) {
$model->last_conversation_id = null;
$model->save();

foreach ($model->conversations as $conversation) {
$conversation->delete();
}
});
}

/**
Expand All @@ -52,7 +74,7 @@ public function slugStrategy(): string
*/
public function conversations()
{
return $this->hasMany(DiscussConversation::class);
return $this->hasMany(DiscussConversation::class, 'category_id', 'id');
}

/**
Expand Down
43 changes: 40 additions & 3 deletions app/Models/DiscussConversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Xetaio\Mentions\Models\Traits\HasMentionsTrait;
use Xetaravel\Models\Gates\FloodGate;
use Xetaravel\Models\Presenters\DiscussConversationPresenter;
use Xetaravel\Models\Repositories\DiscussConversationRepository;
use Xetaravel\Models\Repositories\DiscussPostRepository;
use Xetaravel\Models\Scopes\DisplayScope;
use Xetaravel\Models\User;
Expand Down Expand Up @@ -69,14 +70,50 @@ protected static function boot()
{
parent::boot();

// Set the user id to the new conversation before saving it.
static::creating(function ($model) {
$model->user_id = Auth::id();
});

// Generated the slug before updating.
static::updating(function ($model) {
$model->generateSlug();
});

// Set the user id to the new conversation before saving it.
static::creating(function ($model) {
$model->user_id = Auth::id();
// Handle the before deleting the conversation.
static::deleting(function ($model) {
$category = $model->category;

// If the conversation is the last_conversation of the category,
// find the new last_conversation and update the category.
if ($category->last_conversation_id == $model->getKey()) {
$previousConversation = DiscussConversationRepository::findPreviousConversation($model);

if (is_null($previousConversation)) {
$category->last_conversation_id = null;
} else {
$category->last_conversation_id = $previousConversation->getKey();
}

$category->save();
}

// Set the forgein keys to null, else it won't delete since it delete
// the posts before the conversation.
$model->first_post_id = null;
$model->last_post_id = null;
$model->solved_post_id = null;
$model->save();

// We need to do this to refresh the countable cache `discuss_post_count` of the user.
foreach ($model->posts as $post) {
$post->delete();
}

// It don't delete the logs, so we need to do it manually.
foreach ($model->discussLogs as $log) {
$log->delete();
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion app/Models/DiscussLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected static function boot()
{
parent::boot();

// Set the user id to the new post before saving it.
// Set the user id to the new log before saving it.
static::creating(function ($model) {
$model->user_id = Auth::id();
});
Expand Down
Loading

0 comments on commit ff9c1c7

Please sign in to comment.