Skip to content

Commit b49255b

Browse files
author
Shawn McCool
committed
add ability to edit your own posts
1 parent b7bc123 commit b49255b

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

app/controllers/ForumController.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,16 @@ public function postCreateThread()
9999
public function getEditThread($threadId)
100100
{
101101
$thread = $this->comments->requireForumThreadById($threadId);
102-
$tags = $this->tags->getAllForForum();
102+
if (Auth::user()->id != $thread->author_id) return Redirect::to('/');
103103

104+
$tags = $this->tags->getAllForForum();
104105
$this->view('forum.editthread', compact('thread', 'tags'));
105106
}
106107

107108
public function postEditThread($threadId)
108109
{
109-
$comment = $this->comments->requireForumThreadById($threadId);
110+
$thread = $this->comments->requireForumThreadById($threadId);
111+
if (Auth::user()->id != $thread->author_id) return Redirect::to('/');
110112

111113
// i hate everything about these controllers, it's awful
112114
$form = $this->comments->getForumCreateForm();
@@ -115,24 +117,54 @@ public function postEditThread($threadId)
115117
return $this->redirectBack(['errors' => $form->getErrors()]);
116118
}
117119

118-
$comment->fill([
120+
$thread->fill([
119121
'title' => Input::get('title'),
120122
'body' => Input::get('body'),
121123
]);
122124

123-
if ( ! $comment->isValid()) {
124-
return $this->redirectBack(['errors' => $comment->getErrors()]);
125+
if ( ! $thread->isValid()) {
126+
return $this->redirectBack(['errors' => $thread->getErrors()]);
125127
}
126128

127-
$this->comments->save($comment);
129+
$this->comments->save($thread);
128130

129131
// store tags
130132
$tags = $this->tags->getTagsByIds(Input::get('tags'));
131-
$comment->tags()->sync($tags->lists('id'));
133+
$thread->tags()->sync($tags->lists('id'));
132134

133135
// load new slug
134-
$commentSlug = $comment->slug()->first()->slug;
136+
$threadSlug = $thread->slug()->first()->slug;
135137

136-
return $this->redirectAction('ForumController@getThread', [$commentSlug]);
138+
return $this->redirectAction('ForumController@getThread', [$threadSlug]);
139+
}
140+
141+
// oh god it's so bad
142+
public function getEditComment($commentId)
143+
{
144+
$comment = $this->comments->requireForumThreadById($commentId);
145+
if (Auth::user()->id != $comment->author_id) return Redirect::to('/');
146+
$this->view('forum.editcomment', compact('comment'));
147+
}
148+
149+
public function postEditComment($commentId)
150+
{
151+
$comment = $this->comments->requireForumThreadById($commentId);
152+
if (Auth::user()->id != $comment->author_id) return Redirect::to('/');
153+
154+
// i hate everything about these controllers, it's awful
155+
$form = $this->comments->getForumReplyForm();
156+
157+
if ( ! $form->isValid()) return $this->redirectBack(['errors' => $form->getErrors()]);
158+
159+
$comment->fill([
160+
'title' => Input::get('title'),
161+
'body' => Input::get('body'),
162+
]);
163+
164+
if ( ! $comment->isValid()) return $this->redirectBack(['errors' => $comment->getErrors()]);
165+
166+
$this->comments->save($comment);
167+
168+
return $this->redirectAction('ForumController@getThread', [$comment->parent->slug->slug]);
137169
}
138170
}

app/routes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
Route::post('forum/create-thread', ['before' => 'auth', 'uses' => 'ForumController@postCreateThread']);
4141
Route::get('forum/edit-thread/{threadId}', ['before' => 'auth', 'uses' => 'ForumController@getEditThread']);
4242
Route::post('forum/edit-thread/{threadId}', ['before' => 'auth', 'uses' => 'ForumController@postEditThread']);
43+
Route::get('forum/edit-comment/{commentId}', ['before' => 'auth', 'uses' => 'ForumController@getEditComment']);
44+
Route::post('forum/edit-comment/{commentId}', ['before' => 'auth', 'uses' => 'ForumController@postEditComment']);
4345
Route::get('forum/{slug}', ['before' => 'handle_slug', 'uses' => 'ForumController@getThread']);
4446
Route::post('forum/{slug}', ['before' => 'auth|handle_slug', 'uses' => 'ForumController@postThread']);
4547

app/views/forum/_comment.blade.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@endif
1212

1313
{{ $comment->body }}
14-
14+
1515
<ul class="meta">
1616
<li><i class="icon-time"></i>&nbsp;{{ $comment->created_ago }}</li>
1717
<li><i class="icon-user"></i>&nbsp;<a href="{{ $comment->author->profileUrl }}">{{ $comment->author->name }}</a></li>
@@ -23,6 +23,11 @@
2323

2424
@if(Auth::user() && $comment->id == $thread->id && $comment->author_id == Auth::user()->id)
2525
<li><i class="icon-link"></i>&nbsp;<a href="{{ action('ForumController@getEditThread', [$comment->id]) }}">Edit</a></li>
26+
{{-- this code is so awful... --}}
27+
@elseif(Auth::user() && $comment->author_id == Auth::user()->id)
28+
<li><i class="icon-link"></i>&nbsp;<a href="{{ action('ForumController@getEditComment', [$comment->id]) }}">Edit</a></li>
29+
@else
30+
{{-- “That ain't me, that ain't my face. It wasn't even me when I was trying to be that face. I wasn't even really me them; I was just being the way I looked, the way people wanted.” --}}
2631
@endif
2732
</ul>
2833
</div>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@extends('layouts._two_columns_left_sidebar')
2+
3+
@section('sidebar')
4+
@include('forum._sidebar')
5+
@stop
6+
7+
@section('content')
8+
<div class="row forum">
9+
<div class="small-12 columns form">
10+
{{ Form::open() }}
11+
<fieldset>
12+
<legend>Edit Comment</legend>
13+
14+
<div class="row">
15+
<div class="">
16+
{{ Form::label('body', 'Thread') }}
17+
<div id="markdown_editor"></div>
18+
{{ Form::textarea('body', null, ['id' => '_markdown_textarea', 'style' => 'display: none;']) }}
19+
{{ $errors->first('body', '<small class="error">:message</small>') }}
20+
</div>
21+
</div>
22+
23+
<div class="row">
24+
{{ Form::button('Save', ['type' => 'submit', 'class' => 'button']) }}
25+
</div>
26+
27+
</fieldset>
28+
29+
{{ Form::close() }}
30+
31+
</div>
32+
</div>
33+
@stop
34+
35+
{{-- “What do you think you are, for Chrissake, crazy or somethin'? Well you're not! You're not! You're no crazier than the average asshole out walkin' around on the streets and that's it. ” --}}
36+
37+
@include('layouts._markdown_editor')

0 commit comments

Comments
 (0)