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 App\Classes\CardClass; |
9
|
|
|
use Illuminate\Support\Facades\DB; |
10
|
|
|
use Illuminate\Support\Facades\App; |
11
|
|
|
// use App\Classes\ColumnsClass; |
12
|
|
|
// use App\Classes\GalleryClass; |
13
|
|
|
// use App\Classes\AccordionClass; |
14
|
|
|
// use App\Classes\StatsDonateClass; |
15
|
|
|
// use App\Classes\PaypalButtonClass; |
16
|
|
|
use DavideCasiraghi\LaravelSmartBlog\Models\Post; |
17
|
|
|
// use App\Classes\CardsCarouselClass; |
18
|
|
|
use DavideCasiraghi\LaravelSmartBlog\Models\Category; |
19
|
|
|
// use App\Classes\CommunityGoalsClass; |
20
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
21
|
|
|
use DavideCasiraghi\ResponsiveGallery\Facades\ResponsiveGallery; |
22
|
|
|
use DavideCasiraghi\BootstrapAccordion\Facades\BootstrapAccordion; |
23
|
|
|
use DavideCasiraghi\LaravelCards\Facades\LaravelCards; |
24
|
|
|
|
25
|
|
|
class PostController extends Controller |
26
|
|
|
{ |
27
|
11 |
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
//Restrict the access to this resource just to logged in users except show view |
31
|
11 |
|
$this->middleware('admin', ['except' => ['show', 'postBySlug']]); |
32
|
11 |
|
} |
33
|
|
|
|
34
|
|
|
/***************************************************************************/ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Display a listing of the resource. |
38
|
|
|
* @param \Illuminate\Http\Request $request |
39
|
|
|
* @return \Illuminate\Http\Response |
40
|
|
|
*/ |
41
|
5 |
|
public function index(Request $request) |
42
|
|
|
{ |
43
|
|
|
//$categories = Category::getCategoriesArray(); |
44
|
5 |
|
$categories = Category::listsTranslations('name')->pluck('name', 'id'); |
45
|
|
|
|
46
|
5 |
|
$searchKeywords = $request->input('keywords'); |
47
|
5 |
|
$searchCategory = $request->input('category_id'); |
48
|
|
|
|
49
|
|
|
// Returns all countries having translations |
50
|
|
|
//dd(Post::translated()->get()); |
51
|
|
|
|
52
|
|
|
// Countries available for translations |
53
|
5 |
|
$countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
54
|
|
|
//DB::enableQueryLog(); |
55
|
|
|
|
56
|
5 |
|
if ($searchKeywords || $searchCategory) { |
57
|
|
|
$posts = Post:: |
58
|
|
|
select('post_translations.post_id AS id', 'post_translations.title AS title', 'status', 'featured', 'introimage', 'introimage_alt', 'category_id', 'locale') |
59
|
|
|
->join('post_translations', 'posts.id', '=', 'post_translations.post_id') |
60
|
|
|
|
61
|
|
|
->when($searchKeywords, function ($query, $searchKeywords) { |
62
|
|
|
return $query->where('post_translations.locale', '=', 'en') |
63
|
|
|
->where(function ($query) use ($searchKeywords) { |
64
|
|
|
$query->where('post_translations.title', $searchKeywords) |
65
|
|
|
->orWhere('post_translations.title', 'like', '%'.$searchKeywords.'%'); |
66
|
|
|
}); |
67
|
|
|
}) |
68
|
|
|
->when($searchCategory, function ($query, $searchCategory) { |
69
|
|
|
return $query->where('post_translations.locale', '=', 'en') |
70
|
|
|
->where(function ($query) use ($searchCategory) { |
71
|
|
|
$query->where('category_id', '=', $searchCategory); |
72
|
|
|
}); |
73
|
|
|
}) |
74
|
|
|
->paginate(20); |
75
|
|
|
} else { |
76
|
5 |
|
$posts = Post::listsTranslations('title')->select('posts.id', 'title', 'category_id', 'status', 'featured', 'introimage', 'introimage_alt')->orderBy('title')->paginate(20); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
//dd(DB::getQueryLog()); |
80
|
|
|
|
81
|
5 |
|
return view('laravel-smart-blog::posts.index', compact('posts')) |
82
|
5 |
|
->with('i', (request()->input('page', 1) - 1) * 20) |
83
|
5 |
|
->with('categories', $categories) |
84
|
5 |
|
->with('searchKeywords', $searchKeywords) |
85
|
5 |
|
->with('searchCategory', $searchCategory) |
86
|
5 |
|
->with('countriesAvailableForTranslations', $countriesAvailableForTranslations); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/***************************************************************************/ |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Show the form for creating a new resource. |
93
|
|
|
* |
94
|
|
|
* @return \Illuminate\Http\Response |
95
|
|
|
*/ |
96
|
1 |
|
public function create() |
97
|
|
|
{ |
98
|
|
|
//$categories = Category::getCategoriesArray(); |
99
|
1 |
|
$categories = Category::listsTranslations('name')->pluck('name', 'id'); |
100
|
|
|
|
101
|
1 |
|
return view('laravel-smart-blog::posts.create')->with('categories', $categories); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/***************************************************************************/ |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Store a newly created resource in storage. |
108
|
|
|
* |
109
|
|
|
* @param \Illuminate\Http\Request $request |
110
|
|
|
* @return \Illuminate\Http\Response |
111
|
|
|
*/ |
112
|
2 |
|
public function store(Request $request) |
113
|
|
|
{ |
114
|
|
|
// Validate form datas |
115
|
2 |
|
$validator = Validator::make($request->all(), [ |
116
|
2 |
|
'title' => 'required', |
117
|
|
|
'body' => 'required', |
118
|
|
|
'category_id' => 'required', |
119
|
|
|
]); |
120
|
2 |
|
if ($validator->fails()) { |
121
|
1 |
|
return back()->withErrors($validator)->withInput(); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
$post = new Post(); |
125
|
|
|
|
126
|
|
|
// Set the default language to edit the post for the admin to English (to avoid bug with null titles) |
127
|
|
|
//App::setLocale('en'); //removed for the package!!! maybe we still need it!!! |
128
|
|
|
|
129
|
1 |
|
$this->saveOnDb($request, $post); |
130
|
|
|
|
131
|
1 |
|
return redirect()->route('posts.index') |
132
|
1 |
|
->with('success', __('laravel-smart-blog::messages.article_added_successfully')); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/***************************************************************************/ |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Display the specified resource. |
139
|
|
|
* |
140
|
|
|
* @param \DavideCasiraghi\LaravelSmartBlog\Models\Post $post |
141
|
|
|
* @return \Illuminate\Http\Response |
142
|
|
|
*/ |
143
|
1 |
|
public function show(Post $post) |
144
|
|
|
{ |
145
|
|
|
|
146
|
|
|
// Accordion |
147
|
1 |
|
$post->body = BootstrapAccordion::getAccordions($post->body, 'plus-minus-circle'); |
|
|
|
|
148
|
|
|
|
149
|
|
|
// Gallery |
150
|
1 |
|
$publicPath = public_path('storage'); |
151
|
1 |
|
$post->body = ResponsiveGallery::getGallery($post->body, $publicPath); |
152
|
1 |
|
$post->before_content = ResponsiveGallery::getGallery($post->before_content, $publicPath); |
|
|
|
|
153
|
1 |
|
$post->after_content = ResponsiveGallery::getGallery($post->after_content, $publicPath); |
|
|
|
|
154
|
|
|
|
155
|
|
|
// Cards |
156
|
1 |
|
$post->body = LaravelCards::replace_card_snippets_with_template($post->body); |
157
|
|
|
|
158
|
|
|
/* $accordionClass = new AccordionClass(); |
159
|
|
|
$post->body = $accordionClass->getAccordion($post->body); |
160
|
|
|
$post->before_content = $accordionClass->getAccordion($post->before_content); |
161
|
|
|
$post->after_content = $accordionClass->getAccordion($post->after_content); |
162
|
|
|
|
163
|
|
|
// Card |
164
|
|
|
$cardClass = new CardClass(); |
165
|
|
|
$post->body = $cardClass->getCard($post->body); |
166
|
|
|
$post->before_content = $cardClass->getCard($post->before_content); |
167
|
|
|
$post->after_content = $cardClass->getCard($post->after_content); |
168
|
|
|
|
169
|
|
|
// Category Columns |
170
|
|
|
$cardsCarouselClass = new CardsCarouselClass(); |
171
|
|
|
$post->body = $cardsCarouselClass->getColumns($post->body); |
172
|
|
|
$post->before_content = $cardsCarouselClass->getColumns($post->before_content); |
173
|
|
|
$post->after_content = $cardsCarouselClass->getColumns($post->after_content); |
174
|
|
|
|
175
|
|
|
// Category Columns |
176
|
|
|
$columnClass = new ColumnsClass(); |
177
|
|
|
$post->body = $columnClass->getColumns($post->body); |
178
|
|
|
$post->before_content = $columnClass->getColumns($post->before_content); |
179
|
|
|
$post->after_content = $columnClass->getColumns($post->after_content); |
180
|
|
|
|
181
|
|
|
// Stats Donate |
182
|
|
|
$statsDonateClass = new StatsDonateClass(); |
183
|
|
|
$post->body = $statsDonateClass->getStatsDonate($post->body); |
184
|
|
|
$post->before_content = $statsDonateClass->getStatsDonate($post->before_content); |
185
|
|
|
$post->after_content = $statsDonateClass->getStatsDonate($post->after_content); |
186
|
|
|
|
187
|
|
|
// Stats Donate |
188
|
|
|
$communityGoalsClass = new CommunityGoalsClass(); |
189
|
|
|
$post->body = $communityGoalsClass->getCommunityGoals($post->body); |
190
|
|
|
|
191
|
|
|
// Paypal Button |
192
|
|
|
$paypalButton = new PaypalButtonClass(); |
193
|
|
|
$post->body = $paypalButton->getPaypalButton($post->body); |
194
|
|
|
|
195
|
|
|
// Gallery |
196
|
|
|
$storagePath = storage_path('app/public'); |
197
|
|
|
$publicPath = public_path(); |
198
|
|
|
//dump($storagePath,$publicPath); |
199
|
|
|
$galleryClass = new GalleryClass(); |
200
|
|
|
//dump($post->body); |
201
|
|
|
$post->body = $galleryClass->getGallery($post->body, $storagePath, $publicPath); |
202
|
|
|
$post->before_content = $galleryClass->getGallery($post->before_content, $storagePath, $publicPath); |
203
|
|
|
$post->after_content = $galleryClass->getGallery($post->after_content, $storagePath, $publicPath); |
204
|
|
|
*/ |
205
|
|
|
|
206
|
1 |
|
return view('laravel-smart-blog::posts.show', compact('post')); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/***************************************************************************/ |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Show the form for editing the specified resource. |
213
|
|
|
* |
214
|
|
|
* @param \DavideCasiraghi\LaravelSmartBlog\Models\Post $post |
215
|
|
|
* @return \Illuminate\Http\Response |
216
|
|
|
*/ |
217
|
1 |
|
public function edit(Post $post) |
218
|
|
|
{ |
219
|
|
|
//$categories = Category::getCategoriesArray(); |
220
|
1 |
|
$categories = Category::listsTranslations('name')->pluck('name', 'id'); |
221
|
|
|
//$categories = Category::getCategoriesArray(); |
222
|
|
|
|
223
|
1 |
|
return view('laravel-smart-blog::posts.edit', compact('post'))->with('categories', $categories); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/***************************************************************************/ |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Update the specified resource in storage. |
230
|
|
|
* |
231
|
|
|
* @param \Illuminate\Http\Request $request |
232
|
|
|
* @param \DavideCasiraghi\LaravelSmartBlog\Models\Post $post |
233
|
|
|
* @return \Illuminate\Http\Response |
234
|
|
|
*/ |
235
|
2 |
|
public function update(Request $request, Post $post) |
236
|
|
|
{ |
237
|
2 |
|
request()->validate([ |
238
|
2 |
|
'title' => 'required', |
239
|
|
|
'body' => 'required', |
240
|
|
|
'category_id' => 'required', |
241
|
|
|
]); |
242
|
|
|
|
243
|
|
|
// Set the default language to edit the post for the admin to English (to avoid bug with null titles) |
244
|
|
|
//App::setLocale('en'); |
245
|
|
|
|
246
|
1 |
|
$this->saveOnDb($request, $post); |
247
|
|
|
|
248
|
1 |
|
return redirect()->route('posts.index') |
249
|
1 |
|
->with('success', __('laravel-smart-blog::messages.article_updated_successfully')); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/***************************************************************************/ |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Remove the specified resource from storage. |
256
|
|
|
* |
257
|
|
|
* @param \DavideCasiraghi\LaravelSmartBlog\Models\Post $post |
258
|
|
|
* @return \Illuminate\Http\Response |
259
|
|
|
*/ |
260
|
1 |
|
public function destroy(Post $post) |
261
|
|
|
{ |
262
|
1 |
|
$post->delete(); |
263
|
|
|
|
264
|
1 |
|
return redirect()->route('posts.index') |
265
|
1 |
|
->with('success', __('laravel-smart-blog::messages.article_deleted_successfully')); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/***************************************************************************/ |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Return the single post datas by post id [title, body, image]. |
272
|
|
|
* |
273
|
|
|
* @param int $post_id |
274
|
|
|
* @return \DavideCasiraghi\LaravelSmartBlog\Models\Post |
275
|
|
|
*/ |
276
|
|
|
public function postdata($post_id) |
277
|
|
|
{ |
278
|
|
|
$ret = Post::where('id', $post_id)->first(); |
279
|
|
|
|
280
|
|
|
return $ret; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/***************************************************************************/ |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Return the post by SLUG. (eg. http://websitename.com/post/xxxxx). |
287
|
|
|
* |
288
|
|
|
* @param string $slug |
289
|
|
|
* @return \Illuminate\Http\Response |
290
|
|
|
*/ |
291
|
|
|
public function postBySlug($slug) |
292
|
|
|
{ |
293
|
|
|
$post = Post:: |
294
|
|
|
where('post_translations.slug', $slug) |
295
|
|
|
->join('post_translations', 'posts.id', '=', 'post_translations.post_id') |
296
|
|
|
->select('posts.*', 'post_translations.title', 'post_translations.intro_text', 'post_translations.body', 'post_translations.before_content', 'post_translations.after_content') |
297
|
|
|
->first(); |
298
|
|
|
|
299
|
|
|
return $this->show($post); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/***************************************************************************/ |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Save the record on DB. |
306
|
|
|
* @param \Illuminate\Http\Request $request |
307
|
|
|
* @param \DavideCasiraghi\LaravelSmartBlog\Models\Post $post |
308
|
|
|
* @return void |
309
|
|
|
*/ |
310
|
2 |
|
public function saveOnDb($request, $post) |
311
|
|
|
{ |
312
|
2 |
|
$post->translateOrNew('en')->title = $request->get('title'); |
313
|
2 |
|
$post->translateOrNew('en')->body = clean($request->get('body')); |
314
|
2 |
|
$post->translateOrNew('en')->intro_text = $request->get('intro_text'); |
315
|
2 |
|
$post->created_by = \Auth::user()->id; |
|
|
|
|
316
|
2 |
|
$post->translateOrNew('en')->slug = Str::slug($post->title, '-'); |
|
|
|
|
317
|
2 |
|
$post->category_id = $request->get('category_id'); |
318
|
|
|
|
319
|
2 |
|
$post->status = $request->get('status'); |
320
|
2 |
|
$post->featured = $request->get('featured'); |
321
|
|
|
|
322
|
|
|
// Intro image picture upload |
323
|
2 |
|
if ($request->file('introimage')) { |
324
|
|
|
$imageFile = $request->file('introimage'); |
325
|
|
|
$imageName = $imageFile->hashName(); |
326
|
|
|
$imageSubdir = 'posts_intro_images'; |
327
|
|
|
$imageWidth = '968'; |
328
|
|
|
$thumbWidth = '300'; |
329
|
|
|
|
330
|
|
|
$this->uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth); |
|
|
|
|
331
|
|
|
$post->introimage = $imageName; |
332
|
|
|
} else { |
333
|
2 |
|
$post->introimage = $request->introimage; |
334
|
|
|
} |
335
|
|
|
|
336
|
2 |
|
$post->translateOrNew('en')->before_content = $request->get('before_content'); |
337
|
2 |
|
$post->translateOrNew('en')->after_content = $request->get('after_content'); |
338
|
|
|
|
339
|
2 |
|
$post->save(); |
340
|
2 |
|
} |
341
|
|
|
} |
342
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.