source: app/Http/Controllers/Dashboard/SettingsController.php@ 7ed1069

Last change on this file since 7ed1069 was d25ba66, checked in by Özkan İliyaz <iliyaz_96@…>, 4 years ago

ADD post confirmation with multiple phases, notification after succesfully password creation

  • Property mode set to 100644
File size: 9.3 KB
RevLine 
[0924b6c]1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Requests\Dashboard\EmailSettingsRequest;
7use App\Http\Requests\Dashboard\PasswordSettingsRequest;
[d25ba66]8use App\Http\Requests\Dashboard\PhaseRequest;
[0924b6c]9use App\Http\Requests\Dashboard\PhotosSettingsRequest;
[d25ba66]10use App\Http\Requests\Dashboard\PostSecurityRequest;
[0924b6c]11use App\Http\Requests\Dashboard\SocialLinksSettingsRequest;
12use App\Http\Requests\Dashboard\UsernameSettingsRequest;
13use App\Http\Requests\Dashboard\UserProfileSettingsRequest;
[d25ba66]14use App\Models\Phase;
[0924b6c]15use App\Models\Post;
[d25ba66]16use App\Models\PostSecurity;
17use App\Models\Role;
[0924b6c]18use App\Models\User;
19use App\Models\UserProfile;
20use Illuminate\Support\Str;
21use Illuminate\Http\Request;
22use App\Http\Controllers\Controller;
23use Illuminate\Support\Facades\File;
24use Illuminate\Support\Facades\Hash;
25use App\Notifications\VerifyNewEmail;
26use Illuminate\Support\Facades\Storage;
27use Propaganistas\LaravelPhone\PhoneNumber;
28use Propaganistas\LaravelIntl\Facades\Country;
29
30class 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(),
[d25ba66]37 "adminAndEditors" => User::where("role_id", 1)->orWhere("role_id", 2)->get(),
38 "userRoles" => Role::all(),
39 "phases" => Phase::all(),
40 "postSecurities" => PostSecurity::all()
[0924b6c]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
[d25ba66]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
[0924b6c]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}
Note: See TracBrowser for help on using the repository browser.