115 lines
4.3 KiB
PHP
Executable File
115 lines
4.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Models\Preferences;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class PreferencesController extends BaseApiController
|
|
{
|
|
protected Preferences $preferences;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->preferences = model(Preferences::class);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$user = $this->getCurrentUser();
|
|
if (!$user) {
|
|
return $this->respondError('Authentication required', Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
try {
|
|
$preferences = $this->preferences->newQuery()
|
|
->where('user_id', $user->id)
|
|
->first();
|
|
|
|
if (!$preferences) {
|
|
$preferences = [
|
|
'notification_email' => 1,
|
|
'notification_sms' => 1,
|
|
'theme' => 'light',
|
|
'language' => 'en',
|
|
];
|
|
}
|
|
|
|
$styleConfig = config('Style');
|
|
$preferences['style_color'] = session()->get('style_color') ?? ($styleConfig->defaultStyle ?? 'blue');
|
|
$preferences['menu_color'] = session()->get('menu_color') ?? ($styleConfig->defaultMenu ?? 'white');
|
|
|
|
return $this->success($preferences, 'Preferences retrieved successfully');
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Preferences retrieval error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to retrieve preferences', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
|
|
public function update($id = null)
|
|
{
|
|
$user = $this->getCurrentUser();
|
|
if (!$user) {
|
|
return $this->respondError('Authentication required', Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
$data = $this->payloadData();
|
|
if (empty($data)) {
|
|
return $this->error('Invalid request data', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$styleConfig = config('Style');
|
|
$styleOptions = implode(',', array_keys($styleConfig->stylePalettes ?? []));
|
|
$menuOptions = implode(',', array_keys($styleConfig->menuPalettes ?? []));
|
|
|
|
$rules = [
|
|
'notification_email' => 'permit_empty|in_list[0,1]',
|
|
'notification_sms' => 'permit_empty|in_list[0,1]',
|
|
'theme' => 'permit_empty|in_list[light,dark]',
|
|
'language' => 'permit_empty|in_list[en,fr,es]',
|
|
'style_color' => $styleOptions ? 'permit_empty|in_list[' . $styleOptions . ']' : 'permit_empty',
|
|
'menu_color' => $menuOptions ? 'permit_empty|in_list[' . $menuOptions . ']' : 'permit_empty',
|
|
];
|
|
|
|
$errors = $this->validateRequest($data, $rules);
|
|
if (!empty($errors)) {
|
|
return $this->respondValidationError($errors);
|
|
}
|
|
|
|
try {
|
|
$existing = $this->preferences->newQuery()
|
|
->where('user_id', $user->id)
|
|
->first();
|
|
|
|
$prefData = [
|
|
'user_id' => $user->id,
|
|
'notification_email' => $data['notification_email'] ?? 1,
|
|
'notification_sms' => $data['notification_sms'] ?? 1,
|
|
'theme' => $data['theme'] ?? 'light',
|
|
'language' => $data['language'] ?? 'en',
|
|
];
|
|
|
|
if ($existing) {
|
|
$this->preferences->update($existing['id'], $prefData);
|
|
$preferences = $this->preferences->find($existing['id']);
|
|
} else {
|
|
$pref = $this->preferences->create($prefData);
|
|
$preferences = $pref instanceof \Illuminate\Database\Eloquent\Model ? $pref->toArray() : $pref;
|
|
}
|
|
|
|
if (isset($data['style_color'])) {
|
|
session()->put('style_color', $data['style_color']);
|
|
}
|
|
if (isset($data['menu_color'])) {
|
|
session()->put('menu_color', $data['menu_color']);
|
|
}
|
|
|
|
return $this->success($preferences, 'Preferences updated successfully');
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Preferences update error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to update preferences', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
}
|