src/Controller/CashBox/CategoryController.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\Controller\CashBox;
  3. use App\Entity\Category;
  4. use App\Repository\CategoryRepository;
  5. use App\Service\CashBox\ProductService;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use App\Service\CashBox\PriceService;
  13. /**
  14.  * @Route("/")
  15.  */
  16. class CategoryController extends AbstractController
  17. {
  18.     private PriceService $priceService;
  19.     /**
  20.      * CategoryController constructor.
  21.      */
  22.     public function __construct()
  23.     {
  24.         $this->priceService = new PriceService();
  25.     }
  26.     /**
  27.      * @Route("/categories/tree", name="caschbox_categories_tree", methods={"GET"})
  28.      * @param CategoryRepository $categoryRepository
  29.      * @param EntityManagerInterface $entityManager
  30.      * @return Response
  31.      */
  32.     public function tree(CategoryRepository $categoryRepositoryEntityManagerInterface $entityManager): Response
  33.     {
  34.         $repo $entityManager->getRepository(Category::class);
  35.         $ret = [];
  36. //        $categories = $categoryRepository->findAll();
  37.         $categories $repo->getChildren();
  38.         foreach ($categories as $category) {
  39.             $c = [];
  40.             $parent $category->getParent();
  41.             $back null;
  42.             if ($parent === null) {
  43.                 $_parent 0;
  44.             } else {
  45.                 $_parent $parent->getId();
  46.                 $back $parent->getParent();
  47.             }
  48.             if ($back === null) {
  49.                 $_back 0;
  50.             } else {
  51.                 $_back $back->getId();
  52.             }
  53.             $c['id'] = $category->getId();
  54.             $c['name'] = $category->getName();
  55.             $c['lvl'] = $category->getLvl();
  56.             $c['hasChildren'] = count($category->getChildren()) > 0;
  57.             $c['hasProducts'] = count($category->getProducts()) > 0;
  58.             $c['parent'] = $_parent;
  59.             $c['back'] = $_back;
  60.             $pathContainer $repo->getPath($category);
  61.             /**
  62.              * @var Category $item
  63.              */
  64.             $path = [];
  65.             foreach ($pathContainer as $item) {
  66.                 $path[] = [
  67.                     'id' => $item->getId(),
  68.                     'name' => $item->getName(),
  69.                 ];
  70.             }
  71.             $c['path'] = $path;
  72.             $ret[$_parent][$category->getName()] = $c;
  73.         }
  74.         return $this->json($ret);
  75.     }
  76.     /**
  77.      * @Route("/categories/products", name="caschbox_categories_products", methods={"GET"})
  78.      * @param CategoryRepository $categoryRepository
  79.      * @return Response
  80.      */
  81.     public function products(CategoryRepository $categoryRepositoryProductService $productService): Response
  82.     {
  83.         $ret = [];
  84.         $categories $categoryRepository->findAll();
  85.         foreach ($categories as $category) {
  86.             $_p $category->getProducts();
  87.             foreach ($_p as $product) {
  88.                 if ($product->getStatus() === true) {
  89.                     $ret[$category->getId()][$product->getId()] = $productService->buildFrontendProductFromProduct($product);
  90.                 }
  91.             }
  92.         }
  93.         return $this->json($ret);
  94.     }
  95. }