diff --git a/src/Listener/ViewSearchListener.php b/src/Listener/ViewSearchListener.php index c29b329..2a09892 100644 --- a/src/Listener/ViewSearchListener.php +++ b/src/Listener/ViewSearchListener.php @@ -6,7 +6,9 @@ use Cake\Event\EventInterface; use Cake\Routing\Router; use Cake\Utility\Hash; +use Cake\Utility\Inflector; use Crud\Listener\BaseListener; +use function Cake\I18n\__d; /** * @method \Cake\ORM\Table _model() @@ -82,14 +84,14 @@ public function afterPaginate(EventInterface $event): void */ public function fields(): array { - $fields = $this->getConfig('fields') ?: []; + $fields = Hash::normalize($this->getConfig('fields'), default: []) ?: []; $config = $this->getConfig(); $schema = $this->_model()->getSchema(); $request = $this->_request(); if ($fields) { - $fields = Hash::normalize($fields); + $fields = Hash::normalize($fields, default: []); } else { $filters = $this->_model()->searchManager()->getFilters($config['collection']); @@ -109,21 +111,21 @@ public function fields(): array 'type' => 'text', ]; - if (substr($field, -3) === '_id' && $field !== '_id') { + if (str_ends_with($field, '_id') && $field !== '_id') { $input['type'] = 'select'; } - $input = (array)$opts + $input; + $input = $opts + $input; $input['value'] = $request->getQuery($field); if (empty($input['options']) && $schema->getColumnType($field) === 'boolean') { - $input['options'] = ['No', 'Yes']; + $input['options'] = [__d('crud', 'No'), __d('crud', 'Yes')]; $input['type'] = 'select'; } if (!empty($input['options'])) { - $input['empty'] = true; + $input['empty'] ??= $this->getPlaceholder($field); if (empty($input['class']) && !$config['select2']) { $input['class'] = 'no-select2'; } @@ -156,6 +158,13 @@ public function fields(): array ]; } + if ($input['type'] === 'text') { + $input['placeholder'] ??= $this->getPlaceholder($field); + } + if ($input['type'] === 'select') { + $input['empty'] ??= $this->getPlaceholder($field); + } + $urlArgs = []; $fieldKeys = $input['fields'] ?? ['id' => $field, 'value' => $field]; @@ -174,4 +183,17 @@ public function fields(): array return $fields; } + + protected function getPlaceholder(string $field): string + { + if (str_contains($field, '.')) { + [, $field] = explode('.', $field); + } + + if (str_ends_with($field, '_id') && $field !== '_id') { + $field = substr($field, 0, -3); + } + + return Inflector::humanize($field); + } } diff --git a/templates/element/search.php b/templates/element/search.php index 990e513..0c16d1f 100644 --- a/templates/element/search.php +++ b/templates/element/search.php @@ -7,19 +7,19 @@
'form-inline', 'id' => 'searchFilter']; + $searchOptions += ['align' => 'inline', 'id' => 'searchFilter']; echo $this->Form->create(null, $searchOptions); echo $this->Form->hidden('_search'); ?> Form->controls($searchInputs, ['fieldset' => false]); ?> -
- Form->button(__d('crud', 'Filter results'), ['type' => 'submit', 'class' => 'btn btn-primary']); ?> - Search->isSearch()) : ?> + Form->button(__d('crud', 'Filter results'), ['type' => 'submit', 'class' => 'btn btn-primary']); ?> + Search->isSearch()) : ?> +
Search->resetLink(__d('crud', 'Reset'), ['class' => 'btn btn-primary']) ?> - -
+
+ Form->end(); ?>
diff --git a/webroot/css/local.css b/webroot/css/local.css index 94b1ad8..aad75f9 100644 --- a/webroot/css/local.css +++ b/webroot/css/local.css @@ -43,21 +43,25 @@ h2 .actions { margin-left: 15px; } -.search-filters .form-inline .form-group label, -.search-filters .form-inline .btn { - margin-right: .25rem !important; + +.search-filters .form-inline .col-auto { + margin-bottom: var(--bs-gutter-y) !important; } -.search-filters .form-inline .form-group { - margin-right: .5rem !important; +.search-filters .form-inline .form-control input { + min-width: 90px !important; } -.search-filters .form-inline .form-group { - margin-bottom: 1rem !important; +.search-filters .form-inline option { + color: var(--bs-body-color); + font-style: normal; } -.search-filters .form-inline .form-control input { - min-width: 90px !important; +.search-filters .form-inline select:invalid, +.search-filters .form-inline select:has(option[value=""]:checked), +.search-filters .form-inline option[value=""] { + font-style: italic; + color: #777; } .search-filters .form-inline select.autocomplete {