/** * Renders a date time widget. * * @param array $data Data to render with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string A generated select box. * @throws \RuntimeException When option data is invalid. */ public function render(array $data, ContextInterface $context) { $id = $data['id']; $name = $data['name']; $val = $data['val']; $type = $data['type']; $required = $data['required'] ? 'required' : ''; $disabled = isset($data['disabled']) && $data['disabled'] ? 'disabled' : ''; $role = isset($data['role']) ? $data['role'] : 'datetime-picker'; $format = null; $locale = isset($data['locale']) ? $data['locale'] : I18n::locale(); $timezoneAware = Configure::read('CrudView.timezoneAwareDateTimeWidget'); $timestamp = null; $timezoneOffset = null; if (isset($data['data-format'])) { $format = $this->_convertPHPToMomentFormat($data['data-format']); } if (!$val instanceof DateTimeInterface && !empty($val)) { $val = $type === 'date' ? Time::parseDate($val) : Time::parseDateTime($val); } if ($val) { if ($timezoneAware) { $timestamp = $val->format('U'); $dateTimeZone = new DateTimeZone(date_default_timezone_get()); $timezoneOffset = $dateTimeZone->getOffset($val) / 60; } $val = $val->format($type === 'date' ? 'Y-m-d' : 'Y-m-d H:i:s'); } if (!$format) { $format = $type === 'date' ? 'L' : 'L LT'; } $widget = <<<html <div class="input-group {$type}"> <input type="text" class="form-control" name="{$name}" value="{$val}" id="{$id}" role="{$role}" data-locale="{$locale}" data-format="{$format}" html; if ($timezoneAware && isset($timestamp, $timezoneOffset)) { $widget .= <<<html data-timestamp="{$timestamp}" data-timezone-offset="{$timezoneOffset}" html; } $widget .= <<<html {$required} {$disabled} /> <label for="{$id}" class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </label> </div> html; return $widget; }
/** * Renders a date time widget. * * @param array $data Data to render with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string A generated select box. * @throws \RuntimeException When option data is invalid. */ public function render(array $data, ContextInterface $context) { $id = $data['id']; $name = $data['name']; $val = $data['val']; $type = $data['type']; $required = $data['required'] ? 'required' : ''; $role = isset($data['role']) ? $data['role'] : 'datetime-picker'; $format = null; $timestamp = null; $locale = locale_get_primary_language(I18n::locale()); if (isset($data['data-format'])) { $format = $this->_convertPHPToMomentFormat($data['data-format']); } if (!$val instanceof DateTime && !empty($val)) { $val = $type === 'date' ? Time::parseDate($val) : Time::parseDateTime($val); } if ($val) { $timestamp = $val->format('U'); $val = $val->format($type === 'date' ? 'Y-m-d' : 'Y-m-d H:i:s'); } if (!$format) { $format = $type === 'date' ? 'L' : 'L LT'; } $widget = <<<html <div class="input-group {$type}"> <input type="text" class="form-control" name="{$name}" value="{$val}" id="{$id}" role="{$role}" data-locale="{$locale}" data-format="{$format}" data-timestamp="{$timestamp}" {$required} /> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div> html; return $widget; }
/** * Initialize method * * @param array $config The configuration for the Table. * @return void */ public function initialize(array $config) { parent::initialize($config); $this->table('lumisials'); $this->displayField('name'); $this->primaryKey('id'); $this->hasMany('Accounts', ['foreignKey' => 'lumisial_id']); $this->hasMany('Bills', ['foreignKey' => 'lumisial_id']); $this->hasMany('Boards', ['foreignKey' => 'lumisial_id']); $this->hasMany('Furnitures', ['foreignKey' => 'lumisial_id']); $this->hasMany('Materials', ['foreignKey' => 'lumisial_id']); $this->hasMany('Members', ['foreignKey' => 'lumisial_id']); $this->eventManager()->on('Model.beforeMarshal', function ($event, $data) { if (!empty($data['consag_date']) && is_string($data['consag_date'])) { $data['consag_date'] = Time::parseDateTime($data['consag_date'], 'dd/MM/yyyy'); } }); }
/** * Initialize method * * @param array $config The configuration for the Table. * @return void */ public function initialize(array $config) { parent::initialize($config); $this->table('bills'); $this->displayField('name'); $this->primaryKey('id'); $this->belongsTo('Lumisials', ['foreignKey' => 'lumisial_id', 'joinType' => 'INNER']); $this->belongsTo('Accounts', ['foreignKey' => 'account_id', 'joinType' => 'INNER']); $this->hasMany('Statements', ['foreignKey' => 'bill_id', 'joinType' => 'INNER']); $this->eventManager()->on('Model.beforeMarshal', function ($event, $data) { if (!empty($data['expire_date']) && is_string($data['expire_date'])) { $data['expire_date'] = Time::parseDateTime($data['expire_date'], 'dd/MM/yyyy'); } if (!empty($data['payment_date']) && is_string($data['payment_date'])) { $data['payment_date'] = Time::parseDateTime($data['payment_date'], 'dd/MM/yyyy'); } }); }
/** * Initialize method * * @param array $config The configuration for the Table. * @return void */ public function initialize(array $config) { parent::initialize($config); $this->table('members'); $this->displayField('name'); $this->primaryKey('id'); $this->addBehavior('Timestamp'); $this->belongsTo('Lumisials', ['foreignKey' => 'lumisial_id', 'joinType' => 'INNER']); $this->hasMany('Donations', ['foreignKey' => 'member_id']); $this->hasMany('Furnitures', ['foreignKey' => 'member_id']); $this->hasMany('Materials', ['foreignKey' => 'member_id']); $this->belongsToMany('Boards', ['foreignKey' => 'member_id', 'targetForeignKey' => 'board_id', 'joinTable' => 'boards_members']); $this->eventManager()->on('Model.beforeMarshal', function ($event, $data) { if (!empty($data['consag_date']) && is_string($data['consag_date'])) { $data['consag_date'] = Time::parseDateTime($data['consag_date'], 'dd/MM/yyyy'); } }); }
/** * Tests parsing a string into a Time object based on the locale format. * * @return void */ public function testParseDateTime() { $time = Time::parseDateTime('10/13/2013 12:54am'); $this->assertNotNull($time); $this->assertEquals('2013-10-13 00:54', $time->format('Y-m-d H:i')); Time::$defaultLocale = 'fr-FR'; $time = Time::parseDateTime('13 10, 2013 12:54'); $this->assertNotNull($time); $this->assertEquals('2013-10-13 12:54', $time->format('Y-m-d H:i')); $time = Time::parseDateTime('13 foo 10 2013 12:54'); $this->assertNull($time); }