2011-08-30 22:50:55 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EGroupware - eTemplate serverside date widget
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
2011-09-04 10:37:39 +02:00
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
|
|
|
* @copyright 2002-11 by RalfBecker@outdoor-training.de
|
2011-08-30 22:50:55 +02:00
|
|
|
* @author Nathan Gray
|
|
|
|
* @copyright 2011 Nathan Gray
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* eTemplate date widget
|
2011-09-04 10:37:39 +02:00
|
|
|
*
|
2011-08-30 22:50:55 +02:00
|
|
|
* Deals with date and time. Overridden to handle date-houronly as a transform
|
2011-09-04 10:37:39 +02:00
|
|
|
*
|
|
|
|
* Supported attributes: dataformat[,mode]
|
|
|
|
* dataformat: '' = timestamps or automatic conversation, or eg. 'Y-m-d H:i:s' for 2002-12-31 23:59:59
|
|
|
|
* mode: &1 = year is int-input not selectbox, &2 = show a [Today] button, (html-UI always uses jscal and dont care for &1+&2)
|
|
|
|
* &4 = 1min steps for time (default is 5min, with fallback to 1min if value is not in 5min-steps),
|
|
|
|
* &8 = dont show time for readonly and type date-time if time is 0:00,
|
|
|
|
* &16 = prefix r/o display with dow
|
|
|
|
* &32 = prefix r/o display with week-number
|
|
|
|
* &64 = prefix r/o display with weeknumber and dow
|
|
|
|
* &128 = no icon to trigger popup, click into input trigers it, also removing the separators to save space
|
|
|
|
*
|
|
|
|
* @todo validation of date-duration
|
2014-05-05 16:11:00 +02:00
|
|
|
*
|
2014-08-19 23:22:26 +02:00
|
|
|
* @info Communication between client and server is always done as a string in ISO8601/W3C
|
|
|
|
* format ("Y-m-d\TH:i:sP"). If the application specifies a different format
|
|
|
|
* for the field, the conversion is done as needed understand what the application
|
|
|
|
* sends, and to give the application what it wants when the form is submitted.
|
2011-08-30 22:50:55 +02:00
|
|
|
*/
|
|
|
|
class etemplate_widget_date extends etemplate_widget_transformer
|
|
|
|
{
|
|
|
|
protected static $transformation = array(
|
|
|
|
'type' => array('date-houronly' => 'select-hour')
|
|
|
|
);
|
2011-09-04 10:37:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* (Array of) comma-separated list of legacy options to automatically replace when parsing with set_attrs
|
|
|
|
*
|
|
|
|
* @var string|array
|
|
|
|
*/
|
|
|
|
protected $legacy_options = 'dataformat,mode';
|
2014-06-11 11:14:43 +02:00
|
|
|
|
|
|
|
|
2014-08-19 23:22:26 +02:00
|
|
|
/**
|
|
|
|
* Convert the provided date into the format needed for unambiguous communication
|
|
|
|
* with browsers (Javascript). We use W3C format to avoid timestamp issues.
|
|
|
|
*/
|
|
|
|
public function beforeSendToClient($cname)
|
|
|
|
{
|
|
|
|
if($this->type == 'date-houronly')
|
|
|
|
{
|
|
|
|
return parent::beforeSendToClient($cname);
|
|
|
|
}
|
|
|
|
|
|
|
|
$form_name = self::form_name($cname, $this->id);
|
|
|
|
$value =& self::get_array(self::$request->content, $form_name, false, true);
|
2014-09-10 12:22:55 +02:00
|
|
|
|
2014-08-19 23:22:26 +02:00
|
|
|
if($this->type != 'date-duration' && $value)
|
|
|
|
{
|
|
|
|
// string with formatting letters like for php's date() method
|
|
|
|
if ($this->attrs['dataformat'] && !is_numeric($value))
|
|
|
|
{
|
2014-09-10 12:22:55 +02:00
|
|
|
$date = date_create_from_format($this->attrs['dataformat'], $value, egw_time::$user_timezone);
|
2014-08-19 23:22:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-10 12:22:55 +02:00
|
|
|
$date = new egw_time($value);
|
2014-08-19 23:22:26 +02:00
|
|
|
}
|
2014-08-25 21:33:35 +02:00
|
|
|
if($this->type == 'date-timeonly')
|
|
|
|
{
|
|
|
|
$date->setDate(1970, 1, 1);
|
|
|
|
}
|
2014-08-19 23:22:26 +02:00
|
|
|
if($date)
|
|
|
|
{
|
2014-09-10 12:22:55 +02:00
|
|
|
// postfix date-string with "Z" so javascript doesn't add/subtract anything
|
|
|
|
$value = $date->format('Y-m-d\TH:i:s\Z');
|
2014-08-19 23:22:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-04 10:37:39 +02:00
|
|
|
/**
|
|
|
|
* Validate input
|
|
|
|
*
|
2014-08-19 23:22:26 +02:00
|
|
|
* For dates (except duration), it is always a full timestamp in W3C format,
|
|
|
|
* which we then convert to the format the application is expecting. This can
|
|
|
|
* be either a unix timestamp, just a date, just time, or whatever is
|
|
|
|
* specified in the template.
|
|
|
|
*
|
2011-09-04 10:37:39 +02:00
|
|
|
* @param string $cname current namespace
|
2012-05-03 16:17:47 +02:00
|
|
|
* @param array $expand values for keys 'c', 'row', 'c_', 'row_', 'cont'
|
2011-09-04 10:37:39 +02:00
|
|
|
* @param array $content
|
|
|
|
* @param array &$validated=array() validated content
|
|
|
|
* @return boolean true if no validation error, false otherwise
|
|
|
|
*/
|
2012-05-03 16:17:47 +02:00
|
|
|
public function validate($cname, array $expand, array $content, &$validated=array())
|
2011-09-04 10:37:39 +02:00
|
|
|
{
|
2012-05-03 16:17:47 +02:00
|
|
|
$form_name = self::form_name($cname, $this->id, $expand);
|
2011-09-04 10:37:39 +02:00
|
|
|
|
2012-05-03 16:17:47 +02:00
|
|
|
if (!$this->is_readonly($cname, $form_name) && $this->type != 'date-since') // date-since is always readonly
|
|
|
|
{
|
2011-09-04 10:37:39 +02:00
|
|
|
$value = self::get_array($content, $form_name);
|
|
|
|
$valid =& self::get_array($validated, $form_name, true);
|
2014-09-10 12:22:55 +02:00
|
|
|
|
2011-09-04 10:37:39 +02:00
|
|
|
if ((string)$value === '' && $this->attrs['needed'])
|
|
|
|
{
|
|
|
|
self::set_validation_error($form_name,lang('Field must not be empty !!!'));
|
|
|
|
}
|
2014-06-12 09:51:41 +02:00
|
|
|
elseif (is_null($value))
|
|
|
|
{
|
|
|
|
$valid = null;
|
|
|
|
}
|
2013-02-06 15:36:24 +01:00
|
|
|
elseif ($this->type == 'date-duration')
|
|
|
|
{
|
2014-06-11 11:14:43 +02:00
|
|
|
$valid = (string)$value === '' ? '' : (int)$value;
|
2013-02-06 15:36:24 +01:00
|
|
|
}
|
2014-08-19 23:22:26 +02:00
|
|
|
if($value)
|
|
|
|
{
|
|
|
|
$date = new egw_time($value);
|
|
|
|
}
|
|
|
|
if(!$value)
|
|
|
|
{
|
|
|
|
// Not null, blank
|
|
|
|
$value = '';
|
|
|
|
}
|
2011-09-04 10:37:39 +02:00
|
|
|
elseif (empty($this->attrs['dataformat'])) // integer timestamp
|
|
|
|
{
|
2014-08-19 23:22:26 +02:00
|
|
|
$valid = $date->format('ts');
|
2011-09-04 10:37:39 +02:00
|
|
|
}
|
|
|
|
// string with formatting letters like for php's date() method
|
2014-08-19 23:22:26 +02:00
|
|
|
elseif (($valid = $date->format($this->attrs['dataformat'])))
|
2011-09-04 10:37:39 +02:00
|
|
|
{
|
2012-07-05 20:24:20 +02:00
|
|
|
// Nothing to do here
|
2011-09-04 10:37:39 +02:00
|
|
|
}
|
2012-07-05 20:24:20 +02:00
|
|
|
else
|
2011-09-04 10:37:39 +02:00
|
|
|
{
|
|
|
|
// this is not really a user error, but one of the clientside engine
|
2011-10-18 18:33:41 +02:00
|
|
|
self::set_validation_error($form_name,lang("'%1' is not a valid date !!!", $value).' '.$this->dataformat);
|
2011-09-04 10:37:39 +02:00
|
|
|
}
|
2014-08-19 23:22:26 +02:00
|
|
|
//error_log("$this : ($valid)" . egw_time::to($valid));
|
2011-09-04 10:37:39 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-30 22:50:55 +02:00
|
|
|
}
|