Skip to content

Commit

Permalink
Convert timestamps for widgets on encode/decode property value. Fixes c…
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhj committed Aug 14, 2018
1 parent bef74ce commit 52ea395
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* This file is part of contao-community-alliance/dc-general.
*
* (c) 2013-2018 Contao Community Alliance.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package contao-community-alliance/dc-general
* @author Richard Henkenjohann <richardhenkenjohann@googlemail.com>
* @copyright 2013-2018 Contao Community Alliance.
* @license https://github.com/contao-community-alliance/dc-general/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/

namespace ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\EventListener;


use Contao\Config;
use ContaoCommunityAlliance\Contao\Bindings\ContaoEvents;
use ContaoCommunityAlliance\Contao\Bindings\Events\Date\ParseDateEvent;
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\DecodePropertyValueForWidgetEvent;
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\EncodePropertyValueFromWidgetEvent;

class ConvertTimestampWidgetListener
{

/**
* Encode an timestamp attribute value from a widget value.
*
* @param EncodePropertyValueFromWidgetEvent $event The subscribed event.
*
* @return void
*/
public function handleEncodePropertyValueFromWidget(EncodePropertyValueFromWidgetEvent $event)
{
$environment = $event->getEnvironment();
$dataDefinition = $environment->getDataDefinition();
$propertyDefinition = $dataDefinition->getPropertiesDefinition()->getProperty($event->getProperty());

$extra = $propertyDefinition->getExtra();
$rgxp = $extra['rgxp'];
if (!\in_array($rgxp, ['date', 'time', 'datim'])) {
return;
}

$date = \DateTime::createFromFormat($this->getDateTimeFormat($rgxp), $event->getValue());

if ($date) {
$event->setValue($date->getTimestamp());
}
}

/**
* Decode an timestamp attribute value for a widget value.
*
* @param DecodePropertyValueForWidgetEvent $event The subscribed event.
*
* @return void
*/
public function handleDecodePropertyValueForWidgetEvent(DecodePropertyValueForWidgetEvent $event)
{
$environment = $event->getEnvironment();
$dataDefinition = $environment->getDataDefinition();
$propertyDefinition = $dataDefinition->getPropertiesDefinition()->getProperty($event->getProperty());

$extra = $propertyDefinition->getExtra();
$rgxp = $extra['rgxp'];
if (!\in_array($rgxp, ['date', 'time', 'datim'])) {
return;
}

$dispatcher = $event->getEnvironment()->getEventDispatcher();
$value = $event->getValue();

if (\is_numeric($value)) {
$dateEvent = new ParseDateEvent($value, $this->getDateTimeFormat($rgxp));
$dispatcher->dispatch(ContaoEvents::DATE_PARSE, $dateEvent);

$event->setValue($dateEvent->getResult());
}
}

private function getDateTimeFormat($format)
{
return Config::get($format.'Format');
}
}
10 changes: 10 additions & 0 deletions src/Resources/config/contao/widget_backend_listeners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ services:
- name: kernel.event_listener
event: dc-general.view.contao2backend.build-widget
method: handleEvent

cca.dc-general.backend_listener.convert_timestamp_widget:
class: ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\EventListener\ConvertTimestampWidgetListener
tags:
- name: kernel.event_listener
event: dc-general.view.contao2backend.decode-property-value-for-widget
method: handleDecodePropertyValueForWidgetEvent
- name: kernel.event_listener
event: dc-general.view.contao2backend.encode-property-value-from-widget
method: handleEncodePropertyValueFromWidget

0 comments on commit 52ea395

Please sign in to comment.