public function paymentAddressStepAction()
{
$basket = $this->get('sonata.basket');
if ($basket->countBasketElements() == 0) {
return new RedirectResponse($this->generateUrl('sonata_basket_index'));
}
$customer = $basket->getCustomer();
if (!$customer) {
throw new NotFoundHttpException('customer not found');
}
$addresses = $customer->getAddressesByType(AddressInterface::TYPE_BILLING);
// Show address creation / selection form
$form = $this->createForm('sonata_basket_address', null, array('addresses' => $addresses->toArray()));
$template = 'SonataBasketBundle:Basket:payment_address_step.html.twig';
if ($this->get('request')->getMethod() == 'POST') {
$form->bind($this->get('request'));
if ($form->isValid()) {
if ($form->has('useSelected') && $form->get('useSelected')->isClicked()) {
$address = $form->get('addresses')->getData();
} else {
$address = $form->getData();
$address->setType(AddressInterface::TYPE_BILLING);
$customer->addAddress($address);
$this->get('sonata.customer.manager')->save($customer);
$this->get('session')->getFlashBag()->add('sonata_customer_success', 'address_add_success');
}
$basket->setCustomer($customer);
$basket->setBillingAddress($address);
// save the basket
$this->get('sonata.basket.factory')->save($basket);
return new RedirectResponse($this->generateUrl('sonata_basket_payment'));
}
}
// Set URL to be redirected to once edited address
$this->get('session')->set('sonata_address_redirect', $this->generateUrl('sonata_basket_payment_address'));
$this->get('sonata.seo.page')->setTitle($this->get('translator')->trans('basket_payment_title', array(), 'SonataBasketBundle'));
return $this->render($template, array('form' => $form->createView(), 'addresses' => $addresses));
}