| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/project-slim/app/Controllers/ |
Upload File : |
<?php
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Services\UserService;
use App\Services\PublicIdService;
class UserController extends BaseController
{
private UserService $userService;
private PublicIdService $publicIdService;
public function __construct()
{
$this->userService = $this->getService(UserService::class);
$this->publicIdService = $this->getService(PublicIdService::class);
}
/**
* Get all users
*/
public function index(Request $request, Response $response): Response
{
try {
$pagination = $this->getPaginationParams($request);
$filters = $this->getFilterParams($request, [
'company_id', 'is_active', 'is_verified', 'role', 'search'
]);
$users = $this->userService->getUsers(
$pagination['page'],
$pagination['limit'],
$filters
);
return $this->successResponse($response, $this->transformPaginatedResults($users));
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Create new user
*/
public function create(Request $request, Response $response): Response
{
try {
$data = $request->getParsedBody();
$user = $this->userService->createUser($data);
return $this->successResponse($response, $user->toArray(), 'User created successfully', 201);
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Store new user (alias for create)
*/
public function store(Request $request, Response $response): Response
{
return $this->create($request, $response);
}
/**
* Get user by public ID
*/
public function show(Request $request, Response $response, array $args): Response
{
try {
$publicId = $args['id'];
$internalId = $this->publicIdService->getInternalId($publicId, 'User');
if (!$internalId) {
return $this->notFoundResponse($response, 'User not found');
}
$user = $this->userService->getUserById($internalId);
if (!$user) {
return $this->notFoundResponse($response, 'User not found');
}
return $this->successResponse($response, $user->toArray());
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Update user by public ID
*/
public function update(Request $request, Response $response, array $args): Response
{
try {
$publicId = $args['id'];
$internalId = $this->publicIdService->getInternalId($publicId, 'User');
if (!$internalId) {
return $this->notFoundResponse($response, 'User not found');
}
$data = $request->getParsedBody();
$user = $this->userService->updateUser($internalId, $data);
if (!$user) {
return $this->notFoundResponse($response, 'User not found');
}
return $this->successResponse($response, $user->toArray(), 'User updated successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Delete user by public ID
*/
public function delete(Request $request, Response $response, array $args): Response
{
try {
$publicId = $args['id'];
$internalId = $this->publicIdService->getInternalId($publicId, 'User');
if (!$internalId) {
return $this->notFoundResponse($response, 'User not found');
}
$deleted = $this->userService->deleteUser($internalId);
if (!$deleted) {
return $this->notFoundResponse($response, 'User not found');
}
return $this->successResponse($response, null, 'User deleted successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Delete user (alias for delete)
*/
public function destroy(Request $request, Response $response, array $args): Response
{
return $this->delete($request, $response, $args);
}
/**
* Get user profile (self)
*/
public function profile(Request $request, Response $response): Response
{
try {
$userEmail = $this->getUserEmail($request);
if (!$userEmail) {
return $this->unauthorizedResponse($response);
}
$user = $this->userService->getUserByEmail($userEmail);
if (!$user) {
return $this->notFoundResponse($response, 'User not found');
}
return $this->successResponse($response, $user->toArray());
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Get user profile (alternative method name)
*/
public function getProfile(Request $request, Response $response): Response
{
return $this->profile($request, $response);
}
/**
* Update user profile (self)
*/
public function updateProfile(Request $request, Response $response): Response
{
try {
$userEmail = $this->getUserEmail($request);
if (!$userEmail) {
return $this->unauthorizedResponse($response);
}
$data = $request->getParsedBody();
$user = $this->userService->updateProfileByEmail($userEmail, $data);
if (!$user) {
return $this->notFoundResponse($response, 'User not found');
}
return $this->successResponse($response, $user->toArray(), 'Profile updated successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Update user password by public ID
*/
public function updatePassword(Request $request, Response $response, array $args): Response
{
try {
$publicId = $args['id'];
$internalId = $this->publicIdService->getInternalId($publicId, 'User');
if (!$internalId) {
return $this->notFoundResponse($response, 'User not found');
}
$data = $request->getParsedBody();
if (!isset($data['password'])) {
return $this->validationErrorResponse($response, ['password' => 'Password is required']);
}
$success = $this->userService->updatePassword($internalId, $data['password']);
if (!$success) {
return $this->notFoundResponse($response, 'User not found');
}
return $this->successResponse($response, null, 'Password updated successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Update user status by public ID
*/
public function updateStatus(Request $request, Response $response, array $args): Response
{
try {
$publicId = $args['id'];
$internalId = $this->publicIdService->getInternalId($publicId, 'User');
if (!$internalId) {
return $this->notFoundResponse($response, 'User not found');
}
$data = $request->getParsedBody();
if (!isset($data['status'])) {
return $this->validationErrorResponse($response, ['status' => 'Status is required']);
}
$isActive = $data['status'] === 'active';
$success = $isActive
? $this->userService->activateUser($internalId)
: $this->userService->deactivateUser($internalId);
if (!$success) {
return $this->notFoundResponse($response, 'User not found');
}
$status = $isActive ? 'activated' : 'deactivated';
return $this->successResponse($response, null, "User {$status} successfully");
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Activate user by public ID
*/
public function activate(Request $request, Response $response, array $args): Response
{
try {
$publicId = $args['id'];
$internalId = $this->publicIdService->getInternalId($publicId, 'User');
if (!$internalId) {
return $this->notFoundResponse($response, 'User not found');
}
$success = $this->userService->activateUser($internalId);
if (!$success) {
return $this->notFoundResponse($response, 'User not found');
}
return $this->successResponse($response, null, 'User activated successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Deactivate user by public ID
*/
public function deactivate(Request $request, Response $response, array $args): Response
{
try {
$publicId = $args['id'];
$internalId = $this->publicIdService->getInternalId($publicId, 'User');
if (!$internalId) {
return $this->notFoundResponse($response, 'User not found');
}
$success = $this->userService->deactivateUser($internalId);
if (!$success) {
return $this->notFoundResponse($response, 'User not found');
}
return $this->successResponse($response, null, 'User deactivated successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Search users
*/
public function search(Request $request, Response $response): Response
{
try {
$searchTerm = $this->getSearchTerm($request);
if (!$searchTerm) {
return $this->validationErrorResponse($response, ['search' => 'Search term is required']);
}
$pagination = $this->getPaginationParams($request);
$users = $this->userService->searchUsers($searchTerm, $pagination['page'], $pagination['limit']);
return $this->successResponse($response, $this->transformPaginatedResults($users));
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Filter users
*/
public function filter(Request $request, Response $response): Response
{
try {
$pagination = $this->getPaginationParams($request);
$filters = $this->getFilterParams($request, [
'company_id', 'is_active', 'is_verified', 'role'
]);
if (empty($filters)) {
return $this->validationErrorResponse($response, ['filters' => 'At least one filter is required']);
}
$users = $this->userService->getUsers($pagination['page'], $pagination['limit'], $filters);
return $this->successResponse($response, $this->transformPaginatedResults($users));
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Bulk activate users
*/
public function bulkActivate(Request $request, Response $response): Response
{
try {
$data = $request->getParsedBody();
if (!isset($data['user_ids']) || !is_array($data['user_ids'])) {
return $this->validationErrorResponse($response, ['user_ids' => 'User IDs array is required']);
}
$count = $this->userService->bulkActivate($data['user_ids']);
return $this->successResponse($response, ['activated_count' => $count], 'Users activated successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Bulk deactivate users
*/
public function bulkDeactivate(Request $request, Response $response): Response
{
try {
$data = $request->getParsedBody();
if (!isset($data['user_ids']) || !is_array($data['user_ids'])) {
return $this->validationErrorResponse($response, ['user_ids' => 'User IDs array is required']);
}
$count = $this->userService->bulkDeactivate($data['user_ids']);
return $this->successResponse($response, ['deactivated_count' => $count], 'Users deactivated successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Bulk delete users
*/
public function bulkDelete(Request $request, Response $response): Response
{
try {
$data = $request->getParsedBody();
if (!isset($data['user_ids']) || !is_array($data['user_ids'])) {
return $this->validationErrorResponse($response, ['user_ids' => 'User IDs array is required']);
}
$count = $this->userService->bulkDelete($data['user_ids']);
return $this->successResponse($response, ['deleted_count' => $count], 'Users deleted successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Get user statistics
*/
public function getStatistics(Request $request, Response $response): Response
{
try {
$stats = $this->userService->getUserStatistics();
return $this->successResponse($response, $stats);
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Export users to CSV
*/
public function exportCsv(Request $request, Response $response): Response
{
try {
// TODO: Implement CSV export
return $this->successResponse($response, ['message' => 'CSV export feature coming soon']);
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Export users to PDF
*/
public function exportPdf(Request $request, Response $response): Response
{
try {
// TODO: Implement PDF export
return $this->successResponse($response, ['message' => 'PDF export feature coming soon']);
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Upload user avatar
*/
public function uploadAvatar(Request $request, Response $response): Response
{
try {
// TODO: Implement avatar upload
return $this->successResponse($response, ['message' => 'Avatar upload feature coming soon']);
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Delete user avatar
*/
public function deleteAvatar(Request $request, Response $response): Response
{
try {
// TODO: Implement avatar deletion
return $this->successResponse($response, ['message' => 'Avatar deletion feature coming soon']);
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
}