Skip to content
Snippets Groups Projects

hotline #138070 : fix article indexation on form post

All threads resolved!
Compare and Show latest version
3 files
+ 129
43
Preferences
Compare changes
Files
3
+ 123
0
<?php
/**
* Copyright (c) 2012-2021, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Date_Iso {
const
DATE_PART_FR = '(\d{2})\/(\d{2})\/(\d{4})',
DATE_PART_US = '(\d{4})-(\d{2})-(\d{2})',
TIME_PART = '( \d{2}:\d{2})?',
DATE_FORMAT = 'YYYY-MM-dd',
TIME_FORMAT = ' HH:mm';
public function ensureDate($str) {
return $this->ensure($str);
}
public function ensureDateTime($str) {
return (new Class_Date_IsoWithTime)->ensure($str);
}
public function ensure($str) {
if (!$str)
return null;
/** @see http://forge.afi-sa.fr/issues/17968 */
if ($matches = $this->_match($this->_frPattern(), $str))
return $this->_fromFr($matches);
if ($matches = $this->_match($this->_usPattern(), $str))
return $this->_fromUs($matches);
return ($date = Class_Date::humanDate($str, $this->_dateFormat()))
? $date
: null;
}
protected function _match($pattern, $str) {
return preg_match('/^' . $pattern . '/', $str, $matches)
? $matches
: null;
}
protected function _frPattern() {
return static::DATE_PART_FR;
}
protected function _fromFr($matches) {
return $matches[3] . '-' . $matches[2] . '-' . $matches[1];
}
protected function _usPattern() {
return static::DATE_PART_US;
}
protected function _fromUs($matches) {
return $matches[1] . '-' . $matches[2] . '-' . $matches[3];
}
protected function _dateFormat() {
return static::DATE_FORMAT;
}
}
class Class_Date_IsoWithTime extends Class_Date_Iso {
protected function _frPattern() {
return static::DATE_PART_FR . static::TIME_PART;
}
protected function _fromFr($matches) {
return parent::_fromFr($matches) . $this->_ensureTime($matches);
}
protected function _usPattern() {
return static::DATE_PART_US . static::TIME_PART;
}
protected function _fromUs($matches) {
return parent::_fromUs($matches) . $this->_ensureTime($matches);
}
protected function _ensureTime($matches) {
return isset($matches[4]) ? $matches[4] : ' 00:00';
}
protected function _dateFormat() {
return static::DATE_FORMAT . static::TIME_FORMAT;
}
}