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

Response consistency fixes #50

Open
wants to merge 3 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
26 changes: 18 additions & 8 deletions src/object/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,29 @@ public function __construct($resource) {

for ($idx = 0; $idx < count($resource['fields']); $idx++) {
$field = $resource['fields'][$idx];
$value = $field['actual_field_value'];

switch ($field['field_id']) {
case 'native_mc_version':
$this->native_minecraft_version = self::cleanupVersion($field['actual_field_value']);
if (is_null($value) || empty($value)) {
$this->native_minecraft_version = NULL;
} else {
$this->native_minecraft_version = self::cleanupVersion($value);
}
break;
case 'mc_versions':
$versions = array_map(
function($version) {
return self::cleanupVersion($version);
},
unserialize($field['actual_field_value'])
);
$this->supported_minecraft_versions = array_values($versions);
if (is_null($value) || empty($value)) {
$this->supported_minecraft_versions = [];
} else {
$versions = array_map(
function($version) {
return self::cleanupVersion($version);
jacobsandersen marked this conversation as resolved.
Show resolved Hide resolved
},
unserialize($value)
);

$this->supported_minecraft_versions = array_values($versions);
}
break;
}
}
Expand Down
95 changes: 71 additions & 24 deletions src/support/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ public static function initializeViaConfig() {
}

public function listResources($category, $page) {
$page = $page == 1 ? 0 : 10 * ($page - 1);
if ($page <= 0) {
return NULL;
}

$offset = $page === 1 ? 0 : 10 * ($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);
$resStmt->bindParam(':offset', $offset, \PDO::PARAM_INT);

if (!empty($categoryClause)) {
$resStmt->bindParam(':resource_category_id', $category);
Expand All @@ -54,6 +58,10 @@ public function listResources($category, $page) {
if ($resStmt->execute()) {
$resources = $resStmt->fetchAll();

if (is_null($resources) || $resources === false || empty($resources)) {
return NULL;
}

for ($i = 0; $i < count($resources); $i++) {
$resource = $resources[$i];
$resource['fields'] = $this->_resource_fields($resource['resource_id']);
Expand All @@ -74,23 +82,29 @@ public function getResource($resource_id) {

if ($resStmt->execute()) {
$resource = $resStmt->fetch();

if (!is_null($resource) && $resource !== false) {
$resource['fields'] = $this->_resource_fields($resource['resource_id']);
return $resource;
}

return $resource;
}
}

return NULL;
}

public function getResourcesByUser($user_id, $page) {
$page = $page == 1 ? 0 : 10 * ($page - 1);
if ($page <= 0 || !$this->_user_exists($user_id)) {
return NULL;
}

$offset = $page === 1 ? 0 : 10 * ($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);
$resStmt->bindParam(':offset', $offset, \PDO::PARAM_INT);

if ($resStmt->execute()) {
$resources = $resStmt->fetchAll();
Expand Down Expand Up @@ -134,12 +148,16 @@ public function getResourceUpdate($update_id) {
}

public function getResourceUpdates($resource_id, $page) {
$page = $page == 1 ? 0 : 10 * ($page - 1);
if ($page <= 0 || !$this->_resource_exists($resource_id)) {
return NULL;
}

$offset = $page === 1 ? 0 : 10 * ($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);
$updatesStmt->bindParam(':offset', $offset, \PDO::PARAM_INT);

if ($updatesStmt->execute()) {
return $updatesStmt->fetchAll();
Expand All @@ -159,27 +177,30 @@ public function getUser($user_id) {
WHERE u.user_id = :user_id
GROUP BY u.user_id"
);

$userStmt->bindParam(':user_id', $user_id);

$identStmt = $this->conn->prepare(
"SELECT ufv.field_id, ufv.field_value
FROM xf_user_field_value ufv
INNER JOIN xf_user u
ON u.user_id = ufv.user_id
INNER JOIN xf_user_field uf
ON uf.field_id = ufv.field_id AND uf.display_group = 'contact'
WHERE ufv.user_id = :user_id AND ufv.field_value IS NOT NULL AND ufv.field_value != ''"
);
if ($userStmt->execute()) {
$user = $userStmt->fetch();
if (!is_null($user) && $user != false) {
$identStmt = $this->conn->prepare(
"SELECT ufv.field_id, ufv.field_value
FROM xf_user_field_value ufv
INNER JOIN xf_user u
ON u.user_id = ufv.user_id
INNER JOIN xf_user_field uf
ON uf.field_id = ufv.field_id AND uf.display_group = 'contact'
WHERE ufv.user_id = :user_id AND ufv.field_value IS NOT NULL AND ufv.field_value != ''"
);
$identStmt->bindParam(':user_id', $user_id);

$identities = new \stdClass();
if ($identStmt->execute()) {
$identities = $identStmt->fetchAll();
}

$identStmt->bindParam(':user_id', $user_id);

if ($userStmt->execute() && $identStmt->execute()) {
$fetched = $userStmt->fetch();
if (!is_null($fetched) && $fetched !== false) {
$out = new \stdClass();
$out->user = $fetched;
$out->ident = $identStmt->fetchAll();
$out->user = $userStmt->fetch();
$out->ident = $identities;
return $out;
}
}
Expand Down Expand Up @@ -248,4 +269,30 @@ private function _resource_update($suffix) {
$suffix
);
}

private function _resource_exists($resource_id) {
if (!is_null($this->conn)) {
$stmt = $this->conn->prepare("SELECT EXISTS(SELECT 1 FROM xf_resource WHERE resource_id = :resource_id) AS 'exists'");
$stmt->bindParam(":resource_id", $resource_id);

if ($stmt->execute()) {
return (bool) $stmt->fetch()['exists'];
}
}

return FALSE;
}

private function _user_exists($user_id) {
if (!is_null($this->conn)) {
$stmt = $this->conn->prepare("SELECT EXISTS(SELECT 1 FROM xf_user WHERE user_id = :user_id) AS 'exists'");
$stmt->bindParam(":user_id", $user_id);

if ($stmt->execute()) {
return (bool) $stmt->fetch()['exists'];
}
}

return FALSE;
}
}