Issues (11)

src/Http/Controllers/PostController.php (6 issues)

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 DavideCasiraghi\LaravelCards\Facades\LaravelCards;
21
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
22
use DavideCasiraghi\ResponsiveGallery\Facades\ResponsiveGallery;
23
use DavideCasiraghi\BootstrapAccordion\Facades\BootstrapAccordion;
24
use DavideCasiraghi\LaravelJumbotronImages\Facades\LaravelJumbotronImages;
25
26
class PostController extends Controller
27
{
28 11
    public function __construct()
29
    {
30
31
        //Restrict the access to this resource just to logged in users except show view
32 11
        $this->middleware('admin', ['except' => ['show', 'postBySlug']]);
33 11
    }
34
35
    /***************************************************************************/
36
37
    /**
38
     * Display a listing of the resource.
39
     * @param  \Illuminate\Http\Request  $request
40
     * @return \Illuminate\Http\Response
41
     */
42 5
    public function index(Request $request)
43
    {
44
        //$categories = Category::getCategoriesArray();
45 5
        $categories = Category::listsTranslations('name')->pluck('name', 'id');
46
47 5
        $searchKeywords = $request->input('keywords');
48 5
        $searchCategory = $request->input('category_id');
49
50
        // Returns all countries having translations
51
        //dd(Post::translated()->get());
52
53
        // Countries available for translations
54 5
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
55
        //DB::enableQueryLog();
56
57 5
        if ($searchKeywords || $searchCategory) {
58
            $posts = Post::
59
                select('post_translations.post_id AS id', 'post_translations.title AS title', 'status', 'featured', 'introimage', 'introimage_alt', 'category_id', 'locale')
60
                ->join('post_translations', 'posts.id', '=', 'post_translations.post_id')
61
62
                ->when($searchKeywords, function ($query, $searchKeywords) {
63
                    return $query->where('post_translations.locale', '=', 'en')
64
                                 ->where(function ($query) use ($searchKeywords) {
65
                                     $query->where('post_translations.title', $searchKeywords)
66
                                                  ->orWhere('post_translations.title', 'like', '%'.$searchKeywords.'%');
67
                                 });
68
                })
69
                ->when($searchCategory, function ($query, $searchCategory) {
70
                    return $query->where('post_translations.locale', '=', 'en')
71
                                 ->where(function ($query) use ($searchCategory) {
72
                                     $query->where('category_id', '=', $searchCategory);
73
                                 });
74
                })
75
                ->paginate(20);
76
        } else {
77 5
            $posts = Post::listsTranslations('title')->select('posts.id', 'title', 'category_id', 'status', 'featured', 'introimage', 'introimage_alt')->orderBy('title')->paginate(20);
78
        }
79
80
        //dd(DB::getQueryLog());
81
82 5
        return view('laravel-smart-blog::posts.index', compact('posts'))
83 5
            ->with('i', (request()->input('page', 1) - 1) * 20)
84 5
            ->with('categories', $categories)
85 5
            ->with('searchKeywords', $searchKeywords)
86 5
            ->with('searchCategory', $searchCategory)
87 5
            ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations);
88
    }
89
90
    /***************************************************************************/
91
92
    /**
93
     * Show the form for creating a new resource.
94
     *
95
     * @return \Illuminate\Http\Response
96
     */
97 1
    public function create()
98
    {
99
        //$categories = Category::getCategoriesArray();
100 1
        $categories = Category::listsTranslations('name')->pluck('name', 'id');
101
102 1
        return view('laravel-smart-blog::posts.create')->with('categories', $categories);
103
    }
104
105
    /***************************************************************************/
106
107
    /**
108
     * Store a newly created resource in storage.
109
     *
110
     * @param  \Illuminate\Http\Request  $request
111
     * @return \Illuminate\Http\Response
112
     */
113 2
    public function store(Request $request)
114
    {
115
        // Validate form datas
116 2
        $validator = Validator::make($request->all(), [
117 2
                'title' => 'required',
118
                'body' => 'required',
119
                'category_id' => 'required',
120
            ]);
121 2
        if ($validator->fails()) {
122 1
            return back()->withErrors($validator)->withInput();
123
        }
124
125 1
        $post = new Post();
126
127
        // Set the default language to edit the post for the admin to English (to avoid bug with null titles)
128
        //App::setLocale('en'); //removed for the package!!! maybe we still need it!!!
129
130 1
        $this->saveOnDb($request, $post);
131
132 1
        return redirect()->route('posts.index')
133 1
                        ->with('success', __('laravel-smart-blog::messages.article_added_successfully'));
134
    }
135
136
    /***************************************************************************/
137
138
    /**
139
     * Display the specified resource.
140
     *
141
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\Post  $post
142
     * @return \Illuminate\Http\Response
143
     */
144 1
    public function show(Post $post)
145
    {
146
147
        // Accordion
148 1
        $post->body = BootstrapAccordion::getAccordions($post->body, 'plus-minus-circle');
0 ignored issues
show
The property body does not seem to exist on DavideCasiraghi\LaravelSmartBlog\Models\Post. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
149
150
        // Gallery
151 1
        $publicPath = public_path('storage');
152 1
        $post->body = ResponsiveGallery::getGallery($post->body, $publicPath);
153 1
        $post->before_content = ResponsiveGallery::getGallery($post->before_content, $publicPath);
0 ignored issues
show
The property before_content does not seem to exist on DavideCasiraghi\LaravelSmartBlog\Models\Post. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
154 1
        $post->after_content = ResponsiveGallery::getGallery($post->after_content, $publicPath);
0 ignored issues
show
The property after_content does not seem to exist on DavideCasiraghi\LaravelSmartBlog\Models\Post. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
155
156
        // Cards
157 1
        $post->body = LaravelCards::replace_card_snippets_with_template($post->body);
158 1
        $post->before_content = LaravelCards::replace_card_snippets_with_template($post->before_content);
159 1
        $post->after_content = LaravelCards::replace_card_snippets_with_template($post->after_content);
160
161
        // JumbotronImages
162 1
        $post->body = LaravelJumbotronImages::replaceJumbotronSnippetsWithTemplate($post->body);
163 1
        $post->before_content = LaravelJumbotronImages::replaceJumbotronSnippetsWithTemplate($post->before_content);
164 1
        $post->after_content = LaravelJumbotronImages::replaceJumbotronSnippetsWithTemplate($post->after_content);
165
166
        /*
167
                // Card
168
                $cardClass = new CardClass();
169
                $post->body = $cardClass->getCard($post->body);
170
                $post->before_content = $cardClass->getCard($post->before_content);
171
                $post->after_content = $cardClass->getCard($post->after_content);
172
173
                // Category Columns
174
                $cardsCarouselClass = new CardsCarouselClass();
175
                $post->body = $cardsCarouselClass->getColumns($post->body);
176
                $post->before_content = $cardsCarouselClass->getColumns($post->before_content);
177
                $post->after_content = $cardsCarouselClass->getColumns($post->after_content);
178
179
                // Category Columns
180
                $columnClass = new ColumnsClass();
181
                $post->body = $columnClass->getColumns($post->body);
182
                $post->before_content = $columnClass->getColumns($post->before_content);
183
                $post->after_content = $columnClass->getColumns($post->after_content);
184
185
                // Stats Donate
186
                $statsDonateClass = new StatsDonateClass();
187
                $post->body = $statsDonateClass->getStatsDonate($post->body);
188
                $post->before_content = $statsDonateClass->getStatsDonate($post->before_content);
189
                $post->after_content = $statsDonateClass->getStatsDonate($post->after_content);
190
191
                // Stats Donate
192
                $communityGoalsClass = new CommunityGoalsClass();
193
                $post->body = $communityGoalsClass->getCommunityGoals($post->body);
194
195
                // Paypal Button
196
                $paypalButton = new PaypalButtonClass();
197
                $post->body = $paypalButton->getPaypalButton($post->body);
198
199
                // Gallery
200
                $storagePath = storage_path('app/public');
201
                $publicPath = public_path();
202
                //dump($storagePath,$publicPath);
203
                $galleryClass = new GalleryClass();
204
                //dump($post->body);
205
                $post->body = $galleryClass->getGallery($post->body, $storagePath, $publicPath);
206
                $post->before_content = $galleryClass->getGallery($post->before_content, $storagePath, $publicPath);
207
                $post->after_content = $galleryClass->getGallery($post->after_content, $storagePath, $publicPath);
208
        */
209
210 1
        return view('laravel-smart-blog::posts.show', compact('post'));
211
    }
212
213
    /***************************************************************************/
214
215
    /**
216
     * Show the form for editing the specified resource.
217
     *
218
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\Post  $post
219
     * @return \Illuminate\Http\Response
220
     */
221 1
    public function edit(Post $post)
222
    {
223
        //$categories = Category::getCategoriesArray();
224 1
        $categories = Category::listsTranslations('name')->pluck('name', 'id');
225
        //$categories = Category::getCategoriesArray();
226
227 1
        return view('laravel-smart-blog::posts.edit', compact('post'))->with('categories', $categories);
228
    }
229
230
    /***************************************************************************/
231
232
    /**
233
     * Update the specified resource in storage.
234
     *
235
     * @param  \Illuminate\Http\Request  $request
236
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\Post  $post
237
     * @return \Illuminate\Http\Response
238
     */
239 2
    public function update(Request $request, Post $post)
240
    {
241 2
        request()->validate([
242 2
            'title' => 'required',
243
            'body' => 'required',
244
            'category_id' => 'required',
245
        ]);
246
247
        // Set the default language to edit the post for the admin to English (to avoid bug with null titles)
248
        //App::setLocale('en');
249
250 1
        $this->saveOnDb($request, $post);
251
252 1
        return redirect()->route('posts.index')
253 1
                        ->with('success', __('laravel-smart-blog::messages.article_updated_successfully'));
254
    }
255
256
    /***************************************************************************/
257
258
    /**
259
     * Remove the specified resource from storage.
260
     *
261
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\Post  $post
262
     * @return \Illuminate\Http\Response
263
     */
264 1
    public function destroy(Post $post)
265
    {
266 1
        $post->delete();
267
268 1
        return redirect()->route('posts.index')
269 1
                        ->with('success', __('laravel-smart-blog::messages.article_deleted_successfully'));
270
    }
271
272
    /***************************************************************************/
273
274
    /**
275
     * Return the single post datas by post id [title, body, image].
276
     *
277
     * @param  int $post_id
278
     * @return \DavideCasiraghi\LaravelSmartBlog\Models\Post
279
     */
280
    public function postdata($post_id)
281
    {
282
        $ret = Post::where('id', $post_id)->first();
283
284
        return $ret;
285
    }
286
287
    /***************************************************************************/
288
289
    /**
290
     * Return the post by SLUG. (eg. http://websitename.com/post/xxxxx).
291
     *
292
     * @param  string $slug
293
     * @return \Illuminate\Http\Response
294
     */
295
    public function postBySlug($slug)
296
    {
297
        $post = Post::
298
                where('post_translations.slug', $slug)
299
                ->join('post_translations', 'posts.id', '=', 'post_translations.post_id')
300
                ->select('posts.*', 'post_translations.title', 'post_translations.intro_text', 'post_translations.body', 'post_translations.before_content', 'post_translations.after_content')
301
                ->first();
302
303
        return $this->show($post);
304
    }
305
306
    /***************************************************************************/
307
308
    /**
309
     * Save the record on DB.
310
     * @param  \Illuminate\Http\Request  $request
311
     * @param  \DavideCasiraghi\LaravelSmartBlog\Models\Post  $post
312
     * @return void
313
     */
314 2
    public function saveOnDb($request, $post)
315
    {
316 2
        $post->translateOrNew('en')->title = $request->get('title');
317 2
        $post->translateOrNew('en')->body = clean($request->get('body'));
318 2
        $post->translateOrNew('en')->intro_text = $request->get('intro_text');
319 2
        $post->created_by = \Auth::user()->id;
0 ignored issues
show
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...
320 2
        $post->translateOrNew('en')->slug = Str::slug($post->title, '-');
0 ignored issues
show
The property title does not seem to exist on DavideCasiraghi\LaravelSmartBlog\Models\Post. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
321 2
        $post->category_id = $request->get('category_id');
322
323 2
        $post->status = $request->get('status');
324 2
        $post->featured = $request->get('featured');
325
326
        // Intro image  picture upload
327 2
        if ($request->file('introimage')) {
328
            $imageFile = $request->file('introimage');
329
            $imageName = $imageFile->hashName();
330
            $imageSubdir = 'posts_intro_images';
331
            $imageWidth = '968';
332
            $thumbWidth = '300';
333
334
            $this->uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
0 ignored issues
show
It seems like $imageFile can also be of type Illuminate\Http\UploadedFile; however, parameter $imageFile of DavideCasiraghi\LaravelS...::uploadImageOnServer() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

334
            $this->uploadImageOnServer(/** @scrutinizer ignore-type */ $imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
Loading history...
335
            $post->introimage = $imageName;
336
        } else {
337 2
            $post->introimage = $request->introimage;
338
        }
339
340 2
        $post->translateOrNew('en')->before_content = $request->get('before_content');
341 2
        $post->translateOrNew('en')->after_content = $request->get('after_content');
342
343 2
        $post->save();
344 2
    }
345
}
346