HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/lmbm.komma.pro/app/KommaApp/Kms/Core/Attributes/DatePicker.php
<?php
namespace App\KommaApp\Kms\Core\Attributes;
use App\KommaApp\Kms\Core\Attributes\Traits\ExplanationTrait;
use App\KommaApp\Kms\Core\Attributes\Traits\LabelTrait;
use App\KommaApp\Kms\Core\Attributes\Traits\PlaceholderTextTrait;
use App\KommaApp\Kms\Core\Attributes\Traits\ReadOnlyTrait;
use Carbon\Carbon;
use InvalidArgumentException;

/**
 * Class TextField
 * @package App\KommaApp\Kms\Core\Attributes
 */
class DatePicker extends Attribute
{
    use LabelTrait;
    use PlaceholderTextTrait;
    use ReadOnlyTrait;
    use ExplanationTrait;

    public const ANIMATION_SLIDEDOWN = 'slideDown';
    public const ANIMATION_FADEIN = 'fadeIn';
    public const ANIMATION_BLIND = 'blind';
    public const ANIMATION_BOUNCE = 'bounce';
    public const ANIMATION_CLIP = 'clip';
    public const ANIMATION_DROP = 'drop';
    public const ANIMATION_FOLD = 'fold';
    public const ANIMATION_SLIDE = 'slide';

    public const DATE_FORMAT_EUROPEAN = 'dd/mm/yy';
    public const DATE_FORMAT_AMERICAN = 'mm/dd/yy';

    public const TIME_FORMAT_12 = 12;
    public const TIME_FORMAT_24 = 24;

    public const LANGUAGE_ENGLISH = '';
    public const LANGUAGE_DUTCH = 'nl';

    /** @var bool whether or not you can see dates from other months in the current month and select them*/
    private $showAndSelectOtherMonths;

    /** @var string the Animation to use when opening and closing the picker*/
    private $animation;

    /** @var bool Shows the button bar or not. This is the bar below the calendar that has a today button and a done button*/
    private $buttonBar;

    /** @var bool Enables the user to change the month via a select menu */
    private $changeMonth;

    /** @var bool Enables the user to change the year via a select menu */
    private $changeYear;

    /** @var string $dateFormat DatePicker format constant */
    private $dateFormat;

    /** @var string $dateFormat DatePicker format constant */
    private $timeFormat;

    /** @var bool $showLongMonthNames If true, shows the month names in the long form. If false shows the short form of month names*/
    private $showLongMonthNames;

    /**
     * @var string $language DatePicker language constant
     */
    private $language;

    /** @var bool $timeEnabled */
    private $timeEnabled;

    /**
     * TextField constructor.
     * @param string $labelText
     */
    public function __construct(string $labelText)
    {
        $this->setLabelText($labelText);
        $this->language = DatePicker::LANGUAGE_ENGLISH;
        $this->showAndSelectOtherMonths = true;
        $this->animation = null;
        $this->buttonBar = false;
        $this->changeMonth = true;
        $this->changeYear = true;
        $this->showLongMonthNames = true;
        $this->dateFormat = DatePicker::DATE_FORMAT_EUROPEAN;
        $this->timeFormat = DatePicker::TIME_FORMAT_24;
        $this->timeEnabled = true;

        parent::__construct();
    }

    /**
     * Returns a view that visually represents this attribute
     */
    public function render()
    {
        return \View::make('kms/attributes.datePicker', [
            'attribute' => $this
        ]);
    }

    /**
     * @return bool
     */
    public function getShowAndSelectOtherMonths(): bool
    {
        return $this->showAndSelectOtherMonths;
    }

    /**
     * @param bool $showAndSelectOtherMonths
     */
    public function setShowAndSelectOtherMonths(bool $showAndSelectOtherMonths): void
    {
        $this->showAndSelectOtherMonths = $showAndSelectOtherMonths;
    }

    /**
     * @param string $animationConstant
     * @return DatePicker
     */
    public function setAnimation(string $animationConstant): DatePicker
    {
        switch($animationConstant)
        {
            case DatePicker::ANIMATION_SLIDEDOWN:
                $this->animation = DatePicker::ANIMATION_SLIDEDOWN;
                break;
            case DatePicker::ANIMATION_FADEIN:
                $this->animation = DatePicker::ANIMATION_FADEIN;
                break;
            case DatePicker::ANIMATION_BLIND:
                $this->animation = DatePicker::ANIMATION_BLIND;
                break;
            case DatePicker::ANIMATION_BOUNCE:
                $this->animation = DatePicker::ANIMATION_BOUNCE;
                break;
            case DatePicker::ANIMATION_CLIP:
                $this->animation = DatePicker::ANIMATION_CLIP;
                break;
            case DatePicker::ANIMATION_DROP:
                $this->animation = DatePicker::ANIMATION_DROP;
                break;
            case DatePicker::ANIMATION_FOLD:
                $this->animation = DatePicker::ANIMATION_FOLD;
                break;
            case DatePicker::ANIMATION_SLIDE:
                $this->animation = DatePicker::ANIMATION_SLIDE;
                break;
            default:
                $this->animation = null;
        }

        return $this;
    }

    /**
     * @return string
     */
    public function getAnimation(): ?string
    {
        return $this->animation;
    }

    /**
     * @return bool
     */
    public function getButtonBar(): bool
    {
        return $this->buttonBar;
    }

    /**
     * @param bool $buttonBar
     */
    public function setButtonBar(bool $buttonBar): void
    {
        $this->buttonBar = $buttonBar;
    }

    /**
     * @return bool
     */
    public function getChangeMonth(): bool
    {
        return $this->changeMonth;
    }

    /**
     * @param bool $changeMonth
     */
    public function setChangeMonth(bool $changeMonth): void
    {
        $this->changeMonth = $changeMonth;
    }

    /**
     * @return bool
     */
    public function getShowLongMonthNames(): bool
    {
        return $this->showLongMonthNames;
    }

    /**
     * @param bool $showLongMonthNames
     */
    public function setShowLongMonthNames(bool $showLongMonthNames): void
    {
        $this->showLongMonthNames = $showLongMonthNames;
    }

    /**
     * @return bool
     */
    public function getChangeYear(): bool
    {
        return $this->changeYear;
    }

    /**
     * @param bool $changeYear
     */
    public function setChangeYear(bool $changeYear): void
    {
        $this->changeYear = $changeYear;
    }

    /**
     * @return bool
     */
    public function getTimeEnabled(): bool
    {
        return $this->timeEnabled;
    }

    /**
     * @param bool $timeEnabled
     * @return DatePicker
     */
    public function setTimeEnabled(bool $timeEnabled): DatePicker
    {
        $this->timeEnabled = $timeEnabled;
        return $this;
    }

    /**
     * @param bool $changeYearAndMonth
     * @return DatePicker
     */
    public function setChangeYearAndMonth(bool $changeYearAndMonth): DatePicker
    {
        $this->changeYear = $changeYearAndMonth;
        $this->changeMonth = $changeYearAndMonth;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getDateFormat():string
    {
        return $this->dateFormat;
    }

    /**
     * @param mixed $format
     * @return DatePicker
     */
    public function setDateFormat($format): DatePicker
    {
        switch ($format)
        {
            case DatePicker::DATE_FORMAT_AMERICAN:
                $this->dateFormat = DatePicker::DATE_FORMAT_AMERICAN;
                break;
            case DatePicker::DATE_FORMAT_EUROPEAN:
            default:
                $this->dateFormat = DatePicker::DATE_FORMAT_EUROPEAN;
                break;
        }

        return $this;
    }

    /**
     * @return mixed
     */
    public function getTimeFormat():string
    {
        return $this->timeFormat;
    }

    /**
     * @param mixed $format
     * @return DatePicker
     */
    public function setTimeFormat($format): DatePicker
    {
        switch ($format)
        {
            case DatePicker::TIME_FORMAT_12:
                throw new \RuntimeException('Not implemented');
                $this->timeFormat = DatePicker::TIME_FORMAT_12;
                break;
            case DatePicker::TIME_FORMAT_24:
            default:
                $this->timeFormat = DatePicker::TIME_FORMAT_24;
                break;
        }

        return $this;
    }

    /**
     * @return string
     */
    public function getLanguage():string
    {
        return $this->language;
    }

    /**
     * @param string $language
     * @return DatePicker
     */
    public function setLanguage($language): DatePicker
    {
        switch ($language)
        {

            case DatePicker::LANGUAGE_ENGLISH:

                $this->language = DatePicker::LANGUAGE_ENGLISH;
                break;

            case DatePicker::LANGUAGE_DUTCH:
                $this->language = DatePicker::LANGUAGE_DUTCH;
                break;
        }

        return $this;
    }

    /**
     * @param string $value The value associated with the attribute. Specified in this format:
     * 2018-02-23 00:00:00 (Y-m-d H:i:s) or this format: {"year":2018,"month":4,"day":14,"hour":18,"minute":2,"second":0}.
     * Internally always stores it as as the json format
     * @throws InvalidArgumentException
     * @return DatePicker
     */
    public function setValue(string $value): DatePicker
    {
        $decodedData = json_decode($value, true);
        if($decodedData === null) {
            //Value is a database date time string like this: 2018-02-23 00:00:00
            $dateTime = Carbon::createFromFormat('Y-m-d H:i:s', $value, 'Europe/Amsterdam');

            $dateArray = [
                'year' => $dateTime->year,
                'month' => $dateTime->month,
                'day' => $dateTime->day,
                'hour' => $dateTime->hour,
                'minute' => $dateTime->minute,
                'second' => $dateTime->second,
            ];

            $valueForParent = json_encode($dateArray);
            parent::setValue($valueForParent);
        } else {
            if(count(array_diff(['year', 'month', 'day', 'hour', 'minute', 'second'], array_keys($decodedData))) > 0) throw new \InvalidArgumentException('When you specify the value as a json string it must have these properties: year, month, day, hour, minute and second');
            parent::setValue($value);
        }

        return $this;
    }

    /**
     * @return string Returns the date in the format of 2018-02-23 00:00:00 (Y-m-d H:i:s)
     */
    public function getValue(): string
    {
        $dateArray = json_decode($this->value, true);
        $value = Carbon::create(intval($dateArray['year']), intval($dateArray['month']), intval($dateArray['day']), intval($dateArray['hour']), intval($dateArray['minute']), intval($dateArray['second']), 'Europe/Amsterdam');

        return $value->toDateTimeString();
    }

    /**
     * @return string the value as json like this: {"year":2018,"month":4,"day":14,"hour":18,"minute":2,"second":0}
     */
    public function getValueAsJson():string
    {
        return $this->value;
    }
}