diff --git a/src/controller/ResourceController.php b/src/controller/ResourceController.php index 578d0cd..584f324 100644 --- a/src/controller/ResourceController.php +++ b/src/controller/ResourceController.php @@ -12,16 +12,17 @@ public function __construct($database) { } public function listResources() { - $out = array(); - $resources = $this->database->listResources(Req::category(), Req::page()); - - if (is_null($resources)) { + if (is_null($resources->resources)) { return NULL; } - foreach ($resources as $resource) { - array_push($out, new Resource($resource)); + $out = new \stdClass(); + $out->pagination = $resources->pagination; + $out->resources = array(); + + foreach ($resources->resources as $resource) { + array_push($out->resources, new Resource($resource)); } return $out; @@ -40,17 +41,20 @@ public function getResource() { } public function getResourcesByAuthor() { - $out = array(); + $out = new \stdClass(); + $out->pagination = new \stdClass(); + $out->resources = array(); if (Req::checkIdParam()) { - $resources = $this->database->getResourcesByUser(Req::id(), Req::page()); - - if (is_null($resources)) { + $resources = $this->database->getResourcesByUser(Req::id(), Req::category(), Req::page()); + if (is_null($resources->resources)) { return NULL; } - foreach ($resources as $resource) { - array_push($out, new Resource($resource)); + $out->pagination = $resources->pagination; + + foreach ($resources->resources as $resource) { + array_push($out->resources, new Resource($resource)); } } diff --git a/src/controller/ResourceUpdateController.php b/src/controller/ResourceUpdateController.php index 92f4f67..01369e4 100644 --- a/src/controller/ResourceUpdateController.php +++ b/src/controller/ResourceUpdateController.php @@ -23,14 +23,20 @@ public function getResourceUpdate() { } public function getResourceUpdates() { - $out = array(); + $out = new \stdClass(); + $out->pagination = new \stdClass(); + $out->updates = array(); if (Req::checkIdParam()) { $updates = $this->database->getResourceUpdates($_GET['id'], Req::page()); - if (is_null($updates)) return NULL; + if (is_null($updates)) { + return NULL; + } + + $out->pagination = $updates->pagination; - foreach ($updates as $update) { - array_push($out, new ResourceUpdate($update)); + foreach ($updates->updates as $update) { + array_push($out->updates, new ResourceUpdate($update)); } } diff --git a/src/imports.php b/src/imports.php index d219cdd..0260986 100644 --- a/src/imports.php +++ b/src/imports.php @@ -9,6 +9,7 @@ require_once(__DIR__ . '/object/Resource.php'); require_once(__DIR__ . '/object/ResourceCategory.php'); require_once(__DIR__ . '/object/ResourceUpdate.php'); +require_once(__DIR__ . '/object/PaginationInfo.php'); require_once(__DIR__ . '/support/Config.php'); require_once(__DIR__ . '/support/Database.php'); diff --git a/src/object/PaginationInfo.php b/src/object/PaginationInfo.php new file mode 100644 index 0000000..4de0d46 --- /dev/null +++ b/src/object/PaginationInfo.php @@ -0,0 +1,18 @@ +current_page = $current_page; + $this->total_pages = $total_pages; + $this->items_per_page = $items_per_page; + $this->results = $results; + $this->total_results = $total_results; + } +} \ No newline at end of file diff --git a/src/support/Config.php b/src/support/Config.php index e6db6d4..b8dd2cb 100644 --- a/src/support/Config.php +++ b/src/support/Config.php @@ -10,6 +10,10 @@ class Config { 'MYSQL_PORT' => 3306, 'MYSQL_DATABASE' => 'database', + // Pagination settings + 'MAX_RESULTS_PER_PAGE' => 10, + + // set PUBLIC_PATH to the root the XenForo installation, including trailing slash 'PUBLIC_PATH' => 'https://spigotmc.org/' ); diff --git a/src/support/Database.php b/src/support/Database.php index 0f71a84..9a65720 100644 --- a/src/support/Database.php +++ b/src/support/Database.php @@ -2,6 +2,7 @@ defined('_XFRM_API') or exit('No direct script access allowed here.'); use XFRM\Support\Config as Config; +use XFRM\Object\PaginationInfo as PaginationInfo; class Database { private $conn; @@ -39,28 +40,46 @@ public static function initializeViaConfig() { } public function listResources($category, $page) { - $page = $page == 1 ? 0 : 10 * ($page - 1); + $perPage = Config::$data['MAX_RESULTS_PER_PAGE']; + $itemOffset = $page == 1 ? 0 : $perPage * ($page - 1); if (!is_null($this->conn)) { $categoryClause = is_null($category) ? '' : 'AND r.resource_category_id = :resource_category_id'; - - $resStmt = $this->conn->prepare($this->_resource(sprintf('%s LIMIT 10 OFFSET :offset', $categoryClause))); - $resStmt->bindParam(':offset', $page, \PDO::PARAM_INT); + + $resCountStmt = $this->conn->prepare($this->_resource_count($categoryClause)); + + $resStmt = $this->conn->prepare($this->_resource(sprintf('%s LIMIT :per_page OFFSET :offset', $categoryClause))); + $resStmt->bindParam(':per_page', $perPage, \PDO::PARAM_INT); + $resStmt->bindParam(':offset', $itemOffset, \PDO::PARAM_INT); if (!empty($categoryClause)) { + $resCountStmt->bindParam(':resource_category_id', $category); $resStmt->bindParam(':resource_category_id', $category); } if ($resStmt->execute()) { + $out = new \stdClass(); + $resources = $resStmt->fetchAll(); + $numResources = count($resources); - for ($i = 0; $i < count($resources); $i++) { + for ($i = 0; $i < $numResources; $i++) { $resource = $resources[$i]; $resource['fields'] = $this->_resource_fields($resource['resource_id']); $resources[$i] = $resource; } - return $resources; + $out->resources = $resources; + + $totalResources = NULL; + if ($resCountStmt->execute()) { + $totalNumResources = intval($resCountStmt->fetch()[0]); + } + + $totalPages = ceil($totalNumResources / $perPage); + $out->pagination = new PaginationInfo(intval($page), $totalPages, $perPage, $numResources, $totalNumResources); + + return $out; } } @@ -84,16 +103,31 @@ public function getResource($resource_id) { return NULL; } - public function getResourcesByUser($user_id, $page) { - $page = $page == 1 ? 0 : 10 * ($page - 1); + public function getResourcesByUser($user_id, $category, $page) { + $perPage = Config::$data['MAX_RESULTS_PER_PAGE']; + $itemOffset = $page == 1 ? 0 : $perPage * ($page - 1); if (!is_null($this->conn)) { - $resStmt = $this->conn->prepare($this->_resource('AND r.user_id = :user_id LIMIT 10 OFFSET :offset')); - $resStmt->bindParam(':user_id', $user_id); - $resStmt->bindParam(':offset', $page, \PDO::PARAM_INT); + $categoryClause = is_null($category) ? '' : 'AND r.resource_category_id = :resource_category_id'; + + $resCountStmt = $this->conn->prepare($this->_resource_count(sprintf('AND r.user_id = :user_id %s', $categoryClause))); + $resCountStmt->bindParam(':user_id', $user_id, \PDO::PARAM_INT); + + $resStmt = $this->conn->prepare($this->_resource(sprintf('AND r.user_id = :user_id %s LIMIT :per_page OFFSET :offset', $categoryClause))); + $resStmt->bindParam(':user_id', $user_id, \PDO::PARAM_INT); + $resStmt->bindParam(':per_page', $perPage, \PDO::PARAM_INT); + $resStmt->bindParam(':offset', $itemOffset, \PDO::PARAM_INT); + + if (!empty($categoryClause)) { + $resCountStmt->bindParam(':resource_category_id', $category); + $resStmt->bindParam(':resource_category_id', $category); + } if ($resStmt->execute()) { + $out = new \stdClass(); + $resources = $resStmt->fetchAll(); + $numResources = count($resources); for ($i = 0; $i < count($resources); $i++) { $resource = $resources[$i]; @@ -101,7 +135,17 @@ public function getResourcesByUser($user_id, $page) { $resources[$i] = $resource; } - return $resources; + $out->resources = $resources; + + $totalResources = NULL; + if ($resCountStmt->execute()) { + $totalNumResources = intval($resCountStmt->fetch()[0]); + } + + $totalPages = ceil($totalNumResources / $perPage); + $out->pagination = new PaginationInfo(intval($page), $totalPages, $perPage, $numResources, $totalNumResources); + + return $out; } } @@ -134,15 +178,33 @@ public function getResourceUpdate($update_id) { } public function getResourceUpdates($resource_id, $page) { - $page = $page == 1 ? 0 : 10 * ($page - 1); + $perPage = Config::$data['MAX_RESULTS_PER_PAGE']; + $itemOffset = $page == 1 ? 0 : $perPage * ($page - 1); if (!is_null($this->conn)) { - $updatesStmt = $this->conn->prepare($this->_resource_update('AND r.resource_id = :resource_id LIMIT 10 OFFSET :offset')); - $updatesStmt->bindParam(':resource_id', $resource_id); - $updatesStmt->bindParam(':offset', $page, \PDO::PARAM_INT); + $updateCountStmt = $this->conn->prepare($this->_resource_update_count('AND r.resource_id = :resource_id')); + $updateCountStmt->bindParam(':resource_id', $resource_id, \PDO::PARAM_INT); + + $updatesStmt = $this->conn->prepare($this->_resource_update('AND r.resource_id = :resource_id LIMIT :per_page OFFSET :offset')); + $updatesStmt->bindParam(':resource_id', $resource_id, \PDO::PARAM_INT); + $updatesStmt->bindParam(':per_page', $perPage, \PDO::PARAM_INT); + $updatesStmt->bindParam(':offset', $itemOffset, \PDO::PARAM_INT); if ($updatesStmt->execute()) { - return $updatesStmt->fetchAll(); + $out = new \stdClass(); + + $out->updates = $updatesStmt->fetchAll(); + $numUpdates = count($out->updates); + + $totalUpdates = NULL; + if ($updateCountStmt->execute()) { + $totalNumUpdates = intval($updateCountStmt->fetch()[0]); + } + + $totalPages = ceil($totalNumUpdates / $perPage); + $out->pagination = new PaginationInfo(intval($page), $totalPages, $perPage, $numUpdates, $totalNumUpdates); + + return $out; } } @@ -185,6 +247,9 @@ public function getUser($user_id) { } } + if ($resCount != false) { + $totalResources = $resCount[0]; + } return NULL; } @@ -217,6 +282,13 @@ private function _resource($suffix) { ); } + private function _resource_count($suffix) { + return sprintf( + "SELECT COUNT(r.resource_id) FROM xf_resource r WHERE r.resource_state = 'visible' %s", + $suffix + ); + } + private function _resource_fields($resource_id) { if (!is_null($this->conn)) { $fieldsStmt = $this->conn->prepare( @@ -248,4 +320,11 @@ private function _resource_update($suffix) { $suffix ); } + + private function _resource_update_count($suffix) { + return sprintf( + "SELECT COUNT(r.resource_update_id) FROM xf_resource_update r WHERE r.message_state = 'visible' %s", + $suffix + ); + } } \ No newline at end of file