src/Controller/Customer/CustomerController.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Customer;
  4. use App\Entity\Customer\CustomerInterface;
  5. use App\Entity\Customer\CustomerNote;
  6. use Sylius\Bundle\CoreBundle\Form\Type\Customer\CustomerDefaultAddressType;
  7. use Sylius\Bundle\CustomerBundle\Form\Type\CustomerType;
  8. use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
  9. use Sylius\Component\Core\Model\AdminUserInterface;
  10. use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
  11. use Sylius\Component\Resource\Repository\RepositoryInterface;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. class CustomerController extends ResourceController
  16. {
  17.     public function addCustomerNote(Request $requeststring $customerIdstring $adminId): Response
  18.     {
  19.         /** @var CustomerRepositoryInterface $customerRepository */
  20.         $customerRepository $this->container->get('sylius.repository.customer');
  21.         /** @var RepositoryInterface $adminUserRepository */
  22.         $adminUserRepository $this->container->get('sylius.repository.admin_user');
  23.         /** @var CustomerInterface $customer */
  24.         $customer $customerRepository->findOneBy(['id' => (int)$customerId]);
  25.         /** @var AdminUserInterface $admin */
  26.         $admin $adminUserRepository->findOneBy(['id' => (int)$adminId]);
  27.         $router $this->container->get('router');
  28.         $form $this->createForm(CustomerType::class);
  29.         $form->handleRequest($request);
  30.         if (in_array($request->getMethod(), ['POST''PUT''PATCH'], true)) {
  31.             $customerNotes $request->request->get('sylius_customer')['notes'];
  32.             foreach ($customerNotes as $customerNote) {
  33.                 $newCustomerNote = new CustomerNote();
  34.                 $newCustomerNote->setMessage($customerNote['message']);
  35.                 $newCustomerNote->setCreatedAt(new \DateTime());
  36.                 $newCustomerNote->setCustomer($customer);
  37.                 $newCustomerNote->setAuthor($admin);
  38.                 $customer->addNote($newCustomerNote);
  39.             }
  40.             $customerRepository->add($customer);
  41.             return RedirectResponse::create($router->generate(
  42.                 'sylius_admin_customer_show',
  43.                 ['id' => $customer->getId()]
  44.             ));
  45.         }
  46.         return $this->render('Admin/Customer/customerNotes.html.twig', [
  47.             'form' => $form->createView(),
  48.             'resource' => $customer,
  49.             'customer' => $customer,
  50.         ]);
  51.     }
  52.     public function setCustomerDefaultAddress(Request $requeststring $customerId): Response
  53.     {
  54.         /** @var CustomerRepositoryInterface $customerRepository */
  55.         $customerRepository $this->container->get('sylius.repository.customer');
  56.         /** @var CustomerInterface $customer */
  57.         $customer $customerRepository->findOneBy(['id' => (int)$customerId]);
  58.         $router $this->container->get('router');
  59.         $form $this->createForm(CustomerDefaultAddressType::class, null, ['customer' => $customer]);
  60.         $form->handleRequest($request);
  61.         if (in_array($request->getMethod(), ['POST''PUT''PATCH'], true)) {
  62.             $data $form->getData();
  63.             $address $data['defaultAddress'];
  64.             $customer->setDefaultAddress($address);
  65.             $customerRepository->add($customer);
  66.             return RedirectResponse::create($router->generate(
  67.                 'sylius_admin_customer_show',
  68.                 ['id' => $customer->getId()]
  69.             ));
  70.         }
  71.         return $this->render('Admin/Customer/defaultAddress.html.twig', [
  72.             'form' => $form->createView(),
  73.             'resource' => $customer,
  74.             'customer' => $customer,
  75.         ]);
  76.     }
  77. }