From 656f4306325d7944280ce111d95aeaeacab8d3cd Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 5 Jun 2016 21:28:56 -0400 Subject: Changed DateParser to use only user format and ISO-8601 --- app/Controller/TaskModificationController.php | 5 +- app/Controller/TaskViewController.php | 2 +- app/Core/DateParser.php | 66 ++++++++++++++++++++++++--- app/Export/TransitionExport.php | 2 +- app/Validator/TaskValidator.php | 4 +- 5 files changed, 65 insertions(+), 14 deletions(-) (limited to 'app') diff --git a/app/Controller/TaskModificationController.php b/app/Controller/TaskModificationController.php index fc9113dd..50583df4 100644 --- a/app/Controller/TaskModificationController.php +++ b/app/Controller/TaskModificationController.php @@ -91,11 +91,10 @@ class TaskModificationController extends BaseController $values = $task; $values = $this->hook->merge('controller:task:form:default', $values, array('default_values' => $values)); $values = $this->hook->merge('controller:task-modification:form:default', $values, array('default_values' => $values)); + $values = $this->dateParser->format($values, array('date_due'), $this->dateParser->getUserDateFormat()); + $values = $this->dateParser->format($values, array('date_started'), $this->dateParser->getUserDateTimeFormat()); } - $values = $this->dateParser->format($values, array('date_due'), $this->configModel->get('application_date_format', DateParser::DATE_FORMAT)); - $values = $this->dateParser->format($values, array('date_started'), $this->configModel->get('application_datetime_format', DateParser::DATE_TIME_FORMAT)); - $this->response->html($this->template->render('task_modification/edit_task', array( 'project' => $project, 'values' => $values, diff --git a/app/Controller/TaskViewController.php b/app/Controller/TaskViewController.php index 2a79ee45..073434b2 100644 --- a/app/Controller/TaskViewController.php +++ b/app/Controller/TaskViewController.php @@ -70,7 +70,7 @@ class TaskViewController extends BaseController 'time_spent' => $task['time_spent'] ?: '', ); - $values = $this->dateParser->format($values, array('date_started'), $this->configModel->get('application_datetime_format', DateParser::DATE_TIME_FORMAT)); + $values = $this->dateParser->format($values, array('date_started'), $this->dateParser->getUserDateTimeFormat()); $this->response->html($this->helper->layout->task('task/show', array( 'task' => $task, diff --git a/app/Core/DateParser.php b/app/Core/DateParser.php index 835eb3e3..bdab7998 100644 --- a/app/Core/DateParser.php +++ b/app/Core/DateParser.php @@ -15,6 +15,28 @@ class DateParser extends Base const DATE_FORMAT = 'm/d/Y'; const DATE_TIME_FORMAT = 'm/d/Y H:i'; + /** + * Get date format from settings + * + * @access public + * @return string + */ + public function getUserDateFormat() + { + return $this->configModel->get('application_date_format', DateParser::DATE_FORMAT); + } + + /** + * Get date time format from settings + * + * @access public + * @return string + */ + public function getUserDateTimeFormat() + { + return $this->configModel->get('application_datetime_format', DateParser::DATE_TIME_FORMAT); + } + /** * List of time formats * @@ -38,19 +60,29 @@ class DateParser extends Base */ public function getDateFormats($iso = false) { - $iso_formats = array( + $formats = array( + $this->getUserDateFormat(), + ); + + $isoFormats = array( 'Y-m-d', 'Y_m_d', ); - $user_formats = array( + $userFormats = array( 'm/d/Y', 'd/m/Y', 'Y/m/d', 'd.m.Y', ); - return $iso ? array_merge($iso_formats, $user_formats) : $user_formats; + if ($iso) { + $formats = array_merge($formats, $isoFormats, $userFormats); + } else { + $formats = array_merge($formats, $userFormats); + } + + return array_unique($formats); } /** @@ -62,7 +94,9 @@ class DateParser extends Base */ public function getDateTimeFormats($iso = false) { - $formats = array(); + $formats = array( + $this->getUserDateTimeFormat(), + ); foreach ($this->getDateFormats($iso) as $date) { foreach ($this->getTimeFormats() as $time) { @@ -70,7 +104,7 @@ class DateParser extends Base } } - return $formats; + return array_unique($formats); } /** @@ -97,12 +131,30 @@ class DateParser extends Base $values = array(); foreach ($formats as $format) { - $values[$format] = date($format); + $values[$format] = date($format).' ('.$format.')'; } return $values; } + /** + * Get formats for date parsing + * + * @access public + * @return array + */ + public function getParserFormats() + { + return array( + $this->getUserDateFormat(), + 'Y-m-d', + 'Y_m_d', + $this->getUserDateTimeFormat(), + 'Y-m-d H:i', + 'Y_m_d H:i', + ); + } + /** * Parse a date and return a unix timestamp, try different date formats * @@ -116,7 +168,7 @@ class DateParser extends Base return (int) $value; } - foreach ($this->getAllDateFormats(true) as $format) { + foreach ($this->getParserFormats() as $format) { $timestamp = $this->getValidDate($value, $format); if ($timestamp !== 0) { diff --git a/app/Export/TransitionExport.php b/app/Export/TransitionExport.php index 0df1f70e..5399edd8 100644 --- a/app/Export/TransitionExport.php +++ b/app/Export/TransitionExport.php @@ -68,7 +68,7 @@ class TransitionExport extends Base $transition['src_column'], $transition['dst_column'], $transition['name'] ?: $transition['username'], - date($this->configModel->get('application_datetime_format', DateParser::DATE_TIME_FORMAT), $transition['date']), + date($this->dateParser->getUserDateTimeFormat(), $transition['date']), round($transition['time_spent'] / 3600, 2) ); diff --git a/app/Validator/TaskValidator.php b/app/Validator/TaskValidator.php index 7c39ff51..90bda6f3 100644 --- a/app/Validator/TaskValidator.php +++ b/app/Validator/TaskValidator.php @@ -40,8 +40,8 @@ class TaskValidator extends BaseValidator new Validators\Integer('priority', t('This value must be an integer')), new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200), new Validators\MaxLength('reference', t('The maximum length is %d characters', 50), 50), - new Validators\Date('date_due', t('Invalid date'), $this->dateParser->getDateFormats(true)), - new Validators\Date('date_started', t('Invalid date'), $this->dateParser->getDateTimeFormats(true)), + new Validators\Date('date_due', t('Invalid date'), $this->dateParser->getParserFormats()), + new Validators\Date('date_started', t('Invalid date'), $this->dateParser->getParserFormats()), new Validators\Numeric('time_spent', t('This value must be numeric')), new Validators\Numeric('time_estimated', t('This value must be numeric')), ); -- cgit v1.2.3