Fixed labels, using templates for HTML fragments

This commit is contained in:
Franck Allimant
2014-05-02 12:07:17 +02:00
parent bbe7634ad4
commit 6b020fda13
9 changed files with 150 additions and 195 deletions

View File

@@ -18,6 +18,7 @@ use Thelia\Condition\SerializableCondition;
use Thelia\Core\Translation\Translator;
use Thelia\Coupon\FacadeInterface;
use Thelia\Exception\InvalidConditionValueException;
use Thelia\Model\Base\CurrencyQuery;
use Thelia\Model\Currency;
use Thelia\Type\FloatType;
@@ -35,10 +36,10 @@ abstract class ConditionAbstract implements ConditionInterface
protected $serviceId = null;
/** @var array Available Operators (Operators::CONST) */
protected $availableOperators = array();
protected $availableOperators = [];
/** @var array Parameters validating parameters against */
protected $validators = array();
protected $validators = [];
/** @var FacadeInterface Provide necessary value from Thelia */
protected $facade = null;
@@ -47,10 +48,10 @@ abstract class ConditionAbstract implements ConditionInterface
protected $translator = null;
/** @var array Operators set by Admin in BackOffice */
protected $operators = array();
protected $operators = [];
/** @var array Values set by Admin in BackOffice */
protected $values = array();
protected $values = [];
/** @var ConditionEvaluator Conditions validator */
protected $conditionValidator = null;
@@ -64,6 +65,7 @@ abstract class ConditionAbstract implements ConditionInterface
{
$this->facade = $facade;
$this->translator = $facade->getTranslator();
$this->parser = $facade->getParser();
$this->conditionValidator = $facade->getConditionEvaluator();
}
@@ -86,9 +88,9 @@ abstract class ConditionAbstract implements ConditionInterface
{
$this->validators = $this->generateInputs();
$translatedInputs = array();
$translatedInputs = [];
foreach ($this->validators as $key => $validator) {
$translatedOperators = array();
$translatedOperators = [];
foreach ($validator['availableOperators'] as $availableOperators) {
$translatedOperators[$availableOperators] = Operators::getI18n(
$this->translator,
@@ -99,7 +101,7 @@ abstract class ConditionAbstract implements ConditionInterface
$validator['availableOperators'] = $translatedOperators;
$translatedInputs[$key] = $validator;
}
$validators = array();
$validators = [];
$validators['inputs'] = $translatedInputs;
$validators['setOperators'] = $this->operators;
$validators['setValues'] = $this->values;
@@ -216,27 +218,21 @@ abstract class ConditionAbstract implements ConditionInterface
*/
protected function drawBackOfficeInputOperators($inputKey)
{
$selectHtml = '';
$optionHtml = '';
$inputs = $this->getValidators();
if (isset($inputs['inputs'][$inputKey])) {
$operators = $inputs['inputs'][$inputKey]['availableOperators'];
foreach ($operators as $key => $operator) {
$selected = '';
if (isset($this->operators) && isset($this->operators[$inputKey]) && $this->operators[$inputKey] == $key) {
$selected = ' selected="selected"';
}
$optionHtml .= '<option value="' . $key . '" '. $selected . '>' . $operator . '</option>';
}
$html = '';
$selectHtml .= '
<select class="form-control" id="' . $inputKey . '-operator" name="' . $inputKey . '[operator]">
' . $optionHtml . '
</select>
';
$inputs = $this->getValidators();
if (isset($inputs['inputs'][$inputKey])) {
$html = $this->facade->getParser()->render('coupon/condition-fragments/condition-selector.html', [
'operators' => $inputs['inputs'][$inputKey]['availableOperators'],
'value' => isset($this->operators[$inputKey]) ? $this->operators[$inputKey] : '',
'inputKey' => $inputKey
]
);
}
return $selectHtml;
return $html;
}
/**
@@ -251,26 +247,19 @@ abstract class ConditionAbstract implements ConditionInterface
protected function drawBackOfficeBaseInputsText($label, $inputKey)
{
$operatorSelectHtml = $this->drawBackOfficeInputOperators($inputKey);
$currentValue = '';
if (isset($this->values) && isset($this->values[$inputKey])) {
$currentValue = $this->values[$inputKey];
}
$html = '
<div id="condition-add-operators-values" class="form-group col-md-6">
<label for="operator">' . $label . '</label>
<div class="row">
<div class="col-lg-6">
' . $operatorSelectHtml . '
</div>
<div class="input-group col-lg-6">
<input type="text" class="form-control" id="' . $inputKey . '-value" name="' . $inputKey . '[value]" value="' . $currentValue . '">
</div>
</div>
</div>
';
return $html;
return $this->facade->getParser()->render('coupon/conditions-fragments/base-input-text.html', [
'label' => $label,
'inputKey' => $inputKey,
'currentValue' => $currentValue,
'operatorSelectHtml' => $operatorSelectHtml
]
);
}
/**
@@ -285,23 +274,39 @@ abstract class ConditionAbstract implements ConditionInterface
*/
protected function drawBackOfficeInputQuantityValues($inputKey, $max = 10, $min = 0)
{
$selectHtml = '';
$optionHtml = '';
for ($i = $min; $i <= $max; $i++) {
$selected = '';
if (isset($this->values) && isset($this->values[$inputKey]) && $this->values[$inputKey] == $i) {
$selected = ' selected="selected"';
}
$optionHtml .= '<option value="' . $i . '" ' . $selected . '>' . $i . '</option>';
}
$selectHtml .= '
<select class="form-control" id="' . $inputKey . '-value" name="' . $inputKey . '[value]">
' . $optionHtml . '
</select>
';
return $selectHtml;
return $this->facade->getParser()->render('coupon/condition-fragments/quantity-selector.html', [
'min' => $min,
'max' => $max,
'value' => isset($this->values[$inputKey]) ? $this->values[$inputKey] : '',
'inputKey' => $inputKey
]
);
}
}
/**
* Draw the currency input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @param string $inputKey Input key (ex: self::INPUT1)
*
* @return string HTML string
*/
protected function drawBackOfficeCurrencyInput($inputKey)
{
$currencies = CurrencyQuery::create()->find();
$cleanedCurrencies = [];
/** @var Currency $currency */
foreach ($currencies as $currency) {
$cleanedCurrencies[$currency->getCode()] = $currency->getSymbol();
}
return $this->facade->getParser()->render('coupon/condition-fragments/currency-selector.html', [
'currencies' => $cleanedCurrencies,
'value' => isset($this->values[$inputKey]) ? $this->values[$inputKey] : '',
'inputKey' => $inputKey
]
);
}
}

View File

@@ -25,7 +25,7 @@ class MatchForEveryone extends ConditionAbstract
protected $serviceId = 'thelia.condition.match_for_everyone';
/** @var array Available Operators (Operators::CONST) */
protected $availableOperators = array();
protected $availableOperators = [];
/**
* Check validators relevancy and store them
@@ -51,8 +51,8 @@ class MatchForEveryone extends ConditionAbstract
*/
protected function setValidators()
{
$this->operators = array();
$this->values = array();
$this->operators = [];
$this->values = [];
return $this;
}
@@ -75,8 +75,8 @@ class MatchForEveryone extends ConditionAbstract
public function getName()
{
return $this->translator->trans(
'Everybody can use it (no condition)',
array(),
'Unconditional usage',
[],
'condition'
);
}
@@ -90,8 +90,8 @@ class MatchForEveryone extends ConditionAbstract
public function getToolTip()
{
$toolTip = $this->translator->trans(
'Will return always true',
array(),
'This condition is always true',
[],
'condition'
);
@@ -107,8 +107,8 @@ class MatchForEveryone extends ConditionAbstract
public function getSummary()
{
$toolTip = $this->translator->trans(
'Will return always true',
array(),
'Unconditionnal usage',
[],
'condition'
);
@@ -122,7 +122,7 @@ class MatchForEveryone extends ConditionAbstract
*/
protected function generateInputs()
{
return array();
return [];
}
/**

View File

@@ -153,7 +153,7 @@ class MatchForTotalAmount extends ConditionAbstract
{
return $this->translator->trans(
'By cart total amount',
array(),
[],
'condition'
);
}
@@ -168,7 +168,7 @@ class MatchForTotalAmount extends ConditionAbstract
{
$toolTip = $this->translator->trans(
'Check the total Cart amount in the given currency',
array(),
[],
'condition'
);
@@ -208,7 +208,7 @@ class MatchForTotalAmount extends ConditionAbstract
protected function generateInputs()
{
$currencies = CurrencyQuery::create()->find();
$cleanedCurrencies = array();
$cleanedCurrencies = [];
/** @var Currency $currency */
foreach ($currencies as $currency) {
$cleanedCurrencies[$currency->getCode()] = $currency->getSymbol();
@@ -240,7 +240,7 @@ class MatchForTotalAmount extends ConditionAbstract
{
$labelPrice = $this->facade
->getTranslator()
->trans('Price', array(), 'condition');
->trans('Cart total amount is', [], 'condition');
$html = $this->drawBackOfficeBaseInputsText($labelPrice, self::INPUT1);
@@ -258,66 +258,17 @@ class MatchForTotalAmount extends ConditionAbstract
*/
protected function drawBackOfficeBaseInputsText($label, $inputKey)
{
$operatorSelectHtml = $this->drawBackOfficeInputOperators(self::INPUT1);
$currencySelectHtml = $this->drawBackOfficeCurrencyInput(self::INPUT2);
$selectedAmount = '';
if (isset($this->values) && isset($this->values[$inputKey])) {
$selectedAmount = $this->values[$inputKey];
}
return $this->facade->getParser()->render('coupon/condition-fragments/cart-total-amount-condition.html', [
'label' => $label,
'inputKey' => $inputKey,
'value' => isset($this->values[$inputKey]) ? $this->values[$inputKey] : '',
$html = '
<label for="operator">' . $label . '</label>
<div class="row">
<div class="col-lg-6">
' . $operatorSelectHtml . '
</div>
<div class="input-group col-lg-3">
<input type="text" class="form-control" id="' . self::INPUT1 . '-value" name="' . self::INPUT1 . '[value]" value="' . $selectedAmount . '">
</div>
<div class="input-group col-lg-3">
<input type="hidden" id="' . self::INPUT2 . '-operator" name="' . self::INPUT2 . '[operator]" value="==" />
' . $currencySelectHtml . '
</div>
</div>
';
'field_1_name' => self::INPUT1,
'field_2_name' => self::INPUT2,
return $html;
'operatorSelectHtml' => $this->drawBackOfficeInputOperators(self::INPUT1),
'currencySelectHtml' => $this->drawBackOfficeCurrencyInput(self::INPUT2),
]
);
}
/**
* Draw the currency input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @param string $inputKey Input key (ex: self::INPUT1)
*
* @return string HTML string
*/
protected function drawBackOfficeCurrencyInput($inputKey)
{
$optionHtml = '';
$currencies = CurrencyQuery::create()->find();
$cleanedCurrencies = array();
/** @var Currency $currency */
foreach ($currencies as $currency) {
$cleanedCurrencies[$currency->getCode()] = $currency->getSymbol();
}
foreach ($cleanedCurrencies as $key => $cleanedCurrency) {
$selected = '';
if (isset($this->values) && isset($this->values[$inputKey]) && $this->values[$inputKey] == $key) {
$selected = ' selected="selected"';
}
$optionHtml .= '<option value="' . $key . '" ' . $selected . '>' . $cleanedCurrency . '</option>';
}
$selectHtml = '
<select class="form-control" id="' . $inputKey . '-value" name="' . $inputKey . '[value]">
' . $optionHtml . '
</select>
';
return $selectHtml;
}
}
}

View File

@@ -127,8 +127,8 @@ class MatchForXArticles extends ConditionAbstract
public function getName()
{
return $this->translator->trans(
'By number of articles in cart',
array(),
'Cart item count condition',
[],
'condition'
);
}
@@ -142,8 +142,8 @@ class MatchForXArticles extends ConditionAbstract
public function getToolTip()
{
$toolTip = $this->translator->trans(
'Check the amount of product in the Cart',
array(),
'The cart item count should match the condition',
[],
'condition'
);
@@ -163,7 +163,7 @@ class MatchForXArticles extends ConditionAbstract
);
$toolTip = $this->translator->trans(
'If cart products quantity is <strong>%operator%</strong> %quantity%',
'If cart item count is <strong>%operator%</strong> %quantity%',
array(
'%operator%' => $i18nOperator,
'%quantity%' => $this->values[self::INPUT1]
@@ -200,7 +200,7 @@ class MatchForXArticles extends ConditionAbstract
{
$labelQuantity = $this->facade
->getTranslator()
->trans('Quantity', array(), 'condition');
->trans('Cart item count is', [], 'condition');
$html = $this->drawBackOfficeBaseInputsText($labelQuantity, self::INPUT1);
@@ -218,24 +218,11 @@ class MatchForXArticles extends ConditionAbstract
*/
protected function drawBackOfficeBaseInputsText($label, $inputKey)
{
$operatorSelectHtml = $this->drawBackOfficeInputOperators($inputKey);
$quantitySelectHtml = $this->drawBackOfficeInputQuantityValues($inputKey, 20, 1);
$html = '
<div id="condition-add-operators-values" class="form-group col-md-6">
<label for="operator">' . $label . '</label>
<div class="row">
<div class="col-lg-6">
' . $operatorSelectHtml . '
</div>
<div class="input-group col-lg-6">
' . $quantitySelectHtml . '
</div>
</div>
</div>
';
return $html;
return $this->facade->getParser()->render('coupon/condition-fragments/cart-item-count-condition.html', [
'label' => $label,
'operatorSelectHtml' => $this->drawBackOfficeInputOperators($inputKey),
'quantitySelectHtml' => $this->drawBackOfficeInputQuantityValues($inputKey, 20, 1)
]
);
}
}
}