first commit
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace KupShop\OrderRestrictionsBundle\Admin;
|
||||
|
||||
class OrderRestrictions extends \Window
|
||||
{
|
||||
protected $template = 'window/OrderRestrictions.tpl';
|
||||
protected $tableName = 'order_restrictions';
|
||||
protected $nameField;
|
||||
}
|
||||
|
||||
return OrderRestrictions::class;
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace KupShop\OrderRestrictionsBundle\Admin;
|
||||
|
||||
use KupShop\AdminBundle\AdminRegister\AdminRegister;
|
||||
use KupShop\AdminBundle\AdminRegister\IAdminRegisterStatic;
|
||||
|
||||
class OrderRestrictionsAdminRegister extends AdminRegister implements IAdminRegisterStatic
|
||||
{
|
||||
public static function getMenu(): array
|
||||
{
|
||||
return [
|
||||
static::createMenuItem('ordersMenu',
|
||||
[
|
||||
'name' => 'OrderRestrictions',
|
||||
'left' => 's=menu.php&type=OrderRestrictions',
|
||||
'right' => 's=list.php&type=OrderRestrictions',
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPermissions(): array
|
||||
{
|
||||
return [
|
||||
static::createPermissions('OrderRestrictions', [\Modules::ORDER_RESTRICTIONS], ['ORDER'], false),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
$txt_str['OrderRestrictions'] = [
|
||||
'email' => 'Email',
|
||||
'id' => 'ID',
|
||||
'ip' => 'IP adresa',
|
||||
'description' => 'Důvod omezení',
|
||||
'date_created' => 'Datum vytvoření',
|
||||
|
||||
'flapOrderRestrictions' => 'Omezení objednávek',
|
||||
'titleEdit' => 'Omezení objednávek',
|
||||
|
||||
'toolbar_list' => 'Seznam omezení objednávek',
|
||||
'toolbar_add' => 'Přidat omezení objednávek',
|
||||
];
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
$txt_str['OrderRestrictions'] = [
|
||||
'email' => 'Email',
|
||||
'id' => 'ID',
|
||||
'ip' => 'IP address',
|
||||
'description' => 'Reason of the restriction',
|
||||
'date_created' => 'Creation date',
|
||||
|
||||
'flapOrderRestrictions' => 'Order restrictions',
|
||||
'titleEdit' => 'Order Restrictions',
|
||||
|
||||
'toolbar_list' => 'List of order restrictions',
|
||||
'toolbar_add' => 'Add an order restriction',
|
||||
];
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace KupShop\OrderRestrictionsBundle\Admin\lists;
|
||||
|
||||
use KupShop\AdminBundle\AdminList\BaseList;
|
||||
|
||||
class OrderRestrictionsList extends BaseList
|
||||
{
|
||||
protected $tableDef = [
|
||||
'id' => 'id',
|
||||
'fields' => [
|
||||
'id' => ['field' => 'id', 'translate' => true],
|
||||
'date_created' => ['field' => 'date_created', 'render' => 'renderDateTime', 'translate' => true],
|
||||
'email' => ['field' => 'email', 'translate' => true],
|
||||
'ip' => ['field' => 'ip', 'translate' => true],
|
||||
'description' => ['field' => 'description', 'translate' => true],
|
||||
],
|
||||
];
|
||||
|
||||
public function getQuery()
|
||||
{
|
||||
$qb = sqlQueryBuilder()
|
||||
->select('*')
|
||||
->from('order_restrictions');
|
||||
|
||||
return $qb;
|
||||
}
|
||||
}
|
||||
|
||||
return OrderRestrictionsList::class;
|
||||
@@ -0,0 +1,34 @@
|
||||
{extends file="[shared]/window.tpl"}
|
||||
|
||||
{block tabs}
|
||||
{windowTab id='flapOrderRestrictions'}
|
||||
{/block}
|
||||
|
||||
{block tabsContent}
|
||||
<div id="flapOrderRestrictions" class="tab-pane fade active in boxFlex">
|
||||
<div class="form-group">
|
||||
<div class="col-md-2 control-label">
|
||||
<label>{'email'|translate}</label>
|
||||
</div>
|
||||
<div class="col-md-5">
|
||||
<input type="email" class="form-control input-sm" name="data[email]" maxlength="100" value="{$body.data.email}"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-md-2 control-label">
|
||||
<label>{'ip'|translate}</label>
|
||||
</div>
|
||||
<div class="col-md-5">
|
||||
<input type="text" class="form-control input-sm" name="data[ip]" maxlength="100" value="{$body.data.ip}"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-md-2 control-label">
|
||||
<label>{'description'|translate}</label>
|
||||
</div>
|
||||
<div class="col-md-5">
|
||||
<textarea class="form-control" name="data[description]">{$body.data.description}</textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace KupShop\OrderRestrictionsBundle\EventListener;
|
||||
|
||||
use KupShop\KupShopBundle\Event\CreateMenuEvent;
|
||||
use KupShop\KupShopBundle\Util\Compat\SymfonyBridge;
|
||||
use KupShop\OrderingBundle\Event\CartEvent;
|
||||
use KupShop\OrderingBundle\Exception\CartValidationException;
|
||||
use Query\Operator;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
class CartEventListener implements EventSubscriberInterface
|
||||
{
|
||||
/**
|
||||
* @var RequestStack
|
||||
*/
|
||||
protected $requestStack;
|
||||
|
||||
public function __construct(RequestStack $requestStack)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
CartEvent::CHECK_CART => [
|
||||
['checkRestrictions', 200],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws CartValidationException
|
||||
*
|
||||
* @var CreateMenuEvent
|
||||
*/
|
||||
public function checkRestrictions(CartEvent $event)
|
||||
{
|
||||
$cart = $event->getCart();
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
|
||||
if (!$request) {
|
||||
$request = SymfonyBridge::getCurrentRequest();
|
||||
}
|
||||
|
||||
$restriction = sqlQueryBuilder()->select('*')
|
||||
->from('order_restrictions')
|
||||
->where(Operator::equals([
|
||||
'email' => $cart->invoice['email'] ?? null,
|
||||
'ip' => $request->getClientIp(),
|
||||
], 'OR'))
|
||||
->execute()
|
||||
->fetch();
|
||||
|
||||
if ($restriction) {
|
||||
throw new CartValidationException(translate('error', 'OrderRestrictions'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace KupShop\OrderRestrictionsBundle;
|
||||
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
|
||||
class OrderRestrictionsBundle extends Bundle
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
services:
|
||||
_defaults:
|
||||
autoconfigure: true
|
||||
autowire: true
|
||||
|
||||
KupShop\OrderRestrictionsBundle\:
|
||||
resource: ../../{Utils,EventListener,Admin/OrderRestrictionsAdminRegister.php}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace KupShop\OrderRestrictionsBundle\Resources\upgrade;
|
||||
|
||||
class OrderRestrictionsUpgrade extends \UpgradeNew
|
||||
{
|
||||
public function check_OrderRestrictionsTable()
|
||||
{
|
||||
return $this->checkTableExists('order_restrictions');
|
||||
}
|
||||
|
||||
/** Create order restrictions table */
|
||||
public function upgrade_OrderRestrictionsTable()
|
||||
{
|
||||
sqlQuery('CREATE TABLE order_restrictions (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
date_created DATETIME,
|
||||
email VARCHAR(255) DEFAULT NULL,
|
||||
ip VARCHAR(255) DEFAULT NULL,
|
||||
description VARCHAR(255) DEFAULT "",
|
||||
UNIQUE (email),
|
||||
UNIQUE (ip)
|
||||
);
|
||||
');
|
||||
|
||||
$this->upgradeOK();
|
||||
}
|
||||
|
||||
public function check_orderRestrictionsDateCreatedDefault()
|
||||
{
|
||||
return $this->checkColumnDefault('order_restrictions', 'date_created', 'current_timestamp()');
|
||||
}
|
||||
|
||||
/** Set order_restrictions.date_created default to current_timestamp */
|
||||
public function upgrade_orderRestrictionsDateCreatedDefault()
|
||||
{
|
||||
sqlQuery('ALTER TABLE `order_restrictions` MODIFY COLUMN `date_created` DATETIME DEFAULT NOW()');
|
||||
|
||||
$this->upgradeOK();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;
|
||||
use KupShop\KupShopBundle\Util\Compat\SymfonyBridge;
|
||||
use KupShop\OrderingBundle\Exception\CartValidationException;
|
||||
|
||||
class OrderRestrictionsTest extends DatabaseTestCase
|
||||
{
|
||||
use CartTestTrait;
|
||||
|
||||
public function getDataSet()
|
||||
{
|
||||
return $this->getJsonDataSet('
|
||||
{
|
||||
"order_restrictions": [
|
||||
{
|
||||
"id":1,
|
||||
"date_created":"2019-09-05 23:19:47",
|
||||
"email":"petr@wpj.cz",
|
||||
"ip":null,
|
||||
"description":"blokace emailu"
|
||||
},
|
||||
{
|
||||
"id":2,
|
||||
"date_created":"2019-09-05 23:19:59",
|
||||
"email":null,
|
||||
"ip":"8.8.8.8",
|
||||
"description":"blokace IP"
|
||||
}
|
||||
]
|
||||
}
|
||||
');
|
||||
}
|
||||
|
||||
public function testEmailRestriction()
|
||||
{
|
||||
$this->loginUser(1);
|
||||
|
||||
$this->createCart();
|
||||
$this->insertProduct(1, 16, 1.35);
|
||||
|
||||
$this->expectException(CartValidationException::class);
|
||||
$this->checkOrderPriceIsSameAsCart();
|
||||
}
|
||||
|
||||
public function testIPRestrictions()
|
||||
{
|
||||
$this->loginUser(2);
|
||||
SymfonyBridge::getCurrentRequest()->server->set('REMOTE_ADDR', '8.8.8.8');
|
||||
$this->createCart();
|
||||
$this->insertProduct(1, 16, '1,35');
|
||||
|
||||
$this->expectException(CartValidationException::class);
|
||||
$this->checkOrderPriceIsSameAsCart();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user