Skip to content

Commit 4eca39e

Browse files
committed
Added option to import and export user data
1 parent 3d27749 commit 4eca39e

File tree

3 files changed

+149
-25
lines changed

3 files changed

+149
-25
lines changed

app/Http/Controllers/UserController.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,113 @@ public function delProfilePicture()
741741
return back();
742742
}
743743

744+
//Export user links
745+
public function exportLinks(request $request)
746+
{
747+
$userId = Auth::id();
748+
$user = User::find($userId);
749+
$links = Link::where('user_id', $userId)->get();
750+
751+
if (!$user) {
752+
// handle the case where the user is null
753+
return response()->json(['message' => 'User not found'], 404);
754+
}
755+
756+
$userData['links'] = $links->toArray();
757+
758+
$fileName = 'links.json';
759+
$headers = [
760+
'Content-Type' => 'application/json',
761+
'Content-Disposition' => 'attachment; filename="'.$fileName.'"',
762+
];
763+
return response()->json($userData, 200, $headers);
764+
765+
return back();
766+
}
767+
768+
//Export all user data
769+
public function exportAll(request $request)
770+
{
771+
$userId = Auth::id();
772+
$user = User::find($userId);
773+
$links = Link::where('user_id', $userId)->get();
774+
775+
if (!$user) {
776+
// handle the case where the user is null
777+
return response()->json(['message' => 'User not found'], 404);
778+
}
779+
780+
$userData = $user->toArray();
781+
$userData['links'] = $links->toArray();
782+
783+
$fileName = 'user_data.json';
784+
$headers = [
785+
'Content-Type' => 'application/json',
786+
'Content-Disposition' => 'attachment; filename="'.$fileName.'"',
787+
];
788+
return response()->json($userData, 200, $headers);
789+
790+
return back();
791+
}
792+
793+
//Import user data from file
794+
public function importData(Request $request)
795+
{
796+
try {
797+
// Get the JSON data from the uploaded file
798+
if (!$request->hasFile('import') || !$request->file('import')->isValid()) {
799+
throw new \Exception('File not uploaded or is faulty');
800+
}
801+
$file = $request->file('import');
802+
$jsonString = $file->get();
803+
$userData = json_decode($jsonString, true);
804+
805+
// Update the authenticated user's profile data if defined in the JSON file
806+
$user = auth()->user();
807+
if (isset($userData['name'])) {
808+
$user->name = $userData['name'];
809+
}
810+
if (isset($userData['littlelink_name'])) {
811+
$user->littlelink_name = $userData['littlelink_name'];
812+
}
813+
if (isset($userData['littlelink_description'])) {
814+
$user->littlelink_description = $userData['littlelink_description'];
815+
}
816+
if (isset($userData['image'])) {
817+
$user->image = $userData['image'];
818+
}
819+
$user->save();
820+
821+
// Delete all links for the authenticated user
822+
Link::where('user_id', $user->id)->delete();
823+
824+
// Loop through each link in $userData and create a new link for the user
825+
foreach ($userData['links'] as $linkData) {
826+
$newLink = new Link();
827+
828+
// Copy over the link data from $linkData to $newLink
829+
$newLink->button_id = $linkData['button_id'];
830+
$newLink->link = $linkData['link'];
831+
$newLink->title = $linkData['title'];
832+
$newLink->order = $linkData['order'];
833+
$newLink->click_number = $linkData['click_number'];
834+
$newLink->up_link = $linkData['up_link'];
835+
$newLink->custom_css = $linkData['custom_css'];
836+
$newLink->custom_icon = $linkData['custom_icon'];
837+
838+
// Set the user ID to the current user's ID
839+
$newLink->user_id = $user->id;
840+
841+
// Save the new link to the database
842+
$newLink->save();
843+
}
844+
845+
return redirect('studio/profile')->with('success', 'Profile updated successfully!');
846+
} catch (\Exception $e) {
847+
return redirect('studio/profile')->with('error', 'An error occurred while updating your profile.');
848+
}
849+
}
850+
744851
//Edit/save page icons
745852
public function editIcons(request $request)
746853
{

resources/views/studio/profile.blade.php

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22

33
@section('content')
44

5+
@if(session()->has('success'))
6+
<div class="alert alert-success">
7+
{{ session()->get('success') }}
8+
</div>
9+
@endif
10+
11+
@if(session()->has('error'))
12+
<div class="alert alert-danger">
13+
{{ session()->get('error') }}
14+
</div>
15+
@endif
16+
517
@if($_SERVER['QUERY_STRING'] === '')
618
<section class="shadow text-gray-400">
719
<h3 class="mb-4 card-header"><i class="bi bi-person"> Account Settings</i></h3>
820
<div class="card-body p-0 p-md-3">
921

1022
@foreach($profile as $profile)
1123

12-
{{-- <form action="{{ route('editProfile') }}" method="post">
13-
@csrf
14-
<div class="form-group col-lg-8">
15-
<h3>Name</h3>
16-
<input type="text" class="form-control" name="name" value="{{ $profile->name }}" required>
17-
</div>
18-
<button type="Change " class="mt-3 ml-3 btn btn-info">Change name</button>
19-
</form><br><br> --}}
20-
2124
@if(env('REGISTER_AUTH') != 'verified' or auth()->user()->role == 'admin')
2225
<form action="{{ route('editProfile') }}" method="post">
2326
@csrf
@@ -29,27 +32,38 @@
2932
</form>
3033
@endif
3134

32-
<br><br><form action="{{ route('editProfile') }}" method="post">
33-
@csrf
34-
<div class="form-group col-lg-8">
35-
<h4>Password</h4>
36-
<input type="password" name="password" class="form-control" placeholder="At least 8 characters" required>
37-
</div>
38-
<button type="Change " class="mt-3 ml-3 btn btn-info">Change password</button>
39-
</form>
40-
41-
@csrf
42-
<br><br><div class="form-group col-lg-8">
43-
<h4>Role</h4>
44-
<input type="text" class="form-control" value="{{ strtoupper($profile->role) }}" readonly>
45-
</div>
46-
<br><button class="mt-3 ml-3 btn btn-primary" style="margin-bottom:2rem;margin-top:2rem!important;background-color:tomato!important;border-color:tomato!important;"><a href="{{ url('/studio/profile/?delete')}}" style="color:#FFFFFF;"><i class="bi bi-exclamation-octagon-fill"></i> Delete your account</a></button>
35+
<br><br><br>
36+
37+
<div class="form-group col-lg-8">
38+
<h4>Export user data</h4>
39+
<label>Export your user data to transfer to a different instance.</label>
40+
<div class="row">
41+
<button class="mt-3 ml-3 btn btn-outline-secondary"><a href="{{ route('exportAll') }}" style="color:#fff;"><i class="bi bi-layer-backward"></i> Export all data</a></button>
42+
<button class="mt-3 ml-3 btn btn-outline-secondary"><a href="{{ route('exportLinks') }}" style="color:#fff;"><i class="bi bi-layer-backward"></i> Export links only</a></button>
43+
</div></div>
44+
45+
<form action="{{ route('importData') }}" enctype="multipart/form-data" method="post">
46+
@csrf
47+
<div class="form-group col-lg-8"><br><br><br>
48+
<h4>Import user data</h4>
49+
<label>Import your user data from another instance.</label>
50+
<input type="file" accept="application/JSON" class="form-control-file" name="import">
51+
</div>
52+
53+
<button type="submit" class="mt-3 ml-3 btn btn-info" onclick="return confirm('Are you sure you want to import this file? This action will replace all your current data, including links!')">Import</button>
54+
</form>
55+
56+
<br>
57+
58+
<br><button class="mt-3 ml-3 btn btn-primary"
59+
style="margin-bottom:2rem;margin-top:2rem!important;background-color:tomato!important;border-color:tomato!important;"><a
60+
href="{{ url('/studio/profile/?delete') }}" style="color:#FFFFFF;"><i class="bi bi-exclamation-octagon-fill"></i>
61+
Delete your account</a></button>
4762
</div>
4863
</section>
4964
@endforeach
5065
@endif
5166

52-
5367
@if($_SERVER['QUERY_STRING'] === 'delete')
5468
<center style="margin-top: 14%;">
5569
<h2 style="text-decoration: underline;">You are about to delete your account!</h2>

routes/web.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@
123123
Route::get('/clearIcon/{id}', [UserController::class, 'clearIcon'])->name('clearIcon');
124124
Route::get('/studio/page/delprofilepicture', [UserController::class, 'delProfilePicture'])->name('delProfilePicture');
125125
Route::get('/studio/delete-user/{id}', [UserController::class, 'deleteUser'])->name('deleteUser')->middleware('verified');
126+
Route::get('/export-links', [UserController::class, 'exportLinks'])->name('exportLinks');
127+
Route::get('/export-all', [UserController::class, 'exportAll'])->name('exportAll');
128+
Route::post('/import-data', [UserController::class, 'importData'])->name('importData');
126129
Route::get('/studio/linkparamform_part/{typeid}/{linkid}', [LinkTypeViewController::class, 'getParamForm'])->name('linkparamform.part');
127130
});
128131

0 commit comments

Comments
 (0)