1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\Dashboard;
|
---|
4 |
|
---|
5 | use App\Helpers\Alert;
|
---|
6 | use App\Http\Requests\Dashboard\PostRequest;
|
---|
7 | use App\Models\Tag;
|
---|
8 | use App\Models\Post;
|
---|
9 | use App\Models\User;
|
---|
10 | use App\Models\Category;
|
---|
11 | use Illuminate\Support\Str;
|
---|
12 | use Illuminate\Http\Request;
|
---|
13 | use Mews\Purifier\Facades\Purifier;
|
---|
14 | use App\Http\Controllers\Controller;
|
---|
15 | use App\Notifications\PostConfirmed;
|
---|
16 | use Illuminate\Support\Facades\File;
|
---|
17 | use App\Notifications\NewPostCreated;
|
---|
18 | use Illuminate\Support\Facades\Storage;
|
---|
19 | use Illuminate\Support\Facades\Notification;
|
---|
20 |
|
---|
21 | class PostsController extends Controller
|
---|
22 | {
|
---|
23 | public function index()
|
---|
24 | {
|
---|
25 | if (auth()->user()->hasPermission("access_all_posts")) $posts = Post::all();
|
---|
26 | else $posts = Post::where("user_id", auth()->user()->id)->get();
|
---|
27 |
|
---|
28 | return view("dashboard.posts.index")->with([
|
---|
29 | "posts" => $posts,
|
---|
30 | "currentUser" => auth()->user()
|
---|
31 | ]);
|
---|
32 | }
|
---|
33 |
|
---|
34 | public function create()
|
---|
35 | {
|
---|
36 | return view("dashboard.posts.create")->with([
|
---|
37 | "categories" => Category::all(),
|
---|
38 | "tags" => Tag::all()
|
---|
39 | ]);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public function editShow($id)
|
---|
43 | {
|
---|
44 | $post = Post::findOrFail($id);
|
---|
45 |
|
---|
46 | if (!auth()->user()->hasPermission("edit_all_posts") && ($post->user->id != auth()->user()->id || !$post->is_confirmed)) {
|
---|
47 | return redirect()->route("dashboard.posts.index");
|
---|
48 | }
|
---|
49 |
|
---|
50 | return view("dashboard.posts.edit")->with([
|
---|
51 | "post" => $post,
|
---|
52 | "postTags" => $post->tag()->pluck("id")->toArray(),
|
---|
53 | "categories" => Category::all(),
|
---|
54 | "tags" => Tag::all()
|
---|
55 | ]);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public function edit(PostRequest $request, $id)
|
---|
59 | {
|
---|
60 | $post = Post::findOrFail($id);
|
---|
61 |
|
---|
62 | $category = Category::find($request->category);
|
---|
63 |
|
---|
64 | $post->category()->associate($category);
|
---|
65 |
|
---|
66 | $post->title = $request->title;
|
---|
67 |
|
---|
68 | if ($request->hasFile("image")) {
|
---|
69 |
|
---|
70 | Storage::disk('uploads')->delete($post->image_link);
|
---|
71 |
|
---|
72 | $image = $request->file("image");
|
---|
73 | $extension = $image->getClientOriginalExtension();
|
---|
74 | $imageName = $this->createImageName($extension);
|
---|
75 | Storage::disk('uploads')->put($imageName, File::get($image));
|
---|
76 |
|
---|
77 | $post->image_link = $imageName;
|
---|
78 | }
|
---|
79 |
|
---|
80 | $post->content = clean($request->post_content);
|
---|
81 | $post->slug = $post->createSlug(true);
|
---|
82 |
|
---|
83 | $post->save();
|
---|
84 | $this->checkNewAndSaveTags($post, $request->tags, true);
|
---|
85 |
|
---|
86 | Alert::flash("Post edited successfully");
|
---|
87 |
|
---|
88 | return redirect()->route("dashboard.posts.index");
|
---|
89 | }
|
---|
90 |
|
---|
91 | public function store(PostRequest $request)
|
---|
92 | {
|
---|
93 | $post = new Post();
|
---|
94 | $user = auth()->user();
|
---|
95 | $category = Category::find($request->category);
|
---|
96 |
|
---|
97 | $post->user()->associate($user);
|
---|
98 | $post->category()->associate($category);
|
---|
99 |
|
---|
100 | $post->title = $request->title;
|
---|
101 |
|
---|
102 | $image = $request->file("image");
|
---|
103 | $extension = $image->getClientOriginalExtension();
|
---|
104 | $imageName = $this->createImageName($extension);
|
---|
105 | Storage::disk('uploads')->put($imageName, File::get($image));
|
---|
106 |
|
---|
107 | $post->image_link = $imageName;
|
---|
108 | $post->content = Purifier::clean($request->post_content, 'youtube');
|
---|
109 |
|
---|
110 | $post->slug = $post->createSlug();
|
---|
111 |
|
---|
112 | if ($post->user->hasPermission("publish_post")) {
|
---|
113 | $post->confirmed_by = $post->user->id;
|
---|
114 | $post->is_active = 1;
|
---|
115 | $post->is_confirmed = true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | $post->save();
|
---|
119 | $this->checkNewAndSaveTags($post, $request->tags);
|
---|
120 |
|
---|
121 | if ($post->user->hasPermission("publish_post")) {
|
---|
122 | Alert::flash("New posts published successfully");
|
---|
123 | } else {
|
---|
124 | Alert::flash("New posts submitted for review successfully");
|
---|
125 | $adminsAndEditors = User::where("role_id", 1)->orWhere("role_id", 2)->get();
|
---|
126 | Notification::send($adminsAndEditors, new NewPostCreated("Have new post for review"));
|
---|
127 | }
|
---|
128 |
|
---|
129 | return redirect()->route("dashboard.posts.create");
|
---|
130 | }
|
---|
131 |
|
---|
132 | public function confirm(Request $request, $id)
|
---|
133 | {
|
---|
134 | $post = Post::find($id);
|
---|
135 | $flag = false;
|
---|
136 |
|
---|
137 | if (auth()->user()->hasPermission("confirm_post")) {
|
---|
138 | $flag = true;
|
---|
139 | }
|
---|
140 |
|
---|
141 | if ($flag) {
|
---|
142 |
|
---|
143 | $post->is_confirmed = true;
|
---|
144 | $post->confirmed_by = auth()->user()->id;
|
---|
145 |
|
---|
146 | $post->save();
|
---|
147 |
|
---|
148 | Alert::flash("Post confirmed successfully");
|
---|
149 |
|
---|
150 | $post->user->notify(new PostConfirmed("Your post has been confirmed"));
|
---|
151 | }
|
---|
152 |
|
---|
153 | return redirect()->route("dashboard.posts.index");
|
---|
154 | }
|
---|
155 |
|
---|
156 | public function block(Request $request, $id)
|
---|
157 | {
|
---|
158 | $post = Post::find($id);
|
---|
159 | $flag = false;
|
---|
160 |
|
---|
161 | if (auth()->user()->hasPermission("edit_all_posts")) {
|
---|
162 | $flag = true;
|
---|
163 | } else if ($post->is_active && auth()->user()->id == $post->user->id) {
|
---|
164 | $flag = true;
|
---|
165 | }
|
---|
166 |
|
---|
167 | if ($flag) {
|
---|
168 | $post->is_active = false;
|
---|
169 | $post->save();
|
---|
170 | Alert::flash("Post blocked successfully");
|
---|
171 | }
|
---|
172 |
|
---|
173 | return redirect()->route("dashboard.posts.index");
|
---|
174 | }
|
---|
175 |
|
---|
176 | public function unblock(Request $request, $id)
|
---|
177 | {
|
---|
178 | $post = Post::find($id);
|
---|
179 | $flag = false;
|
---|
180 |
|
---|
181 | if (auth()->user()->hasPermission("edit_all_posts")) {
|
---|
182 | $flag = true;
|
---|
183 | } else if (!$post->is_active && auth()->user()->id == $post->user->id) {
|
---|
184 | $flag = true;
|
---|
185 | }
|
---|
186 |
|
---|
187 | if ($flag) {
|
---|
188 | $post->is_active = true;
|
---|
189 | $post->save();
|
---|
190 | Alert::flash("Post unblocked successfully.");
|
---|
191 | }
|
---|
192 |
|
---|
193 | return redirect()->route("dashboard.posts.index");
|
---|
194 | }
|
---|
195 |
|
---|
196 | public function destroy(Request $request, $id)
|
---|
197 | {
|
---|
198 | $post = Post::find($id);
|
---|
199 | $flag = false;
|
---|
200 |
|
---|
201 | if (auth()->user()->hasPermission("delete_all_posts")) {
|
---|
202 | $flag = true;
|
---|
203 | } else if ($post->is_confirmed && auth()->user()->id == $post->user->id) {
|
---|
204 | $flag = true;
|
---|
205 | }
|
---|
206 |
|
---|
207 | if ($flag) {
|
---|
208 | $usedTags = $post->tag->pluck("id")->toArray();
|
---|
209 | $post->tag()->detach($usedTags);
|
---|
210 | $post->delete();
|
---|
211 | Storage::disk('uploads')->delete($post->image_link);
|
---|
212 | Alert::flash("Post deleted successfully.");
|
---|
213 | }
|
---|
214 |
|
---|
215 | return redirect()->route("dashboard.posts.index");
|
---|
216 | }
|
---|
217 |
|
---|
218 | private function checkNewAndSaveTags(Post $post, $tags, $isEdit = false)
|
---|
219 | {
|
---|
220 | $tagsArray = explode(",", $tags);
|
---|
221 | $ids = array();
|
---|
222 |
|
---|
223 | foreach ($tagsArray as $t) {
|
---|
224 |
|
---|
225 | $isNewTag = false;
|
---|
226 |
|
---|
227 | if (!is_numeric($t)) {
|
---|
228 |
|
---|
229 | $isNewTag = true;
|
---|
230 |
|
---|
231 | $tag = new Tag();
|
---|
232 | $tag->name = strtolower(trim($t));
|
---|
233 |
|
---|
234 | if (strlen($tag->name) > 0) {
|
---|
235 | $tag->save();
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | if ($isNewTag) {
|
---|
240 | array_push($ids, $tag->id);
|
---|
241 | } else {
|
---|
242 | array_push($ids, $t);
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | if ($isEdit) {
|
---|
247 | $post->tag()->sync($ids);
|
---|
248 | } else {
|
---|
249 | $post->tag()->attach($ids);
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | private function createImageName($extension)
|
---|
254 | {
|
---|
255 | return Str::random(10) . "." . $extension;
|
---|
256 | }
|
---|
257 | }
|
---|