summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrédéric Guillot <fguillot@users.noreply.github.com>2014-04-27 19:48:36 -0400
committerFrédéric Guillot <fguillot@users.noreply.github.com>2014-04-27 19:48:36 -0400
commit12d7138a47d2c7a50ca8f0b2abb2266239c6197f (patch)
tree97e4024190d2cb09af9428993b76b77a82178a93
parent349b5f3f56f83d681fe6658c1a8475992dde9eea (diff)
Add support for the ISO 8601 date format (for due date)
-rw-r--r--locales/es_ES/translations.php1
-rw-r--r--locales/fr_FR/translations.php1
-rw-r--r--locales/pl_PL/translations.php1
-rw-r--r--locales/pt_BR/translations.php1
-rw-r--r--models/task.php53
-rw-r--r--templates/task_edit.php1
-rw-r--r--templates/task_new.php1
-rw-r--r--tests/TaskTest.php20
-rw-r--r--vendor/SimpleValidator/Validators/Date.php31
9 files changed, 87 insertions, 23 deletions
diff --git a/locales/es_ES/translations.php b/locales/es_ES/translations.php
index bf6f80e6..4c036f00 100644
--- a/locales/es_ES/translations.php
+++ b/locales/es_ES/translations.php
@@ -289,4 +289,5 @@ return array(
// 'Nothing found.' => '',
// 'Search in the project "%s"' => '',
// 'Due date' => '',
+ // 'Others formats accepted: %s and %s' => '',
);
diff --git a/locales/fr_FR/translations.php b/locales/fr_FR/translations.php
index 54659276..82467acd 100644
--- a/locales/fr_FR/translations.php
+++ b/locales/fr_FR/translations.php
@@ -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',
);
diff --git a/locales/pl_PL/translations.php b/locales/pl_PL/translations.php
index ed16779f..a1be50d3 100644
--- a/locales/pl_PL/translations.php
+++ b/locales/pl_PL/translations.php
@@ -294,4 +294,5 @@ return array(
// 'Nothing found.' => '',
// 'Search in the project "%s"' => '',
// 'Due date' => '',
+ // 'Others formats accepted: %s and %s' => '',
);
diff --git a/locales/pt_BR/translations.php b/locales/pt_BR/translations.php
index 54b357db..54538b1c 100644
--- a/locales/pt_BR/translations.php
+++ b/locales/pt_BR/translations.php
@@ -290,4 +290,5 @@ return array(
// 'Nothing found.' => '',
// 'Search in the project "%s"' => '',
// 'Due date' => '',
+ // 'Others formats accepted: %s and %s' => '',
);
diff --git a/models/task.php b/models/task.php
index bef92f20..f08f0b28 100644
--- a/models/task.php
+++ b/models/task.php
@@ -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',
+ );
+ }
}
diff --git a/templates/task_edit.php b/templates/task_edit.php
index 856f7d5d..9fe0418f 100644
--- a/templates/task_edit.php
+++ b/templates/task_edit.php
@@ -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/>
diff --git a/templates/task_new.php b/templates/task_new.php
index efb9cb62..f5797cdf 100644
--- a/templates/task_new.php
+++ b/templates/task_new.php
@@ -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/>
diff --git a/tests/TaskTest.php b/tests/TaskTest.php
index 701ba611..594d0e13 100644
--- a/tests/TaskTest.php
+++ b/tests/TaskTest.php
@@ -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()
diff --git a/vendor/SimpleValidator/Validators/Date.php b/vendor/SimpleValidator/Validators/Date.php
index 36b59fbe..54c949b0 100644
--- a/vendor/SimpleValidator/Validators/Date.php
+++ b/vendor/SimpleValidator/Validators/Date.php
@@ -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;
+ }
}