vendor/pimcore/pimcore/models/Document/Editable/Date.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @category   Pimcore
  12.  * @package    Document
  13.  *
  14.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  15.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  16.  */
  17. namespace Pimcore\Model\Document\Editable;
  18. use Pimcore\Model;
  19. /**
  20.  * @method \Pimcore\Model\Document\Editable\Dao getDao()
  21.  */
  22. class Date extends Model\Document\Editable
  23. {
  24.     /**
  25.      * Contains the date
  26.      *
  27.      * @var \Carbon\Carbon|null
  28.      */
  29.     public $date;
  30.     /**
  31.      * @see EditableInterface::getType
  32.      *
  33.      * @return string
  34.      */
  35.     public function getType()
  36.     {
  37.         return 'date';
  38.     }
  39.     /**
  40.      * @see EditableInterface::getData
  41.      *
  42.      * @return mixed
  43.      */
  44.     public function getData()
  45.     {
  46.         return $this->date;
  47.     }
  48.     /**
  49.      * Converts the data so it's suitable for the editmode
  50.      *
  51.      * @return int|null
  52.      */
  53.     public function getDataEditmode()
  54.     {
  55.         if ($this->date) {
  56.             return $this->date->getTimestamp();
  57.         }
  58.         return null;
  59.     }
  60.     /**
  61.      * @see EditableInterface::frontend
  62.      */
  63.     public function frontend()
  64.     {
  65.         $format null;
  66.         if (isset($this->config['outputFormat']) && $this->config['outputFormat']) {
  67.             $format $this->config['outputFormat'];
  68.         } elseif (isset($this->config['format']) && $this->config['format']) {
  69.             $format $this->config['format'];
  70.         } else {
  71.             $format = \DateTime::ISO8601;
  72.         }
  73.         if ($this->date instanceof \DateTimeInterface) {
  74.             return $this->date->formatLocalized($format);
  75.         }
  76.     }
  77.     /**
  78.      * @see Tag::getDataForResource
  79.      *
  80.      * @return int|null
  81.      */
  82.     public function getDataForResource()
  83.     {
  84.         if ($this->date) {
  85.             return $this->date->getTimestamp();
  86.         }
  87.         return null;
  88.     }
  89.     /**
  90.      * @see EditableInterface::setDataFromResource
  91.      *
  92.      * @param mixed $data
  93.      *
  94.      * @return $this
  95.      */
  96.     public function setDataFromResource($data)
  97.     {
  98.         if ($data) {
  99.             $this->setDateFromTimestamp($data);
  100.         }
  101.         return $this;
  102.     }
  103.     /**
  104.      * @see EditableInterface::setDataFromEditmode
  105.      *
  106.      * @param mixed $data
  107.      *
  108.      * @return $this
  109.      */
  110.     public function setDataFromEditmode($data)
  111.     {
  112.         if (strlen($data) > 5) {
  113.             $timestamp strtotime($data);
  114.             $this->setDateFromTimestamp($timestamp);
  115.         }
  116.         return $this;
  117.     }
  118.     /**
  119.      * @return bool
  120.      */
  121.     public function isEmpty()
  122.     {
  123.         if ($this->date) {
  124.             return false;
  125.         }
  126.         return true;
  127.     }
  128.     /**
  129.      * Receives a Webservice\Data\Document\Element from webservice import and fill the current tag's data
  130.      *
  131.      * @deprecated
  132.      *
  133.      * @param Model\Webservice\Data\Document\Element $wsElement
  134.      * @param Model\Document\PageSnippet $document
  135.      * @param array $params
  136.      * @param Model\Webservice\IdMapperInterface|null $idMapper
  137.      *
  138.      * @throws \Exception
  139.      */
  140.     public function getFromWebserviceImport($wsElement$document null$params = [], $idMapper null)
  141.     {
  142.         if (!$wsElement or empty($wsElement->value)) {
  143.             $this->date null;
  144.         } elseif (is_numeric($wsElement->value)) {
  145.             $this->setDateFromTimestamp($wsElement->value);
  146.         } else {
  147.             throw new \Exception('cannot get document tag date from WS - invalid value [  '.$wsElement->value.' ]');
  148.         }
  149.     }
  150.     /**
  151.      * Returns the current tag's data for web service export
  152.      *
  153.      * @deprecated
  154.      *
  155.      * @param Model\Document\PageSnippet|null $document
  156.      * @param array $params
  157.      * @abstract
  158.      *
  159.      * @return int|null
  160.      */
  161.     public function getForWebserviceExport($document null$params = [])
  162.     {
  163.         if ($this->date) {
  164.             return $this->date->getTimestamp();
  165.         } else {
  166.             return null;
  167.         }
  168.     }
  169.     /**
  170.      * @param int $timestamp
  171.      */
  172.     protected function setDateFromTimestamp($timestamp)
  173.     {
  174.         $this->date = new \Carbon\Carbon();
  175.         $this->date->setTimestamp($timestamp);
  176.     }
  177. }
  178. class_alias(Date::class, 'Pimcore\Model\Document\Tag\Date');