Skip to content

Commit f5a0bc1

Browse files
author
Shawn McCool
committed
refactor more forum controller stuff
1 parent d58dcdf commit f5a0bc1

12 files changed

+163
-93
lines changed

app/Lio/Comments/CommentPresenter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function forumThreadUrl()
99
{
1010
$slug = $this->resource->slug;
1111
if ( ! $slug) return '';
12-
return action('ForumController@getShowThread', [$slug->slug]);
12+
return action('ForumThreadController@getShowThread', [$slug->slug]);
1313
}
1414

1515
public function commentUrl()
@@ -18,7 +18,7 @@ public function commentUrl()
1818
$slug = $this->resource->parent->slug;
1919
if ( ! $slug) return '';
2020

21-
$url = action('ForumController@getCommentRedirect', [$slug->slug, $this->id]);
21+
$url = action('ForumReplyController@getCommentRedirect', [$slug->slug, $this->id]);
2222
return $url;
2323
}
2424

app/Lio/Tags/TagCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function getTagList()
99
$tagLinks = [];
1010

1111
foreach ($this->items as $item) {
12-
$tagLinks[] = HTML::link(action('ForumController@getIndex') . '?tags=' . $item->slug, $item->name);
12+
$tagLinks[] = HTML::link(action('ForumThreadController@getIndex') . '?tags=' . $item->slug, $item->name);
1313
}
1414

1515
return implode(', ', $tagLinks);
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
use Lio\Comments\CommentRepository;
4+
use Lio\Comments\Comment;
5+
use Lio\Tags\TagRepository;
6+
use Lio\Forum\ForumSectionCountManager;
7+
use Lio\Forum\ForumReplyForm;
8+
9+
class ForumReplyController extends BaseController implements
10+
\Lio\Forum\ForumReplyCreatorObserver,
11+
\Lio\Forum\ForumReplyUpdaterObserver,
12+
\Lio\Forum\ForumReplyDeleterObserver
13+
{
14+
protected $comments;
15+
protected $tags;
16+
protected $sections;
17+
18+
protected $threadsPerPage = 20;
19+
protected $commentsPerPage = 20;
20+
21+
public function __construct(CommentRepository $comments, TagRepository $tags, ForumSectionCountManager $sections)
22+
{
23+
$this->comments = $comments;
24+
$this->tags = $tags;
25+
$this->sections = $sections;
26+
27+
$this->prepareViewData();
28+
}
29+
30+
// bounces the user to the correct page of a thread for the indicated comment
31+
public function getCommentRedirect($thread, $commentId)
32+
{
33+
// refactor this
34+
$comment = Comment::findOrFail($commentId);
35+
$numberCommentsBefore = Comment::where('parent_id', '=', $comment->parent_id)->where('created_at', '<', $comment->created_at)->count();
36+
$page = round($numberCommentsBefore / $this->commentsPerPage, 0, PHP_ROUND_HALF_DOWN) + 1;
37+
38+
return Redirect::to(action('ForumThreadController@getShowThread', [$thread]) . "?page={$page}#comment-{$commentId}");
39+
}
40+
41+
// reply deletion
42+
public function getDelete($commentId)
43+
{
44+
// user owns the comment
45+
$comment = $this->comments->requireById($commentId);
46+
if (Auth::user()->id != $comment->author_id) return Redirect::to('/');
47+
48+
$this->view('forum.delete', compact('comment'));
49+
}
50+
51+
public function postDelete($commentId)
52+
{
53+
// user owns the comment
54+
$comment = $this->comments->requireById($commentId);
55+
if (Auth::user()->id != $comment->author_id) return Redirect::to('/');
56+
57+
if ($comment->parent) {
58+
return App::make('Lio\Forum\ForumReplyDeleter')->delete($this, $comment);
59+
}
60+
return App::make('Lio\Forum\ForumThreadDeleter')->delete($this, $comment);
61+
}
62+
63+
// observer methods
64+
public function forumReplyDeleted($thread)
65+
{
66+
return Redirect::action('ForumThreadController@getShowThread', [$thread->slug->slug]);
67+
}
68+
69+
// reply to a thread
70+
public function postCreateReply()
71+
{
72+
$thread = App::make('slugModel');
73+
74+
return App::make('Lio\Forum\ForumReplyCreator')->create($this, [
75+
'body' => Input::get('body'),
76+
'author_id' => Auth::user()->id,
77+
], $thread->id, new ForumReplyForm);
78+
}
79+
80+
// edit a reply
81+
public function getEditReply($replyId)
82+
{
83+
$reply = $this->comments->requireForumThreadById($replyId);
84+
if (Auth::user()->id != $reply->author_id) return Redirect::to('/');
85+
86+
$this->view('forum.editreply', compact('reply'));
87+
}
88+
89+
public function postEditReply($replyId)
90+
{
91+
$reply = $this->comments->requireForumThreadById($replyId);
92+
if (Auth::user()->id != $reply->author_id) return Redirect::to('/');
93+
94+
return App::make('Lio\Forum\ForumReplyUpdater')->update($reply, $this, [
95+
'body' => Input::get('body'),
96+
], new ForumReplyForm);
97+
}
98+
99+
// observer methods
100+
public function forumReplyValidationError($errors)
101+
{
102+
return $this->redirectBack(['errors' => $errors]);
103+
}
104+
105+
public function forumReplyCreated($reply)
106+
{
107+
// awful demeter chain - clean up
108+
return $this->redirectAction('ForumThreadController@getShowThread', [$reply->parent()->first()->slug->slug]);
109+
}
110+
111+
public function forumReplyUpdated($reply)
112+
{
113+
return $this->redirectAction('ForumThreadController@getShowThread', [$reply->parent->slug->slug]);
114+
}
115+
116+
// ------------------------- //
117+
private function prepareViewData()
118+
{
119+
$forumSections = Config::get('forum.sections');
120+
$sectionCounts = $this->sections->getCounts(Session::get('forum_last_visited'));
121+
View::share(compact('forumSections', 'sectionCounts'));
122+
}
123+
}

app/controllers/ForumController.php renamed to app/controllers/ForumThreadController.php

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
use Lio\Forum\ForumThreadForm;
1010
use Lio\Forum\ForumReplyForm;
1111

12-
class ForumController extends BaseController implements
12+
class ForumThreadController extends BaseController implements
1313
\Lio\Forum\ForumThreadCreatorObserver,
1414
\Lio\Forum\ForumThreadUpdaterObserver,
15-
\Lio\Forum\ForumThreadDeleterObserver,
16-
\Lio\Forum\ForumReplyCreatorObserver,
17-
\Lio\Forum\ForumReplyUpdaterObserver,
18-
\Lio\Forum\ForumReplyDeleterObserver
15+
\Lio\Forum\ForumThreadDeleterObserver
1916
{
2017
protected $comments;
2118
protected $tags;
@@ -116,12 +113,12 @@ public function forumThreadValidationError($errors)
116113

117114
public function forumThreadCreated($thread)
118115
{
119-
return $this->redirectAction('ForumController@getShowThread', [$thread->slug()->first()->slug]);
116+
return $this->redirectAction('ForumThreadController@getShowThread', [$thread->slug()->first()->slug]);
120117
}
121118

122119
public function forumThreadUpdated($thread)
123120
{
124-
return $this->redirectAction('ForumController@getShowThread', [$thread->slug->slug]);
121+
return $this->redirectAction('ForumThreadController@getShowThread', [$thread->slug->slug]);
125122
}
126123

127124
// bounces the user to the correct page of a thread for the indicated comment
@@ -132,7 +129,7 @@ public function getCommentRedirect($thread, $commentId)
132129
$numberCommentsBefore = Comment::where('parent_id', '=', $comment->parent_id)->where('created_at', '<', $comment->created_at)->count();
133130
$page = round($numberCommentsBefore / $this->commentsPerPage, 0, PHP_ROUND_HALF_DOWN) + 1;
134131

135-
return Redirect::to(action('ForumController@getShowThread', [$thread]) . "?page={$page}#comment-{$commentId}");
132+
return Redirect::to(action('ForumThreadController@getShowThread', [$thread]) . "?page={$page}#comment-{$commentId}");
136133
}
137134

138135
// thread deletion
@@ -160,12 +157,7 @@ public function postDelete($commentId)
160157
// observer methods
161158
public function forumThreadDeleted()
162159
{
163-
return Redirect::action('ForumController@getIndex');
164-
}
165-
166-
public function forumReplyDeleted($thread)
167-
{
168-
return Redirect::action('ForumController@getShowThread', [$thread->slug->slug]);
160+
return Redirect::action('ForumThreadController@getIndex');
169161
}
170162

171163
// forum search
@@ -175,57 +167,11 @@ public function getSearch()
175167

176168
$query = Input::get('query');
177169
$results = App::make('Lio\Comments\ForumSearch')->searchPaginated($query, $this->threadsPerPage);
170+
$results->appends(array('query' => $query));
178171

179172
$this->view('forum.search', compact('query', 'results'));
180173
}
181174

182-
// reply to a thread
183-
public function postCreateReply()
184-
{
185-
$thread = App::make('slugModel');
186-
187-
return App::make('Lio\Forum\ForumReplyCreator')->create($this, [
188-
'body' => Input::get('body'),
189-
'author_id' => Auth::user()->id,
190-
], $thread->id, new ForumReplyForm);
191-
}
192-
193-
// edit a reply
194-
public function getEditReply($replyId)
195-
{
196-
$reply = $this->comments->requireForumThreadById($replyId);
197-
if (Auth::user()->id != $reply->author_id) return Redirect::to('/');
198-
199-
$this->view('forum.editreply', compact('reply'));
200-
}
201-
202-
public function postEditReply($replyId)
203-
{
204-
$reply = $this->comments->requireForumThreadById($replyId);
205-
if (Auth::user()->id != $reply->author_id) return Redirect::to('/');
206-
207-
return App::make('Lio\Forum\ForumReplyUpdater')->update($reply, $this, [
208-
'body' => Input::get('body'),
209-
], new ForumReplyForm);
210-
}
211-
212-
// observer methods
213-
public function forumReplyValidationError($errors)
214-
{
215-
return $this->redirectBack(['errors' => $errors]);
216-
}
217-
218-
public function forumReplyCreated($reply)
219-
{
220-
// awful demeter chain - clean up
221-
return $this->redirectAction('ForumController@getShowThread', [$reply->parent()->first()->slug->slug]);
222-
}
223-
224-
public function forumReplyUpdated($reply)
225-
{
226-
return $this->redirectAction('ForumController@getShowThread', [$reply->parent->slug->slug]);
227-
}
228-
229175
// ------------------------- //
230176
private function prepareViewData()
231177
{

app/controllers/HomeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(ArticleRepository $articles, CommentRepository $comm
1515

1616
public function getIndex()
1717
{
18-
return Redirect::action('ForumController@getIndex');
18+
return Redirect::action('ForumThreadController@getIndex');
1919

2020
$articles = $this->articles->getFeaturedArticles(3);
2121
$threads = $this->comments->getFeaturedForumThreads(3);

app/routes.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,29 @@
4141
Route::get('articles/search', 'ArticlesController@getSearch');
4242

4343
// forum
44-
Route::get('forum', 'ForumController@getIndex');
45-
Route::get('forum/search', 'ForumController@getSearch');
44+
Route::get('forum', 'ForumThreadController@getIndex');
45+
Route::get('forum/search', 'ForumThreadController@getSearch');
4646

4747
// move to new controller
48-
Route::get('forum/{slug}/comment/{commentId}', 'ForumController@getCommentRedirect');
48+
Route::get('forum/{slug}/comment/{commentId}', 'ForumReplyController@getCommentRedirect');
4949

5050
Route::group(['before' => 'auth'], function() {
51-
Route::get('forum/create-thread', 'ForumController@getCreateThread');
52-
Route::post('forum/create-thread', 'ForumController@postCreateThread');
53-
Route::get('forum/edit-thread/{threadId}', 'ForumController@getEditThread');
54-
Route::post('forum/edit-thread/{threadId}', 'ForumController@postEditThread');
55-
Route::get('forum/edit-reply/{commentId}', 'ForumController@getEditReply');
56-
Route::post('forum/edit-reply/{commentId}', 'ForumController@postEditReply');
57-
58-
// move to new controller
59-
Route::get('forum/delete/{commentId}', 'ForumController@getDelete');
60-
Route::post('forum/delete/{commentId}', 'ForumController@postDelete');
61-
62-
Route::post('forum/{slug}', ['before' => 'handle_slug', 'uses' => 'ForumController@postCreateReply']);
51+
Route::get('forum/create-thread', 'ForumThreadController@getCreateThread');
52+
Route::post('forum/create-thread', 'ForumThreadController@postCreateThread');
53+
Route::get('forum/edit-thread/{threadId}', 'ForumThreadController@getEditThread');
54+
Route::post('forum/edit-thread/{threadId}', 'ForumThreadController@postEditThread');
55+
Route::get('forum/edit-reply/{replyId}', 'ForumReplyController@getEditReply');
56+
Route::post('forum/edit-reply/{replyId}', 'ForumReplyController@postEditReply');
57+
58+
Route::get('forum/delete/thread/{threadId}', 'ForumThreadController@getDelete');
59+
Route::post('forum/delete/thread/{threadId}', 'ForumThreadController@postDelete');
60+
Route::get('forum/delete/reply/{replyId}', 'ForumReplyController@getDelete');
61+
Route::post('forum/delete/reply/{replyId}', 'ForumReplyController@postDelete');
62+
63+
Route::post('forum/{slug}', ['before' => 'handle_slug', 'uses' => 'ForumReplyController@postCreateReply']);
6364
});
6465

65-
Route::get('forum/{slug}', ['before' => 'handle_slug', 'uses' => 'ForumController@getShowThread']);
66+
Route::get('forum/{slug}', ['before' => 'handle_slug', 'uses' => 'ForumThreadController@getShowThread']);
6667

6768
// admin
6869
Route::group(['before' => 'auth', 'prefix' => 'admin'], function() {

app/views/forum/_comment.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
</div>
1313
@if(Auth::user() && $comment->author_id == Auth::user()->id)
1414
<div class="admin-bar">
15-
<li><a class="button" href="{{ action('ForumController@getEditReply', [$comment->id]) }}">Edit</a></li>
16-
<li><a class="button" href="{{ action('ForumController@getDelete', [$comment->id]) }}">Delete</a></li>
15+
<li><a class="button" href="{{ action('ForumReplyController@getEditReply', [$comment->id]) }}">Edit</a></li>
16+
<li><a class="button" href="{{ action('ForumReplyController@getDelete', [$comment->id]) }}">Delete</a></li>
1717
</div>
1818
@endif
1919
</div>

app/views/forum/_sidebar.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="search">
2-
{{ Form::open(['action' => 'ForumController@getSearch', 'method' => 'GET']) }}
2+
{{ Form::open(['action' => 'ForumThreadController@getSearch', 'method' => 'GET']) }}
33
{{ Form::text('query', isset($query) ? $query : '', ['placeholder' => 'search the laravel.io forum'] )}}
44
{{ Form::close() }}
55
</div>
@@ -8,7 +8,7 @@
88
{{-- $forumSections is set in the constructor of the ForumController class --}}
99
@foreach($forumSections as $sectionTitle => $sectionTags)
1010
<li>
11-
<a href="{{ action('ForumController@getIndex') }}{{ $sectionTags ? '?tags=' . $sectionTags : '' }}">{{ $sectionTitle }}
11+
<a href="{{ action('ForumThreadController@getIndex') }}{{ $sectionTags ? '?tags=' . $sectionTags : '' }}">{{ $sectionTitle }}
1212
@if($sectionCounts[$sectionTags] > 0)
1313
<span class="new">{{ $sectionCounts[$sectionTags] < 10 ? $sectionCounts[$sectionTags] : '9+' }}</span>
1414
@endif

app/views/forum/index.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{{ Input::get('tags') }}
1515
</div>
1616
@endif
17-
<a class="button" href="{{ action('ForumController@getCreateThread') }}">Create Thread</a>
17+
<a class="button" href="{{ action('ForumThreadController@getCreateThread') }}">Create Thread</a>
1818
</div>
1919

2020
<div class="threads">
@@ -25,7 +25,7 @@
2525
@if(!$threads->count())
2626
<div class="empty-state">
2727
<h3>No threads found that are tagged with {{ Input::get('tags') }}</h3>
28-
<a class="button" href="{{ action('ForumController@getCreateThread') }}">Create a new thread</a>
28+
<a class="button" href="{{ action('ForumThreadController@getCreateThread') }}">Create a new thread</a>
2929
</div>
3030
@endif
3131
</div>

app/views/forum/search.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
</div>
3535

3636
<div class="pagination">
37-
{{ $results->appends(array('query' => $query))->links() }}
37+
{{ $results->links() }}
3838
</div>
3939
@else
4040
<div class="padding">
41-
{{ Form::open(['action' => 'ForumController@getSearch', 'method' => 'GET']) }}
41+
{{ Form::open(['action' => 'ForumThreadController@getSearch', 'method' => 'GET']) }}
4242
<div class="form-row">
4343
{{ Form::label('query', 'Search the laravel.io forum', ['class' => 'field-title']) }}
4444
{{ Form::text('query', null, ['placeholder' => 'search the laravel.io forum'] )}}

0 commit comments

Comments
 (0)