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