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

Fix: Don't break when passing in params for "Search Query" #1922

Merged
merged 4 commits into from
Sep 30, 2020
Merged
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
4 changes: 2 additions & 2 deletions config/bolt/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ listing_template: listing.twig
listing_records: 6
listing_sort: datepublish DESC

# Allow filtering on listing pages using query parameters, much like you
# would with {% setcontent %}. E.g. /pages?order=id and /pages?title=%voluptat%
# Allow filtering on listing pages using query parameters, much like you would
# with {% setcontent %}. E.g. /pages?order=id and /pages?title--like=voluptat
# Useful for search.
query_search: true

Expand Down
4 changes: 4 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
use PhpCsFixer\Fixer\Semicolon\NoSinglelineWhitespaceBeforeSemicolonsFixer;
use PhpCsFixer\Fixer\Whitespace\NoTrailingWhitespaceFixer;
use SlevomatCodingStandard\Sniffs\ControlStructures\DisallowYodaComparisonSniff;
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayListItemNewlineFixer;
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayOpenerAndCloserNewlineFixer;
use Symplify\CodingStandard\Fixer\ArrayNotation\StandaloneLineInMultilineArrayFixer;
use Symplify\CodingStandard\Fixer\Commenting\RemoveSuperfluousDocBlockWhitespaceFixer;
use Symplify\CodingStandard\Fixer\Strict\BlankLineAfterStrictTypesFixer;
Expand All @@ -66,6 +68,8 @@
NativeConstantInvocationFixer::class => null,
NativeFunctionInvocationFixer::class => null,
UnaryOperatorSpacesFixer::class => null,
ArrayOpenerAndCloserNewlineFixer::class => null,
ArrayListItemNewlineFixer::class => null,
]);

$services = $containerConfigurator->services();
Expand Down
21 changes: 19 additions & 2 deletions src/Controller/Frontend/ListingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Bolt\Entity\Content;
use Bolt\Repository\ContentRepository;
use Bolt\Storage\Query;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -61,8 +62,7 @@ public function listing(ContentRepository $contentRepository, string $contentTyp
return $this->forward($controller, ['slugOrId' => $content->getId()]);
}

$records = $content->setMaxPerPage($amountPerPage)
->setCurrentPage($page);
$records = $this->setRecords($content, $amountPerPage, $page);

$templates = $this->templateChooser->forListing($contentType);
$this->twig->addGlobal('records', $records);
Expand All @@ -85,6 +85,11 @@ private function parseQueryParams(Request $request): array
$queryParams = collect($request->query->all());

return $queryParams->mapWithKeys(function ($value, $key) {
// Ensure we don't have arrays, if we get something like `title[]=…` passed in.
if (is_array($value)) {
$value = current($value);
}

if (str::endsWith($key, '--like')) {
$key = str::removeLast($key, '--like');
$value = '%' . $value . '%';
Expand All @@ -93,4 +98,16 @@ private function parseQueryParams(Request $request): array
return [$key => $value];
})->toArray();
}

private function setRecords($content, int $amountPerPage, int $page): Pagerfanta
{
if ($content instanceof Pagerfanta) {
$records = $content->setMaxPerPage($amountPerPage)
->setCurrentPage($page);
} else {
$records = new Pagerfanta(new ArrayAdapter([]));
}

return $records;
}
}
8 changes: 6 additions & 2 deletions src/Storage/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bolt\Storage;

use Bolt\Common\Arr;
use Bolt\Configuration\Config;
use Bolt\Configuration\Content\ContentType;
use Bolt\Doctrine\JsonHelper;
Expand Down Expand Up @@ -175,8 +176,11 @@ public function shouldReturnSingle(): bool
*/
public function setParameters(array $params): void
{
// array_map('strtolower', $params) to change all params to lowercase.
$this->params = array_filter(array_map('strtolower', $params));
// Change all params to lowercase, filter out empty ones
$this->params = array_filter(Arr::mapRecursive($params, function ($a) {
return mb_strtolower((string) $a, 'utf-8');
}));

$this->processFilters();
}

Expand Down
4 changes: 3 additions & 1 deletion src/Twig/TokenParser/SetcontentTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public function parse(Token $token): Node
// where parameter
if ($this->parser->getStream()->test(Token::NAME_TYPE, 'where')) {
$this->parser->getStream()->next();
$whereArguments = ['wherearguments' => $this->parser->getExpressionParser()->parseExpression()];
$whereArguments = [
'wherearguments' => $this->parser->getExpressionParser()->parseExpression(),
];
}

// limit parameter
Expand Down