source: app/Http/Controllers/Blog/BlogController.php@ ff9da8b

Last change on this file since ff9da8b was f457265, checked in by Berat Kjufliju <kufliju@…>, 4 years ago

ADD technoweek offer, companies

  • Property mode set to 100644
File size: 7.0 KB
RevLine 
[0924b6c]1<?php
2
3namespace App\Http\Controllers\Blog;
4
5use App\Http\Requests\Blog\CommentRequest;
[f457265]6use App\Http\Requests\Blog\CompanyRequest;
[0924b6c]7use App\Http\Requests\Blog\PostLikeRequest;
8use App\Http\Requests\Blog\PostUnlikeRequest;
[f457265]9use App\Models\Company;
[0924b6c]10use App\Models\Tag;
11use App\Models\Like;
12use App\Models\Post;
13use App\Models\Comment;
14use App\Models\Category;
15use App\Models\UserProfile;
16use Illuminate\Http\Request;
17use App\Http\Controllers\Controller;
18use App\Notifications\NewCommentAdded;
[f457265]19use Illuminate\Support\Facades\DB;
[0924b6c]20
21class BlogController extends Controller
22{
23 public function __construct()
24 {
25 $this->middleware("web");
26 }
27
28 public function index()
29 {
30 $posts = collect();
31
32 $categories = collect();
33 $allCategories = Category::all();
34
35 foreach ($allCategories as $c) {
36 if (Post::where([
37 "category_id" => $c->id,
38 "is_confirmed" => true,
39 "is_active" => true
40 ])->count() > 0) {
41 $categories->push($c);
42 }
43 }
44
45 foreach ($categories as $category) {
46 $tempPosts = Post::where([
47 "category_id" => $category->id,
48 "is_confirmed" => true,
49 "is_active" => true
50 ])
51 ->orderBy("created_at", "desc")
52 ->take(3)
53 ->get();
54
55 foreach ($tempPosts as $post) {
56 $posts->push($post);
57 }
58 }
59
60 $posts = $posts->flatten(1);
61
62 return view("blog.index")->with([
63 "latestPosts" => Post::where([
64 "is_confirmed" => true,
65 "is_active" => true
66 ])->orderBy("created_at", "desc")->take(5)->get(),
67 "posts" => $posts
68 ]);
69 }
70
71 public function about()
72 {
73 return view("blog.about");
74 }
75
76 public function showCategory($category)
77 {
78 if (Category::whereName(ucfirst($category))->count() == 0) {
79 return abort(404);
80 }
81
82 if (Post::where([
83 "category_id" => Category::whereName(ucfirst($category))->first()->id,
84 "is_confirmed" => true,
85 "is_active" => true
86 ])->count() == 0) {
87 return abort(404);
88 }
89
90 $posts = Post::where([
91 "category_id" => Category::whereName(ucfirst($category))->first()->id,
92 "is_confirmed" => true,
93 "is_active" => true
94 ])
95 ->orderBy("created_at", "desc")
96 ->paginate(8);
97
98 return view("blog.category")->with([
99 "category" => Category::whereName(ucfirst($category))->first(),
100 "posts" => $posts
101 ]);
102 }
103
104 public function showPost(Request $request, $category, $slug)
105 {
106 $post = Post::where([
107 "slug" => $slug,
108 "is_confirmed" => true,
109 "is_active" => true
110 ])->firstOrFail();
111
112 if ($post->isLiked($request->ip())) {
113 $likedId = Like::where([
114 "post_id" => $post->id,
115 "ip_address" => $request->ip()
116 ])->firstOrFail()->id;
117 } else $likedId = null;
118
119 if (auth()->check()) $comments = $post->comment;
120 else $comments = $post->comment()->where([
121 "post_id" => $post->id,
122 "is_active" => true,
123 ])->get();
124
125 return view("blog.post")->with([
126 "comments" => $comments,
127 "category" => $category,
128 "likeCounter" => $post->total_likes,
129 "post" => $post,
130 "ip" => $request->ip(),
131 "likedId" => $likedId
132 ]);
133 }
134
135 public function showTagPosts($tag)
136 {
137 return view("blog.tag")->with([
138 "tag" => $tag,
139 "posts" => Tag::whereName($tag)
140 ->firstOrFail()
141 ->post()
142 ->where([
143 "is_confirmed" => true,
144 "is_active" => true,
145 ])
146 ->orderBy("created_at", "desc")
147 ->paginate(5)
148 ]);
149 }
150
151 public function showProfile($profileLink)
152 {
153 $userProfile = UserProfile::where("profile_link", $profileLink)->firstOrFail();
154
155 $posts = Post::where([
156 "user_id" => $userProfile->user_id,
157 "is_confirmed" => true,
158 "is_active" => true,
159 ])
160 ->orderBy("created_at", "desc")
161 ->paginate(5);
162
163 if ($posts->count() == 0) {
164 return abort(404);
165 }
166
167 return view("blog.profile")->with("posts", $posts);
168 }
169
170 public function comment(CommentRequest $request)
171 {
172 $comment = new Comment();
173
174 $comment->post_id = $request->post_id;
175 $comment->name = $request->name;
176 $comment->email = $request->email;
177 $comment->comment = $request->comment;
178
179 $comment->save();
180
181 $commentedPost = Post::find($request->post_id);
182
183 $commentedPost->user->notify(new NewCommentAdded("Have new comment for review"));
184
185 return response()->json([
186 "type" => "alert-success",
187 "message" => "Comment submited successfully and will be published after review."
188 ]);
189 }
190
191 public function like(PostLikeRequest $request)
192 {
193 $like = new Like();
194 $post = Post::find($request->post_id);
195 $likes = $post->like->all();
196
197 foreach ($likes as $l) {
198 if ($l->post_id == $request->post_id && $l->ip_address == $request->ip()) {
199 return false;
200 }
201 }
202
203 $like->post_id = $request->post_id;
204 $like->ip_address = $request->ip();
205
206 $like->save();
207
208 $post->increment("total_likes");
209 $post->save();
210
211 return response()->json([
212 "unlike_id" => $like->id,
213 "likeCounter" => $post->total_likes
214 ]);
215 }
216
217 public function unlike(PostUnlikeRequest $request)
218 {
219 $like = Like::find($request->unlike_id);
220 $post = Post::find($like->post_id);
221 $like->delete();
222
223 $post->decrement("total_likes");
224 $post->save();
225
226 return response()->json([
227 "post_id" => $post->id,
228 "likeCounter" => $post->total_likes
229 ]);
230 }
[f457265]231
232 public function storeCompany(CompanyRequest $request)
233 {
234 DB::transaction(function () use ($request) {
235
236 $company = new Company();
237 $company->name = $request->name;
238 $company->website = $request->website;
239 $company->email = $request->email;
240
241 $company->save();
242
243 if ($company->wasRecentlyCreated) {
244 $this->disableOfferModal();
245 }
246 });
247
248 return redirect()->route("blog.index");
249 }
250
251 public function disableOffer()
252 {
253 $this->disableOfferModal();
254 return redirect()->route("blog.index");
255 }
256
257 private function disableOfferModal()
258 {
259 if (!session()->has("offer_disabled")) {
260 session()->push("offer_disabled", true);
261 }
262 }
[0924b6c]263}
Note: See TracBrowser for help on using the repository browser.