Added option to import and export user data

This commit is contained in:
Julian Prieber 2023-03-08 17:20:39 +01:00
parent 3d277491e7
commit 4eca39e9ce
3 changed files with 149 additions and 25 deletions

View File

@ -741,6 +741,113 @@ class UserController extends Controller
return back(); 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 //Edit/save page icons
public function editIcons(request $request) public function editIcons(request $request)
{ {

View File

@ -2,6 +2,18 @@
@section('content') @section('content')
@if(session()->has('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
@endif
@if(session()->has('error'))
<div class="alert alert-danger">
{{ session()->get('error') }}
</div>
@endif
@if($_SERVER['QUERY_STRING'] === '') @if($_SERVER['QUERY_STRING'] === '')
<section class="shadow text-gray-400"> <section class="shadow text-gray-400">
<h3 class="mb-4 card-header"><i class="bi bi-person"> Account Settings</i></h3> <h3 class="mb-4 card-header"><i class="bi bi-person"> Account Settings</i></h3>
@ -9,15 +21,6 @@
@foreach($profile as $profile) @foreach($profile as $profile)
{{-- <form action="{{ route('editProfile') }}" method="post">
@csrf
<div class="form-group col-lg-8">
<h3>Name</h3>
<input type="text" class="form-control" name="name" value="{{ $profile->name }}" required>
</div>
<button type="Change " class="mt-3 ml-3 btn btn-info">Change name</button>
</form><br><br> --}}
@if(env('REGISTER_AUTH') != 'verified' or auth()->user()->role == 'admin') @if(env('REGISTER_AUTH') != 'verified' or auth()->user()->role == 'admin')
<form action="{{ route('editProfile') }}" method="post"> <form action="{{ route('editProfile') }}" method="post">
@csrf @csrf
@ -29,27 +32,38 @@
</form> </form>
@endif @endif
<br><br><form action="{{ route('editProfile') }}" method="post"> <br><br><br>
@csrf
<div class="form-group col-lg-8"> <div class="form-group col-lg-8">
<h4>Password</h4> <h4>Export user data</h4>
<input type="password" name="password" class="form-control" placeholder="At least 8 characters" required> <label>Export your user data to transfer to a different instance.</label>
<div class="row">
<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>
<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>
</div></div>
<form action="{{ route('importData') }}" enctype="multipart/form-data" method="post">
@csrf
<div class="form-group col-lg-8"><br><br><br>
<h4>Import user data</h4>
<label>Import your user data from another instance.</label>
<input type="file" accept="application/JSON" class="form-control-file" name="import">
</div> </div>
<button type="Change " class="mt-3 ml-3 btn btn-info">Change password</button>
<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>
</form> </form>
@csrf <br>
<br><br><div class="form-group col-lg-8">
<h4>Role</h4> <br><button class="mt-3 ml-3 btn btn-primary"
<input type="text" class="form-control" value="{{ strtoupper($profile->role) }}" readonly> style="margin-bottom:2rem;margin-top:2rem!important;background-color:tomato!important;border-color:tomato!important;"><a
</div> href="{{ url('/studio/profile/?delete') }}" style="color:#FFFFFF;"><i class="bi bi-exclamation-octagon-fill"></i>
<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> Delete your account</a></button>
</div> </div>
</section> </section>
@endforeach @endforeach
@endif @endif
@if($_SERVER['QUERY_STRING'] === 'delete') @if($_SERVER['QUERY_STRING'] === 'delete')
<center style="margin-top: 14%;"> <center style="margin-top: 14%;">
<h2 style="text-decoration: underline;">You are about to delete your account!</h2> <h2 style="text-decoration: underline;">You are about to delete your account!</h2>

View File

@ -123,6 +123,9 @@ Route::post('/edit-icons', [UserController::class, 'editIcons'])->name('editIcon
Route::get('/clearIcon/{id}', [UserController::class, 'clearIcon'])->name('clearIcon'); Route::get('/clearIcon/{id}', [UserController::class, 'clearIcon'])->name('clearIcon');
Route::get('/studio/page/delprofilepicture', [UserController::class, 'delProfilePicture'])->name('delProfilePicture'); 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('/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'); Route::get('/studio/linkparamform_part/{typeid}/{linkid}', [LinkTypeViewController::class, 'getParamForm'])->name('linkparamform.part');
}); });