File size: 6,396 Bytes
5f32fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php

namespace App\Http\Controllers;

use App\Models\Blog;
use App\Models\FrontendSectionsStatusses;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class BlogController extends Controller
{
    // single post

    public function post($slug)
    {
        $post = Blog::where('slug', $slug)->first();

        // Check post status
        if (isset($post->status) && ! $post->status && ! Auth::user()->isAdmin()) {
            abort(404);
        }

        $previousPost = Blog::where('id', '<', $post->id)
            ->where('status', true)
            ->orderByDesc('id')
            ->first();

        $nextPost = Blog::where('id', '>', $post->id)
            ->where('status', true)
            ->orderBy('id')
            ->first();

        $relatedPosts = Blog::where('id', '!=', $post->id)
            ->where('status', true)
            ->where(function ($query) use ($post) {
                $categories = explode(',', $post->category);
                $tags = explode(',', $post->tag);
                foreach ($categories as $category) {
                    $query->orWhere('category', 'LIKE', '%' . $category . '%');
                }
                foreach ($tags as $tag) {
                    $query->orWhere('tag', 'LIKE', '%' . $tag . '%');
                }
            })
            ->orderByDesc('id')
            ->take(2)
            ->get();

        if ($post) {
            return view('blog.post', compact('post', 'previousPost', 'nextPost', 'relatedPosts'));
        } else {
            abort(404);
        }
    }

    // archive pages

    public function index()
    {

        $fSecSettings = FrontendSectionsStatusses::first();
        $posts_per_page = $fSecSettings->blog_a_posts_per_page;

        $posts = Blog::where('status', 1)->orderBy('id', 'desc')->paginate($posts_per_page);
        $hero = [
            'type'        => 'blog',
            'title'       => __($fSecSettings->blog_a_title),
            'subtitle'    => __($fSecSettings->blog_a_subtitle),
            'description' => __($fSecSettings->blog_a_description),
        ];

        return view('blog.index', compact('posts', 'hero'));
    }

    public function tags($slug)
    {

        $fSecSettings = FrontendSectionsStatusses::first();
        $posts_per_page = $fSecSettings->blog_a_posts_per_page;

        $posts = Blog::where('tag', 'like', "%{$slug}%")->where('status', 1)->orderBy('id', 'desc')->paginate($posts_per_page);
        $hero = [
            'type'        => 'tag',
            'title'       => $slug,
            'subtitle'    => __('Tag Archive'),
            'description' => __($fSecSettings->blog_a_description),
        ];

        if ($posts->isEmpty()) {
            abort(404);
        }

        return view('blog.index', compact('posts', 'hero'));
    }

    public function categories($slug)
    {

        $fSecSettings = FrontendSectionsStatusses::first();
        $posts_per_page = $fSecSettings->blog_a_posts_per_page;

        $posts = Blog::where('category', 'like', "%{$slug}%")->where('status', 1)->orderBy('id', 'desc')->paginate($posts_per_page);
        $hero = [
            'type'        => 'category',
            'title'       => $slug,
            'subtitle'    => __('Category Archive'),
            'description' => __($fSecSettings->blog_a_description),
        ];

        if ($posts->isEmpty()) {
            abort(404);
        }

        return view('blog.index', compact('posts', 'hero'));
    }

    public function author($user_id)
    {

        $fSecSettings = FrontendSectionsStatusses::first();
        $posts_per_page = $fSecSettings->blog_a_posts_per_page;

        $posts = Blog::where('user_id', $user_id)->where('status', 1)->orderBy('id', 'desc')->paginate($posts_per_page);
        $hero = [
            'type'        => 'author',
            'title'       => $user_id,
            'subtitle'    => 'Author Archive',
            'description' => __($fSecSettings->blog_a_description),
        ];

        if ($posts->isEmpty()) {
            abort(404);
        }

        return view('blog.index', compact('posts', 'hero'));
    }

    // dashboard

    public function blogList()
    {
        $list = Blog::orderBy('id', 'desc')->get();

        return view('panel.blog.list', compact('list'));
    }

    public function blogAddOrUpdate($id = null)
    {
        if ($id == null) {
            $blog = null;
        } else {
            $blog = Blog::where('id', $id)->firstOrFail();
        }

        return view('panel.blog.form', compact('blog'));
    }

    public function blogDelete($id = null)
    {
        $post = Blog::where('id', $id)->firstOrFail();
        $post->delete();

        return back()->with(['message' => __('Deleted Successfully'), 'type' => 'success']);
    }

    public function blogAddOrUpdateSave(Request $request)
    {

        if ($request->post_id != 'undefined') {
            $post = Blog::where('id', $request->post_id)->firstOrFail();
        } else {
            $post = new Blog;
        }

        if ($request->hasFile('feature_image')) {
            $path = 'upload/images/blog/';
            $image = $request->file('feature_image');
            $image_name = Str::random(4) . '-' . Str::slug($request->slug) . '.' . $image->getClientOriginalExtension();

            //Resim uzantı kontrolü
            $imageTypes = ['jpg', 'jpeg', 'png', 'svg', 'webp'];
            if (! in_array(Str::lower($image->getClientOriginalExtension()), $imageTypes)) {
                $data = [
                    'errors' => ['The file extension must be jpg, jpeg, png, webp or svg.'],
                ];

                return response()->json($data, 419);
            }

            $image->move($path, $image_name);

            $feature_image = $path . $image_name;
        }

        $post->title = $request->title;
        $post->content = $request->get('content');
        $post->feature_image = $feature_image ?? $post->feature_image;
        $post->slug = Str::slug($request->slug);
        $post->seo_title = $request->seo_title;
        $post->seo_description = $request->seo_description;
        $post->category = $request->category;
        $post->tag = $request->tag;
        $post->status = $request->status;
        $post->user_id = \Illuminate\Support\Facades\Auth::user()->id;
        $post->save();
    }
}