From 4eca39e9ce59f5705859c6b49f704d6796e74112 Mon Sep 17 00:00:00 2001 From: Julian Prieber Date: Wed, 8 Mar 2023 17:20:39 +0100 Subject: [PATCH] Added option to import and export user data --- app/Http/Controllers/UserController.php | 107 +++++++++++++++++++++++ resources/views/studio/profile.blade.php | 64 ++++++++------ routes/web.php | 3 + 3 files changed, 149 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index c16186d..df34906 100755 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -741,6 +741,113 @@ class UserController extends Controller return back(); } + //Export user links + public function exportLinks(request $request) + { + $userId = Auth::id(); + $user = User::find($userId); + $links = Link::where('user_id', $userId)->get(); + + if (!$user) { + // handle the case where the user is null + return response()->json(['message' => 'User not found'], 404); + } + + $userData['links'] = $links->toArray(); + + $fileName = 'links.json'; + $headers = [ + 'Content-Type' => 'application/json', + 'Content-Disposition' => 'attachment; filename="'.$fileName.'"', + ]; + return response()->json($userData, 200, $headers); + + return back(); + } + + //Export all user data + public function exportAll(request $request) + { + $userId = Auth::id(); + $user = User::find($userId); + $links = Link::where('user_id', $userId)->get(); + + if (!$user) { + // handle the case where the user is null + return response()->json(['message' => 'User not found'], 404); + } + + $userData = $user->toArray(); + $userData['links'] = $links->toArray(); + + $fileName = 'user_data.json'; + $headers = [ + 'Content-Type' => 'application/json', + 'Content-Disposition' => 'attachment; filename="'.$fileName.'"', + ]; + return response()->json($userData, 200, $headers); + + return back(); + } + +//Import user data from file +public function importData(Request $request) +{ + try { + // Get the JSON data from the uploaded file + if (!$request->hasFile('import') || !$request->file('import')->isValid()) { + throw new \Exception('File not uploaded or is faulty'); + } + $file = $request->file('import'); + $jsonString = $file->get(); + $userData = json_decode($jsonString, true); + + // Update the authenticated user's profile data if defined in the JSON file + $user = auth()->user(); + if (isset($userData['name'])) { + $user->name = $userData['name']; + } + if (isset($userData['littlelink_name'])) { + $user->littlelink_name = $userData['littlelink_name']; + } + if (isset($userData['littlelink_description'])) { + $user->littlelink_description = $userData['littlelink_description']; + } + if (isset($userData['image'])) { + $user->image = $userData['image']; + } + $user->save(); + + // Delete all links for the authenticated user + Link::where('user_id', $user->id)->delete(); + + // Loop through each link in $userData and create a new link for the user + foreach ($userData['links'] as $linkData) { + $newLink = new Link(); + + // Copy over the link data from $linkData to $newLink + $newLink->button_id = $linkData['button_id']; + $newLink->link = $linkData['link']; + $newLink->title = $linkData['title']; + $newLink->order = $linkData['order']; + $newLink->click_number = $linkData['click_number']; + $newLink->up_link = $linkData['up_link']; + $newLink->custom_css = $linkData['custom_css']; + $newLink->custom_icon = $linkData['custom_icon']; + + // Set the user ID to the current user's ID + $newLink->user_id = $user->id; + + // Save the new link to the database + $newLink->save(); + } + + return redirect('studio/profile')->with('success', 'Profile updated successfully!'); + } catch (\Exception $e) { + return redirect('studio/profile')->with('error', 'An error occurred while updating your profile.'); + } +} + //Edit/save page icons public function editIcons(request $request) { diff --git a/resources/views/studio/profile.blade.php b/resources/views/studio/profile.blade.php index 2a661e3..89d8340 100755 --- a/resources/views/studio/profile.blade.php +++ b/resources/views/studio/profile.blade.php @@ -2,6 +2,18 @@ @section('content') +@if(session()->has('success')) +
+ {{ session()->get('success') }} +
+@endif + +@if(session()->has('error')) +
+ {{ session()->get('error') }} +
+@endif + @if($_SERVER['QUERY_STRING'] === '')

Account Settings

@@ -9,15 +21,6 @@ @foreach($profile as $profile) - {{--
- @csrf -
-

Name

- -
- -


--}} - @if(env('REGISTER_AUTH') != 'verified' or auth()->user()->role == 'admin')
@csrf @@ -29,27 +32,38 @@
@endif -

- @csrf -
-

Password

- -
- -
- - @csrf -

-

Role

- -
-
+


+ +
+

Export user data

+ +
+ +
+ @csrf +



+

Import user data

+ + +
+ + +
+ +
+ +
@endforeach @endif - @if($_SERVER['QUERY_STRING'] === 'delete')

You are about to delete your account!

diff --git a/routes/web.php b/routes/web.php index 5fdcd73..b845733 100755 --- a/routes/web.php +++ b/routes/web.php @@ -123,6 +123,9 @@ Route::post('/edit-icons', [UserController::class, 'editIcons'])->name('editIcon Route::get('/clearIcon/{id}', [UserController::class, 'clearIcon'])->name('clearIcon'); Route::get('/studio/page/delprofilepicture', [UserController::class, 'delProfilePicture'])->name('delProfilePicture'); Route::get('/studio/delete-user/{id}', [UserController::class, 'deleteUser'])->name('deleteUser')->middleware('verified'); +Route::get('/export-links', [UserController::class, 'exportLinks'])->name('exportLinks'); +Route::get('/export-all', [UserController::class, 'exportAll'])->name('exportAll'); +Route::post('/import-data', [UserController::class, 'importData'])->name('importData'); Route::get('/studio/linkparamform_part/{typeid}/{linkid}', [LinkTypeViewController::class, 'getParamForm'])->name('linkparamform.part'); });