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

Limit result with SQL if property 'sortBy' is used #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
46 changes: 31 additions & 15 deletions core/components/simplesearch/src/Driver/SimpleSearchDriverBasic.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,31 @@ public function search($string, array $scriptProperties = array()) {
$total = $this->modx->getCount(modResource::class, $c);

$c->query['distinct'] = 'DISTINCT';
if (!empty($scriptProperties['sortBy'])) {

$perPage = (int) $this->modx->getOption('perPage', $this->config, 10);
$offset = $this->modx->getOption('start', $this->config, 0);
$offsetIndex = $this->modx->getOption('offsetIndex', $this->config, 'simplesearch_offset');
if (isset($_REQUEST[$offsetIndex])) {
$offset = (int) $_REQUEST[$offsetIndex];
}

$sortBy = $this->modx->getOption('sortBy', $scriptProperties, '');
$maxCountPhpSort = (int) $this->modx->getOption('maxCountPhpSort', $scriptProperties, 0);
if (empty($sortBy) && $maxCountPhpSort && $total > $maxCountPhpSort){
// Too many results to sort in PHP -> Use limit in SQL-Query
$fallbackSortBy = $this->modx->getOption('fallbackSortBy', $scriptProperties, '');
if ($fallbackSortBy) {
$sortBy = $fallbackSortBy;
} else {
$sortBy = 'id';
}
}

if (!empty($sortBy)) {
// sort and limit resources with SQL
$sortDir = $this->modx->getOption('sortDir', $scriptProperties, 'DESC');
$sortDirs = explode(',', $sortDir);
$sortBys = explode(',', $scriptProperties['sortBy']);
$sortBys = explode(',', $sortBy);
$dir = 'desc';
for ($i = 0, $iMax = count($sortBys); $i < $iMax; $i++) {
if (isset($sortDirs[$i])) {
Expand All @@ -251,24 +272,19 @@ public function search($string, array $scriptProperties = array()) {

$c->sortby('modResource.' . $sortBys[$i], strtoupper($dir));
}
if ($perPage > 0) {
$c->limit($perPage, $offset);
}
}

$resources = $this->modx->getCollection(modResource::class, $c);
if (empty($scriptProperties['sortBy'])) {
$resources = $this->sortResults($resources, $scriptProperties);
}

/* Set limit */
$perPage = (int) $this->modx->getOption('perPage', $this->config, 10);
if ($perPage > 0) {
$offset = $this->modx->getOption('start', $this->config, 0);
$offsetIndex = $this->modx->getOption('offsetIndex', $this->config, 'simplesearch_offset');

if (isset($_REQUEST[$offsetIndex])) {
$offset = (int) $_REQUEST[$offsetIndex];
if (empty($sortBy)) {
// sort and limit resources in PHP
$resources = $this->sortResults($resources, $scriptProperties);
if ($perPage > 0) {
$resources = array_slice($resources, $offset, $perPage);
}

$resources = array_slice($resources, $offset, $perPage);
}

$includeTVs = $this->modx->getOption('includeTVs', $scriptProperties, '');
Expand Down