Completed
Push — master ( 876046...6e04bc )
by Joao
02:55
created

BlogController::search()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
rs 8.8571
cc 1
eloc 10
nc 1
nop 1
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
11
class BlogController extends Controller
12
{
13
14
    /**
15
     * Show the posts.
16
     *
17
     * @return \Illuminate\Http\Response
18
     */
19
    public function index()
20
    {
21
        $posts = Blog::getPostsRepository()->all();
22
        $categories = Blog::getCategoriesRepository()->all();
23
24
        return view('public.blog.list', compact('posts', 'categories'));
25
    }
26
27
    public function edit($id)
28
    {
29
        $post = Blog::getPostsRepository()->findOrFail($id);
30
31
        return view('public.blog.edit', compact('post'));
32
    }
33
34
    public function update($id, Request $request)
35
    {
36
        $this->validate($request, [ 'value' => 'required' ]);
37
38
        $move = Blog::getPostsRepository()->findOrFail($id);
39
40
        $move->update($request->all());
41
42
        return redirect('blog');
43
    }
44
45
    public function show($id)
46
    {
47
        $post = Blog::getPostsRepository()->findOrFail($id);
48
        $categories = Blog::getCategoriesRepository()->all();
49
50
        // Show the page
51
        return View('public.blog.post', compact('post', 'categories'));
52
    }
53
54
    public function showByCategory($id)
55
    {
56
        $category = Blog::getCategoriesRepository()->findOrFail($id);
57
        $posts = $category->posts;
58
        $categories = Blog::getCategoriesRepository()->all();
59
60
        // Show the page
61
        return view('public.blog.list', compact('posts', 'categories'));
62
    }
63
64
    public function search($in)
65
    {
66
        // Setting up vars
67
        $search = $in;
68
        $terms = explode(' ', $search);
69
        array_push($terms, $search);
70
71
        // Checking the users
72
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
        $users = $this->applySearch(Sentinel::createModel(), $terms, ['first_name', 'last_name'])->join('BlogPost', 'User.id', '=', 'BlogPost.author')
74
            ->select(DB::raw('CONCAT(User.first_name, " ", User.last_name) AS a'), DB::raw('\'User\' AS type'))->distinct()->get()->toArray();
75
76
        $users = $this->evaluate($users, $terms, ['first_name', 'last_name']);*/
77
78
        $users = Sentinel::createModel()->search($in)
79
            /*->distinct()*/->select(DB::raw('CONCAT(User.first_name, " ", User.last_name) AS a'), DB::raw('\'User\' AS type'))->get()->toArray();
80
81
        dd($users);
82
83
        // Checking the posts
84
        $posts = $this->applySearch(Blog::getPostsRepository(), $terms, ['title', 'contents', 'keywords'])->select('title as a', DB::raw('\'Post\' AS type'))->distinct()->get()->toArray();
85
86
        // Checking the Categories
87
        $categories = $this->applySearch(Blog::getCategoriesRepository(), $terms, ['name', 'description'])->select('name as a', DB::raw('\'Category\' AS type'))->distinct()->get()->toArray();
88
89
        // Returning the results
90
        return response()->json( [ 'data' => array_merge($users, $categories, $posts) ] );
91
    }
92
93
    public function applySearch($repo, $terms, $fields)
94
    {
95
        foreach($terms as $term)
96
            foreach($fields as $field)
97
                $repo = $repo->orWhere($field, 'LIKE', '%' . $term . '%');
98
99
        foreach($repo as $data)
100
        {
101
            dd($repo);
102
        }
103
        dd($repo);
104
105
        return $repo;
106
    }
107
108
    public function evaluate($repo, $terms, $fields)
109
    {
110
111
        return $repo;
112
    }
113
114
    /*
115
     * Admin section
116
     */
117
    public function getAdminIndex()
118
    {
119
        // Grab all the users
120
        $posts = Blog::getPostsRepository()->all();
121
122
        // Show the page
123
        return View('admin.blog.list', compact('posts'));
124
    }
125
126
}
127