Skip to content

Commit

Permalink
FEATURE: It's now possible to define a filter end date.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Glasl committed Jan 14, 2019
1 parent 2f32b92 commit 7a99f2d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion code/model/ListFilterCalendarEventDateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
if (class_exists('CalendarDateTime')) {

class ListFilterCalendarEventDateRange extends ListFilterDateRange {

/**
* Use 'StartDate' from CalendarDateTime class.
*
Expand Down Expand Up @@ -74,7 +75,7 @@ public function getFilterData(DataObject $record) {
*/
public function applyFilter(SS_List $list, array $data) {
$start = isset($data['StartDate']) ? $data['StartDate'] : null;
$end = isset($data['EndDate']) ? $data['EndDate'] : null;
$end = isset($data['EndDate']) ? $data['EndDate'] : $this->getEndDateDefault();

// Get `CalendarDateTime` class used. Defaults to `CalendarDateTime`.
$class = $list->dataClass();
Expand Down
15 changes: 11 additions & 4 deletions code/model/ListFilterDateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ListFilterDateRange extends ListFilterBase {
private static $db = array(
'StartDateField' => 'Varchar(60)',
'EndDateField' => 'Varchar(60)',
'DefaultDaysToDisplay' => 'Int'
);

private static $defaults = array(
Expand All @@ -30,8 +31,10 @@ public function getCMSFields() {
$classDbFields = ArrayLib::valuekey($classDbFields);
$dbFields = array_merge($dbFields, $classDbFields);
}
$fields->addFieldToTab('Root.Main', $field = DropdownField::create('StartDateField', 'Start Date Field', $dbFields));
$fields->addFieldToTab('Root.Main', $field = DropdownField::create('EndDateField', 'End Date Field', $dbFields));
$fields->addFieldToTab('Root.Main', DropdownField::create('StartDateField', 'Start Date Field', $dbFields));
$fields->addFieldToTab('Root.Main', DropdownField::create('EndDateField', 'End Date Field', $dbFields));
$fields->addFieldToTab('Root.Main', NumericField::create('DefaultDaysToDisplay')
);
});
$fields = parent::getCMSFields();
return $fields;
Expand All @@ -43,7 +46,7 @@ public function getCMSFields() {
public function getFilterFields() {
$fields = parent::getFilterFields();
$fields->push(DateField::create('StartDate', _t('ListFilterDateRange.START_DATE', 'Start Date')));
$fields->push(DateField::create('EndDate', _t('ListFilterDateRange.END_DATE', 'End Date')));
$fields->push(DateField::create('EndDate', _t('ListFilterDateRange.END_DATE', 'End Date'), $this->getEndDateDefault()));
return $fields;
}

Expand Down Expand Up @@ -73,7 +76,7 @@ public function getFilterData(DataObject $record) {
*/
public function applyFilter(SS_List $list, array $data) {
$start = isset($data['StartDate']) ? $data['StartDate'] : null;
$end = isset($data['EndDate']) ? $data['EndDate'] : null;
$end = isset($data['EndDate']) ? $data['EndDate'] : $this->getEndDateDefault();
if (!$start && !$end) {
return;
}
Expand Down Expand Up @@ -142,4 +145,8 @@ public function getContext() {
return implode(',', $result);
}
}

public function getEndDateDefault() {
return $this->DefaultDaysToDisplay ? date('Y-m-d', strtotime("+{$this->DefaultDaysToDisplay} days")) : null;
}
}
2 changes: 1 addition & 1 deletion code/model/ListFilterSolrDateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function getCMSFields()
*/
public function applyFilter(SS_List $list, array $data) {
$start = isset($data['StartDate']) ? $data['StartDate'] : null;
$end = isset($data['EndDate']) ? $data['EndDate'] : null;
$end = isset($data['EndDate']) ? $data['EndDate'] : $this->getEndDateDefault();

if (!$start && !$end) {
return;
Expand Down

0 comments on commit 7a99f2d

Please sign in to comment.