diff --git a/controllers/SiteController.php b/controllers/SiteController.php index 54a70f1..b59d6c5 100644 --- a/controllers/SiteController.php +++ b/controllers/SiteController.php @@ -33,4 +33,57 @@ public function actionIndex(){ public function actionTest1(){ return $this->render('test1'); } + + /** + * Displays Test Posts Francisco Girona + * @return array $posts + */ + public function actionPosts(){ + + $url = 'https://jsonplaceholder.typicode.com/posts'; + + $context = stream_context_create([ + 'http' => [ + 'method' => 'GET', + 'timeout' => 5, + 'header' => "Accept: application/json\r\n", + ], + 'ssl' => [ + 'verify_peer' => true, + 'verify_peer_name' => true, + ], + ]); + + $raw = @file_get_contents($url, false, $context); + + if($raw === false){ + return $this->render('posts', [ + 'data' => [], 'error' => 'Failed to fetch data from the API' + ]); + } + + $posts = json_decode($raw, true); + + if(!is_array($posts)){ + return $this->render('posts', [ + 'data' => [], 'error' => 'Invalid data format' + ]); + } + + // Pagination + $totalPosts = count($posts); + $postsPerPage = 10; + $totalPages = ceil($totalPosts / $postsPerPage); + $currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1; + + // Control Get Parameters page is not valid + if($currentPage < 1 || $currentPage > $totalPages){ + return $this->render('posts', ['data' => [], 'error' => 'Invalid page number', 'totalPages' => $totalPages, 'currentPage' => $currentPage]); + } + + $offset = ($currentPage - 1) * $postsPerPage; + $posts = array_slice($posts, $offset, $postsPerPage); + + return $this->render('posts', ['data' => $posts, 'error' => '', 'totalPages' => $totalPages, 'currentPage' => $currentPage]); + } } diff --git a/views/site/index.php b/views/site/index.php index 50f2905..2841c0a 100644 --- a/views/site/index.php +++ b/views/site/index.php @@ -16,6 +16,9 @@ +

If you have any questions or issues, feel free to send an email with your queries to asunyer@ooptimo.com, and we will try to assist you as soon as possible.

diff --git a/views/site/posts.php b/views/site/posts.php new file mode 100644 index 0000000..c8d4f9a --- /dev/null +++ b/views/site/posts.php @@ -0,0 +1,39 @@ +title = 'Posts — JSONPlaceholder'; +$this->params['breadcrumbs'][] = $this->title; +?> + + +
+ +
+ + + +
+

Posts

+
+ + + +
+

+

+
+ + + 1): ?> + + \ No newline at end of file diff --git a/web/css/site.css b/web/css/site.css index 9b92ad0..0386aac 100644 --- a/web/css/site.css +++ b/web/css/site.css @@ -28,3 +28,44 @@ main > .container { margin-top: 5px; color: #999; } + +.posts { + max-width: 1200px; + margin: 0 auto; + padding: 20px; +} + +.post { + margin: 0 auto 20px; + padding: 20px; + border: 1px solid #ccc; + border-radius: 5px; +} + +.post-title { + font-size: 20px; + font-weight: bold; +} + +.pagination { + display: flex; + justify-content: center; + margin-top: 20px; + gap: 10px; +} + +.pagination-link { + padding: 10px 20px; + border: 1px solid #ccc; + border-radius: 5px; + text-decoration: none; +} +.pagination-link:hover { + background-color: #007bff; + color: #fff; +} + +.pagination-link.active { + background-color: #007bff; + color: #fff; +}