Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrannosaurusjames committed Sep 10, 2015
0 parents commit 3d2ce3b
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
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 added _config.php
Empty file.
69 changes: 69 additions & 0 deletions code/DateRangeField.php
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);
}
}

}
102 changes: 102 additions & 0 deletions code/DateRangeFilter.php
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;
}

}
16 changes: 16 additions & 0 deletions composer.json
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"
}
}

0 comments on commit 3d2ce3b

Please sign in to comment.