Added ForSomeCustomer condition

This commit is contained in:
Franck Allimant
2014-05-12 23:37:43 +02:00
parent eb0054d21e
commit 112cb8eeb0
3 changed files with 204 additions and 0 deletions

View File

@@ -0,0 +1,185 @@
<?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\InvalidConditionValueException;
use Thelia\Model\Customer;
use Thelia\Model\CustomerQuery;
/**
* Check a Checkout against its Product number
*
* @package Condition
* @author Franck Allimant <franck@cqfdev.fr>
*
*/
class ForSomeCustomers extends ConditionAbstract
{
const CUSTOMERS_LIST = 'customers';
/**
* @inheritdoc
*/
public function __construct(FacadeInterface $facade)
{
$this->availableOperators = [
self::CUSTOMERS_LIST => [
Operators::IN,
Operators::OUT
]
];
parent::__construct($facade);
}
/**
* @inheritdoc
*/
public function getServiceId()
{
return 'thelia.condition.for_some_customers';
}
/**
* @inheritdoc
*/
public function setValidatorsFromForm(array $operators, array $values)
{
$this->checkComparisonOperatorValue($operators, self::CUSTOMERS_LIST);
// Use default values if data is not defined.
if (! isset($operators[self::CUSTOMERS_LIST]) || ! isset($values[self::CUSTOMERS_LIST])) {
$operators[self::CUSTOMERS_LIST] = Operators::IN;
$values[self::CUSTOMERS_LIST] = [];
}
// Be sure that the value is an array, make one if required
if (! is_array($values[self::CUSTOMERS_LIST])) {
$values[self::CUSTOMERS_LIST] = array($values[self::CUSTOMERS_LIST]);
}
// Check that at least one product is selected
if (empty($values[self::CUSTOMERS_LIST])) {
throw new InvalidConditionValueException(
get_class(), self::CUSTOMERS_LIST
);
}
$this->operators = [ self::CUSTOMERS_LIST => $operators[self::CUSTOMERS_LIST] ];
$this->values = [ self::CUSTOMERS_LIST => $values[self::CUSTOMERS_LIST] ];
return $this;
}
/**
* @inheritdoc
*/
public function isMatching()
{
$customer = $this->facade->getCustomer();
return $this->conditionValidator->variableOpComparison(
$customer->getId(),
$this->operators[self::CUSTOMERS_LIST],
$this->values[self::CUSTOMERS_LIST]
);
}
/**
* @inheritdoc
*/
public function getName()
{
return $this->translator->trans(
'For one ore more customers',
[],
'condition'
);
}
/**
* @inheritdoc
*/
public function getToolTip()
{
$toolTip = $this->translator->trans(
'The coupon applies to some customers only',
[],
'condition'
);
return $toolTip;
}
/**
* @inheritdoc
*/
public function getSummary()
{
$i18nOperator = Operators::getI18n(
$this->translator, $this->operators[self::CUSTOMERS_LIST]
);
$custStrList = '';
$custIds = $this->values[self::CUSTOMERS_LIST];
if (null !== $custList = CustomerQuery::create()->findPks($custIds)) {
/** @var Customer $cust */
foreach($custList as $cust) {
$custStrList .= $cust->getLastname() . ' ' . $cust->getFirstname() . ' ('.$cust->getRef().'), ';
}
$custStrList = rtrim($custStrList, ', ');
}
$toolTip = $this->translator->trans(
'Customer is %op% <strong>%customer_list%</strong>', [
'%customer_list%' => $custStrList,
'%op%' => $i18nOperator
], 'condition'
);
return $toolTip;
}
/**
* @inheritdoc
*/
protected function generateInputs()
{
return array(
self::CUSTOMERS_LIST => array(
'availableOperators' => $this->availableOperators[self::CUSTOMERS_LIST],
'value' => '',
'selectedOperator' => Operators::IN
)
);
}
/**
* @inheritdoc
*/
public function drawBackOfficeInputs()
{
return $this->facade->getParser()->render('coupon/condition-fragments/customers-condition.html', [
'operatorSelectHtml' => $this->drawBackOfficeInputOperators(self::CUSTOMERS_LIST),
'customers_field_name' => self::CUSTOMERS_LIST,
'values' => isset($this->values[self::CUSTOMERS_LIST]) ? $this->values[self::CUSTOMERS_LIST] : array()
]
);
}
}

View File

@@ -77,6 +77,11 @@
<tag name="thelia.coupon.addCondition"/>
</service>
<service id="thelia.condition.for_some_customers" class="Thelia\Condition\Implementation\ForSomeCustomers">
<argument type="service" id="thelia.facade" />
<tag name="thelia.coupon.addCondition"/>
</service>
</services>
</config>

View File

@@ -0,0 +1,14 @@
<div class="form-group">
<label for="operator">{intl l="Customer is"}</label>
{$operatorSelectHtml nofilter}
</div>
<div class="form-group">
<label for="{$customers_field_name}-value">{intl l="The selected customer :"}</label>
<select required multiple size="5" class="form-control" id="{$customers_field_name}-value" name="{$customers_field_name}[value][]">
{loop type="customer" name="list-of-customers" order="lastname,firstname" current="0" backend_context="1"}
<option value="{$ID}" {if in_array($ID, $values)}selected="selected"{/if}>{$LASTNAME} {$FIRSTNAME} {$REF}</option>
{/loop}
</select>
<span class="label-help-block">{intl l='Use Ctrl+click to select (or deselect) more that one country'}</span>
</div>