vendor/metamodels/attribute_timestamp/src/Attribute/Timestamp.php line 98

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of MetaModels/attribute_timestamp.
  4.  *
  5.  * (c) 2012-2019 The MetaModels team.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  *
  10.  * This project is provided in good faith and hope to be usable by anyone.
  11.  *
  12.  * @package    MetaModels/attribute_timestamp
  13.  * @author     Christian Schiffler <c.schiffler@cyberspectrum.de>
  14.  * @author     Stefan Heimes <stefan_heimes@hotmail.com>
  15.  * @author     Andreas Isaak <info@andreas-isaak.de>
  16.  * @author     David Greminger <david.greminger@1up.io>
  17.  * @author     David Maack <david.maack@arcor.de>
  18.  * @author     David Molineus <david.molineus@netzmacht.de>
  19.  * @author     Henry Lamorski <henry.lamorski@mailbox.org>
  20.  * @author     Ingolf Steinhardt <info@e-spin.de>
  21.  * @author     Sven Baumann <baumann.sv@gmail.com>
  22.  * @copyright  2012-2019 The MetaModels team.
  23.  * @license    https://github.com/MetaModels/attribute_timestamp/blob/master/LICENSE LGPL-3.0-or-later
  24.  * @filesource
  25.  */
  26. namespace MetaModels\AttributeTimestampBundle\Attribute;
  27. use Contao\System;
  28. use Contao\Config;
  29. use ContaoCommunityAlliance\Contao\Bindings\ContaoEvents;
  30. use ContaoCommunityAlliance\Contao\Bindings\Events\Date\ParseDateEvent;
  31. use Doctrine\DBAL\Connection;
  32. use MetaModels\AttributeNumericBundle\Attribute\Numeric;
  33. use MetaModels\Helper\TableManipulator;
  34. use MetaModels\IMetaModel;
  35. use MetaModels\Render\Setting\ISimple;
  36. use MetaModels\Render\Template;
  37. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  38. /**
  39.  * This is the MetaModelAttribute class for handling text fields.
  40.  */
  41. class Timestamp extends Numeric
  42. {
  43.     /**
  44.      * The event dispatcher.
  45.      *
  46.      * @var EventDispatcherInterface
  47.      */
  48.     private $dispatcher;
  49.     /**
  50.      * Instantiate an MetaModel attribute.
  51.      *
  52.      * Note that you should not use this directly but use the factory classes to instantiate attributes.
  53.      *
  54.      * @param IMetaModel                    $objMetaModel     The MetaModel instance this attribute belongs to.
  55.      * @param array                         $arrData          The information array for the attribute.
  56.      * @param Connection                    $connection       The database connection.
  57.      * @param TableManipulator              $tableManipulator Table manipulator instance.
  58.      * @param EventDispatcherInterface|null $dispatcher       The event dispatcher.
  59.      */
  60.     public function __construct(
  61.         IMetaModel $objMetaModel,
  62.         array $arrData = [],
  63.         Connection $connection null,
  64.         TableManipulator $tableManipulator null,
  65.         EventDispatcherInterface $dispatcher null
  66.     ) {
  67.         parent::__construct($objMetaModel$arrData$connection$tableManipulator);
  68.         if (null === $dispatcher) {
  69.             // @codingStandardsIgnoreStart
  70.             @\trigger_error(
  71.                 'Table event dispatcher is missing. It has to be passed in the constructor. Fallback will be dropped.',
  72.                 E_USER_DEPRECATED
  73.             );
  74.             // @codingStandardsIgnoreEnd
  75.             $dispatcher System::getContainer()->get('event_dispatcher');
  76.         }
  77.         $this->dispatcher $dispatcher;
  78.     }
  79.     /**
  80.      * {@inheritDoc}
  81.      */
  82.     public function getSQLDataType()
  83.     {
  84.         return 'bigint(10) NULL';
  85.     }
  86.     /**
  87.      * {@inheritDoc}
  88.      */
  89.     public function getFieldDefinition($arrOverrides = [])
  90.     {
  91.         $strDateType                 $this->getDateTimeType();
  92.         $arrFieldDef                 parent::getFieldDefinition($arrOverrides);
  93.         $arrFieldDef['eval']['rgxp'] = $strDateType;
  94.         if (empty($arrFieldDef['eval']['readonly'])) {
  95.             $arrFieldDef['eval']['datepicker'] = true;
  96.             $arrFieldDef['eval']['tl_class']  .= ' wizard';
  97.         }
  98.         return $arrFieldDef;
  99.     }
  100.     /**
  101.      * {@inheritDoc}
  102.      */
  103.     public function getAttributeSettingNames()
  104.     {
  105.         return \array_merge(
  106.             parent::getAttributeSettingNames(),
  107.             [
  108.                 'timetype',
  109.             ]
  110.         );
  111.     }
  112.     /**
  113.      * {@inheritDoc}
  114.      */
  115.     protected function prepareTemplate(Template $objTemplate$arrRowData$objSettings)
  116.     {
  117.         parent::prepareTemplate($objTemplate$arrRowData$objSettings);
  118.         /** @var ISimple $objSettings */
  119.         if ($objSettings->get('timeformat')) {
  120.             $objTemplate->format $objSettings->get('timeformat');
  121.         } else {
  122.             $objTemplate->format $this->getDateTimeFormatString();
  123.         }
  124.         if (!empty($objTemplate->raw)) {
  125.             $event = new ParseDateEvent($objTemplate->raw$objTemplate->format);
  126.             $this->dispatcher->dispatch(ContaoEvents::DATE_PARSE$event);
  127.             $objTemplate->parsedDate $event->getResult();
  128.         } else {
  129.             $objTemplate->parsedDate null;
  130.         }
  131.     }
  132.     /**
  133.      * Retrieve the selected type or fallback to 'date' if none selected.
  134.      *
  135.      * @return string
  136.      */
  137.     public function getDateTimeType()
  138.     {
  139.         return $this->get('timetype') ?: 'date';
  140.     }
  141.     /**
  142.      * Retrieve the selected type or fallback to 'date' if none selected.
  143.      *
  144.      * @return string
  145.      */
  146.     public function getDateTimeFormatString()
  147.     {
  148.         $format $this->getDateTimeType() . 'Format';
  149.         $page   $this->getObjPage();
  150.         if ($page && $page->$format) {
  151.             return $page->$format;
  152.         }
  153.         return Config::get($format);
  154.     }
  155.     /**
  156.      * {@inheritdoc}
  157.      */
  158.     public function widgetToValue($varValue$itemId)
  159.     {
  160.         // Check if we have some data.
  161.         if ($varValue === '') {
  162.             return null;
  163.         }
  164.         // If numeric we have already a integer value.
  165.         if (\is_numeric($varValue)) {
  166.             return (int) $varValue;
  167.         }
  168.         // @deprecated Parsing of string times is deprecated. Use the EncodePropertyValueFromWidgetEvent of the DCG
  169.         // instead.
  170.         // Make a unix timestamp from the string.
  171.         $date = new \DateTime($varValue);
  172.         return $date->getTimestamp();
  173.     }
  174.     /**
  175.      * {@inheritdoc}
  176.      */
  177.     public function getFilterOptions($idList$usedOnly, &$arrCount null)
  178.     {
  179.         $format $this->getDateTimeFormatString();
  180.         return \array_map(
  181.             function ($value) use ($format) {
  182.                 $event = new ParseDateEvent($value$format);
  183.                 $this->dispatcher->dispatch(ContaoEvents::DATE_PARSE$event);
  184.                 return $event->getResult();
  185.             },
  186.             parent::getFilterOptions($idList$usedOnly$arrCount)
  187.         );
  188.     }
  189.     /**
  190.      * Returns the global page object (replacement for super globals access).
  191.      *
  192.      * @return mixed The global page object
  193.      *
  194.      * @SuppressWarnings(PHPMD.Superglobals)
  195.      * @SuppressWarnings(PHPMD.CamelCaseVariableName)
  196.      */
  197.     protected function getObjPage()
  198.     {
  199.         return isset($GLOBALS['objPage']) ? $GLOBALS['objPage'] : null;
  200.     }
  201.     /**
  202.      * {@inheritDoc}
  203.      *
  204.      * This is needed for compatibility with MySQL strict mode.
  205.      */
  206.     public function serializeData($value)
  207.     {
  208.         return $value === '' null $value;
  209.     }
  210. }