create new method allowing to add paramter to the current query string

This commit is contained in:
Manuel Raynaud
2013-08-06 16:13:34 +02:00
parent 86161f0320
commit 75929e1d4f
3 changed files with 111 additions and 29 deletions

View File

@@ -23,6 +23,7 @@
namespace Thelia\Action; namespace Thelia\Action;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\Config\Definition\Exception\Exception; use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@@ -38,6 +39,7 @@ use Thelia\Model\CartQuery;
use Thelia\Model\Cart as CartModel; use Thelia\Model\Cart as CartModel;
use Thelia\Model\ConfigQuery; use Thelia\Model\ConfigQuery;
use Thelia\Model\Customer; use Thelia\Model\Customer;
use Thelia\Tools\Redirect;
/** /**
* *
@@ -71,37 +73,45 @@ class Cart implements EventSubscriberInterface
{ {
$request = $event->getRequest(); $request = $event->getRequest();
$form = $this->getAddCartForm($request); $cartAdd = $this->getAddCartForm($request);
$form = $cartAdd->getForm();
$form->bind($request); $form->bind($request);
if($form->isValid()) { if($form->isValid()) {
$cart = $this->getCart($request); try {
$cart = $this->getCart($request);
$productSaleElementsId = $form->get("product_sale_elements_id")->getData(); $productSaleElementsId = $form->get("product_sale_elements_id")->getData();
$productPrice = ProductPriceQuery::create() $productPrice = ProductPriceQuery::create()
->filterByProductSaleElementsId($productSaleElementsId) ->filterByProductSaleElementsId($productSaleElementsId)
->findOne() ->findOne()
; ;
$cartItem = new CartItem(); $cartItem = new CartItem();
$cartItem->setDisptacher($event->getDispatcher()); $cartItem->setDisptacher($event->getDispatcher());
$cartItem $cartItem
->setCart($cart) ->setCart($cart)
->setProductId($form->get("product")->getData()) ->setProductId($form->get("product")->getData())
->setProductSaleElementsId($productSaleElementsId) ->setProductSaleElementsId($productSaleElementsId)
->setQuantity($form->get("quantity")->getData()) ->setQuantity($form->get("quantity")->getData())
->setPrice($productPrice->getPrice()) ->setPrice($productPrice->getPrice())
->setPromoPrice($productPrice->getPromoPrice()) ->setPromoPrice($productPrice->getPromoPrice())
->setPriceEndOfLife(time() + ConfigQuery::read("cart.priceEOF", 60*60*24*30)) ->setPriceEndOfLife(time() + ConfigQuery::read("cart.priceEOF", 60*60*24*30))
->save(); ->save();
; ;
Redirect::exec($cartAdd->getSuccessUrl());
} catch (PropelException $e) {
\Thelia\Log\Tlog::getInstance()->error(sptinf("error on adding item to cart with message : %s", $e->getMessage()));
$message = "Impossible to add this article to your cart, please try again";
}
} else { } else {
$message = "Missing or invalid data";
} }
} }
@@ -120,7 +130,7 @@ class Cart implements EventSubscriberInterface
); );
} }
return $cartAdd->getForm(); return $cartAdd;
} }

View File

@@ -1,12 +1,25 @@
<?php <?php
/** /*************************************************************************************/
* Created by JetBrains PhpStorm. /* */
* User: manu /* Thelia */
* Date: 08/07/13 /* */
* Time: 11:41 /* Copyright (c) OpenStudio */
* To change this template use File | Settings | File Templates. /* email : info@thelia.net */
*/ /* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\HttpFoundation; namespace Thelia\Core\HttpFoundation;
use Symfony\Component\HttpFoundation\Request as BaseRequest; use Symfony\Component\HttpFoundation\Request as BaseRequest;
@@ -19,4 +32,20 @@ class Request extends BaseRequest{
return $this->get("product_id"); return $this->get("product_id");
} }
public function getUriAddingParameters(array $parameters = null)
{
$uri = $this->getUri();
$additionalQs = '';
foreach ($parameters as $key => $value) {
$additionalQs .= sprintf("&%s=%s", $key, $value);
}
if ('' == $this->getQueryString()) {
$additionalQs = '?'. ltrim($additionalQs, '&');
}
return $uri . $additionalQs;
}
} }

View File

@@ -0,0 +1,43 @@
<?php
namespace Thelia\Tests\Core\HttpFoundation;
use Thelia\Core\HttpFoundation\Request;
class RequestTest extends \PHPUnit_Framework_TestCase
{
public function testGetUriAddingParameters()
{
$request = $this->getMock(
"Thelia\Core\HttpFoundation\Request",
array("getUri", "getQueryString")
);
$request->expects($this->any())
->method("getUri")
->will($this->onConsecutiveCalls(
"http://localhost/",
"http://localhost/?test=fu"
));
$request->expects($this->any())
->method("getQueryString")
->will($this->onConsecutiveCalls(
"",
"test=fu"
));
$result = $request->getUriAddingParameters(array("foo" => "bar"));
$this->assertEquals("http://localhost/?foo=bar", $result);
$result = $request->getUriAddingParameters(array("foo" => "bar"));
$this->assertEquals("http://localhost/?test=fu&foo=bar", $result);
}
}