Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pagination to listResources, getResourcesByUser, getResourceUpdates #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/controller/ResourceUpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/imports.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
18 changes: 18 additions & 0 deletions src/object/PaginationInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php namespace XFRM\Object;
defined('_XFRM_API') or exit('No direct script access allowed here.');

class PaginationInfo {
public $current_page;
public $total_pages;
public $items_per_page;
public $results;
public $total_results;

public function __construct($current_page, $total_pages, $items_per_page, $results, $total_results) {
$this->current_page = $current_page;
$this->total_pages = $total_pages;
$this->items_per_page = $items_per_page;
$this->results = $results;
$this->total_results = $total_results;
}
}
4 changes: 4 additions & 0 deletions src/support/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
);
Expand Down
113 changes: 96 additions & 17 deletions src/support/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -84,24 +103,49 @@ 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];
$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;
}
}

Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -185,6 +247,9 @@ public function getUser($user_id) {
}
}

if ($resCount != false) {
$totalResources = $resCount[0];
}
return NULL;
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
);
}
}