WIP : coupon refactor + fix

This commit is contained in:
gmorel
2013-09-26 17:47:41 +02:00
parent c521fb2696
commit 73fde64f5b
38 changed files with 3091 additions and 2682 deletions

View File

@@ -1,16 +1,16 @@
$(function($){
// Manage how coupon and rules are saved
// Manage how coupon and conditions are saved
$.couponManager = {};
// Rule to be saved
$.couponManager.ruleToSave = {};
$.couponManager.ruleToSave.serviceId = false;
$.couponManager.ruleToSave.operators = {};
$.couponManager.ruleToSave.values = {};
// Rules payload to save
$.couponManager.rulesToSave = [];
// Rule being updated id
$.couponManager.ruleToUpdateId = false;
// Condition to be saved
$.couponManager.conditionToSave = {};
$.couponManager.conditionToSave.serviceId = false;
$.couponManager.conditionToSave.operators = {};
$.couponManager.conditionToSave.values = {};
// Conditions payload to save
$.couponManager.conditionsToSave = [];
// Condition being updated id
$.couponManager.conditionToUpdateId = false;
// Clean array from deleteValue (undefined) keys
Array.prototype.clean = function(deleteValue) {
@@ -23,110 +23,109 @@ $(function($){
return this;
};
// Remove 1 Rule then Save Rules AJAX
$.couponManager.removeRuleAjax = function(id) {
// Delete rule in temporary array
delete $.couponManager.rulesToSave[id];
$.couponManager.rulesToSave.clean(undefined);
// Remove 1 Condition then Save Conditions AJAX
$.couponManager.removeConditionAjax = function(id) {
// Delete condition in temporary array
delete $.couponManager.conditionsToSave[id];
$.couponManager.conditionsToSave.clean(undefined);
// Save
$.couponManager.saveRuleAjax();
$.couponManager.saveConditionAjax();
};
// Add 1 Rule / or update the temporary Rules array then Save Rules via AJAX
$.couponManager.createOrUpdateRuleAjax = function() {
var id = $.couponManager.ruleToUpdateId;
// Add 1 Condition / or update the temporary Conditions array then Save Conditions via AJAX
$.couponManager.createOrUpdateConditionAjax = function() {
var id = $.couponManager.conditionToUpdateId;
// If create
if(!id) {
$.couponManager.rulesToSave.push($.couponManager.ruleToSave);
$.couponManager.conditionsToSave.push($.couponManager.conditionToSave);
} else { // else update
$.couponManager.rulesToSave[id] = $.couponManager.ruleToSave;
$.couponManager.conditionsToSave[id] = $.couponManager.conditionToSave;
// reset edit mode to off
$.couponManager.ruleToUpdateId = false;
$.couponManager.conditionToUpdateId = false;
}
// Save
$.couponManager.saveRuleAjax();
$.couponManager.saveConditionAjax();
};
// Set rule inputs to allow editing
$.couponManager.updateRuleSelectAjax = function(id) {
$.couponManager.ruleToUpdateId = id;
$.couponManager.ruleToSave = $.couponManager.rulesToSave[id];
// Set condition inputs to allow editing
$.couponManager.updateConditionSelectAjax = function(id) {
$.couponManager.conditionToUpdateId = id;
$.couponManager.conditionToSave = $.couponManager.conditionsToSave[id];
// Set the rule selector
// Set the condition selector
$("#category-rule option").filter(function() {
return $(this).val() == $.couponManager.ruleToSave.serviceId;
return $(this).val() == $.couponManager.conditionToSave.serviceId;
}).prop('selected', true);
// Force rule input refresh
$.couponManager.loadRuleInputs($.couponManager.ruleToSave.serviceId, function() {
$.couponManager.fillInRuleInputs();
// Force condition input refresh
$.couponManager.loadConditionInputs($.couponManager.conditionToSave.serviceId, function() {
$.couponManager.fillInConditionInputs();
});
};
// Fill in rule inputs
$.couponManager.fillInRuleInputs = function() {
// Fill in condition inputs
$.couponManager.fillInConditionInputs = function() {
var operatorId = null;
var valueId = null;
var idName = null;
var id = $.couponManager.ruleToUpdateId;
var id = $.couponManager.conditionToUpdateId;
if(id) {
$.couponManager.ruleToSave = $.couponManager.rulesToSave[id];
$.couponManager.conditionToSave = $.couponManager.conditionsToSave[id];
}
for (idName in $.couponManager.ruleToSave.operators) {
for (idName in $.couponManager.conditionToSave.operators) {
// Setting idName operator select
operatorId = idName + '-operator';
$('#' + operatorId).val($.couponManager.ruleToSave.operators[idName]);
$('#' + operatorId).val($.couponManager.conditionToSave.operators[idName]);
// Setting idName value input
valueId = idName + '-value';
$('#' + valueId).val($.couponManager.ruleToSave.values[idName]);
$('#' + valueId).val($.couponManager.conditionToSave.values[idName]);
}
};
// Save rules on click
$.couponManager.onClickSaveRule = function() {
// Save conditions on click
$.couponManager.onClickSaveCondition = function() {
$('#constraint-save-btn').on('click', function () {
console.log('constraint-save-btn');
if($('#category-rule').val() == 'thelia.condition.match_for_everyone') {
// // @todo translate + modal
// // @todo translate message + put it in modal
var r = confirm("Do you really want to set this coupon available to everyone ?");
if (r == true) {
$.couponManager.createOrUpdateRuleAjax();
$.couponManager.createOrUpdateConditionAjax();
}
} else {
$.couponManager.createOrUpdateRuleAjax();
$.couponManager.createOrUpdateConditionAjax();
}
});
};
$.couponManager.onClickSaveRule();
$.couponManager.onClickSaveCondition();
// Remove rule on click
$.couponManager.onClickDeleteRule = function() {
// Remove condition on click
$.couponManager.onClickDeleteCondition = function() {
$('.constraint-delete-btn').on('click', function (e) {
e.preventDefault();
var $this = $(this);
$.couponManager.removeRuleAjax($this.attr('data-int'));
$.couponManager.removeConditionAjax($this.attr('data-int'));
});
};
$.couponManager.onClickDeleteRule();
$.couponManager.onClickDeleteCondition();
// Update rule on click
$.couponManager.onClickUpdateRule = function() {
$('.constraint-update-btn').on('click', function (e) {
// Update condition on click
$.couponManager.onClickUpdateCondition = function() {
$('.condition-update-btn').on('click', function (e) {
e.preventDefault();
var $this = $(this);
$.couponManager.updateRuleSelectAjax($this.attr('data-int'));
$.couponManager.updateConditionSelectAjax($this.attr('data-int'));
// Hide row being updated
$this.parent().parent().remove();
});
};
$.couponManager.onClickUpdateRule();
$.couponManager.onClickUpdateCondition();
// Reload effect inputs when changing effect
$.couponManager.onEffectChange = function() {
@@ -139,15 +138,15 @@ $(function($){
};
$.couponManager.onEffectChange();
// Reload rule inputs when changing effect
$.couponManager.onRuleChange = function() {
// Reload condition inputs when changing effect
$.couponManager.onConditionChange = function() {
$('#category-rule').on('change', function () {
$.couponManager.loadRuleInputs($(this).val(), function() {});
$.couponManager.loadConditionInputs($(this).val(), function() {});
});
};
$.couponManager.onRuleChange();
$.couponManager.onConditionChange();
// Fill in ready to be saved rule array
// Fill in ready to be saved condition array
// var onInputsChange = function()
// In AJAX response

View File

@@ -18,7 +18,7 @@
</div>
{form name="thelia.admin.coupon.creation"}
{include file='coupon/form.html' formAction={url path={$formAction}} noRules=true}
{include file='coupon/form.html' formAction={url path={$formAction}} noConditions=true}
{/form}
</section> <!-- #wrapper -->

View File

@@ -122,11 +122,11 @@
<td>{intl l='Application field'}</td>
<td>
<ul class="list-unstyled">
{foreach from=$APPLICATION_CONDITIONS item=rule name=rulesForeach}
{if !$smarty.foreach.rulesForeach.first}
{foreach from=$APPLICATION_CONDITIONS item=condition name=conditionsForeach}
{if !$smarty.foreach.conditionsForeach.first}
<li><span class="label label-info">{intl l='And'}</span></li>
{/if}
<li>{$rule nofilter}</li>
<li>{$condition nofilter}</li>
{/foreach}
</ul>
</td>

View File

@@ -18,7 +18,7 @@
</div>
{form name="thelia.admin.coupon.creation"}
{include file='coupon/form.html' formAction={url path={$formAction}} form=$form noRules=false}
{include file='coupon/form.html' formAction={url path={$formAction}} form=$form noConditions=false}
{/form}
</section> <!-- #wrapper -->
@@ -45,87 +45,87 @@
$(function($){
miniBrowser(0, '/test_to_remove/datas_coupon_edit.json');
// Init Rules
$.couponManager.initRules = function() {
var rules = [];
{foreach from=$rulesObject key=k item=rule}
// Init rule
var rule = {};
rule['serviceId'] = '{$rule.serviceId nofilter}';
rule['operators'] = {};
rule['values'] = {};
// Init Conditions
$.couponManager.initConditions = function() {
var conditions = [];
{foreach from=$conditionsObject key=k item=condition}
// Init condition
var condition = {};
condition['serviceId'] = '{$condition.serviceId nofilter}';
condition['operators'] = {};
condition['values'] = {};
{foreach from=$rule.validators.setOperators key=input item=operator}
rule['operators']['{$input nofilter}'] = '{$operator nofilter}';
rule['values']['{$input nofilter}'] = '{$rule.validators.setValues[$input] nofilter}';
{foreach from=$condition.validators.setOperators key=input item=operator}
condition['operators']['{$input nofilter}'] = '{$operator nofilter}';
condition['values']['{$input nofilter}'] = '{$condition.validators.setValues[$input] nofilter}';
{/foreach}
// Add rule
rules.push(rule);
// Add condition
conditions.push(condition);
{/foreach}
return rules;
return conditions;
};
// Save Rules AJAX
$.couponManager.saveRuleAjax = function() {
$('#constraint-add-operators-values').html('<div class="loading" ></div>');
var $url = '{$urlAjaxUpdateRules}';
// Save Conditions AJAX
$.couponManager.saveConditionAjax = function() {
$('#condition-add-operators-values').html('<div class="loading" ></div>');
var $url = '{$urlAjaxUpdateConditions}';
$.ajax({
type: "POST",
url: $url,
data: {literal}{{/literal}rules:JSON.stringify($.couponManager.rulesToSave){literal}}{/literal},
data: {literal}{{/literal}conditions:JSON.stringify($.couponManager.conditionsToSave){literal}}{/literal},
statusCode: {
404: function() {
$('#constraint-add-operators-values').html(
$('#condition-add-operators-values').html(
'{intl l='Please retry'}'
);
}
}
}).done(function(data) {
$('#constraint-list').html(data);
$('#constraint-add-operators-values').html('');
// Set the rule selector
$("#category-rule option").filter(function() {
$('#condition-list').html(data);
$('#condition-add-operators-values').html('');
// Set the condition selector
$("#category-condition option").filter(function() {
return $(this).val() == 'thelia.condition.match_for_everyone';
}).prop('selected', true);
$.couponManager.onClickUpdateRule();
$.couponManager.onClickDeleteRule();
$.couponManager.onClickUpdateCondition();
$.couponManager.onClickDeleteCondition();
});
};
// Reload rule inputs
$.couponManager.loadRuleInputs = function(ruleId, callBack) {
$('#constraint-add-operators-values').html('<div class="loading" ></div>');
var url = "{$urlAjaxGetRuleInput}";
url = url.replace('ruleId', ruleId)
// Reload condition inputs
$.couponManager.loadConditionInputs = function(conditionId, callBack) {
$('#condition-add-operators-values').html('<div class="loading" ></div>');
var url = "{$urlAjaxGetConditionInput}";
url = url.replace('conditionId', conditionId)
$.ajax({
url: url,
statusCode: {
404: function() {
$('#constraint-add-operators-values').html(
'{intl l='Please select another rule'}'
$('#condition-add-operators-values').html(
'{intl l='Please select another condition'}'
);
}
}
}).done(function(data) {
$('#constraint-add-operators-values').html(data);
$.couponManager.ruleToSave.serviceId = ruleId;
if (ruleId == -1) {
$('#condition-add-operators-values').html(data);
$.couponManager.conditionToSave.serviceId = conditionId;
if (conditionId == -1) {
// Placeholder can't be saved
$('#constraint-save-btn').hide();
$('#condition-save-btn').hide();
} else {
$('#constraint-save-btn').show();
$('#condition-save-btn').show();
}
return callBack();
});
};
// Rules which will be saved
$.couponManager.rulesToSave = $.couponManager.initRules();
// Conditions which will be saved
$.couponManager.conditionsToSave = $.couponManager.initConditions();
$('#constraint-save-btn').hide();
$('#condition-save-btn').hide();
});
</script>

View File

@@ -71,32 +71,32 @@
<script>
// Init Rules to set
// Update only if no rule are already set
if(!$.couponManager.ruleToSave){
$.couponManager.ruleToSave['serviceId'] = '{$ruleId}';
$.couponManager.ruleToSave['operators'] = {literal}{}{/literal};
$.couponManager.ruleToSave['values'] = {literal}{}{/literal};
// Init conditions to set
// Update only if no condition are already set
if(!$.couponManager.conditionToSave){
$.couponManager.conditionToSave['serviceId'] = '{$conditionId}';
$.couponManager.conditionToSave['operators'] = {literal}{}{/literal};
$.couponManager.conditionToSave['values'] = {literal}{}{/literal};
} else {
}
{foreach from=$inputs.inputs key=name item=input}
$.couponManager.ruleToSave['operators']['{$name nofilter}'] = '{foreach from=$inputs.inputs[$name].availableOperators key=keyOperator item=valueOperator name=operators}{if $smarty.foreach.operators.first}{$keyOperator nofilter}{/if}{/foreach}';
$.couponManager.ruleToSave['values']['{$name nofilter}'] = '{if count($inputs.inputs[$name].availableValues) != 0}{foreach from=$inputs.inputs[$name].availableValues key=keyValue item=valueValue name=values}{if $smarty.foreach.values.first}{$keyValue nofilter}{/if}{/foreach}{else}to set{/if}';
$.couponManager.conditionToSave['operators']['{$name nofilter}'] = '{foreach from=$inputs.inputs[$name].availableOperators key=keyOperator item=valueOperator name=operators}{if $smarty.foreach.operators.first}{$keyOperator nofilter}{/if}{/foreach}';
$.couponManager.conditionToSave['values']['{$name nofilter}'] = '{if count($inputs.inputs[$name].availableValues) != 0}{foreach from=$inputs.inputs[$name].availableValues key=keyValue item=valueValue name=values}{if $smarty.foreach.values.first}{$keyValue nofilter}{/if}{/foreach}{else}to set{/if}';
{/foreach}
// Fill in ready to be saved rule array
// Fill in ready to be saved condition array
$.couponManager.onInputsChange = function() {literal}{{/literal}
{foreach from=$inputs.inputs key=name item=input}
// Operator selector
$('#{$name}-operator').change(function (e) {
var $this = $(this);
$.couponManager.ruleToSave['operators']['{$name nofilter}'] = $this.val();
$.couponManager.conditionToSave['operators']['{$name nofilter}'] = $this.val();
});
// Value input
$('#{$name}-value').change(function (e) {
var $this = $(this);
$.couponManager.ruleToSave['values']['{$name nofilter}'] = $this.val();
$.couponManager.conditionToSave['values']['{$name nofilter}'] = $this.val();
});
{/foreach}
{literal}}{/literal}

View File

@@ -7,11 +7,11 @@
{$condition nofilter}
</td>
<td>
<a data-int="{$i}" class="btn btn-default btn-primary btn-medium constraint-update-btn" href="{$urlEdit}">
<a data-int="{$i}" class="btn btn-default btn-primary btn-medium condition-update-btn" href="{$urlEdit}">
<span class="glyphicon glyphicon-edit"></span> {intl l='Edit'}
</a>
{if $conditions|count != 1}
<a data-int="{$i}" data-target="#delete" data-toggle="confirm" class="btn btn-default btn-danger btn-medium constraint-delete-btn" href="{$urlDelete}">
<a data-int="{$i}" data-target="#delete" data-toggle="confirm" class="btn btn-default btn-danger btn-medium condition-delete-btn" href="{$urlDelete}">
<span class="glyphicon glyphicon-remove"></span> {intl l='Delete'}
</a>
{/if}

View File

@@ -36,7 +36,7 @@
{form_field form=$form field='isEnabled'}
<div class="form-group {if $error}has-error{/if}">
<label for="is-enabled" class="checkbox control-label">
<input id="is-enabled" type="checkbox" name="{$name}" {if $value}value="1" checked{else}value="0"{/if} >
<input id="is-enabled" type="checkbox" name="{$name}" {if $value}value="1" checked{/if} />
{if $error}{$message}{/if}
{intl l='Is enabled'}
</label>
@@ -45,8 +45,9 @@
{form_field form=$form field='isAvailableOnSpecialOffers'}
<div class="form-group {if $error}has-error{/if}">
{$value|var_dump}
<label for="is-available-on-special-offers" class="checkbox control-label">
<input id="is-available-on-special-offers" type="checkbox" name="{$name}" {if $value}value="1" checked{else}value="0"{/if} >
<input id="is-available-on-special-offers" type="checkbox" name="{$name}" {if $value}value="1" checked{/if} />
{if $error}{$message}{/if}
{intl l='Is available on special offers'}
</label>
@@ -56,7 +57,7 @@
{form_field form=$form field='isCumulative'}
<div class="form-group {if $error}has-error{/if}">
<label for="is-cumulative" class="checkbox control-label">
<input id="is-cumulative" type="checkbox" name="{$name}" {if $value}value="1" checked{else}value="0"{/if} >
<input id="is-cumulative" type="checkbox" name="{$name}" {if $value}value="1" checked{/if} />
{if $error}{$message}{/if}
{intl l='Is cumulative'}
</label>
@@ -66,7 +67,7 @@
{form_field form=$form field='isRemovingPostage'}
<div class="form-group {if $error}has-error{/if}">
<label for="is-removing-postage" class="checkbox control-label">
<input id="is-removing-postage" type="checkbox" name="{$name}" {if $value}value="1" checked{else}value="0"{/if} >
<input id="is-removing-postage" type="checkbox" name="{$name}" {if $value}value="1" checked{/if} />
{if $error}{$message}{/if}
{intl l='Is removing postage'}
</label>
@@ -166,7 +167,7 @@
</div>
</section>
{if $noRules}
{if $noConditions}
{include file='includes/notifications.html' message={intl l='Please save your Coupon in oder to affect it some conditions'}}
{else}
<section class="row">
@@ -181,8 +182,8 @@
<th>{intl l='Actions'}</th>
</tr>
</thead>
<tbody id="constraint-list">
{include file='coupon/conditions.html' conditions=$rules}
<tbody id="condition-list">
{include file='coupon/conditions.html' conditions=$conditions}
</tbody>
</table>
</div>
@@ -190,11 +191,11 @@
<section class="row">
<div class="col-md-12 general-block-decorator clearfix">
<a id="constraint-save-btn" title="{intl l='Save this rule'}" class="btn btn-default btn-primary pull-right" data-toggle="confirm" data-script="">
<a id="condition-save-btn" title="{intl l='Save this condition'}" class="btn btn-default btn-primary pull-right" data-toggle="confirm" data-script="">
<span class="glyphicon glyphicon-plus-sign"></span> {intl l='Save this condition'}
</a>
<div id="rule-add-organizer" class="form-group col-md-2">
<div id="condition-add-organizer" class="form-group col-md-2">
<label for="type">{intl l='Condition type :'}</label>
<label class="radio">
<input type="radio" name="type" class="form-control" id="type" value="1" checked> {intl l='And'}
@@ -204,17 +205,17 @@
</label>
</div>
<div id="rule-add-type" class="form-group col-md-4">
<label for="categoryRule">{intl l='Rule\'s category :'}</label>
<select name="categoryRule" id="category-rule" class="form-control">
<option value="-1" >{intl l='Please select a rule category'}</option>
{foreach from=$availableRules item=availableRule}
<option value="{$availableRule.serviceId}" data-description="{$availableRule.toolTip}">{$availableRule.name}</option>
<div id="condition-add-type" class="form-group col-md-4">
<label for="categoryCondition">{intl l='Condition\'s category :'}</label>
<select name="categoryCondition" id="category-condition" class="form-control">
<option value="-1" >{intl l='Please select a condition category'}</option>
{foreach from=$availableConditions item=availableCondition}
<option value="{$availableCondition.serviceId}" data-description="{$availableCondition.toolTip}">{$availableCondition.name}</option>
{/foreach}
</select>
</div>
<div id="constraint-add-operators-values" class="form-group col-md-6">
<div id="condition-add-operators-values" class="form-group col-md-6">
{*<label for="operator">{intl l='Operator :'}</label>*}
{*<div class="row">*}
{*<div class="col-lg-6">*}