1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\Dashboard;
|
---|
4 |
|
---|
5 | use App\Helpers\Alert;
|
---|
6 | use App\Http\Requests\Dashboard\EmailSettingsRequest;
|
---|
7 | use App\Http\Requests\Dashboard\PasswordSettingsRequest;
|
---|
8 | use App\Http\Requests\Dashboard\PhaseRequest;
|
---|
9 | use App\Http\Requests\Dashboard\PhotosSettingsRequest;
|
---|
10 | use App\Http\Requests\Dashboard\PostSecurityRequest;
|
---|
11 | use App\Http\Requests\Dashboard\SocialLinksSettingsRequest;
|
---|
12 | use App\Http\Requests\Dashboard\UsernameSettingsRequest;
|
---|
13 | use App\Http\Requests\Dashboard\UserProfileSettingsRequest;
|
---|
14 | use App\Models\Phase;
|
---|
15 | use App\Models\Post;
|
---|
16 | use App\Models\PostSecurity;
|
---|
17 | use App\Models\Role;
|
---|
18 | use App\Models\User;
|
---|
19 | use App\Models\UserProfile;
|
---|
20 | use Illuminate\Support\Str;
|
---|
21 | use Illuminate\Http\Request;
|
---|
22 | use App\Http\Controllers\Controller;
|
---|
23 | use Illuminate\Support\Facades\File;
|
---|
24 | use Illuminate\Support\Facades\Hash;
|
---|
25 | use App\Notifications\VerifyNewEmail;
|
---|
26 | use Illuminate\Support\Facades\Storage;
|
---|
27 | use Propaganistas\LaravelPhone\PhoneNumber;
|
---|
28 | use Propaganistas\LaravelIntl\Facades\Country;
|
---|
29 |
|
---|
30 | class SettingsController extends Controller
|
---|
31 | {
|
---|
32 | public function settings()
|
---|
33 | {
|
---|
34 | return view("dashboard.settings.index")->with([
|
---|
35 | "userProfile" => auth()->user()->userProfile,
|
---|
36 | "countries" => Country::all(),
|
---|
37 | "adminAndEditors" => User::where("role_id", 1)->orWhere("role_id", 2)->get(),
|
---|
38 | "userRoles" => Role::all(),
|
---|
39 | "phases" => Phase::all(),
|
---|
40 | "postSecurities" => PostSecurity::all()
|
---|
41 | ]);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public function updatePersonalInformation(UserProfileSettingsRequest $request)
|
---|
45 | {
|
---|
46 | $userProfile = auth()->user()->userProfile;
|
---|
47 |
|
---|
48 | $userProfile->user->name = $request->name;
|
---|
49 | $userProfile->user->surname = $request->surname;
|
---|
50 | $userProfile->user->country_code = $request->mobile_number_country;
|
---|
51 | $userProfile->user->mobile_number = PhoneNumber::make($request->mobile_number, $request->mobile_number_country)->formatInternational();
|
---|
52 |
|
---|
53 | $userProfile->profile_link = $request->profile_link;
|
---|
54 | $userProfile->short_bio = $request->short_bio;
|
---|
55 | $userProfile->technoblog_email = $userProfile->user->generateTechnoblogEmail($request->name, $request->surname);
|
---|
56 |
|
---|
57 | $userProfile->user->save();
|
---|
58 | $userProfile->save();
|
---|
59 |
|
---|
60 | Alert::flash("Updated successfully");
|
---|
61 |
|
---|
62 | return redirect()->route("dashboard.settings.index");
|
---|
63 | }
|
---|
64 |
|
---|
65 | public function updatePhotos(PhotosSettingsRequest $request)
|
---|
66 | {
|
---|
67 | $userProfile = auth()->user()->userProfile;
|
---|
68 | $this->updatePhotosHelper($request, $userProfile);
|
---|
69 | $userProfile->save();
|
---|
70 |
|
---|
71 | Alert::flash("Updated successfully");
|
---|
72 |
|
---|
73 | return redirect()->route("dashboard.settings.index");
|
---|
74 | }
|
---|
75 |
|
---|
76 | public function updateSocialLinks(SocialLinksSettingsRequest $request)
|
---|
77 | {
|
---|
78 | $userProfile = auth()->user()->userProfile;
|
---|
79 |
|
---|
80 | $userProfile->facebook_link = $request->facebook;
|
---|
81 | $userProfile->instagram_link = $request->instagram;
|
---|
82 | $userProfile->twitter_link = $request->twitter;
|
---|
83 | $userProfile->youtube_link = $request->youtube;
|
---|
84 | $userProfile->skype_link = $request->skype;
|
---|
85 |
|
---|
86 | $userProfile->save();
|
---|
87 |
|
---|
88 | Alert::flash("Updated successfully");
|
---|
89 |
|
---|
90 | return redirect()->route("dashboard.settings.index");
|
---|
91 | }
|
---|
92 |
|
---|
93 | public function updateUsername(UsernameSettingsRequest $request)
|
---|
94 | {
|
---|
95 | $user = auth()->user();
|
---|
96 | $user->username = $request->username;
|
---|
97 | $user->save();
|
---|
98 |
|
---|
99 | auth()->logout();
|
---|
100 | session()->flush();
|
---|
101 |
|
---|
102 | return redirect()->route("auth.loginShow");
|
---|
103 | }
|
---|
104 |
|
---|
105 | public function updatePassword(PasswordSettingsRequest $request)
|
---|
106 | {
|
---|
107 | $user = auth()->user();
|
---|
108 | $user->password = bcrypt($request->password);
|
---|
109 | $user->save();
|
---|
110 |
|
---|
111 | auth()->logout();
|
---|
112 | session()->flush();
|
---|
113 |
|
---|
114 | return redirect()->route("auth.loginShow");
|
---|
115 | }
|
---|
116 |
|
---|
117 | public function updateEmail(EmailSettingsRequest $request)
|
---|
118 | {
|
---|
119 | $user = auth()->user();
|
---|
120 |
|
---|
121 | $user->email = $request->email;
|
---|
122 | $user->is_active = false;
|
---|
123 | $user->security_code = $user->generateSecurityCode();
|
---|
124 | $user->verify_token = $user->generateVerifyToken();
|
---|
125 |
|
---|
126 | $user->save();
|
---|
127 |
|
---|
128 | $user->notify(new VerifyNewEmail($user));
|
---|
129 |
|
---|
130 | auth()->logout();
|
---|
131 | session()->flush();
|
---|
132 |
|
---|
133 | return redirect()->route("auth.loginShow");
|
---|
134 | }
|
---|
135 |
|
---|
136 | public function transferPostsAndDeleteUser(Request $request)
|
---|
137 | {
|
---|
138 | $from = $request->from;
|
---|
139 | $to = $request->to;
|
---|
140 |
|
---|
141 | if ($from == $to) {
|
---|
142 | return redirect()->back()->withInput()->withErrors([
|
---|
143 | "cantDelete" => "Can't transfer posts to same user"
|
---|
144 | ]);
|
---|
145 | }
|
---|
146 |
|
---|
147 | $posts = Post::where("user_id", $from)->get();
|
---|
148 | $userFrom = User::find($from);
|
---|
149 | $userTo = User::find($to);
|
---|
150 | $selfAccount = false;
|
---|
151 |
|
---|
152 | if ($request->has("password")) {
|
---|
153 | $selfAccount = true;
|
---|
154 | if (!Hash::check($request->password, $userFrom->password)) {
|
---|
155 | return redirect()->back()->withInput()->withErrors([
|
---|
156 | "cantDelete" => "Your password is invalid"
|
---|
157 | ]);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | if ($posts != null) {
|
---|
162 | foreach ($posts as $post) {
|
---|
163 | $post->user()->associate($userTo);
|
---|
164 | $post->confirmed_by = $userTo->id;
|
---|
165 | $post->save();
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (Storage::disk("uploads")->exists("/users/" . $userFrom->userProfile->profile_photo_link)) {
|
---|
170 | Storage::disk("uploads")->delete("/users/" . $userFrom->userProfile->profile_photo_link);
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (Storage::disk("uploads")->exists("/users/" . $userFrom->userProfile->cover_photo_link)) {
|
---|
174 | Storage::disk("uploads")->delete("/users/" . $userFrom->userProfile->cover_photo_link);
|
---|
175 | }
|
---|
176 |
|
---|
177 | $userFrom->delete();
|
---|
178 | $userFrom->userProfile->delete();
|
---|
179 |
|
---|
180 | if ($selfAccount) {
|
---|
181 | return redirect()->route("blog.index");
|
---|
182 | }
|
---|
183 |
|
---|
184 | Alert::flash($userFrom->name . " deleted successfully");
|
---|
185 |
|
---|
186 | return redirect()->route("dashboard.users.index");
|
---|
187 | }
|
---|
188 |
|
---|
189 | public function phaseStore(PhaseRequest $request)
|
---|
190 | {
|
---|
191 | if ($request->has("phase_id") && !is_null($request->phase_id)) {
|
---|
192 | $phase = Phase::findOrFail($request->phase_id);
|
---|
193 | } else {
|
---|
194 | $phase = new Phase();
|
---|
195 | }
|
---|
196 |
|
---|
197 | $phase->name = $request->name;
|
---|
198 | $phase->reviewer()->associate(Role::find($request->reviewer_role_id));
|
---|
199 | $phase->user()->associate(auth()->user());
|
---|
200 | $phase->save();
|
---|
201 |
|
---|
202 | if ($request->has("phase_id") && !is_null($request->phase_id)) {
|
---|
203 | Alert::flash($phase->name . " updated successfully");
|
---|
204 | } else {
|
---|
205 | Alert::flash($phase->name . " created successfully");
|
---|
206 | }
|
---|
207 |
|
---|
208 | return redirect()->route("dashboard.settings.index");
|
---|
209 | }
|
---|
210 |
|
---|
211 | public function phaseDestroy($id)
|
---|
212 | {
|
---|
213 | $phase = Phase::findOrFail($id);
|
---|
214 | $phaseName = $phase->name;
|
---|
215 | $phase->delete();
|
---|
216 |
|
---|
217 | Alert::flash($phaseName . " deleted successfully");
|
---|
218 |
|
---|
219 | return redirect()->route("dashboard.settings.index");
|
---|
220 | }
|
---|
221 |
|
---|
222 | public function postSecurityStore(PostSecurityRequest $request)
|
---|
223 | {
|
---|
224 | if ($request->has("post_security_id") && !is_null($request->post_security_id)) {
|
---|
225 | $postSecurity = PostSecurity::findOrFail($request->post_security_id);
|
---|
226 | } else {
|
---|
227 | $postSecurity = new PostSecurity();
|
---|
228 | }
|
---|
229 |
|
---|
230 | $postSecurity->role()->associate(Role::find($request->role_id_to_be_reviewed));
|
---|
231 | $postSecurity->phase_ids = $request->phase_ids;
|
---|
232 |
|
---|
233 | $postSecurity->save();
|
---|
234 |
|
---|
235 | if ($request->has("post_security_id") && !is_null($request->post_security_id)) {
|
---|
236 | Alert::flash("Post security updated successfully");
|
---|
237 | } else {
|
---|
238 | Alert::flash("Post security created successfully");
|
---|
239 | }
|
---|
240 |
|
---|
241 | return redirect()->route("dashboard.settings.index");
|
---|
242 | }
|
---|
243 |
|
---|
244 | public function postSecurityDestroy($id)
|
---|
245 | {
|
---|
246 | PostSecurity::findOrFail($id)->delete();
|
---|
247 | Alert::flash("Post security deleted successfully");
|
---|
248 | return redirect()->route("dashboard.settings.index");
|
---|
249 | }
|
---|
250 |
|
---|
251 | private function updatePhotosHelper(Request $request, UserProfile $userProfile)
|
---|
252 | {
|
---|
253 | if ($request->hasFile("profile_image")) {
|
---|
254 |
|
---|
255 | $image = $request->file("profile_image");
|
---|
256 | $extension = $image->getClientOriginalExtension();
|
---|
257 | $imageName = $this->createImageName($extension);
|
---|
258 |
|
---|
259 | if (!empty($userProfile->profile_photo_link)) {
|
---|
260 | Storage::disk('uploads')->delete("/users/" . $userProfile->profile_photo_link);
|
---|
261 | }
|
---|
262 |
|
---|
263 | Storage::disk('uploads')->put("/users/" . $imageName, File::get($image));
|
---|
264 | $userProfile->profile_photo_link = $imageName;
|
---|
265 | }
|
---|
266 |
|
---|
267 | if ($request->hasFile("cover_image")) {
|
---|
268 |
|
---|
269 | $image = $request->file("cover_image");
|
---|
270 | $extension = $image->getClientOriginalExtension();
|
---|
271 | $imageName = $this->createImageName($extension);
|
---|
272 |
|
---|
273 | if (!empty($userProfile->cover_photo_link)) {
|
---|
274 | Storage::disk('uploads')->delete("/users/" . $userProfile->cover_photo_link);
|
---|
275 | }
|
---|
276 |
|
---|
277 | Storage::disk('uploads')->put("/users/" . $imageName, File::get($image));
|
---|
278 | $userProfile->cover_photo_link = $imageName;
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | private function createImageName($extension)
|
---|
283 | {
|
---|
284 | return auth()->user()->id . "-" . Str::random(10) . "." . $extension;
|
---|
285 | }
|
---|
286 | }
|
---|