1
|
|
|
<?php namespace jlourenco\blog\Controllers;
|
2
|
|
|
|
3
|
|
|
use Illuminate\Http\Request;
|
4
|
|
|
use App\Http\Requests;
|
5
|
|
|
use App\Http\Controllers\Controller;
|
6
|
|
|
use Blog;
|
7
|
|
|
use Sentinel;
|
8
|
|
|
use DB;
|
9
|
|
|
use Searchy;
|
10
|
|
|
use Validator;
|
11
|
|
|
use Input;
|
12
|
|
|
use Comments;
|
13
|
|
|
use Base;
|
14
|
|
|
use Lang;
|
15
|
|
|
use Redirect;
|
16
|
|
|
|
17
|
|
|
class BlogController extends Controller
|
18
|
|
|
{
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* Declare the rules for the form validation
|
22
|
|
|
*
|
23
|
|
|
* @var array
|
24
|
|
|
*/
|
25
|
|
|
protected $validationRules = array(
|
26
|
|
|
'comment' => 'required|min:3',
|
27
|
|
|
);
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* Show the posts.
|
31
|
|
|
*
|
32
|
|
|
* @return \Illuminate\Http\Response
|
33
|
|
|
*/
|
34
|
|
View Code Duplication |
public function index()
|
|
|
|
|
35
|
|
|
{
|
36
|
|
|
$posts = Blog::getPostsRepository()->OrderBy('created_at')->get();
|
37
|
|
|
$categories = Blog::getCategoriesRepository()->OrderBy('created_at')->get();
|
38
|
|
|
$subTitle = null;
|
39
|
|
|
|
40
|
|
|
return view('public.blog.list', compact('posts', 'categories', 'subTitle'));
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
View Code Duplication |
public function show($id)
|
|
|
|
|
44
|
|
|
{
|
45
|
|
|
$post = Blog::getPostsRepository()->findOrFail($id);
|
46
|
|
|
$categories = Blog::getCategoriesRepository()->OrderBy('created_at')->get();
|
47
|
|
|
|
48
|
|
|
// Show the page
|
49
|
|
|
return View('public.blog.post', compact('post', 'categories'));
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
public function showByCategory($id)
|
53
|
|
|
{
|
54
|
|
|
$category = Blog::getCategoriesRepository()->findOrFail($id);
|
55
|
|
|
$posts = $category->posts;
|
56
|
|
|
$categories = Blog::getCategoriesRepository()->OrderBy('created_at')->get();
|
57
|
|
|
$subTitle = $category->name;
|
58
|
|
|
|
59
|
|
|
// Show the page
|
60
|
|
|
return view('public.blog.list', compact('posts', 'categories', 'subTitle'));
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
public function search($in)
|
64
|
|
|
{
|
65
|
|
|
// Setting up vars
|
66
|
|
|
$search = $in;
|
67
|
|
|
$terms = explode(' ', $search);
|
68
|
|
|
array_push($terms, $search);
|
69
|
|
|
|
70
|
|
|
// Checking the users
|
71
|
|
|
/*
|
|
|
|
|
72
|
|
|
$users = $this->applySearch(Sentinel::createModel(), $terms, ['first_name', 'last_name'])->join('BlogPost', 'User.id', '=', 'BlogPost.author')
|
73
|
|
|
->select(DB::raw('CONCAT(User.first_name, " ", User.last_name) AS a'), DB::raw('\'User\' AS type'))->distinct()->get()->toArray();
|
74
|
|
|
|
75
|
|
|
$users = $this->evaluate($users, $terms, ['first_name', 'last_name']);*/
|
76
|
|
|
|
77
|
|
|
$users = Sentinel::createModel()->search($in)
|
78
|
|
|
/*->distinct()*/->select(DB::raw('CONCAT(User.first_name, " ", User.last_name) AS a'), DB::raw('\'User\' AS type'))->get()->toArray();
|
79
|
|
|
|
80
|
|
|
dd($users);
|
81
|
|
|
|
82
|
|
|
// Checking the posts
|
83
|
|
|
$posts = $this->applySearch(Blog::getPostsRepository(), $terms, ['title', 'contents', 'keywords'])->select('title as a', DB::raw('\'Post\' AS type'))->distinct()->get()->toArray();
|
84
|
|
|
|
85
|
|
|
// Checking the Categories
|
86
|
|
|
$categories = $this->applySearch(Blog::getCategoriesRepository(), $terms, ['name', 'description'])->select('name as a', DB::raw('\'Category\' AS type'))->distinct()->get()->toArray();
|
87
|
|
|
|
88
|
|
|
// Returning the results
|
89
|
|
|
return response()->json( [ 'data' => array_merge($users, $categories, $posts) ] );
|
90
|
|
|
}
|
91
|
|
|
|
92
|
|
|
public function applySearch($repo, $terms, $fields)
|
93
|
|
|
{
|
94
|
|
|
foreach($terms as $term)
|
95
|
|
|
foreach($fields as $field)
|
96
|
|
|
$repo = $repo->orWhere($field, 'LIKE', '%' . $term . '%');
|
97
|
|
|
|
98
|
|
|
foreach($repo as $data)
|
99
|
|
|
{
|
100
|
|
|
dd($repo);
|
101
|
|
|
}
|
102
|
|
|
dd($repo);
|
103
|
|
|
|
104
|
|
|
return $repo;
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
public function evaluate($repo, $terms, $fields)
|
108
|
|
|
{
|
109
|
|
|
|
110
|
|
|
return $repo;
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
/**
|
114
|
|
|
* Post comment form processing.
|
115
|
|
|
*
|
116
|
|
|
* @return Redirect
|
117
|
|
|
*/
|
118
|
|
|
public function postComment($id)
|
119
|
|
|
{
|
120
|
|
|
// Get the post information
|
121
|
|
|
$post = Blog::getPostsRepository()->find($id);
|
122
|
|
|
|
123
|
|
|
if ($post == null)
|
124
|
|
|
{
|
125
|
|
|
// Prepare the error message
|
126
|
|
|
$error = Lang::get('blog.posts.not_found');
|
127
|
|
|
|
128
|
|
|
// Redirect to the post management page
|
129
|
|
|
return Redirect('blog')->with('error', $error);
|
130
|
|
|
}
|
131
|
|
|
|
132
|
|
|
// Create a new validator instance from our validation rules
|
133
|
|
|
$validator = Validator::make(Input::all(), $this->validationRules);
|
134
|
|
|
|
135
|
|
|
// If validation fails, we'll exit the operation now.
|
136
|
|
|
if ($validator->fails()) {
|
137
|
|
|
// Ooops.. something went wrong
|
138
|
|
|
return Redirect::back()->withInput()->withErrors($validator);
|
139
|
|
|
}
|
140
|
|
|
|
141
|
|
|
// Create the comment
|
142
|
|
|
$c = Comments::getCommentsRepository()->createModel();
|
143
|
|
|
|
144
|
|
|
$c->parent = null;
|
145
|
|
|
$c->comment = Input::get('comment');
|
146
|
|
|
|
147
|
|
|
// Was the post updated?
|
148
|
|
|
if ($post->comments()->save($c))
|
149
|
|
|
{
|
150
|
|
|
Base::Log('Comment added to (' . $post->id . ').');
|
151
|
|
|
|
152
|
|
|
// Prepare the success message
|
153
|
|
|
$success = Lang::get('blog.comment.created');
|
154
|
|
|
|
155
|
|
|
// Redirect to the user page
|
156
|
|
|
return Redirect('blog/' . $post->id)->with('success', $success);
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
$error = Lang::get('blog.comment.error');
|
160
|
|
|
|
161
|
|
|
// Redirect to the post page
|
162
|
|
|
return Redirect('blog/' . $post->id)->withInput()->with('error', $error);
|
163
|
|
|
}
|
164
|
|
|
|
165
|
|
|
}
|
166
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.