Files
My-KOPKB/be/app/Http/Controllers/ContactController.php
T
ISMAIL MASSERAN 94ecbe5887 first init
2026-06-08 11:37:14 +08:00

210 lines
6.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Services\ContactService;
use Illuminate\Support\Facades\DB;
class ContactController extends Controller
{
protected $contactService;
public function __construct(ContactService $contactService)
{
$this->contactService = $contactService;
}
/**
* Get all active contact persons
*/
public function getContacts(): JsonResponse
{
try {
$contacts = $this->contactService->getActiveContacts();
return response()->json([
'success' => true,
'data' => $contacts,
'message' => 'Contact persons retrieved successfully'
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'Failed to retrieve contact persons',
'error' => $e->getMessage()
], 500);
}
}
/**
* Get contact settings for admin (including inactive)
*/
public function getContactSettings(): JsonResponse
{
try {
$settings = $this->contactService->getAllContactSettings();
return response()->json([
'success' => true,
'data' => $settings,
'message' => 'Contact settings retrieved successfully'
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'Failed to retrieve contact settings',
'error' => $e->getMessage()
], 500);
}
}
/**
* Update contact settings (Admin only)
*/
public function updateContacts(Request $request): JsonResponse
{
$request->validate([
'contacts' => 'required|array',
'contacts.*.name' => 'required|string|max:100',
'contacts.*.position' => 'nullable|string|max:100',
'contacts.*.email' => 'nullable|email|max:255',
'contacts.*.phone' => 'nullable|string|max:50',
'contacts.*.department' => 'nullable|string|max:100',
'contacts.*.is_active' => 'boolean',
'contacts.*.sort_order' => 'integer|min:0'
]);
try {
DB::beginTransaction();
$result = $this->contactService->updateContactSettings($request->contacts);
DB::commit();
return response()->json([
'success' => true,
'data' => $result,
'message' => 'Contact settings updated successfully'
]);
} catch (\Exception $e) {
DB::rollBack();
return response()->json([
'success' => false,
'message' => 'Failed to update contact settings',
'error' => $e->getMessage()
], 500);
}
}
/**
* Add a new contact person (Admin only)
*/
public function addContact(Request $request): JsonResponse
{
$request->validate([
'name' => 'required|string|max:100',
'position' => 'nullable|string|max:100',
'email' => 'nullable|email|max:255',
'phone' => 'nullable|string|max:50',
'department' => 'nullable|string|max:100',
'is_active' => 'boolean',
'sort_order' => 'integer|min:0'
]);
try {
$result = $this->contactService->addContactPerson($request->all());
return response()->json([
'success' => true,
'data' => $result,
'message' => 'Contact person added successfully'
], 201);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'Failed to add contact person',
'error' => $e->getMessage()
], 500);
}
}
/**
* Update a specific contact person (Admin only)
*/
public function updateContact(Request $request, int $id): JsonResponse
{
$request->validate([
'name' => 'required|string|max:100',
'position' => 'nullable|string|max:100',
'email' => 'nullable|email|max:255',
'phone' => 'nullable|string|max:50',
'department' => 'nullable|string|max:100',
'is_active' => 'boolean',
'sort_order' => 'integer|min:0'
]);
try {
$result = $this->contactService->updateContactPerson($id, $request->all());
return response()->json([
'success' => true,
'data' => $result,
'message' => 'Contact person updated successfully'
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'Failed to update contact person',
'error' => $e->getMessage()
], 500);
}
}
/**
* Delete a contact person (Admin only)
*/
public function deleteContact(int $id): JsonResponse
{
try {
$result = $this->contactService->deleteContactPerson($id);
return response()->json([
'success' => true,
'data' => $result,
'message' => 'Contact person deleted successfully'
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'Failed to delete contact person',
'error' => $e->getMessage()
], 500);
}
}
/**
* Toggle active status of a contact person (Admin only)
*/
public function toggleContact(int $id): JsonResponse
{
try {
$result = $this->contactService->toggleContactPerson($id);
return response()->json([
'success' => true,
'data' => $result,
'message' => 'Contact person status toggled successfully'
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'Failed to toggle contact person status',
'error' => $e->getMessage()
], 500);
}
}
}