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\PhotosSettingsRequest;
|
---|
9 | use App\Http\Requests\Dashboard\SocialLinksSettingsRequest;
|
---|
10 | use App\Http\Requests\Dashboard\UsernameSettingsRequest;
|
---|
11 | use App\Http\Requests\Dashboard\UserProfileSettingsRequest;
|
---|
12 | use App\Models\Post;
|
---|
13 | use App\Models\User;
|
---|
14 | use App\Models\UserProfile;
|
---|
15 | use Illuminate\Support\Str;
|
---|
16 | use Illuminate\Http\Request;
|
---|
17 | use App\Http\Controllers\Controller;
|
---|
18 | use Illuminate\Support\Facades\File;
|
---|
19 | use Illuminate\Support\Facades\Hash;
|
---|
20 | use App\Notifications\VerifyNewEmail;
|
---|
21 | use Illuminate\Support\Facades\Storage;
|
---|
22 | use Propaganistas\LaravelPhone\PhoneNumber;
|
---|
23 | use Propaganistas\LaravelIntl\Facades\Country;
|
---|
24 |
|
---|
25 | class SettingsController extends Controller
|
---|
26 | {
|
---|
27 | public function settings()
|
---|
28 | {
|
---|
29 | return view("dashboard.settings.index")->with([
|
---|
30 | "userProfile" => auth()->user()->userProfile,
|
---|
31 | "countries" => Country::all(),
|
---|
32 | "adminAndEditors" => User::where("role_id", 1)->orWhere("role_id", 2)->get()
|
---|
33 | ]);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public function updatePersonalInformation(UserProfileSettingsRequest $request)
|
---|
37 | {
|
---|
38 | $userProfile = auth()->user()->userProfile;
|
---|
39 |
|
---|
40 | $userProfile->user->name = $request->name;
|
---|
41 | $userProfile->user->surname = $request->surname;
|
---|
42 | $userProfile->user->country_code = $request->mobile_number_country;
|
---|
43 | $userProfile->user->mobile_number = PhoneNumber::make($request->mobile_number, $request->mobile_number_country)->formatInternational();
|
---|
44 |
|
---|
45 | $userProfile->profile_link = $request->profile_link;
|
---|
46 | $userProfile->short_bio = $request->short_bio;
|
---|
47 | $userProfile->technoblog_email = $userProfile->user->generateTechnoblogEmail($request->name, $request->surname);
|
---|
48 |
|
---|
49 | $userProfile->user->save();
|
---|
50 | $userProfile->save();
|
---|
51 |
|
---|
52 | Alert::flash("Updated successfully");
|
---|
53 |
|
---|
54 | return redirect()->route("dashboard.settings.index");
|
---|
55 | }
|
---|
56 |
|
---|
57 | public function updatePhotos(PhotosSettingsRequest $request)
|
---|
58 | {
|
---|
59 | $userProfile = auth()->user()->userProfile;
|
---|
60 | $this->updatePhotosHelper($request, $userProfile);
|
---|
61 | $userProfile->save();
|
---|
62 |
|
---|
63 | Alert::flash("Updated successfully");
|
---|
64 |
|
---|
65 | return redirect()->route("dashboard.settings.index");
|
---|
66 | }
|
---|
67 |
|
---|
68 | public function updateSocialLinks(SocialLinksSettingsRequest $request)
|
---|
69 | {
|
---|
70 | $userProfile = auth()->user()->userProfile;
|
---|
71 |
|
---|
72 | $userProfile->facebook_link = $request->facebook;
|
---|
73 | $userProfile->instagram_link = $request->instagram;
|
---|
74 | $userProfile->twitter_link = $request->twitter;
|
---|
75 | $userProfile->youtube_link = $request->youtube;
|
---|
76 | $userProfile->skype_link = $request->skype;
|
---|
77 |
|
---|
78 | $userProfile->save();
|
---|
79 |
|
---|
80 | Alert::flash("Updated successfully");
|
---|
81 |
|
---|
82 | return redirect()->route("dashboard.settings.index");
|
---|
83 | }
|
---|
84 |
|
---|
85 | public function updateUsername(UsernameSettingsRequest $request)
|
---|
86 | {
|
---|
87 | $user = auth()->user();
|
---|
88 | $user->username = $request->username;
|
---|
89 | $user->save();
|
---|
90 |
|
---|
91 | auth()->logout();
|
---|
92 | session()->flush();
|
---|
93 |
|
---|
94 | return redirect()->route("auth.loginShow");
|
---|
95 | }
|
---|
96 |
|
---|
97 | public function updatePassword(PasswordSettingsRequest $request)
|
---|
98 | {
|
---|
99 | $user = auth()->user();
|
---|
100 | $user->password = bcrypt($request->password);
|
---|
101 | $user->save();
|
---|
102 |
|
---|
103 | auth()->logout();
|
---|
104 | session()->flush();
|
---|
105 |
|
---|
106 | return redirect()->route("auth.loginShow");
|
---|
107 | }
|
---|
108 |
|
---|
109 | public function updateEmail(EmailSettingsRequest $request)
|
---|
110 | {
|
---|
111 | $user = auth()->user();
|
---|
112 |
|
---|
113 | $user->email = $request->email;
|
---|
114 | $user->is_active = false;
|
---|
115 | $user->security_code = $user->generateSecurityCode();
|
---|
116 | $user->verify_token = $user->generateVerifyToken();
|
---|
117 |
|
---|
118 | $user->save();
|
---|
119 |
|
---|
120 | $user->notify(new VerifyNewEmail($user));
|
---|
121 |
|
---|
122 | auth()->logout();
|
---|
123 | session()->flush();
|
---|
124 |
|
---|
125 | return redirect()->route("auth.loginShow");
|
---|
126 | }
|
---|
127 |
|
---|
128 | public function transferPostsAndDeleteUser(Request $request)
|
---|
129 | {
|
---|
130 | $from = $request->from;
|
---|
131 | $to = $request->to;
|
---|
132 |
|
---|
133 | if ($from == $to) {
|
---|
134 | return redirect()->back()->withInput()->withErrors([
|
---|
135 | "cantDelete" => "Can't transfer posts to same user"
|
---|
136 | ]);
|
---|
137 | }
|
---|
138 |
|
---|
139 | $posts = Post::where("user_id", $from)->get();
|
---|
140 | $userFrom = User::find($from);
|
---|
141 | $userTo = User::find($to);
|
---|
142 | $selfAccount = false;
|
---|
143 |
|
---|
144 | if ($request->has("password")) {
|
---|
145 | $selfAccount = true;
|
---|
146 | if (!Hash::check($request->password, $userFrom->password)) {
|
---|
147 | return redirect()->back()->withInput()->withErrors([
|
---|
148 | "cantDelete" => "Your password is invalid"
|
---|
149 | ]);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | if ($posts != null) {
|
---|
154 | foreach ($posts as $post) {
|
---|
155 | $post->user()->associate($userTo);
|
---|
156 | $post->confirmed_by = $userTo->id;
|
---|
157 | $post->save();
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (Storage::disk("uploads")->exists("/users/" . $userFrom->userProfile->profile_photo_link)) {
|
---|
162 | Storage::disk("uploads")->delete("/users/" . $userFrom->userProfile->profile_photo_link);
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (Storage::disk("uploads")->exists("/users/" . $userFrom->userProfile->cover_photo_link)) {
|
---|
166 | Storage::disk("uploads")->delete("/users/" . $userFrom->userProfile->cover_photo_link);
|
---|
167 | }
|
---|
168 |
|
---|
169 | $userFrom->delete();
|
---|
170 | $userFrom->userProfile->delete();
|
---|
171 |
|
---|
172 | if ($selfAccount) {
|
---|
173 | return redirect()->route("blog.index");
|
---|
174 | }
|
---|
175 |
|
---|
176 | Alert::flash($userFrom->name . " deleted successfully");
|
---|
177 |
|
---|
178 | return redirect()->route("dashboard.users.index");
|
---|
179 | }
|
---|
180 |
|
---|
181 | private function updatePhotosHelper(Request $request, UserProfile $userProfile)
|
---|
182 | {
|
---|
183 | if ($request->hasFile("profile_image")) {
|
---|
184 |
|
---|
185 | $image = $request->file("profile_image");
|
---|
186 | $extension = $image->getClientOriginalExtension();
|
---|
187 | $imageName = $this->createImageName($extension);
|
---|
188 |
|
---|
189 | if (!empty($userProfile->profile_photo_link)) {
|
---|
190 | Storage::disk('uploads')->delete("/users/" . $userProfile->profile_photo_link);
|
---|
191 | }
|
---|
192 |
|
---|
193 | Storage::disk('uploads')->put("/users/" . $imageName, File::get($image));
|
---|
194 | $userProfile->profile_photo_link = $imageName;
|
---|
195 | }
|
---|
196 |
|
---|
197 | if ($request->hasFile("cover_image")) {
|
---|
198 |
|
---|
199 | $image = $request->file("cover_image");
|
---|
200 | $extension = $image->getClientOriginalExtension();
|
---|
201 | $imageName = $this->createImageName($extension);
|
---|
202 |
|
---|
203 | if (!empty($userProfile->cover_photo_link)) {
|
---|
204 | Storage::disk('uploads')->delete("/users/" . $userProfile->cover_photo_link);
|
---|
205 | }
|
---|
206 |
|
---|
207 | Storage::disk('uploads')->put("/users/" . $imageName, File::get($image));
|
---|
208 | $userProfile->cover_photo_link = $imageName;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | private function createImageName($extension)
|
---|
213 | {
|
---|
214 | return auth()->user()->id . "-" . Str::random(10) . "." . $extension;
|
---|
215 | }
|
---|
216 | }
|
---|