Add support for the ISO 8601 date format (for due date)
This commit is contained in:
parent
349b5f3f56
commit
12d7138a47
|
|
@ -289,4 +289,5 @@ return array(
|
|||
// 'Nothing found.' => '',
|
||||
// 'Search in the project "%s"' => '',
|
||||
// 'Due date' => '',
|
||||
// 'Others formats accepted: %s and %s' => '',
|
||||
);
|
||||
|
|
|
|||
|
|
@ -289,4 +289,5 @@ return array(
|
|||
'Nothing found.' => 'Rien trouvé.',
|
||||
'Search in the project "%s"' => 'Rechercher dans le projet « %s »',
|
||||
'Due date' => 'Date d\'échéance',
|
||||
'Others formats accepted: %s and %s' => 'Autres formats acceptés : %s et %s',
|
||||
);
|
||||
|
|
|
|||
|
|
@ -294,4 +294,5 @@ return array(
|
|||
// 'Nothing found.' => '',
|
||||
// 'Search in the project "%s"' => '',
|
||||
// 'Due date' => '',
|
||||
// 'Others formats accepted: %s and %s' => '',
|
||||
);
|
||||
|
|
|
|||
|
|
@ -290,4 +290,5 @@ return array(
|
|||
// 'Nothing found.' => '',
|
||||
// 'Search in the project "%s"' => '',
|
||||
// 'Due date' => '',
|
||||
// 'Others formats accepted: %s and %s' => '',
|
||||
);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ require_once __DIR__.'/base.php';
|
|||
|
||||
use \SimpleValidator\Validator;
|
||||
use \SimpleValidator\Validators;
|
||||
use DateTime;
|
||||
|
||||
/**
|
||||
* Task model
|
||||
|
|
@ -301,7 +302,7 @@ class Task extends Base
|
|||
}
|
||||
|
||||
if (! empty($values['date_due']) && ! is_numeric($values['date_due'])) {
|
||||
$values['date_due'] = $this->getTimestampFromDate($values['date_due'], t('m/d/Y')) ?: null;
|
||||
$values['date_due'] = $this->parseDate($values['date_due']);
|
||||
}
|
||||
|
||||
$values['date_creation'] = time();
|
||||
|
|
@ -334,7 +335,7 @@ class Task extends Base
|
|||
{
|
||||
// Prepare data
|
||||
if (! empty($values['date_due']) && ! is_numeric($values['date_due'])) {
|
||||
$values['date_due'] = $this->getTimestampFromDate($values['date_due'], t('m/d/Y')) ?: null;
|
||||
$values['date_due'] = $this->parseDate($values['date_due']);
|
||||
}
|
||||
|
||||
$original_task = $this->getById($values['id']);
|
||||
|
|
@ -472,7 +473,7 @@ class Task extends Base
|
|||
new Validators\Integer('score', t('This value must be an integer')),
|
||||
new Validators\Required('title', t('The title is required')),
|
||||
new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200),
|
||||
new Validators\Date('date_due', t('Invalid date'), t('m/d/Y')),
|
||||
new Validators\Date('date_due', t('Invalid date'), $this->getDateFormats()),
|
||||
));
|
||||
|
||||
return array(
|
||||
|
|
@ -523,7 +524,7 @@ class Task extends Base
|
|||
new Validators\Integer('score', t('This value must be an integer')),
|
||||
new Validators\Required('title', t('The title is required')),
|
||||
new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200),
|
||||
new Validators\Date('date_due', t('Invalid date'), t('m/d/Y')),
|
||||
new Validators\Date('date_due', t('Invalid date'), $this->getDateFormats()),
|
||||
));
|
||||
|
||||
return array(
|
||||
|
|
@ -557,19 +558,19 @@ class Task extends Base
|
|||
}
|
||||
|
||||
/**
|
||||
* Parse a date (different format for each locale) to a timestamp
|
||||
* Return a timestamp if the given date format is correct otherwise return 0
|
||||
*
|
||||
* @access public
|
||||
* @param string $value Date to parse
|
||||
* @param string $format Date format
|
||||
* @return integer
|
||||
*/
|
||||
public function getTimestampFromDate($value, $format)
|
||||
public function getValidDate($value, $format)
|
||||
{
|
||||
$date = \DateTime::createFromFormat($format, $value);
|
||||
$date = DateTime::createFromFormat($format, $value);
|
||||
|
||||
if ($date !== false) {
|
||||
$errors = \DateTime::getLastErrors();
|
||||
$errors = DateTime::getLastErrors();
|
||||
if ($errors['error_count'] === 0 && $errors['warning_count'] === 0) {
|
||||
$timestamp = $date->getTimestamp();
|
||||
return $timestamp > 0 ? $timestamp : 0;
|
||||
|
|
@ -578,4 +579,40 @@ class Task extends Base
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a date ad return a unix timestamp, try different date formats
|
||||
*
|
||||
* @access public
|
||||
* @param string $value Date to parse
|
||||
* @return integer
|
||||
*/
|
||||
public function parseDate($value)
|
||||
{
|
||||
foreach ($this->getDateFormats() as $format) {
|
||||
|
||||
$timestamp = $this->getValidDate($value, $format);
|
||||
|
||||
if ($timestamp !== 0) {
|
||||
return $timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of supported date formats
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getDateFormats()
|
||||
{
|
||||
return array(
|
||||
t('m/d/Y'),
|
||||
'Y-m-d',
|
||||
'Y_m_d',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@
|
|||
|
||||
<?= Helper\form_label(t('Due Date'), 'date_due') ?>
|
||||
<?= Helper\form_text('date_due', $values, $errors, array('placeholder="'.t('month/day/year').'"'), 'form-date') ?><br/>
|
||||
<div class="form-help"><?= t('Others formats accepted: %s and %s', date('Y-m-d'), date('Y_m_d')) ?></div>
|
||||
|
||||
<?= Helper\form_label(t('Description'), 'description') ?>
|
||||
<?= Helper\form_textarea('description', $values, $errors) ?><br/>
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
<?= Helper\form_label(t('Due Date'), 'date_due') ?>
|
||||
<?= Helper\form_text('date_due', $values, $errors, array('placeholder="'.t('month/day/year').'"'), 'form-date') ?><br/>
|
||||
<div class="form-help"><?= t('Others formats accepted: %s and %s', date('Y-m-d'), date('Y_m_d')) ?></div>
|
||||
|
||||
<?= Helper\form_label(t('Description'), 'description') ?>
|
||||
<?= Helper\form_textarea('description', $values, $errors) ?><br/>
|
||||
|
|
|
|||
|
|
@ -79,13 +79,19 @@ class TaskTest extends Base
|
|||
{
|
||||
$t = new Task($this->db, $this->event);
|
||||
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getTimestampFromDate('05/03/2014', 'd/m/Y')));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getTimestampFromDate('03/05/2014', 'm/d/Y')));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getTimestampFromDate('3/5/2014', 'm/d/Y')));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getTimestampFromDate('5/3/2014', 'd/m/Y')));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getTimestampFromDate('5/3/14', 'd/m/y')));
|
||||
$this->assertEquals(0, $t->getTimestampFromDate('5/3/14', 'd/m/Y'));
|
||||
$this->assertEquals(0, $t->getTimestampFromDate('5-3-2014', 'd/m/Y'));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('2014-03-05', 'Y-m-d')));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('2014_03_05', 'Y_m_d')));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('05/03/2014', 'd/m/Y')));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('03/05/2014', 'm/d/Y')));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('3/5/2014', 'm/d/Y')));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('5/3/2014', 'd/m/Y')));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('5/3/14', 'd/m/y')));
|
||||
$this->assertEquals(0, $t->getValidDate('5/3/14', 'd/m/Y'));
|
||||
$this->assertEquals(0, $t->getValidDate('5-3-2014', 'd/m/Y'));
|
||||
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->parseDate('2014-03-05')));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->parseDate('2014_03_05')));
|
||||
$this->assertEquals('2014-03-05', date('Y-m-d', $t->parseDate('03/05/2014')));
|
||||
}
|
||||
|
||||
public function testDuplicateTask()
|
||||
|
|
|
|||
|
|
@ -3,26 +3,26 @@
|
|||
namespace SimpleValidator\Validators;
|
||||
|
||||
use SimpleValidator\Base;
|
||||
use \DateTime;
|
||||
|
||||
class Date extends Base
|
||||
{
|
||||
private $format;
|
||||
private $formats = array();
|
||||
|
||||
public function __construct($field, $error_message, $format)
|
||||
public function __construct($field, $error_message, array $formats)
|
||||
{
|
||||
parent::__construct($field, $error_message);
|
||||
$this->format = $format;
|
||||
$this->formats = $formats;
|
||||
}
|
||||
|
||||
public function execute(array $data)
|
||||
{
|
||||
if (isset($data[$this->field]) && $data[$this->field] !== '') {
|
||||
|
||||
$date = \DateTime::createFromFormat($this->format, $data[$this->field]);
|
||||
|
||||
if ($date !== false) {
|
||||
$errors = \DateTime::getLastErrors();
|
||||
return $errors['error_count'] === 0 && $errors['warning_count'] === 0;
|
||||
foreach ($this->formats as $format) {
|
||||
if ($this->isValidDate($data[$this->field], $format) === true) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
@ -30,4 +30,19 @@ class Date extends Base
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isValidDate($value, $format)
|
||||
{
|
||||
$date = DateTime::createFromFormat($format, $value);
|
||||
|
||||
if ($date !== false) {
|
||||
$errors = DateTime::getLastErrors();
|
||||
if ($errors['error_count'] === 0 && $errors['warning_count'] === 0) {
|
||||
$timestamp = $date->getTimestamp();
|
||||
return $timestamp > 0 ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue