<?php
namespace Customize\Controller;
use Customize\Repository\ProductRepository;
use Eccube\Repository\CategoryRepository;
use Eccube\Repository\NewsRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class CategoryController extends BaseController
{
protected $categoryRepository;
protected $productRepository;
public function __construct(
CategoryRepository $categoryRepository,
ProductRepository $productRepository
) {
$this->categoryRepository = $categoryRepository;
$this->productRepository = $productRepository;
}
/**
* @Method("GET")
* @Route("/category/{id}")
* @Template("@user_data/category/index.twig")
*/
public function category($id)
{
$category = $this->categoryRepository->find($id);
if (!$category) {
throw new NotFoundHttpException();
}
return ['category' => $category];
}
/**
* @Route("/category-products/{categoryID}", methods={"GET"})
* @return JsonResponse
*/
public function getProductIncategory($categoryID)
{
$products = $this->productRepository->getProductIncategory($categoryID);
$productArray = [];
foreach ($products as $product) {
$productArray[] = [
'id' => $product->getId(),
'name' => $product->getName(),
'image' => $product->getMainFileName()['file_name'] ?? null
];
}
return $this->json(['products' => $productArray]);
}
/**
* @Route("/category-sample-products", methods={"POST"})
* @return JsonResponse
*/
public function getSampleProductIncategory(Request $request)
{
$data = json_decode($request->getContent(), true);
$categoryIDs = $data['category_ids'] ?? null;
$products = $categoryIDs && count($categoryIDs) > 0 ? $this->productRepository->getSampleProductInCategory($categoryIDs) : [];
$productArray = [];
foreach ($products as $product) {
$productArray[] = [
'id' => $product->getId(),
'name' => $product->getName(),
'image' => $product->getMainFileName() ? $product->getMainFileName()['file_name'] : null,
'price' => $product->getPrice02Min(),
'note' => $product->getDescriptionDetail(),
'stock_find' => $product->getStockFind()
];
}
return $this->json(['products' => $productArray]);
}
}