Skip to content

Commit

Permalink
Merge pull request #586 from samrzhevsky/master
Browse files Browse the repository at this point in the history
Fix page count calculation
  • Loading branch information
sergix44 authored Aug 4, 2024
2 parents 687c503 + 8fabc57 commit fa85310
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions app/Database/Repositories/MediaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,38 @@ public function runWithFileSort(int $page): MediaRepository
array_multisort(array_column($files, 'size'), $this->buildOrderBy(), SORT_NUMERIC, $files);

$params = [];
$queryPagesParams = [];

if ($this->text !== null) {
if ($this->isAdmin) {
[$queryMedia,] = $this->buildAdminQueries();
[$queryMedia, $queryPages] = $this->buildAdminQueries();
} else {
[$queryMedia,] = $this->buildUserQueries();
[$queryMedia, $queryPages] = $this->buildUserQueries();
$params[] = $this->userId;
}

$params[] = '%'.htmlentities($this->text).'%';
$queryPagesParams = $params;
$paths = array_column($files, 'path');
} elseif ($this->tagId !== null) {
if ($this->isAdmin) {
[, $queryPages] = $this->buildAdminQueries();
} else {
[, $queryPages] = $this->buildUserQueries();
$queryPagesParams[] = $this->userId;
}

$paths = array_column($files, 'path');
$ids = $this->getMediaIdsByTagId($this->tagId);
$queryMedia = 'SELECT `uploads`.*, `users`.`user_code`, `users`.`username` FROM `uploads` LEFT JOIN `users` ON `uploads`.`user_id` = `users`.`id` WHERE `uploads`.`storage_path` IN ("'.implode('","', $paths).'") AND `uploads`.`id` IN ('.implode(',', $ids).')';
} else {
if ($this->isAdmin) {
[, $queryPages] = $this->buildAdminQueries();
} else {
[, $queryPages] = $this->buildUserQueries();
$queryPagesParams[] = $this->userId;
}

$files = array_slice($files, $offset, $limit, true);
$paths = array_column($files, 'path');
$queryMedia = 'SELECT `uploads`.*, `users`.`user_code`, `users`.`username` FROM `uploads` LEFT JOIN `users` ON `uploads`.`user_id` = `users`.`id` WHERE `uploads`.`storage_path` IN ("'.implode('","', $paths).'")';
Expand Down Expand Up @@ -253,7 +270,7 @@ public function runWithFileSort(int $page): MediaRepository
}
}

$this->pages = count($this->media) / $limit;
$this->pages = $this->db->query($queryPages, $queryPagesParams)->fetch()->count / $limit;

if ($this->text !== null || $this->tagId !== null) {
$this->media = array_slice($this->media, $offset, $limit, true);
Expand Down

0 comments on commit fa85310

Please sign in to comment.