Added start date condition

This commit is contained in:
Franck Allimant
2014-05-10 02:38:03 +02:00
parent c39de8bf87
commit f76e5d7aaa
2 changed files with 199 additions and 0 deletions

View File

@@ -0,0 +1,192 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace Thelia\Condition\Implementation;
use Thelia\Condition\Operators;
use Thelia\Coupon\FacadeInterface;
use Thelia\Exception\InvalidConditionOperatorException;
use Thelia\Exception\InvalidConditionValueException;
use Thelia\Model\Category;
use Thelia\Model\CategoryImageQuery;
use Thelia\Model\CategoryQuery;
use Thelia\Tools\DateTimeFormat;
/**
* Check a Checkout against its Product number
*
* @package Condition
* @author Franck Allimant <franck@cqfdev.fr>
*
*/
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
]);
}
}

View File

@@ -0,0 +1,7 @@
<input type="hidden" name="{$fieldName}[operator]" value="{$criteria}" />
<div id="condition-add-operators-values" class="form-group">
<label for="operator">{intl l="Validity start date"} :</label>
<input type="text" class="form-control" id="{$fieldName}-value" name="{$fieldName}[value]" value="{$currentValue}" placeholder="{intl l="Format: %fmt" fmt=$dateFormat}">
<span class="label-help-block">{intl l='Please enter the date using the %fmt format' fmt=$dateFormat}</span>
</div>