Skip to content

Handle filters of type "date" (such as DOB) (CFM-295) #102

Merged
merged 1 commit into from
Jun 26, 2023
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
3 changes: 3 additions & 0 deletions app/src/Lib/Traits/SearchFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public function whereFilter(\Cake\ORM\Query $query, string $attribute, string|ar
$sub = true;
} elseif(in_array($this->searchFilters[$attribute]['type'], $search_types, true)) {
return $query->where([$attribute => $search]);
} elseif($this->searchFilters[$attribute]['type'] == "date") {
// Parse the date string with FrozenTime to improve error handling
return $query->where([$attribute => FrozenTime::parseDate($search, 'y-M-d')]);
} elseif( $this->searchFilters[$attribute]['type'] == "timestamp") {
// Date between dates
if(!empty($search[0])
Expand Down
37 changes: 29 additions & 8 deletions app/templates/element/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,31 @@
$field_datetime_columns[$key] = $options;
continue;
}
$formParams = [
'label' => !empty($columns[$key]['label']) ? $columns[$key]['label'] : $options['label'],
// The default type is text, but we might convert to select below
'type' => 'text',
'value' => (!empty($query[$key]) ? $query[$key] : ''),
'required' => false,
];


if($options['type'] == 'date') {
// date picker
$formParams = [
'label' => !empty($columns[$key]['label']) ? $columns[$key]['label'] : $options['label'],
'type' => 'text', // date inputs must be text for accessibility reasons for now.
'value' => (!empty($query[$key]) ? $query[$key] : ''),
'required' => false,
'pattern' => '\d{4}-\d{2}-\d{2}',
'placeholder' => 'YYYY-MM-DD',
'title' => __d('field','datepicker.enterDate'),
'class' => 'form-control datepicker'
];
} else {
// text input
$formParams = [
'label' => !empty($columns[$key]['label']) ? $columns[$key]['label'] : $options['label'],
// The default type is text, but we might convert to select below
'type' => 'text',
'value' => (!empty($query[$key]) ? $query[$key] : ''),
'required' => false,
'class' => 'form-control'
];
}

// The populated variables are in plural while the column names are singular
// Convention: It is a prerequisite that the vvar should be the plural of the column name
$populated_vvar = Inflector::pluralize($key);
Expand Down Expand Up @@ -226,6 +243,8 @@
$coptions['required'] = false;
$coptions['placeholder'] = '';
// $coptions['placeholder'] = 'YYYY-MM-DD HH:MM:SS';
$coptions['pattern'] = '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}';
$coptions['title'] = __d('field','datepicker.enterDateTime');
$coptions['id'] = str_replace("_", "-", $starts_field);

$pickerDate = '';
Expand Down Expand Up @@ -259,6 +278,8 @@
$coptions['required'] = false;
$coptions['placeholder'] = ''; // todo: Make this configurable
// $coptions['placeholder'] = 'YYYY-MM-DD HH:MM:SS';
$coptions['pattern'] = '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}';
$coptions['title'] = __d('field','datepicker.enterDateTime');
$coptions['label'] = __d('field','ends_at');
$coptions['id'] = str_replace("_", "-", $ends_field);

Expand Down