Merge branch 'template'

Conflicts:
	templates/admin/default/customer-edit.html
This commit is contained in:
Manuel Raynaud
2013-09-12 15:44:23 +02:00
18 changed files with 619 additions and 108 deletions

View File

@@ -26,6 +26,7 @@ namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\ActionEvent;
use Thelia\Core\Event\CustomerCreateOrUpdateEvent;
use Thelia\Core\Event\CustomerEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Customer as CustomerModel;
use Thelia\Core\Event\CustomerLoginEvent;
@@ -59,6 +60,13 @@ class Customer extends BaseAction implements EventSubscriberInterface
}
public function delete(CustomerEvent $event)
{
$customer = $event->getCustomer();
$customer->delete();
}
private function createOrUpdateCustomer(CustomerModel $customer, CustomerCreateOrUpdateEvent $event)
{
$customer->setDispatcher($this->getDispatcher());
@@ -80,7 +88,8 @@ class Customer extends BaseAction implements EventSubscriberInterface
$event->getLang(),
$event->getReseller(),
$event->getSponsor(),
$event->getDiscount()
$event->getDiscount(),
$event->getCompany()
);
$event->setCustomer($customer);
@@ -143,6 +152,7 @@ class Customer extends BaseAction implements EventSubscriberInterface
TheliaEvents::CUSTOMER_UPDATEACCOUNT => array("modify", 128),
TheliaEvents::CUSTOMER_LOGOUT => array("logout", 128),
TheliaEvents::CUSTOMER_LOGIN => array("login" , 128),
TheliaEvents::CUSTOMER_DELETEACCOUNT => array("delete", 128),
);
}
}

View File

@@ -37,11 +37,20 @@
<default key="_controller">Thelia\Controller\Admin\CustomerController::indexAction</default>
</route>
<route id="admin.customer.update.view" path="/admin/customer/update/{customer_id}">
<route id="admin.customer.update.view" path="/admin/customer/update/{customer_id}" methods="get">
<default key="_controller">Thelia\Controller\Admin\CustomerController::viewAction</default>
<requirement key="customer_id">\d+</requirement>
</route>
<route id="admin.customer.update.process" path="/admin/customer/update/{customer_id}" methods="post">
<default key="_controller">Thelia\Controller\Admin\CustomerController::updateAction</default>
<requirement key="customer_id">\d+</requirement>
</route>
<route id="admin.customer.delete" path="/admin/customer/delete">
<default key="_controller">Thelia\Controller\Admin\CustomerController::deleteAction</default>
</route>
<!-- end Customer rule management -->
<!-- Order rule management -->

View File

@@ -10,15 +10,31 @@
</route>
<!-- Customer routes -->
<route id="customer.create.view" path="/register">
<default key="_controller">Thelia\Controller\Front\DefaultController::noAction</default>
<default key="_view">register</default>
</route>
<route id="customer.login.view" path="/login">
<default key="_controller">Thelia\Controller\Front\DefaultController::noAction</default>
<default key="_view">login</default>
</route>
<route id="customer.logout.process" path="/logout">
<default key="_controller">Thelia\Controller\Front\CustomerController::logoutAction</default>
</route>
<route id="customer.account.view" path="/customer/account">
<default key="_controller">Thelia\Controller\Front\DefaultController::noAction</default>
<default key="_view">account</default>
</route>
<route id="customer.create.process" path="/customer/create" methods="post">
<default key="_controller">Thelia\Controller\Front\CustomerController::createAction</default>
<default key="_view">register</default>
</route>
<route id="customer.create.view" path="/register">
<default key="_controller">Thelia\Controller\Front\DefaultController::noAction</default>
<default key="_view">register</default>
</route>
<route id="customer.update.process" path="/customer/update" methods="post">
<default key="_controller">Thelia\Controller\Front\CustomerController::updateAction</default>
@@ -29,14 +45,6 @@
<default key="_view">login</default>
</route>
<route id="customer.login.view" path="/login">
<default key="_controller">Thelia\Controller\Front\DefaultController::noAction</default>
<default key="_view">login</default>
</route>
<route id="customer.logout.process" path="/logout">
<default key="_controller">Thelia\Controller\Front\CustomerController::logoutAction</default>
</route>
<route id="customer.password.retrieve.view" path="/password" methods="get">
<default key="_controller">Thelia\Controller\Front\DefaultController::noAction</default>

View File

@@ -22,6 +22,15 @@
/*************************************************************************************/
namespace Thelia\Controller\Admin;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\Form\Form;
use Thelia\Core\Event\CustomerCreateOrUpdateEvent;
use Thelia\Core\Event\CustomerEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Form\CustomerModification;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Model\CustomerQuery;
use Thelia\Core\Translation\Translator;
/**
* Class CustomerController
@@ -32,15 +41,141 @@ class CustomerController extends BaseAdminController
{
public function indexAction()
{
if (null !== $response = $this->checkAuth("admin.customers.view")) return $response;
if (null !== $response = $this->checkAuth("admin.customer.view")) return $response;
return $this->render("customers", array("display_customer" => 20));
}
public function viewAction($customer_id)
{
if (null !== $response = $this->checkAuth("admin.customer.view")) return $response;
return $this->render("customer-edit", array(
"customer_id" => $customer_id
));
}
/**
* update customer action
*
* @param $customer_id
* @return mixed|\Symfony\Component\HttpFoundation\Response
*/
public function updateAction($customer_id)
{
if (null !== $response = $this->checkAuth("admin.customer.update")) return $response;
$message = false;
$customerModification = new CustomerModification($this->getRequest());
try {
$customer = CustomerQuery::create()->findPk($customer_id);
if(null === $customer) {
throw new \InvalidArgumentException(sprintf("%d customer id does not exists", $customer_id));
}
$form = $this->validateForm($customerModification);
$event = $this->createEventInstance($form->getData());
$event->setCustomer($customer);
$this->dispatch(TheliaEvents::CUSTOMER_UPDATEACCOUNT, $event);
$customerUpdated = $event->getCustomer();
$this->adminLogAppend(sprintf("Customer with Ref %s (ID %d) modified", $customerUpdated->getRef() , $customerUpdated->getId()));
if($this->getRequest()->get("save_mode") == "close") {
$this->redirectToRoute("admin.customers");
} else {
$this->redirectSuccess($customerModification);
}
} catch (FormValidationException $e) {
$message = sprintf("Please check your input: %s", $e->getMessage());
} catch (PropelException $e) {
$message = $e->getMessage();
} catch (\Exception $e) {
$message = sprintf("Sorry, an error occured: %s", $e->getMessage()." ".$e->getFile());
}
if ($message !== false) {
\Thelia\Log\Tlog::getInstance()->error(sprintf("Error during customer login process : %s.", $message));
$customerModification->setErrorMessage($message);
$this->getParserContext()
->addForm($customerModification)
->setGeneralError($message)
;
}
return $this->render("customer-edit", array(
"customer_id" => $customer_id
));
}
public function deleteAction()
{
if (null !== $response = $this->checkAuth("admin.customer.delete")) return $response;
$message = null;
try {
$customer_id = $this->getRequest()->get("customer_id");
$customer = CustomerQuery::create()->findPk($customer_id);
if(null === $customer) {
throw new \InvalidArgumentException(Translator::getInstance("The customer you want to delete does not exists"));
}
$event = new CustomerEvent($customer);
$this->dispatch(TheliaEvents::CUSTOMER_DELETEACCOUNT, $event);
} catch(\Exception $e) {
$message = $e->getMessage();
}
$params = array(
"customer_page" => $this->getRequest()->get("customer_page", 1)
);
if ($message) {
$params["delete_error_message"] = $message;
}
$this->redirectToRoute("admin.customers", $params);
}
/**
* @param $data
* @return CustomerCreateOrUpdateEvent
*/
private function createEventInstance($data)
{
$customerCreateEvent = new CustomerCreateOrUpdateEvent(
$data["title"],
$data["firstname"],
$data["lastname"],
$data["address1"],
$data["address2"],
$data["address3"],
$data["phone"],
$data["cellphone"],
$data["zipcode"],
$data["city"],
$data["country"],
isset($data["email"])?$data["email"]:null,
isset($data["password"]) ? $data["password"]:null,
$this->getRequest()->getSession()->getLang()->getId(),
isset($data["reseller"])?$data["reseller"]:null,
isset($data["sponsor"])?$data["sponsor"]:null,
isset($data["discount"])?$data["discount"]:null,
isset($data["company"])?$data["company"]:null
);
return $customerCreateEvent;
}
}

View File

@@ -278,7 +278,8 @@ class CustomerController extends BaseFrontController
$this->getRequest()->getSession()->getLang()->getId(),
isset($data["reseller"])?$data["reseller"]:null,
isset($data["sponsor"])?$data["sponsor"]:null,
isset($data["discount"])?$data["discount"]:null
isset($data["discount"])?$data["discount"]:null,
isset($data["company"])?$data["company"]:null
);
return $customerCreateEvent;

View File

@@ -1,17 +1,35 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: manu
* Date: 16/08/13
* Time: 10:24
* To change this template use File | Settings | File Templates.
*/
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* 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\Event;
use Symfony\Component\EventDispatcher\Event;
use Thelia\Model\Customer;
/**
* Class CustomerCreateOrUpdateEvent
* @package Thelia\Core\Event
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class CustomerCreateOrUpdateEvent extends ActionEvent
{
//base parameters for creating new customer
@@ -32,6 +50,7 @@ class CustomerCreateOrUpdateEvent extends ActionEvent
protected $reseller;
protected $sponsor;
protected $discount;
protected $company;
/**
* @var \Thelia\Model\Customer
@@ -39,7 +58,7 @@ class CustomerCreateOrUpdateEvent extends ActionEvent
protected $customer;
/**
* @param int $title the title customer id
* @param int $title the title customer id
* @param string $firstname
* @param string $lastname
* @param string $address1
@@ -49,15 +68,16 @@ class CustomerCreateOrUpdateEvent extends ActionEvent
* @param string $cellphone
* @param string $zipcode
* @param string $city
* @param int $country the country id
* @param int $country the country id
* @param string $email
* @param string $password plain password, don't put hash password, it will hashes again
* @param $lang
* @param int $reseller if customer is a reseller
* @param int $sponsor customer's id sponsor
* @param int $reseller if customer is a reseller
* @param int $sponsor customer's id sponsor
* @param float $discount
* @param string $company
*/
public function __construct($title, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $city, $country, $email, $password, $lang, $reseller, $sponsor, $discount)
public function __construct($title, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $city, $country, $email, $password, $lang, $reseller, $sponsor, $discount, $company)
{
$this->address1 = $address1;
$this->address2 = $address2;
@@ -72,9 +92,18 @@ class CustomerCreateOrUpdateEvent extends ActionEvent
$this->cellphone = $cellphone;
$this->title = $title;
$this->zipcode = $zipcode;
$this->city = $city;
$this->reseller = $reseller;
$this->sponsor = $sponsor;
$this->discount = $discount;
$this->company = $company;
}
/**
* @return mixed
*/
public function getCompany()
{
return $this->company;
}
/**

View File

@@ -71,6 +71,11 @@ final class TheliaEvents
*/
const CUSTOMER_UPDATEACCOUNT = "action.updateCustomer";
/**
* sent on customer removal
*/
const CUSTOMER_DELETEACCOUNT = "action.deleteCustomer";
/**
* sent when a customer need a new password
*/
@@ -103,6 +108,16 @@ final class TheliaEvents
*/
const AFTER_UPDATECUSTOMER = "action.after_updateCustomer";
/**
* sent just before customer removal
*/
const BEFORE_DELETECUSTOMER = "action.before_updateCustomer";
/**
* sent just after customer removal
*/
const AFTER_DELETECUSTOMER = "action.after_deleteCustomer";
// -- ADDRESS MANAGEMENT ---------------------------------------------------------
/**
* sent for address creation

View File

@@ -63,7 +63,7 @@ class Address extends BaseLoop
),
'current'
),
Argument::createBooleanTypeArgument('default', false),
Argument::createBooleanTypeArgument('default'),
Argument::createIntListTypeArgument('exclude')
);
}
@@ -100,6 +100,8 @@ class Address extends BaseLoop
if ($default === true) {
$search->filterByIsDefault(1, Criteria::EQUAL);
} else if($default === false) {
$search->filterByIsDefault(0, Criteria::EQUAL);
}
$exclude = $this->getExclude();

View File

@@ -58,6 +58,12 @@ class CustomerModification extends BaseForm
$this->formBuilder
->add('update_logged_in_user', 'integer') // In a front office context, update the in-memory logged-in user data
->add("company", "text", array(
"label" => Translator::getInstance()->trans("Company"),
"label_attr" => array(
"for" => "company"
)
))
->add("firstname", "text", array(
"constraints" => array(
new Constraints\NotBlank()

View File

@@ -54,7 +54,7 @@ class Customer extends BaseCustomer implements UserInterface
* @param int $discount
* @throws \Exception|\Symfony\Component\Config\Definition\Exception\Exception
*/
public function createOrUpdate($titleId, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $city, $countryId, $email = null, $plainPassword = null, $lang = null, $reseller = 0, $sponsor = null, $discount = 0)
public function createOrUpdate($titleId, $firstname, $lastname, $address1, $address2, $address3, $phone, $cellphone, $zipcode, $city, $countryId, $email = null, $plainPassword = null, $lang = null, $reseller = 0, $sponsor = null, $discount = 0, $company = null)
{
$this
->setTitleId($titleId)
@@ -79,6 +79,7 @@ class Customer extends BaseCustomer implements UserInterface
$address = new Address();
$address
->setCompany($company)
->setTitleId($titleId)
->setFirstname($firstname)
->setLastname($lastname)
@@ -88,6 +89,7 @@ class Customer extends BaseCustomer implements UserInterface
->setPhone($phone)
->setCellphone($cellphone)
->setZipcode($zipcode)
->setCity($city)
->setCountryId($countryId)
->setIsDefault(1)
;
@@ -98,6 +100,7 @@ class Customer extends BaseCustomer implements UserInterface
$address = $this->getDefaultAddress();
$address
->setCompany($company)
->setTitleId($titleId)
->setFirstname($firstname)
->setLastname($lastname)
@@ -107,6 +110,7 @@ class Customer extends BaseCustomer implements UserInterface
->setPhone($phone)
->setCellphone($cellphone)
->setZipcode($zipcode)
->setCity($city)
->setCountryId($countryId)
->save($con)
;
@@ -242,7 +246,7 @@ class Customer extends BaseCustomer implements UserInterface
*/
public function preDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_DELETECONFIG, new CustomerEvent($this));
$this->dispatchEvent(TheliaEvents::BEFORE_DELETECUSTOMER, new CustomerEvent($this));
return true;
}
@@ -251,6 +255,6 @@ class Customer extends BaseCustomer implements UserInterface
*/
public function postDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_DELETECONFIG, new CustomerEvent($this));
$this->dispatchEvent(TheliaEvents::AFTER_DELETECUSTOMER, new CustomerEvent($this));
}
}

View File

@@ -0,0 +1,102 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* 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\Tests\Action\ImageTest;
use Thelia\Action\Customer;
use Thelia\Core\Event\CustomerCreateOrUpdateEvent;
/**
* Class CustomerTest
* @package Thelia\Tests\Action\ImageTest
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class CustomerTest extends \PHPUnit_Framework_TestCase
{
public function getContainer()
{
$container = new \Symfony\Component\DependencyInjection\ContainerBuilder();
$dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface");
$container->set("event_dispatcher", $dispatcher);
return $container;
}
public function testCreatedCustomer()
{
$customerCreateEvent = new CustomerCreateOrUpdateEvent(
1,
"thelia",
"thelia",
"street address 1",
"street address 2",
"street address 3",
"0102030405",
"0607080910",
"63000",
"clermont-ferrand",
64,
sprintf("%s@thelia.fr", uniqid()),
uniqid(),
1,
0,
0,
0,
'My super company'
);
$customerAction = new Customer($this->getContainer());
$customerAction->create($customerCreateEvent);
$customerCreated = $customerCreateEvent->getCustomer();
$this->assertInstanceOf("Thelia\Model\Customer", $customerCreated, "new customer created must be an instance of Thelia\Model\Customer");
$this->assertFalse($customerCreated->isNew());
$this->assertEquals($customerCreateEvent->getFirstname(), $customerCreated->getFirstname());
$this->assertEquals($customerCreateEvent->getLastname(), $customerCreated->getLastname());
$this->assertEquals($customerCreateEvent->getTitle(), $customerCreated->getTitleId());
$this->assertEquals($customerCreateEvent->getEmail(), $customerCreated->getEmail());
$this->assertEquals($customerCreated->getReseller(), $customerCreated->getReseller());
$this->assertEquals($customerCreated->getSponsor(), $customerCreated->getSponsor());
$this->assertEquals($customerCreated->getDiscount(), $customerCreated->getDiscount());
$addressCreated = $customerCreated->getDefaultAddress();
$this->assertEquals($customerCreateEvent->getFirstname(), $addressCreated->getFirstname());
$this->assertEquals($customerCreateEvent->getLastname(), $addressCreated->getLastname());
$this->assertEquals($customerCreateEvent->getTitle(), $addressCreated->getTitleId());
$this->assertEquals($customerCreateEvent->getAddress1(), $addressCreated->getAddress1());
$this->assertEquals($customerCreateEvent->getAddress2(), $addressCreated->getAddress2());
$this->assertEquals($customerCreateEvent->getAddress3(), $addressCreated->getAddress3());
$this->assertEquals($customerCreateEvent->getZipcode(), $addressCreated->getZipcode());
$this->assertEquals($customerCreateEvent->getCity(), $addressCreated->getCity());
$this->assertEquals($customerCreateEvent->getCountry(), $addressCreated->getCountryId());
$this->assertEquals($customerCreateEvent->getPhone(), $addressCreated->getPhone());
$this->assertEquals($customerCreateEvent->getCellphone(), $addressCreated->getCellphone());
$this->assertEquals($customerCreateEvent->getCompany(), $addressCreated->getCompany());
}
}