Completed
Push — master ( 299651...d7cfe4 )
by Davide
03:33
created

BlogController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DavideCasiraghi\LaravelSmartBlog\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Support\Str;
7
use Illuminate\Http\Request;
8
use DavideCasiraghi\LaravelSmartBlog\Models\Blog;
9
use DavideCasiraghi\LaravelSmartBlog\Models\Category;
10
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
11
12
class BlogController extends Controller
13
{
14
    /* Restrict the access to this resource just to logged in users */
15
    public function __construct()
16
    {
17
        $this->middleware('admin');
18
    }
19
    
20
    /**
21
     * Display a listing of the resource.
22
     *
23
     * @return \Illuminate\Http\Response
24
     */
25
    public function index()
26
    {
27
        $categories = Category::get();
28
        $blogs = Blog::latest()->paginate(10);
29
30
        // Countries available for translations
31
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
32
    
33
        return view('laravel-smart-blog::blogs.index', compact('blogs'))
34
            ->with('i', (request()->input('page', 1) - 1) * 10)
35
            ->with('categories', $categories)
36
            ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations);
37
    }
38
    
39
    /**
40
     * Show the form for creating a new resource.
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44
    public function create()
45
    {
46
        $categories = Category::get();
47
        
48
        return view('laravel-smart-blog::blogs.create')
49
            ->with('categories', $categories);
50
    }
51
    
52
    /**
53
     * Store a newly created resource in storage.
54
     *
55
     * @param  \Illuminate\Http\Request  $request
56
     * @return \Illuminate\Http\Response
57
     */
58
    public function store(Request $request)
59
    {
60
        // Validate form datas
61
        $validator = Validator::make($request->all(), [
62
                'category_id' => 'required',
63
                'layout' => 'required',
64
            ]);
65
        if ($validator->fails()) {
66
            return back()->withErrors($validator)->withInput();
67
        }
68
69
        $blog = new Blog();
70
71
        $this->saveOnDb($request, $blog);
72
73
        return redirect()->route('blogs.index')
74
                        ->with('success', __('messages.blog_added_successfully'));
75
    }
76
77
    /**
78
     * Display the specified resource.
79
     *
80
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\Blog  $blog
81
     * @return \Illuminate\Http\Response
82
     */
83
    public function show(Blog $blog)
84
    {
85
        return view('laravel-smart-blog::blogs.show', compact('blog'));
86
    }
87
88
    /**
89
     * Show the form for editing the specified resource.
90
     *
91
     * @param  \App\Blog  $blog
0 ignored issues
show
Bug introduced by
The type App\Blog was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
     * @return \Illuminate\Http\Response
93
     */
94
    public function edit(Blog $blog)
95
    {
96
        $categories = Category::get();
97
        
98
        return view('laravel-smart-blog::blogs.edit', compact('blog'))
99
                    ->with('categories', $categories);
100
    }
101
102
    /**
103
     * Update the specified resource in storage.
104
     *
105
     * @param  \Illuminate\Http\Request  $request
106
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\Blog  $blog
107
     * @return \Illuminate\Http\Response
108
     */
109
    public function update(Request $request, Blog $blog)
110
    {
111
        request()->validate([
112
            'category_id' => 'required',
113
            'layout' => 'required',
114
        ]);
115
116
        $this->saveOnDb($request, $blog);
117
118
        return redirect()->route('blogs.index')
119
                        ->with('success', __('messages.blog_updated_successfully'));
120
    }
121
122
    /**
123
     * Remove the specified resource from storage.
124
     *
125
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\Blog  $blog
126
     * @return \Illuminate\Http\Response
127
     */
128
    public function destroy(Blog $blog)
129
    {
130
        $blog->delete();
131
132
        return redirect()->route('blogs.index')
133
                        ->with('success', __('messages.blog_deleted_successfully'));
134
    }
135
136
    /***************************************************************************/
137
138
    /**
139
     * Save/Update the record on DB.
140
     *
141
     * @param  \Illuminate\Http\Request  $request
142
     * @return void
143
     */
144
    public function saveOnDb($request, $blog)
145
    {
146
        $blog->category_id = $request->get('category_id');
147
        $blog->layout = $request->get('layout');
148
        $blog->columns = $request->get('columns');
149
        $blog->article_order = $request->get('article_order');
150
        
151
        $blog->pagination = $request->get('pagination');
152
        $blog->featured_articles = $request->get('featured_articles');
153
        $blog->show_category_title = $request->get('show_category_title');
154
        $blog->show_category_subtitle = $request->get('show_category_subtitle');
155
        $blog->show_category_description = $request->get('show_category_description');
156
        $blog->show_category_image = $request->get('show_category_image');
157
        
158
        $blog->show_post_title = $request->get('show_post_title');
159
        $blog->post_linked_titles = $request->get('post_linked_titles');
160
        $blog->show_post_intro_text = $request->get('show_post_intro_text');
161
        $blog->show_post_author = $request->get('show_post_author');
162
        $blog->link_post_author = $request->get('link_post_author');
163
        
164
        $blog->show_create_date = $request->get('show_create_date');
165
        $blog->show_modify_date = $request->get('show_modify_date');
166
        $blog->show_publish_date = $request->get('show_publish_date');
167
        $blog->show_read_more = $request->get('show_read_more');
168
        
169
        $blog->created_by = \Auth::user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
170
171
        $blog->save();
172
    }
173
}
174