app/Customize/Controller/FavoriteController.php line 62

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller;
  3. use Eccube\Entity\BaseInfo;
  4. use Eccube\Entity\Product;
  5. use Customize\Entity\FavoriteKey;
  6. use Customize\Entity\FavoriteProduct;
  7. use Eccube\Event\EventArgs;
  8. use Eccube\Event\EccubeEvents;
  9. use Eccube\Controller\AbstractController;
  10. use Eccube\Repository\BaseInfoRepository;
  11. use Customize\Repository\FavoriteKeyRepository;
  12. use Customize\Repository\FavoriteProductRepository;
  13. use Eccube\Service\CalculatePriceService;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  15. use Knp\Component\Pager\PaginatorInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\HttpFoundation\Cookie;
  19. class FavoriteController extends AbstractController
  20. {
  21.     /**
  22.      * @var BaseInfo
  23.      */
  24.     protected $BaseInfo;
  25.     /**
  26.      * @var FavoriteKeyRepository
  27.      */
  28.     protected $favoriteKeyRepository;
  29.     /**
  30.      * @var FavoriteProductRepository
  31.      */
  32.     protected $favoriteProductRepository;
  33.     /**
  34.      * @var CalculatePriceService
  35.      */
  36.     protected $calculatePriceService;
  37.     public function __construct(
  38.         BaseInfoRepository $baseInfoRepository,
  39.         FavoriteKeyRepository $favoriteKeyRepository,
  40.         FavoriteProductRepository $favoriteProductRepository,
  41.         CalculatePriceService $calculatePriceService
  42.     ) {
  43.         $this->BaseInfo $baseInfoRepository->get();
  44.         $this->favoriteKeyRepository $favoriteKeyRepository;
  45.         $this->favoriteProductRepository $favoriteProductRepository;
  46.         $this->calculatePriceService $calculatePriceService;
  47.     }
  48.     /**
  49.      * お気に入り商品一覧
  50.      *
  51.      * @Route("/favorite", name="favorite", priority=10)
  52.      * @Template("Favorite/index.twig")
  53.      */
  54.     public function index(Request $requestPaginatorInterface $paginator)
  55.     {
  56.         if (!$this->BaseInfo->isOptionFavoriteProduct()) {
  57.             throw new NotFoundHttpException();
  58.         }
  59.         // ログイン済みの場合、マイページ/お気に入り一覧へ
  60.         if($this->isGranted('ROLE_USER')) {
  61.             return $this->redirect($this->generateUrl('mypage_favorite'));
  62.         }
  63.         // お気に入りキー
  64.         $FavoriteKey null;
  65.         $cookieKey $request->cookies->get('eccube_favorite');
  66.         if($cookieKey) {
  67.             $FavoriteKey $this->favoriteKeyRepository->findWithCookieKey($cookieKey);
  68.         }
  69.         if(!$FavoriteKey) {
  70.             $FavoriteKey = new FavoriteKey();
  71.             
  72.             log_warning('Show Favorite without cookie_key');
  73.         } else {
  74.             log_warning('Show Favorite with id=[' $FavoriteKey->getId() . ']');
  75.         }
  76.         // paginator
  77.         $qb $this->favoriteProductRepository->getQueryBuilderByFavoriteKey($FavoriteKey);
  78.         $event = new EventArgs(
  79.             [
  80.                 'qb' => $qb,
  81. //                'Customer' => $Customer,
  82.             ],
  83.             $request
  84.         );
  85. //        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH, $event);
  86.         $pagination $paginator->paginate(
  87.             $qb,
  88.             $request->get('pageno'1),
  89.             $this->eccubeConfig['eccube_search_pmax'],
  90.             ['wrap-queries' => true]
  91.         );
  92.         //シミュレーション料金を計算
  93.         foreach($pagination as $FavoriteProduct) {
  94.             $ShelvingOption $FavoriteProduct->getShelvingOption();
  95.             if($ShelvingOption != null) {
  96.                 $shelvingPrice $this->calculatePriceService->calculate($ShelvingOptiontrue);
  97.                 $FavoriteProduct->setShelvingPrice($shelvingPrice);
  98.             }
  99.         }
  100.         return [
  101.             'pagination' => $pagination,
  102.         ];
  103.     }
  104.     /**
  105.      * お気に入り商品を削除する.
  106.      *
  107.      * @Route("/favorite/{id}/delete", name="favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"}, priority=10)
  108.      */
  109.     public function delete(Request $requestProduct $Product)
  110.     {
  111.         $this->isTokenValid();
  112.         //お気に入りキー
  113.         $FavoriteKey null;
  114.         $cookieKey $request->cookies->get('eccube_favorite');
  115.         if($cookieKey) {
  116.             $FavoriteKey $this->favoriteKeyRepository->findWithCookieKey($cookieKey);
  117.         }
  118.         if(!$FavoriteKey) {
  119.             $FavoriteKey = new FavoriteKey();
  120.         }
  121.         // FavoriteProduct ID
  122.         $fp $request->query->get('fp');
  123.         log_info('お気に入り商品削除開始', [$cookieKey$Product->getId(), $fp]);
  124.         if($fp) {
  125.             $FavoriteProduct $this->favoriteProductRepository->findOneBy(['FavoriteKey' => $FavoriteKey'id' => $fp]);
  126.         } else {
  127.             $FavoriteProduct $this->favoriteProductRepository->findOneBy(['FavoriteKey' => $FavoriteKey'Product' => $Product]);
  128.         }
  129.         if ($FavoriteProduct) {
  130.             $this->favoriteProductRepository->delete($FavoriteProduct);
  131.         } else {
  132.             throw new BadRequestHttpException();
  133.         }
  134.         $event = new EventArgs(
  135.             [
  136. //                'Customer' => $Customer,
  137.                 'FavoriteProduct' => $FavoriteProduct,
  138.             ], $request
  139.         );
  140.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
  141.         log_info('お気に入り商品削除完了', [$cookieKey$FavoriteProduct->getId()]);
  142.         return $this->redirect($this->generateUrl('favorite'));
  143.     }
  144. }