<?php
declare(strict_types=1);
namespace App\Controller\Customer;
use App\Entity\Customer\CustomerInterface;
use App\Entity\Customer\CustomerNote;
use Sylius\Bundle\CoreBundle\Form\Type\Customer\CustomerDefaultAddressType;
use Sylius\Bundle\CustomerBundle\Form\Type\CustomerType;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Component\Core\Model\AdminUserInterface;
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class CustomerController extends ResourceController
{
public function addCustomerNote(Request $request, string $customerId, string $adminId): Response
{
/** @var CustomerRepositoryInterface $customerRepository */
$customerRepository = $this->container->get('sylius.repository.customer');
/** @var RepositoryInterface $adminUserRepository */
$adminUserRepository = $this->container->get('sylius.repository.admin_user');
/** @var CustomerInterface $customer */
$customer = $customerRepository->findOneBy(['id' => (int)$customerId]);
/** @var AdminUserInterface $admin */
$admin = $adminUserRepository->findOneBy(['id' => (int)$adminId]);
$router = $this->container->get('router');
$form = $this->createForm(CustomerType::class);
$form->handleRequest($request);
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) {
$customerNotes = $request->request->get('sylius_customer')['notes'];
foreach ($customerNotes as $customerNote) {
$newCustomerNote = new CustomerNote();
$newCustomerNote->setMessage($customerNote['message']);
$newCustomerNote->setCreatedAt(new \DateTime());
$newCustomerNote->setCustomer($customer);
$newCustomerNote->setAuthor($admin);
$customer->addNote($newCustomerNote);
}
$customerRepository->add($customer);
return RedirectResponse::create($router->generate(
'sylius_admin_customer_show',
['id' => $customer->getId()]
));
}
return $this->render('Admin/Customer/customerNotes.html.twig', [
'form' => $form->createView(),
'resource' => $customer,
'customer' => $customer,
]);
}
public function setCustomerDefaultAddress(Request $request, string $customerId): Response
{
/** @var CustomerRepositoryInterface $customerRepository */
$customerRepository = $this->container->get('sylius.repository.customer');
/** @var CustomerInterface $customer */
$customer = $customerRepository->findOneBy(['id' => (int)$customerId]);
$router = $this->container->get('router');
$form = $this->createForm(CustomerDefaultAddressType::class, null, ['customer' => $customer]);
$form->handleRequest($request);
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) {
$data = $form->getData();
$address = $data['defaultAddress'];
$customer->setDefaultAddress($address);
$customerRepository->add($customer);
return RedirectResponse::create($router->generate(
'sylius_admin_customer_show',
['id' => $customer->getId()]
));
}
return $this->render('Admin/Customer/defaultAddress.html.twig', [
'form' => $form->createView(),
'resource' => $customer,
'customer' => $customer,
]);
}
}