- Coupon - add rule AJAX
This commit is contained in:
gmorel
2013-09-09 16:40:53 +02:00
parent 849520eff9
commit b778898d92
23 changed files with 689 additions and 132 deletions

View File

@@ -0,0 +1,135 @@
<?php
/**********************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/**********************************************************************************/
namespace Thelia\Constraint\Rule;
use InvalidArgumentException;
use Symfony\Component\Translation\Translator;
use Thelia\Constraint\ConstraintValidator;
use Thelia\Constraint\Validator\QuantityParam;
use Thelia\Constraint\Validator\RuleValidator;
use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Exception\InvalidRuleException;
use Thelia\Exception\InvalidRuleValueException;
use Thelia\Type\FloatType;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Allow every one, perform no check
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForEveryoneManager extends CouponRuleAbstract
{
/** @var string Service Id from Resources/config.xml */
protected $serviceId = 'thelia.constraint.rule.available_for_everyone';
/** @var array Available Operators (Operators::CONST) */
protected $availableOperators = array();
/**
* Check validators relevancy and store them
*
* @param array $operators Operators the Admin set in BackOffice
* @param array $values Values the Admin set in BackOffice
*
* @throws \InvalidArgumentException
* @return $this
*/
public function setValidatorsFromForm(array $operators, array $values)
{
$this->setValidators();
return $this;
}
/**
* Check validators relevancy and store them
*
* @throws \InvalidArgumentException
* @return $this
*/
protected function setValidators()
{
$this->operators = array();
$this->values = array();
return $this;
}
/**
* Test if Customer meets conditions
*
* @return bool
*/
public function isMatching()
{
return true;
}
/**
* Get I18n name
*
* @return string
*/
public function getName()
{
return $this->translator->trans(
'Everybody can use it (no condition)',
array(),
'constraint'
);
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
$toolTip = $this->translator->trans(
'Will return always true',
array(),
'constraint'
);
return $toolTip;
}
/**
* Generate inputs ready to be drawn
*
* @return array
*/
protected function generateInputs()
{
return array();
}
}

View File

@@ -32,6 +32,8 @@ use Thelia\Constraint\Validator\RuleValidator;
use Thelia\Exception\InvalidRuleException;
use Thelia\Exception\InvalidRuleOperatorException;
use Thelia\Exception\InvalidRuleValueException;
use Thelia\Model\Currency;
use Thelia\Model\CurrencyQuery;
use Thelia\Type\FloatType;
/**
@@ -297,7 +299,7 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
*/
public function getName()
{
return $this->adapter->get('thelia.translator')->trans(
return $this->translator->trans(
'Cart total amount',
array(),
'constraint'
@@ -328,6 +330,40 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
return $toolTip;
}
/**
* Generate inputs ready to be drawn
*
* @return array
*/
protected function generateInputs()
{
$currencies = CurrencyQuery::create()->find();
$cleanedCurrencies = array();
/** @var Currency $currency */
foreach ($currencies as $currency) {
$cleanedCurrencies[$currency->getCode()] = $currency->getSymbol();
}
return array(
self::INPUT1 => array(
'availableOperators' => $this->availableOperators[self::INPUT1],
'availableValues' => '',
'type' => 'text',
'class' => 'form-control',
'value' => '',
'selectedOperator' => ''
),
self::INPUT2 => array(
'availableOperators' => $this->availableOperators[self::INPUT2],
'availableValues' => $cleanedCurrencies,
'type' => 'select',
'class' => 'form-control',
'value' => '',
'selectedOperator' => Operators::EQUAL
)
);
}
// /**
// * Populate a Rule from a form admin
// *

View File

@@ -297,4 +297,22 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
// return $this;
// }
/**
* Generate inputs ready to be drawn
*
* @return array
*/
protected function generateInputs()
{
return array(
self::INPUT1 => array(
'availableOperators' => $this->availableOperators[self::INPUT1],
'type' => 'text',
'class' => 'form-control',
'value' => '',
'selectedOperator' => ''
)
);
}
}

View File

@@ -188,9 +188,35 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
*/
public function getValidators()
{
return array(
$this->operators,
$this->values
$this->validators = $this->generateInputs();
$translatedInputs = array();
foreach ($this->validators as $key => $validator) {
$translatedOperators = array();
foreach ($validator['availableOperators'] as $availableOperators) {
$translatedOperators[$availableOperators] = Operators::getI18n(
$this->translator,
$availableOperators
);
}
$validator['availableOperators'] = $translatedOperators;
$translatedInputs[$key] = $validator;
}
return $translatedInputs;
}
/**
* Generate inputs ready to be drawn
*
* @throws \Thelia\Exception\NotImplementedException
* @return array
*/
protected function generateInputs()
{
throw new \Thelia\Exception\NotImplementedException(
'The generateInputs method must be implemented in ' . get_class()
);
}

View File

@@ -141,4 +141,6 @@ interface CouponRuleInterface
}