-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3d2ce3b
Showing
5 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# SilverStripe date range field | ||
|
||
A date range field for SilverStripe. Based heavily on dnadesign/silverstripe-datedropdownselectorfield but using SilverStripe's DateField. | ||
|
||
Our primary use case for this module was for use in a ModelAdmin within the CMS - it hasn't been tested anywhere else so use with care. | ||
|
||
## Installation | ||
|
||
Use composer: | ||
|
||
composer require 'deptinternalaffairsnz/silverstripe-date-range-field' '1.0.0' | ||
|
||
## Usage | ||
|
||
When configuring `searchable_fields` for a `DataObject` you can make use of the `DateRangeField` and `DateRangeFilter` like so: | ||
|
||
private static $searchable_fields = array( | ||
'Created' => array( | ||
'title' => 'created date', | ||
'field' => 'DeptInternalAffairsNZ\SilverStripe\DateRangeField', | ||
'filter' => 'DeptInternalAffairsNZ\SilverStripe\DateRangeFilter' | ||
) | ||
); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace DeptInternalAffairsNZ\SilverStripe; | ||
|
||
use CompositeField; | ||
use DateField; | ||
|
||
class DateRangeField extends CompositeField { | ||
|
||
/* @var $from DateField */ | ||
protected $from; | ||
|
||
/* @var $to DateField */ | ||
protected $to; | ||
|
||
public function __construct($name, $title = null, $value = null) { | ||
$this->name = $name; | ||
$this->setTitle($title); | ||
|
||
$this->from = new DateField($this->name . '[From]', $title, null); | ||
$this->to = new DateField($this->name . '[To]', $title, null); | ||
|
||
parent::__construct(array( | ||
$this->from, | ||
$this->to | ||
)); | ||
|
||
$this->setConfig('showcalendar', true); | ||
$this->setConfig('dateformat', 'yyyy-MM-dd'); | ||
|
||
$this->setValue($value); | ||
} | ||
|
||
public function setConfig($key, $value) { | ||
$this->from->setConfig($key, $value); | ||
$this->to->setConfig($key, $value); | ||
} | ||
|
||
public function hasData() { | ||
return true; | ||
} | ||
|
||
public function getValue() { | ||
return $this->value; | ||
} | ||
|
||
/** | ||
* Set the field name | ||
*/ | ||
public function setName($name) { | ||
$this->name = $name; | ||
$this->from->setName($name . '[From]'); | ||
$this->to->setName($name . '[To]'); | ||
return $this; | ||
} | ||
|
||
public function setTitle($title) { | ||
parent::setTitle($title); | ||
|
||
if ($this->from instanceof DateField) { | ||
$this->from->setTitle('From ' . $title); | ||
} | ||
|
||
if ($this->to instanceof DateField) { | ||
$this->to->setTitle('To ' . $title); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace DeptInternalAffairsNZ\SilverStripe; | ||
|
||
use Convert; | ||
use DataQuery; | ||
use SearchFilter; | ||
|
||
class DateRangeFilter extends SearchFilter { | ||
|
||
private $min, $max; | ||
|
||
public function findMinMax() { | ||
if (!isset($this->value)) { | ||
return false; | ||
} | ||
|
||
$value = $this->value; | ||
|
||
if(is_array($value)) { | ||
if (isset($value['From'])) { | ||
$fromDate = $value['From']; | ||
} | ||
|
||
if (isset($value['To'])) { | ||
$toDate = $value['To']; | ||
} | ||
|
||
$value = $fromDate . '-to-' . $toDate; | ||
} | ||
|
||
if (strpos($value, '-to-') !== FALSE) { | ||
$valueArray = explode('-to-', $value); | ||
|
||
if ($valueArray[0] != 0) { | ||
$this->setMin($valueArray[0]); | ||
} | ||
|
||
if ($valueArray[1] != 0) { | ||
$this->setMax($valueArray[1]); | ||
} | ||
} | ||
} | ||
|
||
public function setMin($min) { | ||
$this->min = $min; | ||
} | ||
|
||
public function setMax($max) { | ||
$this->max = $max; | ||
} | ||
|
||
public function apply(DataQuery $query) { | ||
if (!isset($this->min) || !isset($this->max)) { | ||
$this->findMinMax(); | ||
} | ||
|
||
if ($this->min) { | ||
$query->where(sprintf( | ||
"%s >= '%s'", | ||
$this->getDbName(), | ||
Convert::raw2sql($this->min) | ||
)); | ||
} | ||
|
||
if ($this->max) { | ||
$query->where(sprintf( | ||
"%s <= '%s'", | ||
$this->getDbName(), | ||
Convert::raw2sql($this->max) | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
* Applies a match on the starting characters of a field value. | ||
* | ||
* @return DataQuery | ||
*/ | ||
protected function applyOne(DataQuery $query) { | ||
return true; | ||
} | ||
|
||
/** | ||
* Applies a match on the starting characters of a field value. | ||
* | ||
* @return DataQuery | ||
*/ | ||
protected function applyMany(DataQuery $query) { | ||
return true; | ||
} | ||
|
||
/** | ||
* Excludes a match on the starting characters of a field value. | ||
* | ||
* @return DataQuery | ||
*/ | ||
protected function excludeOne(DataQuery $query) { | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "deptinternalaffairsnz/silverstripe-date-range-field", | ||
"description": "A date range field and filter for SilverStripe", | ||
"type": "silverstripe-module", | ||
"license": "BSD-3-Clause", | ||
"authors": [ | ||
{ | ||
"name": "James Goodman", | ||
"email": "james.s.goodman@gmail.com" | ||
} | ||
], | ||
"require": {}, | ||
"extra": { | ||
"installer-name": "date-range-field" | ||
} | ||
} |