Skip to content

Commit

Permalink
Added post comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ConsoleTVs committed Jun 25, 2016
1 parent e7614ec commit f4bbf1e
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 17 deletions.
36 changes: 36 additions & 0 deletions app/Http/Controllers/Admin/Data/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,40 @@
],
],
],

'comments' => [

'table' => 'post_comments',
'create' => [
'hidden' => ['id', 'post_id', 'user_id', 'created_at', 'updated_at'],
'default_random' => [],
'confirmed' => [],
'encrypted' => [],
'hashed' => [],
'masked' => [],
'code' => [],
'wysiwyg' => [],
'validator' => [
'name' => 'sometimes|required',
'email' => 'sometimes|required',
'content' => 'required',
],
],
'edit' => [
'hidden' => ['id', 'post_id', 'user_id', 'created_at', 'updated_at'],
'empty' => [],
'default_random' => [],
'confirmed' => [],
'encrypted' => [],
'hashed' => [],
'masked' => [],
'code' => [],
'wysiwyg' => [],
'validator' => [
'name' => 'sometimes|required',
'email' => 'sometimes|required',
'content' => 'required',
],
],
],
];
80 changes: 79 additions & 1 deletion app/Http/Controllers/Admin/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Http\Controllers\Controller;
use App\Post;
use Auth;
use App\Post_Comment;

class PostsController extends Controller
{
Expand Down Expand Up @@ -36,7 +37,27 @@ public function index($id)

$post->addView();

return view('admin/blogs/posts/index', ['post' => $post]);
$data_index = 'comments';
require('Data/Create/Get.php');

if($post->logged_in_comments){
$fields = array_diff($fields, array("name", "email"));
}

$comments = $post->comments()->orderBy('created_at', 'desc')->get();

return view('admin/blogs/posts/index', [
'comments' => $comments,
'post' => $post,
'fields' => $fields,
'confirmed' => $confirmed,
'encrypted' => $encrypted,
'hashed' => $hashed,
'masked' => $masked,
'table' => $table,
'code' => $code,
'wysiwyg' => $wysiwyg,
]);
}

public function graphics($id){
Expand Down Expand Up @@ -164,6 +185,63 @@ public function update($id, Request $request)
return redirect(url('/admin/blogs', [$row->blog->id]))->with('success', "The post has been updated");
}

public function comments($id){
# Check permissions
if(!Auth::user()->has('admin.posts.comments')) {
return redirect('/admin')->with('warning', "You are not allowed to perform this action")->send();
}

# Check blog permissions
if(!Auth::user()->has_blog(Post::findOrFail($id)->blog->id) and !Auth::user()->owns_blog($id)){
return redirect('/admin')->with('warning', "You are not allowed to perform this action")->send();
}

$post = Post::findOrFail($id);
$comments = $post->comments()->orderBy('created_at', 'desc')->get();

return view('admin/blogs/posts/comments', [
'comments' => $comments,
'post' => $post,
]);
}

public function createComment($id, Request $request)
{
# Check permissions
if(!Auth::user()->has('admin.posts.comments')) {
return redirect('/admin')->with('warning', "You are not allowed to perform this action")->send();
}

# Check blog permissions
if(!Auth::user()->has_blog(Post::findOrFail($id)->blog->id) and !Auth::user()->owns_blog($id)){
return redirect('/admin')->with('warning', "You are not allowed to perform this action")->send();
}

$post = Post::findOrFail($id);

# Check if comments are enabled
if($post->logged_in_comments or $post->anonymous_comments) {

# create the user
$row = new Post_Comment;

# Save the data
$data_index = 'comments';
require('Data/Create/Save.php');

$row->post_id = $post->id;

if($post->logged_in_comments) {
$row->user_id = Auth::user()->id;
}
$row->save();

return redirect(url('admin/posts', [$post->id]));
} else {
return redirect('/admin')->with('warning', "Comments are not enabled on this post")->send();
}
}

public function destroy($id)
{
# Check permissions
Expand Down
3 changes: 3 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@

Route::get('/posts/{id}/graphics', 'PostsController@graphics');

Route::get('/posts/{id}/comments', 'PostsController@comments');
Route::post('/posts/{id}/comments/create', 'PostsController@createComment');

Route::get('/posts/{id}/delete', 'SecurityController@confirm');
Route::post('/posts/{id}/delete', 'PostsController@destroy');

Expand Down
5 changes: 5 additions & 0 deletions app/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public function addView()

return True;
}

public function comments()
{
return $this->hasmany('App\Post_Comment');
}
}
20 changes: 20 additions & 0 deletions app/Post_Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post_Comment extends Model
{
protected $table = 'post_comments';

public function post()
{
return $this->belongsTo('App\Post', 'post_id');
}

public function author()
{
return $this->belongsTo('App\User', 'user_id');
}
}
8 changes: 8 additions & 0 deletions database/migrations/2016_02_10_152932_Create_Permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ public function up()
$perm->su = true;
$perm->save();

$perm = new Permission;
$perm->slug = 'admin.posts.comments';
$perm->name = 'Posts Comments';
$perm->info = 'Grants access to posts comments';
$perm->type_id = Permission_Types::where('type', 'Post Administration')->first()->id;
$perm->su = true;
$perm->save();

$perm = new Permission;
$perm->slug = 'admin.developer.access';
$perm->name = 'Developer Access';
Expand Down
2 changes: 2 additions & 0 deletions database/migrations/2016_05_15_115828_Create_Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function up()
$table->string('image');
$table->string('title');
$table->text('description');
$table->boolean('logged_in_comments');
$table->boolean('anonymous_comments');
$table->text('body');
$table->integer('user_id');
$table->integer('blog_id');
Expand Down
35 changes: 35 additions & 0 deletions database/migrations/2016_06_24_162123_create_post_comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostComments extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('post_comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->nullable();
$table->integer('post_id');
$table->string('name')->nullable();
$table->string('email')->nullable();
$table->text('content');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('post_comments');
}
}
26 changes: 21 additions & 5 deletions resources/views/admin/blogs/posts.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,26 @@
<div class="thumbnail panel panel-default">
<div class="blog-img" style="background-image:url('{{ $post->image }}');"></div>
<div class="caption">
<h3>{{ $post->title }}</h3>
<div class="row">
<div class="col-sm-12 col-md-10">
<h3>{{ $post->title }}</h3>
</div>
<div class="col-sm-12 col-md-2">
<center>
<div class="btn-group more-small-spacer">
<a href="#" class="btn btn-primary dropdown-toggle" data-toggle="dropdown"><i class="mdi mdi-arrow-down"></i></a>
<ul class="dropdown-menu">
<li><a href="{{ url('admin/posts', $post->id) }}">View Post</a></li>
<li><a href="{{ url('admin/posts', [$post->id, 'edit']) }}">Edit Post</a></li>
<li><a href="{{ url('admin/posts', [$post->id, 'graphics']) }}">Post Graphics</a></li>
<li><a href="{{ url('admin/posts', [$post->id, 'comments']) }}">Post Comments</a></li>
<li class="divider"></li>
<li><a href="{{ url('admin/posts', [$post->id, 'delete']) }}">Delete Post</a></li>
</ul>
</div>
</center>
</div>
</div>
<p>
<b>Posted by:</b> {{ $post->author->name }}
- <b>Posted on:</b> {{ substr($post->created_at, 0, 10) }}
Expand All @@ -37,10 +56,7 @@
<div class="post-text">{{ $post->description }}</div>
<br>
<p>
<a href="{{ url('admin/posts', [$post->id, 'edit']) }}" class="btn btn-primary" role="button">Edit Post</a>
<a href="{{ url('admin/posts', [$post->id, 'graphics']) }}" class="btn btn-primary" role="button">Post Graphics</a>
<a href="{{ url('admin/posts', $post->id) }}" class="btn btn-primary" role="button">View Post</a>
<a href="{{ url('admin/posts', [$post->id, 'delete']) }}" class="btn btn-danger pull-right" role="button">Delete Post</a>

</p>
</div>
</div>
Expand Down
60 changes: 60 additions & 0 deletions resources/views/admin/blogs/posts/comments.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
@extends('layouts.admin.index')
@section('title', $post->title . " Comments")
@section('content')
<a href="{{ url('admin/blogs', [$post->blog->id]) }}" class="btn btn-primary" role="button">Back</a><br><br>
<div class="row">
<div class="col-sm-12 col-md-6 col-md-offset-3">
<center>
<h3>
{{ $post->title }} Comments
</h3>
</center>
<br>
@if(count($comments) == 0)
<center>
This post have no comments yet.
</center>
@endif
@foreach($comments as $comment)
<div class="panel panel-default">
<div class="panel-body">
@if($comment->user_id)
<div class="row">
<div class="col-sm-6 col-md-2">
<?php $grav_url = "https://www.gravatar.com/avatar/".md5(strtolower(trim($comment->author->email)))."?s=75";?>
<center>
<img height="75" width="75" class="img-responsive img-circle" src="{!! $grav_url !!}">
</center>
</div>
<div class="col-sm-6 col-md-10">
<span class="comment-author">{{ $comment->author->name }}</span>
<span class="pull-right">{{ date("F jS, Y",strtotime($comment->created_at)) }}</span>
<br>
<i>{{ $comment->author->email }}</i>
</div>
</div>
@else
<div class="row">
<div class="col-sm-6 col-md-2">
<center>
<img height="75" width="75" class="img-responsive img-circle" src="{{ url('admin_panel/img/avatar.jpg') }}">
</center>
</div>
<div class="col-sm-6 col-md-10">
<span class="comment-author">{{ $comment->name }}</span>
<span class="pull-right">{{ date("F jS, Y",strtotime($comment->created_at)) }}</span>
<br>
<i>{{ $comment->email }}</i>
</div>
</div>
@endif
<br>
<span class="comment-content">
{{ $comment->content }}
</span>
</div>
</div>
@endforeach
</div>
</div>
@endsection
12 changes: 6 additions & 6 deletions resources/views/admin/blogs/posts/graphics.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@extends('layouts.admin.index')
@section('title', $post->name . ' Graphics')
@section('title', $post->title . ' Graphics')
@section('content')
<a href="{{ url('admin/blogs', [$post->blog->id]) }}" class="btn btn-primary" role="button">Back</a><br><br>
<div class="row">
Expand Down Expand Up @@ -66,11 +66,11 @@
</script>
<?php
$dates = array_reverse([
date("Y-m-d"),
date("Y-m-d", strtotime("-1 Day")),
date("Y-m-d", strtotime("-2 Day")),
date("Y-m-d", strtotime("-3 Day")),
date("Y-m-d", strtotime("-4 Day")),
date("F j, Y"),
date("F j, Y", strtotime("-1 Day")),
date("F j, Y", strtotime("-2 Day")),
date("F j, Y", strtotime("-3 Day")),
date("F j, Y", strtotime("-4 Day")),
]);
$values = array_reverse([
count($post->views()->whereDate('created_at', '=', date("Y-m-d"))->get()),
Expand Down
Loading

0 comments on commit f4bbf1e

Please sign in to comment.