From 18d2e2b78b15e1ab16e51cb7011a1a6743f2592d Mon Sep 17 00:00:00 2001 From: armiejean Date: Fri, 16 May 2025 14:53:05 +0800 Subject: [PATCH] terms-and-privacy functionality works --- .../Controllers/TermsAndPrivacyController.php | 329 +++++++++++++++ .../components/terms-component.blade.php | 373 ++++++++++++++++++ .../about us/terms-and-privacy.blade.php | 104 ++--- .../pages/terms-and-privacy/create.blade.php | 63 +++ .../pages/terms-and-privacy/edit.blade.php | 64 +++ .../pages/terms-and-privacy/show.blade.php | 31 ++ routes/web.php | 18 +- 7 files changed, 908 insertions(+), 74 deletions(-) create mode 100644 app/Http/Controllers/TermsAndPrivacyController.php create mode 100644 resources/views/components/terms-component.blade.php create mode 100644 resources/views/pages/terms-and-privacy/create.blade.php create mode 100644 resources/views/pages/terms-and-privacy/edit.blade.php create mode 100644 resources/views/pages/terms-and-privacy/show.blade.php diff --git a/app/Http/Controllers/TermsAndPrivacyController.php b/app/Http/Controllers/TermsAndPrivacyController.php new file mode 100644 index 0000000..0b9f17a --- /dev/null +++ b/app/Http/Controllers/TermsAndPrivacyController.php @@ -0,0 +1,329 @@ +route('login')->with('error', 'Please log in to view terms and privacy.'); + } + + $page = $request->input('page', 1); + $pageSize = 5; + $search = $request->input('_search', null); + + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $accessToken, + ])->get("{$this->apiBaseUrl}/TermsAndPrivacy", [ + 'page' => $page, + 'page_size' => $pageSize, + '_search' => $search, + ]); + + if ($response->status() === 401 || $response->status() === 403) { + Log::warning('Unauthorized or Forbidden API response: ', $response->json()); + return redirect()->route('login')->with('error', 'Your session has expired. Please log in again.'); + } + + $json = $response->json(); + + if ($response->successful() && isset($json['data']) && is_array($json['data'])) { + $termsAndPrivacy = array_map(function ($item) { + return [ + 'id' => $item['tp_uuid'] ?? null, + 'title' => $item['title'] ?? '', + 'details' => $item['details'] ?? '', + 'type' => $item['type'] == 1 ? 'Terms' : ($item['type'] == 2 ? 'Privacy' : 'Unknown'), + ]; + }, $json['data']); + + $total = $json['meta']['total'] ?? count($termsAndPrivacy); + $lastPage = $json['meta']['last_page'] ?? ceil($total / $pageSize); + } else { + Log::warning('No terms and privacy data found or invalid API response: ', $json); + $termsAndPrivacy = []; + $total = 0; + $lastPage = 1; + } + + return view('pages.about us.terms-and-privacy', [ + 'termsAndPrivacy' => $termsAndPrivacy, + 'currentPage' => $page, + 'lastPage' => $lastPage, + 'total' => $total, + 'search' => $search, + ]); + } catch (\Exception $e) { + Log::error('Error fetching terms and privacy: ' . $e->getMessage()); + return view('pages.about us.terms-and-privacy', [ + 'termsAndPrivacy' => [], + 'currentPage' => 1, + 'lastPage' => 1, + 'total' => 0, + 'search' => $search, + ]); + } + } + + public function create() + { + try { + $user = Session::get('user'); + $accessToken = $user['access_token'] ?? null; + + if (!$accessToken) { + return redirect()->route('login')->with('error', 'Please log in to add terms or privacy.'); + } + + return view('pages.terms-and-privacy.create', ['types' => ['1' => 'Terms', '2' => 'Privacy']]); + } catch (\Exception $e) { + Log::error('Error loading create terms and privacy page: ' . $e->getMessage()); + return view('pages.terms-and-privacy.create', ['types' => ['1' => 'Terms', '2' => 'Privacy']]); + } + } + + public function store(Request $request) + { + try { + $user = Session::get('user'); + $accessToken = $user['access_token'] ?? null; + + if (!$accessToken) { + return redirect()->route('login')->with('error', 'Please log in to add terms or privacy.'); + } + + $request->validate([ + 'title' => 'required|string|max:255', + 'details' => 'required|string', + 'type' => 'required|in:1,2', + ]); + + $payload = [ + 'title' => $request->input('title'), + 'details' => $request->input('details'), + 'type' => (int) $request->input('type'), + ]; + + Log::info('Data being sent to API for creating terms or privacy: ', $payload); + + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $accessToken, + ])->post("{$this->apiBaseUrl}/TermsAndPrivacy", $payload); + + $json = $response->json(); + Log::info('API response for creating terms or privacy: ', $json); + + if ($response->successful()) { + Log::info('Terms or privacy created successfully: ', $json); + return redirect()->route('terms-and-privacy')->with('success', $json['message'] ?? 'Terms or privacy added successfully.'); + } else { + $errorMessage = $json['message'] ?? 'Failed to add terms or privacy. Please try again.'; + if (isset($json['errors'])) { + $errorMessage .= ' Errors: ' . json_encode($json['errors']); + } + throw new \Exception($errorMessage); + } + } catch (\Exception $e) { + Log::error('Error creating terms or privacy: ' . $e->getMessage()); + return redirect()->back()->with('error', 'An error occurred while adding the terms or privacy: ' . $e->getMessage()); + } + } + + public function show($uuid) + { + try { + $user = Session::get('user'); + $accessToken = $user['access_token'] ?? null; + + if (!$accessToken) { + return redirect()->route('login')->with('error', 'Please log in to view terms or privacy.'); + } + + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $accessToken, + ])->get("{$this->apiBaseUrl}/TermsAndPrivacy/{$uuid}"); + + $json = $response->json(); + + if ($response->successful() && isset($json['data'])) { + $item = [ + 'id' => $json['data']['tp_uuid'] ?? null, + 'title' => $json['data']['title'] ?? '', + 'details' => $json['data']['details'] ?? '', + 'type' => $json['data']['type'] == 1 ? 'Terms' : ($json['data']['type'] == 2 ? 'Privacy' : 'Unknown'), + ]; + return view('pages.terms-and-privacy.show', ['item' => $item]); + } else { + Log::warning('No terms or privacy found or invalid API response: ', $json); + return redirect()->back()->with('error', 'Terms or privacy not found.'); + } + } catch (\Exception $e) { + Log::error('Error fetching terms or privacy for view: ' . $e->getMessage()); + return redirect()->back()->with('error', 'An error occurred while loading the terms or privacy.'); + } + } + + public function edit($uuid) + { + try { + $user = Session::get('user'); + $accessToken = $user['access_token'] ?? null; + + if (!$accessToken) { + return redirect()->route('login')->with('error', 'Please log in to edit terms or privacy.'); + } + + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $accessToken, + ])->get("{$this->apiBaseUrl}/TermsAndPrivacy/{$uuid}"); + + $json = $response->json(); + + if ($response->successful() && isset($json['data'])) { + $item = [ + 'id' => $json['data']['tp_uuid'] ?? null, + 'title' => $json['data']['title'] ?? '', + 'details' => $json['data']['details'] ?? '', + 'type' => $json['data']['type'] ?? '', + ]; + return view('pages.terms-and-privacy.edit', ['item' => $item, 'types' => ['1' => 'Terms', '2' => 'Privacy']]); + } else { + Log::warning('No terms or privacy found or invalid API response: ', $json); + return redirect()->back()->with('error', 'Terms or privacy not found.'); + } + } catch (\Exception $e) { + Log::error('Error fetching terms or privacy for edit: ' . $e->getMessage()); + return redirect()->back()->with('error', 'An error occurred while loading the terms or privacy.'); + } + } + + public function update(Request $request, $uuid) + { + try { + $user = Session::get('user'); + $accessToken = $user['access_token'] ?? null; + + if (!$accessToken) { + return redirect()->route('login')->with('error', 'Please log in to update terms or privacy.'); + } + + $request->validate([ + 'title' => 'required|string|max:255', + 'details' => 'required|string', + 'type' => 'required|in:1,2', + ]); + + $payload = [ + 'title' => $request->input('title'), + 'details' => $request->input('details'), + 'type' => (int) $request->input('type'), + ]; + + Log::info('Data being sent to API for updating terms or privacy: ', $payload); + + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $accessToken, + ])->put("{$this->apiBaseUrl}/TermsAndPrivacy/{$uuid}", $payload); + + $json = $response->json(); + Log::info('API response for updating terms or privacy: ', $json); + + if ($response->successful()) { + Log::info('Terms or privacy updated successfully: ', $json); + return redirect()->route('terms-and-privacy')->with('success', $json['message'] ?? 'Terms or privacy updated successfully.'); + } else { + $errorMessage = $json['message'] ?? 'Failed to update terms or privacy. Please try again.'; + if (isset($json['errors'])) { + $errorMessage .= ' Errors: ' . json_encode($json['errors']); + } + return redirect()->back()->with('error', $errorMessage); + } + } catch (\Exception $e) { + Log::error('Error updating terms or privacy: ' . $e->getMessage()); + return redirect()->back()->with('error', 'An error occurred while updating the terms or privacy: ' . $e->getMessage()); + } + } + + public function destroy($uuid) + { + try { + $user = Session::get('user'); + $accessToken = $user['access_token'] ?? null; + + if (!$accessToken) { + return redirect()->route('login')->with('error', 'Please log in to delete terms or privacy.'); + } + + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $accessToken, + ])->delete("{$this->apiBaseUrl}/TermsAndPrivacy/{$uuid}"); + + if ($response->successful()) { + Log::info('Terms or privacy deleted successfully: ' . $uuid); + return redirect()->route('terms-and-privacy')->with('success', 'Terms or privacy deleted successfully.'); + } else { + Log::warning('Failed to delete terms or privacy: ', $response->json()); + return redirect()->back()->with('error', $response->json()['message'] ?? 'Failed to delete terms or privacy. Please try again.'); + } + } catch (\Exception $e) { + Log::error('Error deleting terms or privacy: ' . $e->getMessage()); + return redirect()->back()->with('error', 'An error occurred while deleting the terms or privacy.'); + } + } + + public function batchDelete(Request $request) + { + try { + $user = Session::get('user'); + $accessToken = $user['access_token'] ?? null; + + if (!$accessToken) { + return redirect()->route('login')->with('error', 'Please log in to delete terms or privacy.'); + } + + $uuids = $request->input('tp_uuid', []); + + if (empty($uuids)) { + return redirect()->back()->with('error', 'No terms or privacy selected for deletion.'); + } + + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $accessToken, + ])->delete("{$this->apiBaseUrl}/TermsAndPrivacyBatchDelete", [ + 'tp_uuid' => $uuids, + ]); + + if ($response->successful()) { + Log::info('Terms or privacy batch deleted successfully: ', $uuids); + return redirect()->route('terms-and-privacy')->with('success', 'Selected terms or privacy deleted successfully.'); + } else { + Log::warning('Failed to batch delete terms or privacy: ', $response->json()); + return redirect()->back()->with('error', $response->json()['message'] ?? 'Failed to delete terms or privacy. Please try again.'); + } + } catch (\Exception $e) { + Log::error('Error batch deleting terms or privacy: ' . $e->getMessage()); + return redirect()->back()->with('error', 'An error occurred while deleting the terms or privacy.'); + } + } +} \ No newline at end of file diff --git a/resources/views/components/terms-component.blade.php b/resources/views/components/terms-component.blade.php new file mode 100644 index 0000000..62bb3a7 --- /dev/null +++ b/resources/views/components/terms-component.blade.php @@ -0,0 +1,373 @@ +@props([ + 'pageTitle' => '', + 'data' => [], + 'columns' => [], + 'actions' => [], + 'showAddButton' => false, + 'addButtonUrl' => '#', + 'showCheckboxes' => false, + 'showBatchDelete' => false, + 'showEditModal' => false, + 'showViewModal' => false, + 'currentPage' => 1, + 'lastPage' => 1, + 'total' => 0, +]) + +
+
+
{{ $pageTitle }}
+ @if ($showAddButton) + + Add {{ $pageTitle }} + + @endif +
+
+
+ +
+
+
+ + + + +
+
+
+ +
+
+ + +
+ + + + @if ($showCheckboxes) + + @endif + @foreach ($columns as $index => $column) + + @endforeach + @if (!empty($actions)) + + @endif + + + + @foreach ($data as $row) + + @if ($showCheckboxes) + + @endif + @foreach ($columns as $column) + + @endforeach + @if (!empty($actions)) + + @endif + + @endforeach + +
+ + + {{ $column['name'] }} + @if ($column['sortable']) + + @endif + Action
+ + {{ $row[$column['key']] ?? '' }} + @if (in_array('view', $actions)) + + + + @endif + @if (in_array('edit', $actions)) + + + + @endif + @if (in_array('delete', $actions)) +
+ @csrf + @method('DELETE') + + + +
+ @endif +
+
+ + +
+ @if ($showBatchDelete) +
+
+ @csrf + + +
+
+ @endif + +
+
+ + + + \ No newline at end of file diff --git a/resources/views/pages/about us/terms-and-privacy.blade.php b/resources/views/pages/about us/terms-and-privacy.blade.php index a4cb93e..417527f 100644 --- a/resources/views/pages/about us/terms-and-privacy.blade.php +++ b/resources/views/pages/about us/terms-and-privacy.blade.php @@ -3,74 +3,40 @@ @section('page_title', 'Terms and Privacy') @section('content') - @php - $termsAndPrivacy = [ - [ - 'id' => 1, - 'title' => 'User Agreement', - 'details' => 'Users must agree to use the platform responsibly and comply with all applicable laws.', - 'type' => 'Terms' +
+ @if (session('success')) + + @endif + @if (session('error')) + + @endif + @include('components.terms-component', [ + 'pageTitle' => 'Terms and Privacy', + 'data' => $termsAndPrivacy ?? [], + 'columns' => [ + ['name' => 'Title', 'key' => 'title', 'sortable' => true], + ['name' => 'Details', 'key' => 'details', 'sortable' => true], + ['name' => 'Type', 'key' => 'type', 'sortable' => true], ], - [ - 'id' => 2, - 'title' => 'Privacy Policy', - 'details' => 'We collect personal data to improve services, with consent, and protect user information.', - 'type' => 'Privacy' - ], - [ - 'id' => 3, - 'title' => 'Service Terms', - 'details' => 'Details on how services are provided, including limitations and user obligations.', - 'type' => 'Terms' - ], - [ - 'id' => 4, - 'title' => 'Data Protection', - 'details' => 'Our commitment to GDPR compliance and secure data handling practices.', - 'type' => 'Privacy' - ], - [ - 'id' => 5, - 'title' => 'Refund Policy', - 'details' => 'Conditions under which refunds are issued for services or subscriptions.', - 'type' => 'Terms' - ], - [ - 'id' => 6, - 'title' => 'Cookie Policy', - 'details' => 'How we use cookies to enhance user experience and track site usage.', - 'type' => 'Privacy' - ] - ]; - @endphp - - @include('components.table-component', [ - 'pageTitle' => 'Terms and Privacy', - 'data' => $termsAndPrivacy, - 'columns' => [ - ['name' => 'Title', 'key' => 'title', 'sortable' => true], - ['name' => 'Details', 'key' => 'details', 'sortable' => true], - ['name' => 'Type', 'key' => 'type', 'sortable' => true] - ], - 'allFields' => [ - ['name' => 'Title', 'key' => 'title', 'type' => 'text', 'required' => true], - ['name' => 'Details', 'key' => 'details', 'type' => 'textarea', 'required' => true] - ], - 'actions' => ['edit', 'view', 'delete'], - 'showAddButton' => true, - 'addButtonUrl' => '/add-terms-and-privacy', - 'showCheckboxes' => true, - 'showBatchDelete' => true, - 'showEditModal' => true, - 'showViewModal' => true - ]) - - + 'actions' => ['edit', 'view', 'delete'], + 'showAddButton' => true, + 'addButtonUrl' => route('terms-and-privacy.create'), + 'showCheckboxes' => true, + 'showBatchDelete' => true, + 'showEditModal' => false, + 'showViewModal' => false, + 'currentPage' => $currentPage ?? 1, + 'lastPage' => $lastPage ?? 1, + 'total' => $total ?? 0, + ]) +
+

No terms or privacy records found.

+
+
@endsection \ No newline at end of file diff --git a/resources/views/pages/terms-and-privacy/create.blade.php b/resources/views/pages/terms-and-privacy/create.blade.php new file mode 100644 index 0000000..8c69c58 --- /dev/null +++ b/resources/views/pages/terms-and-privacy/create.blade.php @@ -0,0 +1,63 @@ +@extends('layouts.app') + +@section('page_title', 'Add Terms or Privacy') + +@section('content') +
+

Add Terms or Privacy

+ @if (session('success')) + + @endif + @if (session('error')) + + @endif + @if ($errors->any()) + + @endif + +
+ @csrf +
+ + + @error('title') +
{{ $message }}
+ @enderror +
+
+ + + @error('details') +
{{ $message }}
+ @enderror +
+
+ + + @error('type') +
{{ $message }}
+ @enderror +
+ + Cancel +
+
+@endsection \ No newline at end of file diff --git a/resources/views/pages/terms-and-privacy/edit.blade.php b/resources/views/pages/terms-and-privacy/edit.blade.php new file mode 100644 index 0000000..0a62c12 --- /dev/null +++ b/resources/views/pages/terms-and-privacy/edit.blade.php @@ -0,0 +1,64 @@ +@extends('layouts.app') + +@section('page_title', 'Edit Terms or Privacy') + +@section('content') +
+

Edit Terms or Privacy

+ @if (session('success')) + + @endif + @if (session('error')) + + @endif + @if ($errors->any()) + + @endif + +
+ @csrf + @method('PUT') +
+ + + @error('title') +
{{ $message }}
+ @enderror +
+
+ + + @error('details') +
{{ $message }}
+ @enderror +
+
+ + + @error('type') +
{{ $message }}
+ @enderror +
+ + Cancel +
+
+@endsection \ No newline at end of file diff --git a/resources/views/pages/terms-and-privacy/show.blade.php b/resources/views/pages/terms-and-privacy/show.blade.php new file mode 100644 index 0000000..d3c1761 --- /dev/null +++ b/resources/views/pages/terms-and-privacy/show.blade.php @@ -0,0 +1,31 @@ +@extends('layouts.app') + +@section('page_title', 'View Terms or Privacy') + +@section('content') +
+

View Terms or Privacy

+ @if (session('success')) + + @endif + @if (session('error')) + + @endif + +
+
+
{{ $item['title'] }}
+

Details: {{ $item['details'] }}

+

Type: {{ $item['type'] }}

+ Back + Edit +
+
+
+@endsection \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index f020678..8ef1c0c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -9,7 +9,7 @@ use App\Http\Controllers\TopUpController; use App\Http\Controllers\NotificationController; use App\Http\Controllers\CardMemberController; use App\Http\Controllers\PromotionController; - +use App\Http\Controllers\TermsAndPrivacyController; Route::get('/', function () { @@ -45,9 +45,7 @@ Route::get('/card-types', function () { return view('pages.about us.card-types'); })->name('card-types'); -Route::get('/terms-and-privacy', function () { - return view('pages.about us.terms-and-privacy'); -})->name('terms-and-privacy'); + Route::get('/registration-report', function () { return view('pages.reports.registration-report'); @@ -195,4 +193,14 @@ Route::get('/promotions/{uuid}', [PromotionController::class, 'show'])->name('pr Route::get('/promotions/{uuid}/edit', [PromotionController::class, 'edit'])->name('promotions.edit'); Route::put('/promotions/{uuid}', [PromotionController::class, 'update'])->name('promotions.update'); Route::delete('/promotions/{uuid}', [PromotionController::class, 'destroy'])->name('promotions.destroy'); -Route::post('/promotions/batch-delete', [PromotionController::class, 'batchDelete'])->name('promotions.batch-delete'); \ No newline at end of file +Route::post('/promotions/batch-delete', [PromotionController::class, 'batchDelete'])->name('promotions.batch-delete'); + +//Terms-and-Privacy +Route::get('/terms-and-privacy', [TermsAndPrivacyController::class, 'index'])->name('terms-and-privacy'); +Route::get('/terms-and-privacy/create', [TermsAndPrivacyController::class, 'create'])->name('terms-and-privacy.create'); +Route::post('/terms-and-privacy', [TermsAndPrivacyController::class, 'store'])->name('terms-and-privacy.store'); +Route::get('/terms-and-privacy/{uuid}', [TermsAndPrivacyController::class, 'show'])->name('terms-and-privacy.show'); +Route::get('/terms-and-privacy/{uuid}/edit', [TermsAndPrivacyController::class, 'edit'])->name('terms-and-privacy.edit'); +Route::put('/terms-and-privacy/{uuid}', [TermsAndPrivacyController::class, 'update'])->name('terms-and-privacy.update'); +Route::delete('/terms-and-privacy/{uuid}', [TermsAndPrivacyController::class, 'destroy'])->name('terms-and-privacy.destroy'); +Route::delete('/terms-and-privacy/batch-delete', [TermsAndPrivacyController::class, 'batchDelete'])->name('terms-and-privacy.batchDelete'); \ No newline at end of file