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