63 lines
2.0 KiB
PHP
Executable File
63 lines
2.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Models\User;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class InfoIconController extends BaseApiController
|
|
{
|
|
/**
|
|
* Get user profile icon information (name and initials)
|
|
*/
|
|
public function profileIcon()
|
|
{
|
|
$userId = $this->getCurrentUserId();
|
|
|
|
if (!$userId) {
|
|
return $this->respondError('User not logged in', Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
|
|
try {
|
|
$user = model(User::class);
|
|
$user = $user->getUserInfoById($userId);
|
|
|
|
if (!$user) {
|
|
return $this->success([
|
|
'user_name' => 'User not found',
|
|
'user_initials' => '??'
|
|
], 'User info retrieved');
|
|
}
|
|
|
|
$firstname = (string) ($user['firstname'] ?? '');
|
|
$lastname = (string) ($user['lastname'] ?? '');
|
|
|
|
$userName = trim($firstname . ' ' . $lastname);
|
|
if ($userName === '') {
|
|
$userName = $user['email'] ?? 'User #' . $userId;
|
|
}
|
|
|
|
$userInitials = '??';
|
|
if (!empty($firstname) && !empty($lastname)) {
|
|
$userInitials = strtoupper(($firstname[0] ?? '') . ($lastname[0] ?? ''));
|
|
} elseif (!empty($firstname)) {
|
|
$userInitials = strtoupper(substr($firstname, 0, 2));
|
|
} elseif (!empty($lastname)) {
|
|
$userInitials = strtoupper(substr($lastname, 0, 2));
|
|
} elseif (!empty($user['email'])) {
|
|
$email = $user['email'];
|
|
$userInitials = strtoupper(substr($email, 0, 2));
|
|
}
|
|
|
|
return $this->success([
|
|
'user_name' => $userName,
|
|
'user_initials' => $userInitials
|
|
], 'Profile icon info retrieved successfully');
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Profile icon error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to retrieve profile icon info', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
}
|
|
|