1 | <?php |
||
2 | |||
3 | namespace DavideCasiraghi\LaravelSmartBlog\Http\Controllers; |
||
4 | |||
5 | use Validator; |
||
6 | use Illuminate\Http\Request; |
||
7 | use DavideCasiraghi\LaravelSmartBlog\Models\Blog; |
||
8 | use DavideCasiraghi\LaravelSmartBlog\Models\Post; |
||
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 | 11 | public function __construct() |
|
16 | { |
||
17 | 11 | $this->middleware('admin'); |
|
18 | 11 | } |
|
19 | |||
20 | /** |
||
21 | * Display a listing of the resource. |
||
22 | * |
||
23 | * @return \Illuminate\Http\Response |
||
24 | */ |
||
25 | 5 | public function index() |
|
26 | { |
||
27 | 5 | $categories = Category::get(); |
|
28 | 5 | $blogs = Blog::latest()->paginate(10); |
|
29 | |||
30 | // Countries available for translations |
||
31 | 5 | $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
|
32 | |||
33 | 5 | return view('laravel-smart-blog::blogs.index', compact('blogs')) |
|
34 | 5 | ->with('i', (request()->input('page', 1) - 1) * 10) |
|
35 | 5 | ->with('categories', $categories) |
|
36 | 5 | ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations); |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * Show the form for creating a new resource. |
||
41 | * |
||
42 | * @return \Illuminate\Http\Response |
||
43 | */ |
||
44 | 1 | public function create() |
|
45 | { |
||
46 | //$categories = Category::get(); |
||
47 | 1 | $categories = Category::listsTranslations('name')->pluck('name', 'id'); |
|
48 | |||
49 | 1 | return view('laravel-smart-blog::blogs.create') |
|
50 | 1 | ->with('categories', $categories); |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * Store a newly created resource in storage. |
||
55 | * |
||
56 | * @param \Illuminate\Http\Request $request |
||
57 | * @return \Illuminate\Http\Response |
||
58 | */ |
||
59 | 2 | public function store(Request $request) |
|
60 | { |
||
61 | // Validate form datas |
||
62 | 2 | $validator = Validator::make($request->all(), [ |
|
63 | 2 | 'category_id' => 'required', |
|
64 | 'layout' => 'required', |
||
65 | ]); |
||
66 | 2 | if ($validator->fails()) { |
|
67 | 1 | return back()->withErrors($validator)->withInput(); |
|
68 | } |
||
69 | |||
70 | 1 | $blog = new Blog(); |
|
71 | |||
72 | 1 | $this->saveOnDb($request, $blog); |
|
73 | |||
74 | 1 | return redirect()->route('blogs.index') |
|
75 | 1 | ->with('success', __('laravel-smart-blog::messages.blog_added_successfully')); |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Display the specified resource. |
||
80 | * |
||
81 | * @param \DavideCasiraghi\LaravelSmartBlog\Models\Blog $blog |
||
82 | * @return \Illuminate\Http\Response |
||
83 | */ |
||
84 | 1 | public function show(Blog $blog) |
|
85 | { |
||
86 | //$category = Category::where('id', $blog->category_id)->get(); |
||
87 | |||
88 | 1 | $category = Category::select('categories.id AS id', 'category_translations.name AS name', 'category_translations.description AS description', 'category_translations.slug AS slug', 'locale') |
|
89 | 1 | ->join('category_translations', 'categories.id', '=', 'category_translations.category_id')->first(); |
|
90 | |||
91 | 1 | $posts = Post::where('category_id', $blog->category_id)->get(); |
|
92 | |||
93 | /*$posts = Post::select('post.id AS id', 'post_translations.title AS title', 'post_translations.body AS body', 'post_translations.slug AS slug', 'category_id', 'locale') |
||
94 | ->join('post_translations', 'posts.id', '=', 'post_translations.post_id'); |
||
95 | */ |
||
96 | |||
97 | 1 | $columnsBootstrapClass = (! empty($blog->columns_number)) ? 'col-md-'.strval((12 / intval($blog->columns_number))) : ''; |
|
98 | |||
99 | //::where('code', 'gr') |
||
100 | |||
101 | 1 | return view('laravel-smart-blog::blogs.show', compact('blog')) |
|
102 | 1 | ->with('category', $category) |
|
103 | 1 | ->with('posts', $posts) |
|
104 | 1 | ->with('columnsBootstrapClass', $columnsBootstrapClass); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Show the form for editing the specified resource. |
||
109 | * |
||
110 | * @param \DavideCasiraghi\LaravelSmartBlog\Models\Blog $blog |
||
111 | * @return \Illuminate\Http\Response |
||
112 | */ |
||
113 | 1 | public function edit(Blog $blog) |
|
114 | { |
||
115 | 1 | $categories = Category::listsTranslations('name')->pluck('name', 'id'); |
|
116 | |||
117 | 1 | return view('laravel-smart-blog::blogs.edit', compact('blog')) |
|
118 | 1 | ->with('categories', $categories); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Update the specified resource in storage. |
||
123 | * |
||
124 | * @param \Illuminate\Http\Request $request |
||
125 | * @param \DavideCasiraghi\LaravelSmartBlog\Models\Blog $blog |
||
126 | * @return \Illuminate\Http\Response |
||
127 | */ |
||
128 | 2 | public function update(Request $request, Blog $blog) |
|
129 | { |
||
130 | 2 | request()->validate([ |
|
131 | 2 | 'category_id' => 'required', |
|
132 | 'layout' => 'required', |
||
133 | ]); |
||
134 | |||
135 | 1 | $this->saveOnDb($request, $blog); |
|
136 | |||
137 | 1 | return redirect()->route('blogs.index') |
|
138 | 1 | ->with('success', __('laravel-smart-blog::messages.blog_updated_successfully')); |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Remove the specified resource from storage. |
||
143 | * |
||
144 | * @param \DavideCasiraghi\LaravelSmartBlog\Models\Blog $blog |
||
145 | * @return \Illuminate\Http\Response |
||
146 | */ |
||
147 | 1 | public function destroy(Blog $blog) |
|
148 | { |
||
149 | 1 | $blog->delete(); |
|
150 | |||
151 | 1 | return redirect()->route('blogs.index') |
|
152 | 1 | ->with('success', __('laravel-smart-blog::messages.blog_deleted_successfully')); |
|
153 | } |
||
154 | |||
155 | /***************************************************************************/ |
||
156 | |||
157 | /** |
||
158 | * Save/Update the record on DB. |
||
159 | * |
||
160 | * @param \Illuminate\Http\Request $request |
||
161 | * @return void |
||
162 | */ |
||
163 | 2 | public function saveOnDb($request, $blog) |
|
164 | { |
||
165 | /* Blog layout */ |
||
166 | 2 | $blog->category_id = $request->get('category_id'); |
|
167 | 2 | $blog->layout = $request->get('layout'); |
|
168 | 2 | $blog->columns_number = $request->get('columns_number'); |
|
169 | 2 | $blog->columns_width = $request->get('columns_width'); |
|
170 | 2 | $blog->article_order = $request->get('article_order'); |
|
171 | |||
172 | 2 | $blog->items_per_page = $request->get('items_per_page'); |
|
173 | 2 | $blog->featured_articles = $request->get('featured_articles'); |
|
174 | 2 | $blog->show_category_title = filter_var($request->show_category_title, FILTER_VALIDATE_BOOLEAN); |
|
175 | 2 | $blog->show_category_subtitle = filter_var($request->show_category_subtitle, FILTER_VALIDATE_BOOLEAN); |
|
176 | 2 | $blog->show_category_description = filter_var($request->show_category_description, FILTER_VALIDATE_BOOLEAN); |
|
177 | 2 | $blog->show_category_image = filter_var($request->show_category_image, FILTER_VALIDATE_BOOLEAN); |
|
178 | |||
179 | 2 | $blog->show_post_title = filter_var($request->show_post_title, FILTER_VALIDATE_BOOLEAN); |
|
180 | 2 | $blog->post_linked_titles = filter_var($request->post_linked_titles, FILTER_VALIDATE_BOOLEAN); |
|
181 | 2 | $blog->show_post_intro_text = filter_var($request->show_post_intro_text, FILTER_VALIDATE_BOOLEAN); |
|
182 | 2 | $blog->show_post_author = filter_var($request->show_post_author, FILTER_VALIDATE_BOOLEAN); |
|
183 | 2 | $blog->link_post_author = filter_var($request->link_post_author, FILTER_VALIDATE_BOOLEAN); |
|
184 | |||
185 | 2 | $blog->show_create_date = filter_var($request->show_create_date, FILTER_VALIDATE_BOOLEAN); |
|
186 | 2 | $blog->show_modify_date = filter_var($request->show_modify_date, FILTER_VALIDATE_BOOLEAN); |
|
187 | 2 | $blog->show_publish_date = filter_var($request->show_publish_date, FILTER_VALIDATE_BOOLEAN); |
|
188 | 2 | $blog->show_read_more = filter_var($request->show_read_more, FILTER_VALIDATE_BOOLEAN); |
|
189 | |||
190 | 2 | $blog->created_by = \Auth::user()->id; |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
191 | |||
192 | 2 | $blog->save(); |
|
193 | 2 | } |
|
194 | } |
||
195 |