app/Customize/Controller/CategoryController.php line 34

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller;
  3. use Customize\Repository\ProductRepository;
  4. use Eccube\Repository\CategoryRepository;
  5. use Eccube\Repository\NewsRepository;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class CategoryController extends BaseController
  14. {
  15.   protected $categoryRepository;
  16.   protected $productRepository;
  17.   public function __construct(
  18.     CategoryRepository $categoryRepository,
  19.     ProductRepository $productRepository
  20.   ) {
  21.     $this->categoryRepository $categoryRepository;
  22.     $this->productRepository $productRepository;
  23.   }
  24.   /**
  25.    * @Method("GET")
  26.    * @Route("/category/{id}")
  27.    * @Template("@user_data/category/index.twig")
  28.    */
  29.   public function category($id)
  30.   {
  31.     $category $this->categoryRepository->find($id);
  32.     if (!$category) {
  33.       throw new NotFoundHttpException();
  34.     }
  35.     return ['category' => $category];
  36.   }
  37.   /**
  38.    * @Route("/category-products/{categoryID}",  methods={"GET"})
  39.    * @return JsonResponse
  40.    */
  41.   public function getProductIncategory($categoryID)
  42.   {
  43.     $products $this->productRepository->getProductIncategory($categoryID);
  44.     $productArray = [];
  45.     foreach ($products as $product) {
  46.       $productArray[] = [
  47.         'id' => $product->getId(),
  48.         'name' => $product->getName(),
  49.         'image' => $product->getMainFileName()['file_name'] ?? null
  50.       ];
  51.     }
  52.     return $this->json(['products' => $productArray]);
  53.   }
  54.   /**
  55.    * @Route("/category-sample-products",  methods={"POST"})
  56.    * @return JsonResponse
  57.    */
  58.   public function getSampleProductIncategory(Request $request)
  59.   {
  60.     
  61.     $data json_decode($request->getContent(), true);
  62.     $categoryIDs $data['category_ids'] ?? null;    
  63.     $products $categoryIDs && count($categoryIDs) > $this->productRepository->getSampleProductInCategory($categoryIDs) : [];
  64.     $productArray = [];
  65.     foreach ($products as $product) {
  66.       $productArray[] = [
  67.         'id' => $product->getId(),
  68.         'name' => $product->getName(),
  69.         'image' => $product->getMainFileName() ? $product->getMainFileName()['file_name'] : null,
  70.         'price' => $product->getPrice02Min(),
  71.         'note' => $product->getDescriptionDetail(),
  72.         'stock_find' => $product->getStockFind()
  73.       ];
  74.     }
  75.     return $this->json(['products' => $productArray]);
  76.   }
  77. }