src/Controller/BO/HomeController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller\BO;
  3. use App\Entity\City;
  4. use App\Entity\Country;
  5. use App\Entity\CustomerBilling;
  6. use App\Entity\State;
  7. use App\Entity\User;
  8. use App\Form\CustomerBillingType;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. class HomeController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("", name="homepage")
  20.      */
  21.     public function index(): Response
  22.     {
  23.         return $this->render('bo/index.html.twig', [
  24.             'controller_name' => 'DashboardController',
  25.         ]);
  26.     }
  27.     /**
  28.      * @Route("/get_states_by_country/{id}", name="get_states_by_country", methods={"GET"})
  29.      */
  30.     public function getStatesByCountry(Request $requestEntityManagerInterface $emCountry $country): JsonResponse
  31.     {
  32.         $states $em->getRepository(State::class)->findBy(['country' => $country]);
  33.         $options '<option value="">--X--</option>';
  34.         foreach ($states as $state) {
  35.             $options .= '<option value="' $state->getId() . '">' $state->getName() . '</option>';
  36.         }
  37.         return new JsonResponse($options);
  38.     }
  39.     /**
  40.      * @Route("/get_cities_by_state/{id}", name="get_cities_by_state", methods={"GET"})
  41.      */
  42.     public function getCitiesByState(Request $requestEntityManagerInterface $emCountry $country): JsonResponse
  43.     {
  44.         $cities $em->getRepository(City::class)->findBy(['country' => $country]);
  45.         $options '<option value="">--X--</option>';
  46.         foreach ($cities as $city) {
  47.             $options .= '<option value="' $city->getId() . '">' $city->getName() . '</option>';
  48.         }
  49.         return new JsonResponse($options);
  50.     }
  51.     /**
  52.      * @Route("/createBillingInfo", name="create_billing_info", methods={"POST"})
  53.      */
  54.     public function createBillingInfo(Request $requestEntityManagerInterface $em): JsonResponse
  55.     {
  56.         $customerBilling = new CustomerBilling();
  57.         $billingForm $this->createForm(CustomerBillingType::class, $customerBilling);
  58.         $billingForm->handleRequest($request);
  59.         if ($billingForm->isSubmitted() && $billingForm->isValid()) {
  60.             $user $this->getUser();
  61.             $this->executeBillingCreation($em$user$customerBilling);
  62.             return new JsonResponse([
  63.                 'status' => true
  64.             ]);
  65.         }
  66.         $errors $this->getFormErrors($billingForm);
  67.         return new JsonResponse([
  68.             'status' => false,
  69.             'errors'=>$errors
  70.         ]);
  71.     }
  72.     public function executeBillingCreation(EntityManagerInterface $emUser $userCustomerBilling $customerBilling): CustomerBilling
  73.     {
  74.         $customerBilling->setUser($user);
  75.         $em->persist($customerBilling);
  76.         $em->flush($customerBilling);
  77.         $user->setCustomerBilling($customerBilling);
  78.         $em->persist($user);
  79.         $em->flush($user);
  80.         return $customerBilling;
  81.     }
  82.     private function getFormErrors(FormInterface $form): array
  83.     {
  84.         $errors = [];
  85.         // Iterate through each form field
  86.         foreach ($form as $child) {
  87.             // Get errors for each field
  88.             foreach ($child->getErrors() as $error) {
  89.                 $errors[$child->getName()][] = $error->getMessage();
  90.             }
  91.             // If the field is a nested form, recursively call the function
  92.             if ($child->isSubmitted() && $child->count() > 0) {
  93.                 $nestedErrors $this->getFormErrors($child);
  94.                 if (!empty($nestedErrors)) {
  95.                     $errors[$child->getName()] = $nestedErrors;
  96.                 }
  97.             }
  98.         }
  99.         return $errors;
  100.     }
  101. }