Skip to content

Commit

Permalink
Add friendly user page
Browse files Browse the repository at this point in the history
  • Loading branch information
edent committed May 31, 2023
1 parent bb83db9 commit 2ba4d3c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
81 changes: 77 additions & 4 deletions www/src/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}
18 changes: 18 additions & 0 deletions www/src/Service/UserFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2ba4d3c

Please sign in to comment.