<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Controller;
use Eccube\Entity\BaseInfo;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Customize\Form\Type\SearchFavoriteProductType;
use Customize\Form\Type\SearchInvoiceFileType;
use DateTime;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Repository\CategoryRepository;
use Eccube\Repository\CustomerFavoriteProductRepository;
use Eccube\Repository\OrderRepository;
use Eccube\Repository\ProductRepository;
use Eccube\Service\CartService;
use Eccube\Service\PurchaseFlow\PurchaseFlow;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class MypageController extends \Eccube\Controller\Mypage\MypageController
{
private CategoryRepository $categoryRepository;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
* @var CustomerFavoriteProductRepository
*/
protected $customerFavoriteProductRepository;
/**
* @var BaseInfo
*/
protected $BaseInfo;
/**
* @var CartService
*/
protected $cartService;
/**
* @var OrderRepository
*/
protected $orderRepository;
/**
* @var PurchaseFlow
*/
protected $purchaseFlow;
public function __construct(
CategoryRepository $categoryRepository,
OrderRepository $orderRepository,
CustomerFavoriteProductRepository $customerFavoriteProductRepository,
CartService $cartService,
BaseInfoRepository $baseInfoRepository,
PurchaseFlow $purchaseFlow
) {
$this->categoryRepository = $categoryRepository;
$this->orderRepository = $orderRepository;
$this->customerFavoriteProductRepository = $customerFavoriteProductRepository;
$this->BaseInfo = $baseInfoRepository->get();
$this->cartService = $cartService;
$this->purchaseFlow = $purchaseFlow;
}
/**
* お気に入り商品を表示する.
*
* @Route("/mypage/favorite", name="mypage_favorite", methods={"GET"})
* @Template("Mypage/favorite.twig")
*/
public function favorite(Request $request, PaginatorInterface $paginator)
{
if (!$this->BaseInfo->isOptionFavoriteProduct()) {
throw new NotFoundHttpException();
}
if ($request->getMethod() === 'GET') {
$request->query->set('pageno', $request->query->get('pageno', ''));
}
$Customer = $this->getUser();
$builder = $this->formFactory->createNamedBuilder('', SearchFavoriteProductType::class);
if ($request->getMethod() === 'GET') {
$builder->setMethod('GET');
}
/* @var $searchForm \Symfony\Component\Form\FormInterface */
$searchForm = $builder->getForm();
$searchForm->handleRequest($request);
// paginator
$searchData = $searchForm->getData();
$qb = $this->customerFavoriteProductRepository->getQueryBuilderByCustomerWithSearchData($Customer, $searchData);
$event = new EventArgs(
[
'qb' => $qb,
'Customer' => $Customer,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH);
$pagination = $paginator->paginate(
$qb,
!empty($searchData['pageno']) ? $searchData['pageno'] : 1,
!empty($searchData['disp_number']) ? $searchData['disp_number']->getId() : $this->eccubeConfig['eccube_search_pmax']
);
$Category = null;
if (!empty($searchForm->get('category_id')->getData())) {
$Category = $this->categoryRepository->find($searchForm->geT('category_id')->getData());
}
return [
'pagination' => $pagination,
'search_form' => $searchForm->createView(),
'Category' => $Category
];
}
/**
* お気に入り商品を表示する.
*
* @Route("/mypage/invoice", name="mypage_invoice", methods={"GET"})
* @Template("Mypage/invoice.twig")
*/
public function invoice(Request $request)
{
$builder = $this->formFactory->createNamedBuilder('', SearchInvoiceFileType::class);
if ($request->getMethod() === 'GET') {
$builder->setMethod('GET');
}
$searchForm = $builder->getForm();
$searchForm->handleRequest($request);
// paginator
$searchData = $searchForm->getData();
/** @var \Eccube\Entity\Customer $Customer */
$Customer = $this->getUser();
$data = [];
if ($Customer) {
$year = isset($searchData['year']) ? $searchData['year'] : date('Y');
$dirPath = $this->eccubeConfig->get('eccube_html_dir').'/user_data/assets/seikyu/'.$Customer->getId().'/';
if (is_dir($dirPath)) {
$invoices = preg_grep('~^'.$year.'~', scandir($dirPath));
if (!empty($invoices)) {
foreach ($invoices as $key => $fileName) {
$date = explode('_', $fileName)[0];
$date = DateTime::createFromFormat('Ym', $date);
$date = $date->format('n');
$data[$date] = $Customer->getId().'/'.$fileName;
}
uksort($data, function($key1, $key2) {
return $key2 <=> $key1;
});
}
}
}
return [
'invoices' => $data,
'searchForm' => $searchForm->createView(),
];
}
}