Coupon : Condition module refactor

Less crappy unmaintainable javascript
More logic in extendable php
This commit is contained in:
gmorel
2014-01-05 00:00:15 +01:00
parent 49782765b4
commit e606a6f8ce
31 changed files with 1086 additions and 612 deletions

View File

@@ -219,4 +219,101 @@ abstract class ConditionAbstract implements ConditionInterface
return true;
}
/**
* Draw the operator 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 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>';
}
$selectHtml .= '
<select class="form-control" id="' . $inputKey . '-operator" name="' . $inputKey . '[operator]">
' . $optionHtml . '
</select>
';
}
return $selectHtml;
}
/**
* Draw the base input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @param string $label I18n input label
* @param string $inputKey Input key (ex: self::INPUT1)
*
* @return string HTML string
*/
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;
}
/**
* Draw the quantity input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @param string $inputKey Input key (ex: self::INPUT1)
* @param int $max Maximum selectable
* @param int $min Minimum selectable
*
* @return string HTML string
*/
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;
}
}

View File

@@ -85,11 +85,20 @@ interface ConditionInterface
/**
* Get I18n tooltip
* Explain in detail what the Condition checks
*
* @return string
*/
public function getToolTip();
/**
* Get I18n summary
* Explain briefly the condition with given values
*
* @return string
*/
public function getSummary();
/**
* Return all validators
*
@@ -104,4 +113,12 @@ interface ConditionInterface
*/
public function getSerializableCondition();
/**
* Draw the input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @return string HTML string
*/
public function drawBackOfficeInputs();
}

View File

@@ -97,6 +97,7 @@ class MatchForEveryone extends ConditionAbstract
/**
* Get I18n tooltip
* Explain in detail what the Condition checks
*
* @return string
*/
@@ -111,6 +112,23 @@ class MatchForEveryone extends ConditionAbstract
return $toolTip;
}
/**
* Get I18n summary
* Explain briefly the condition with given values
*
* @return string
*/
public function getSummary()
{
$toolTip = $this->translator->trans(
'Will return always true',
array(),
'condition'
);
return $toolTip;
}
/**
* Generate inputs ready to be drawn
*
@@ -121,4 +139,17 @@ class MatchForEveryone extends ConditionAbstract
return array();
}
/**
* Draw the input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @return string HTML string
*/
public function drawBackOfficeInputs()
{
// No input
return '';
}
}

View File

@@ -167,7 +167,7 @@ class MatchForTotalAmount extends ConditionAbstract
public function getName()
{
return $this->translator->trans(
'Cart total amount',
'By cart total amount',
array(),
'condition'
);
@@ -175,10 +175,28 @@ class MatchForTotalAmount extends ConditionAbstract
/**
* Get I18n tooltip
* Explain in detail what the Condition checks
*
* @return string
*/
public function getToolTip()
{
$toolTip = $this->translator->trans(
'Check the total Cart amount in the given currency',
array(),
'condition'
);
return $toolTip;
}
/**
* Get I18n summary
* Explain briefly the condition with given values
*
* @return string
*/
public function getSummary()
{
$i18nOperator = Operators::getI18n(
$this->translator, $this->operators[self::INPUT1]
@@ -227,8 +245,6 @@ class MatchForTotalAmount extends ConditionAbstract
'title' => $name1,
'availableOperators' => $this->availableOperators[self::INPUT1],
'availableValues' => '',
'type' => 'text',
'class' => 'form-control',
'value' => '',
'selectedOperator' => ''
),
@@ -236,12 +252,100 @@ class MatchForTotalAmount extends ConditionAbstract
'title' => $name2,
'availableOperators' => $this->availableOperators[self::INPUT2],
'availableValues' => $cleanedCurrencies,
'type' => 'select',
'class' => 'form-control',
'value' => '',
'selectedOperator' => Operators::EQUAL
)
);
}
/**
* Draw the input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @return string HTML string
*/
public function drawBackOfficeInputs()
{
$labelPrice = $this->facade
->getTranslator()
->trans('Price', array(), 'condition');
$html = $this->drawBackOfficeBaseInputsText($labelPrice, self::INPUT1);
return $html;
}
/**
* Draw the base input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @param string $label I18n input label
* @param string $inputKey Input key (ex: self::INPUT1)
*
* @return string HTML string
*/
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];
}
$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>
';
return $html;
}
/**
* 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

@@ -140,7 +140,7 @@ class MatchForXArticles extends ConditionAbstract
public function getName()
{
return $this->translator->trans(
'Number of articles in cart',
'By number of articles in cart',
array(),
'condition'
);
@@ -148,10 +148,28 @@ class MatchForXArticles extends ConditionAbstract
/**
* Get I18n tooltip
* Explain in detail what the Condition checks
*
* @return string
*/
public function getToolTip()
{
$toolTip = $this->translator->trans(
'Check the amount of product in the Cart',
array(),
'condition'
);
return $toolTip;
}
/**
* Get I18n summary
* Explain briefly the condition with given values
*
* @return string
*/
public function getSummary()
{
$i18nOperator = Operators::getI18n(
$this->translator, $this->operators[self::INPUT1]
@@ -186,11 +204,57 @@ class MatchForXArticles extends ConditionAbstract
self::INPUT1 => array(
'title' => $name1,
'availableOperators' => $this->availableOperators[self::INPUT1],
'type' => 'text',
'class' => 'form-control',
'value' => '',
'selectedOperator' => ''
)
);
}
/**
* Draw the input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @return string HTML string
*/
public function drawBackOfficeInputs()
{
$labelQuantity = $this->facade
->getTranslator()
->trans('Quantity', array(), 'condition');
$html = $this->drawBackOfficeBaseInputsText($labelQuantity, self::INPUT1);
return $html;
}
/**
* Draw the base input displayed in the BackOffice
* allowing Admin to set its Coupon Conditions
*
* @param string $label I18n input label
* @param string $inputKey Input key (ex: self::INPUT1)
*
* @return string HTML string
*/
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;
}
}