From f76e5d7aaa43da9e30164a72b8528071c3f61c92 Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Sat, 10 May 2014 02:38:03 +0200 Subject: [PATCH] Added start date condition --- .../Condition/Implementation/StartDate.php | 192 ++++++++++++++++++ .../start-date-condition.html | 7 + 2 files changed, 199 insertions(+) create mode 100644 core/lib/Thelia/Condition/Implementation/StartDate.php create mode 100644 templates/backOffice/default/coupon/condition-fragments/start-date-condition.html diff --git a/core/lib/Thelia/Condition/Implementation/StartDate.php b/core/lib/Thelia/Condition/Implementation/StartDate.php new file mode 100644 index 000000000..afcbb5814 --- /dev/null +++ b/core/lib/Thelia/Condition/Implementation/StartDate.php @@ -0,0 +1,192 @@ + + * + */ +class StartDate extends ConditionAbstract +{ + const START_DATE = 'start_date'; + + /** + * @inheritdoc + */ + public function __construct(FacadeInterface $facade) + { + $this->availableOperators = [ + self::START_DATE => [ + Operators::SUPERIOR_OR_EQUAL + ] + ]; + + parent::__construct($facade); + } + + /** + * @inheritdoc + */ + public function getServiceId() + { + return 'thelia.condition.start_date'; + } + + /** + * @inheritdoc + */ + public function setValidatorsFromForm(array $operators, array $values) + { + if (! isset($values[self::START_DATE])) { + $values[self::START_DATE] = time(); + } + + // Parse the entered date to get a timestamp, if we don't already have one + if (! is_int($values[self::START_DATE])) { + + $date = \DateTime::createFromFormat($this->getDateFormat(), $values[self::START_DATE]); + + // Check that the date is valid + if (false === $date) { + throw new InvalidConditionValueException( + get_class(), self::START_DATE + ); + } + + $timestamp = $date->getTimestamp(); + } + else { + $timestamp = $values[self::START_DATE]; + } + + $this->operators = [ self::START_DATE => $operators[self::START_DATE] ]; + $this->values = [ self::START_DATE => $timestamp ]; + + return $this; + } + + /** + * @inheritdoc + */ + public function isMatching() + { + return $this->conditionValidator->variableOpComparison( + time(), + $this->operators[self::START_DATE], + $this->values[self::START_DATE] + ); + } + + /** + * @inheritdoc + */ + public function getName() + { + return $this->translator->trans( + 'Start date', + [], + 'condition' + ); + } + + /** + * @inheritdoc + */ + public function getToolTip() + { + $toolTip = $this->translator->trans( + 'The coupon is valid after a given date', + [], + 'condition' + ); + + return $toolTip; + } + + /** + * @inheritdoc + */ + public function getSummary() + { + $i18nOperator = Operators::getI18n( + $this->translator, $this->operators[self::START_DATE] + ); + + $date = new \DateTime(); + $date->setTimestamp($this->values[self::START_DATE]); + $strDate = $date->format($this->getDateFormat()); + + $toolTip = $this->translator->trans( + 'Valid only from %date% to the coupon expiration date', [ + '%date%' => $strDate, + ], 'condition' + ); + + return $toolTip; + } + + private function getDateFormat() { + return DateTimeFormat::getInstance($this->facade->getRequest())->getFormat("date"); + } + + /** + * @inheritdoc + */ + protected function generateInputs() + { + return array( + self::START_DATE => array( + 'availableOperators' => $this->availableOperators[self::START_DATE], + 'value' => '', + 'selectedOperator' => Operators::SUPERIOR_OR_EQUAL + ) + ); + } + + /** + * @inheritdoc + */ + public function drawBackOfficeInputs() + { + if (isset($this->values[self::START_DATE])) { + + $date = new \DateTime(); + + $date->setTimestamp($this->values[self::START_DATE]); + + $strDate = $date->format($this->getDateFormat()); + } + else { + $strDate = ''; + } + + return $this->facade->getParser()->render('coupon/condition-fragments/start-date-condition.html', [ + 'fieldName' => self::START_DATE, + 'criteria' => Operators::SUPERIOR_OR_EQUAL, + 'dateFormat' => $this->getDateFormat(), + 'currentValue' => $strDate + ]); + } +} \ No newline at end of file diff --git a/templates/backOffice/default/coupon/condition-fragments/start-date-condition.html b/templates/backOffice/default/coupon/condition-fragments/start-date-condition.html new file mode 100644 index 000000000..453fe3541 --- /dev/null +++ b/templates/backOffice/default/coupon/condition-fragments/start-date-condition.html @@ -0,0 +1,7 @@ + + +
+ + + {intl l='Please enter the date using the %fmt format' fmt=$dateFormat} +
\ No newline at end of file