diff --git a/www/src/Controller/UserController.php b/www/src/Controller/UserController.php index 68e55cd..13db2a0 100644 --- a/www/src/Controller/UserController.php +++ b/www/src/Controller/UserController.php @@ -100,11 +100,84 @@ public function show_user(int $user_id): Response { ]); } - #[Route('/user/{user_service}/{user_id}', name: 'show_service_user')] - public function show_service_user($user_service, $user_id): Response { + #[Route('/user/{user_service}/{user_name}', name: 'show_service_user')] + public function show_service_user($user_service, $user_name): Response { + $request = Request::createFromGlobals(); + // /user/twitter/edent?page=2&count=5 + $get_page = $request->query->get("page"); + $get_count = $request->query->get("count"); + + // Page + if( isset( $get_page ) ){ + $get_page = (int)$get_page; + } else { + $get_page = 0; + } + + // Items per page + if( isset( $get_count ) ){ + $get_count = (int)$get_count; + } else { + $get_count = 20; + } + + // Pagination for the database + $first = $get_page * $get_count; + $last = $get_count; + + $mediaFunctions = new MediaFunctions(); + $userFunctions = new UserFunctions(); + + $results_array = $userFunctions->getUserDetailsFromSocial( $user_service, $user_name ); + + if (false != $results_array) { + $providerID = $results_array["providerID"]; + $user_id = $results_array["userID"]; + $provider = $user_service; + $name = $user_name; + + $avatar_url = $userFunctions->getUserAvatar( $provider, $providerID, $name ); + $user_url = $userFunctions->getUserURL( $provider, $providerID, $name ); + + // Get the benches associated with this user + $benches_array = $userFunctions->getUserBenches( $user_id, $first, $last ); + $benches_count = $userFunctions->getUserBenchCount( $user_id); + + // Pagination for the UI + if( $get_page > 0 ) { + $previous_page = $get_page - 1; + $previous_url = "?page={$previous_page}&count={$get_count}"; + } else { + $previous_url = null; + } + + if( ( $benches_count > ( ($get_page * $get_count) + $get_count ) ) ) { + $next_page = $get_page + 1; + $next_url = "?page={$next_page}&count={$get_count}"; + } else { + $next_url = null; + } + + // Render the page + return $this->render('user.html.twig', [ + "user_id" => $user_id, + "user_name" => $name, + "avatar_url" => $avatar_url, + "user_external_url" => $user_url, + "user_provider" => $provider, + "benches_count" => $benches_count, + "benches" => $benches_array, + "next_url" => $next_url, + "previous_url" => $previous_url, + + ]); + } else { + // Generate an HTTP 404 response + throw $this->createNotFoundException("The user does not exist"); + } + return $this->render('user.html.twig', [ - 'user_id' => $user_id, - 'user_service' => $user_service, + 'user_id' => $user_id, ]); } } \ No newline at end of file diff --git a/www/src/Service/UserFunctions.php b/www/src/Service/UserFunctions.php index a74bf45..8c66356 100644 --- a/www/src/Service/UserFunctions.php +++ b/www/src/Service/UserFunctions.php @@ -29,6 +29,24 @@ public function getUserDetails( $userID ): array { return $results_array; } + public function getUserDetailsFromSocial( $user_service, $user_name ): array { + $dsnParser = new DsnParser(); + $connectionParams = $dsnParser->parse( $_ENV['DATABASE_URL'] ); + $conn = DriverManager::getConnection($connectionParams); + + // Get user's name and details + $sql = "SELECT `providerID`, `userID` + FROM `users` + WHERE `provider` = ? AND `name` = ? + LIMIT 0 , 1"; + $stmt = $conn->prepare($sql); + $stmt->bindValue(1, $user_service); + $stmt->bindValue(2, $user_name); + $results = $stmt->executeQuery(); + $results_array = $results->fetchAssociative(); + return $results_array; + } + public function getUserBenchCount( $userID ): int { $dsnParser = new DsnParser();