Skip to content

Commit

Permalink
feat(admin): Improve Selectize LoadAction controller
Browse files Browse the repository at this point in the history
Add support for searching multiple fields to Selectize controller.

Resolves #85
  • Loading branch information
mcaskill committed Mar 6, 2024
1 parent d2aba67 commit 21057b7
Showing 1 changed file with 41 additions and 22 deletions.
63 changes: 41 additions & 22 deletions packages/admin/src/Charcoal/Admin/Action/Selectize/LoadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,40 +54,59 @@ public function run(RequestInterface $request, ResponseInterface $response)
{
unset($request);

$failMessage = $this->translator()->translation('Failed to load object(s)');
$errorThrown = strtr($this->translator()->translation('{{ errorMessage }}: {{ errorThrown }}'), [
$failMessage = $this->translator()->trans('Failed to load object(s)');
$errorThrown = strtr($this->translator()->trans('{{ errorMessage }}: {{ errorThrown }}'), [
'{{ errorMessage }}' => $failMessage
]);
$reqMessage = $this->translator()->translation(
'{{ parameter }} required, must be a {{ expectedType }}, received {{ actualType }}'
);

try {
$selectizeInput = $this->selectizeInput();
$sp = $selectizeInput->p();
/** @var Charcoal\Admin\Property\Input\SelectizeInput */
$input = $this->selectizeInput();

$searchField = isset($selectizeInput->selectizeOptions()['searchField'])
? $selectizeInput->selectizeOptions()['searchField']
: $selectizeInput->choiceObjMap()['label'];
/** @var Charcoal\Property\ObjectProperty */
$property = $input->property();

if ($this->query()) {
$query = [
'property' => $searchField,
'operator' => 'LIKE',
'value' => '%' . $this->query() . '%',
];

$filters = $sp->filters();
if (is_array($filters)) {
array_push($filters, $query);
/** @var array<string, mixed> */
$options = $input->selectizeOptions();
$choiceMap = $input->choiceObjMap();

if (!empty($options['searchProperties'])) {
$searchProperties = (array)$options['searchProperties'];
} elseif (
!empty($choiceMap['label']) &&
strpos($choiceMap['label'], '{{') === false
) {
$searchProperties = [ $choiceMap['label'] ];
} else {
$filters = [ $query ];
$searchProperties = [];
}

$sp->setFilters($filters);
if ($searchProperties) {
$search = [
'conjunction' => 'OR',
'conditions' => [],
];
foreach ($searchProperties as $searchProperties) {
$search['conditions'][] = [
'property' => $searchProperties,
'operator' => 'LIKE',
'value' => '%' . $this->query() . '%',
];
}

$filters = $property->filters();
if (is_array($filters)) {
array_push($filters, $search);
} else {
$filters = [ $search ];
}

$property->setFilters($filters);
}
}

$choices = $sp->choices();
$choices = $property->choices();

$this->setSelectizeCollection($this->selectizeVal($choices));

Expand Down

0 comments on commit 21057b7

Please sign in to comment.