From 49efb238b879bce764d04dad99c7a169e80f93dd Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Wed, 14 Jun 2023 22:08:34 -0500 Subject: massive overhaul to implement proper MVC --- index.php | 238 ++------------------------------------------------------------ 1 file changed, 6 insertions(+), 232 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index f85b25b..03ecb6a 100644 --- a/index.php +++ b/index.php @@ -1,236 +1,10 @@ title to use in the HTML - * @return string $html_output The formatted HTML result of stories from the API - * @author cmc - */ -function get_stories(string $api_url, string $inline_title): string -{ - $response_raw = file_get_contents($api_url); - if ($response_raw == "null") { - return '

ERROR: Stories not found. API returned `null`.

'; - } else { - $response = json_decode($response_raw, true); - } - - $html_output = '

' . $inline_title . '

'; - - for ($i = 0; $i < count($response); $i++) { - $sub_url = 'https://hacker-news.firebaseio.com/v0/item/' . $response[$i] . '.json'; - $sub_response_raw = file_get_contents($sub_url); - $sub_response = json_decode($sub_response_raw, true); - - // TODO: Can this be converted to a heredoc string with variables? - $html = '
' . $sub_response['title'] . ''; - $html .= '

by '; - $html .= $sub_response['by'] . ' | ' . $sub_response['score']; - $html .= ' points

'; - $html_output .= $html; - } - - return $html_output; -} - -/** - *Extract a user's profile from Hacker News API and format in HTML - * - * @access public - * @param string $api_url The API endpoint to use for extraction - * @param string $inline_title The

title to use in the HTML - * @return string $html_output The formatted HTML result of stories from the API - * @author cmc - */ -function get_user(string $api_url, string $inline_title): string -{ - $response_raw = file_get_contents($api_url); - if ($response_raw == "null") { - return '

ERROR: User not found.

'; - } else { - $response = json_decode($response_raw, true); - } - - // TODO: Can this be converted to a heredoc string with variables? - $html_output = '

' . $inline_title . '

'; - $html_output .= '

About: ' . $response['about'] . '

'; - $html_output .= '

Karma: ' . $response['karma'] . '

'; - $html_output .= '

Created:

by '; - $html .= $sub_response['by'] . ' | ' . $sub_response['score']; - $html .= ' points

'; - } elseif ($sub_response['type'] == 'poll') { - // TODO: Handle polls - $html = 'TODO: Add logic to handle polls here.'; - } else { - // TODO: Add link to parent with $sub_response['parent'] - $html = '

' . $sub_response['text'] . '

'; - } - $html_output .= $html; - } - } else { - $html_output .= '

User has no submissions.

'; - } - - return $html_output; -} - - -/** - * Send formatted HTML results to the user via a template - * - * @access public - * @param string $page_url Canonical URL for HTML header - * @param string $page_description Page description for HTML header - * @param string $page_title Page title for HTML header - * @param string $page_content Page content to display in
- * @author cmc - */ -function echo_html(string $page_url, string $page_description, string $page_title, string $page_content) -{ - include_once 'src/View/class-template.php'; - - $template = new HN\View\Template( - $page_url, - $page_description, - $page_title, - $page_content - ); - - $template->echo_template('templates/template.html'); -} - -// EOF +$route = new HN\Controllers\RouteController($_SERVER['REQUEST_URI']); +$route->routeUser(); -- cgit v1.2.3-70-g09d2