app/Plugin/Coupon42/Controller/Admin/CouponController.php line 96

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\Coupon42\Controller\Admin;
  13. use Eccube\Common\Constant;
  14. use Eccube\Form\Type\Admin\SearchProductType;
  15. use Plugin\Coupon42\Entity\Coupon;
  16. use Plugin\Coupon42\Entity\CouponDetail;
  17. use Plugin\Coupon42\Form\Type\CouponSearchCategoryType;
  18. use Plugin\Coupon42\Form\Type\CouponType;
  19. use Plugin\Coupon42\Repository\CouponDetailRepository;
  20. use Plugin\Coupon42\Repository\CouponRepository;
  21. use Plugin\Coupon42\Service\CouponService;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Eccube\Controller\AbstractController;
  29. /**
  30.  * Class CouponController
  31.  */
  32. class CouponController extends AbstractController
  33. {
  34.     /**
  35.      * @var CouponRepository
  36.      */
  37.     private $couponRepository;
  38.     /**
  39.      * @var CouponService
  40.      */
  41.     private $couponService;
  42.     /**
  43.      * @var CouponDetailRepository
  44.      */
  45.     private $couponDetailRepository;
  46.     /**
  47.      * CouponController constructor.
  48.      *
  49.      * @param CouponRepository $couponRepository
  50.      * @param CouponService $couponService
  51.      * @param CouponDetailRepository $couponDetailRepository
  52.      */
  53.     public function __construct(CouponRepository $couponRepositoryCouponService $couponServiceCouponDetailRepository $couponDetailRepository)
  54.     {
  55.         $this->couponRepository $couponRepository;
  56.         $this->couponService $couponService;
  57.         $this->couponDetailRepository $couponDetailRepository;
  58.     }
  59.     /**
  60.      * @param Request $request
  61.      *
  62.      * @return array
  63.      * @Route("/%eccube_admin_route%/plugin/coupon", name="plugin_coupon_list")
  64.      * @Template("@Coupon42/admin/index.twig")
  65.      */
  66.     public function index(Request $request)
  67.     {
  68.         $coupons $this->couponRepository->findBy(
  69.             ['visible' => true],
  70.             ['id' => 'DESC']
  71.         );
  72.         return [
  73.             'Coupons' => $coupons,
  74.         ];
  75.     }
  76.     /**
  77.      * クーポンの新規作成/編集確定.
  78.      *
  79.      * @param Request $request
  80.      * @param int     $id
  81.      *
  82.      * @return RedirectResponse|Response
  83.      * @Route("/%eccube_admin_route%/plugin/coupon/new", name="plugin_coupon_new", requirements={"id" = "\d+"})
  84.      * @Route("/%eccube_admin_route%/plugin/coupon/{id}/edit", name="plugin_coupon_edit", requirements={"id" = "\d+"})
  85.      */
  86.     public function edit(Request $request$id null)
  87.     {
  88.         $Coupon null;
  89.         if (!$id) {
  90.             // 新規登録
  91.             $Coupon = new Coupon();
  92.             $Coupon->setEnableFlag(Constant::ENABLED);
  93.             $Coupon->setVisible(true);
  94.         } else {
  95.             // 更新
  96.             $Coupon $this->couponRepository->find($id);
  97.             if (!$Coupon) {
  98.                 $this->addError('plugin_coupon.admin.notfound''admin');
  99.                 return $this->redirectToRoute('plugin_coupon_list');
  100.             }
  101.         }
  102.         $form $this->formFactory->createBuilder(CouponType::class, $Coupon)->getForm();
  103.         // クーポンコードの発行
  104.         if (!$id) {
  105.             $form->get('coupon_cd')->setData($this->couponService->generateCouponCd());
  106.         }
  107.         $details = [];
  108.         $CouponDetails $Coupon->getCouponDetails();
  109.         foreach ($CouponDetails as $CouponDetail) {
  110.             $details[] = clone $CouponDetail;
  111.             $CouponDetail->getCategoryFullName();
  112.         }
  113.         $form->get('CouponDetails')->setData($details);
  114.         $form->handleRequest($request);
  115.         if ($form->isSubmitted() && $form->isValid()) {
  116.             /** @var \Plugin\Coupon42\Entity\Coupon $Coupon */
  117.             $Coupon $form->getData();
  118.             $oldReleaseNumber $request->get('coupon_release_old');
  119.             if (is_null($Coupon->getCouponUseTime())) {
  120.                 $Coupon->setCouponUseTime($Coupon->getCouponRelease());
  121.             } else {
  122.                 if ($Coupon->getCouponRelease() != $oldReleaseNumber) {
  123.                     $Coupon->setCouponUseTime($Coupon->getCouponRelease());
  124.                 }
  125.             }
  126.             $CouponDetails $this->couponDetailRepository->findBy([
  127.                 'Coupon' => $Coupon,
  128.             ]);
  129.             foreach ($CouponDetails as $CouponDetail) {
  130.                 $Coupon->removeCouponDetail($CouponDetail);
  131.                 $this->entityManager->remove($CouponDetail);
  132.                 $this->entityManager->flush($CouponDetail);
  133.             }
  134.             $CouponDetails $form->get('CouponDetails')->getData();
  135.             /** @var CouponDetail $CouponDetail */
  136.             foreach ($CouponDetails as $CouponDetail) {
  137.                 $CouponDetail->setCoupon($Coupon);
  138.                 $CouponDetail->setCouponType($Coupon->getCouponType());
  139.                 $CouponDetail->setVisible(false);
  140.                 $Coupon->addCouponDetail($CouponDetail);
  141.                 $this->entityManager->persist($CouponDetail);
  142.             }
  143.             $this->entityManager->persist($Coupon);
  144.             $this->entityManager->flush($Coupon);
  145.             // 成功時のメッセージを登録する
  146.             $this->addSuccess('plugin_coupon.admin.regist.success''admin');
  147.             return $this->redirectToRoute('plugin_coupon_list');
  148.         }
  149.         return $this->renderRegistView([
  150.             'form' => $form->createView(),
  151.             'id' => $id,
  152.         ]);
  153.     }
  154.     /**
  155.      * クーポンの有効/無効化.
  156.      *
  157.      * @param Request $request
  158.      * @param Coupon  $Coupon
  159.      *
  160.      * @return RedirectResponse
  161.      * @Route("/%eccube_admin_route%/plugin/coupon/{id}/enable", name="plugin_coupon_enable", requirements={"id" = "\d+"}, methods={"put"})
  162.      * @ParamConverter("Coupon")
  163.      */
  164.     public function enable(Request $requestCoupon $Coupon)
  165.     {
  166.         $this->isTokenValid();
  167.         $this->couponRepository->enableCoupon($Coupon);
  168.         $this->addSuccess('plugin_coupon.admin.enable.success''admin');
  169.         log_info('Change status a coupon with ', ['ID' => $Coupon->getId()]);
  170.         return $this->redirectToRoute('plugin_coupon_list');
  171.     }
  172.     /**
  173.      * クーポンの削除.
  174.      *
  175.      * @param Request $request
  176.      * @param Coupon  $Coupon
  177.      *
  178.      * @return RedirectResponse
  179.      * @Route("/%eccube_admin_route%/plugin/coupon/{id}/delete", name="plugin_coupon_delete", requirements={"id" = "\d+"}, methods={"delete"})
  180.      * @ParamConverter("Coupon")
  181.      */
  182.     public function delete(Request $requestCoupon $Coupon)
  183.     {
  184.         $this->isTokenValid();
  185.         $this->couponRepository->deleteCoupon($Coupon);
  186.         $this->addSuccess('plugin_coupon.admin.delete.success''admin');
  187.         log_info('Delete a coupon with ', ['ID' => $Coupon->getId()]);
  188.         return $this->redirectToRoute('plugin_coupon_list');
  189.     }
  190.     /**
  191.      * 編集画面用のrender.
  192.      *
  193.      * @param array       $parameters
  194.      *
  195.      * @return Response
  196.      */
  197.     protected function renderRegistView($parameters = [])
  198.     {
  199.         // 商品検索フォーム
  200.         $searchProductModalForm $this->formFactory->createBuilder(SearchProductType::class)->getForm();
  201.         // カテゴリ検索フォーム
  202.         $searchCategoryModalForm $this->formFactory->createBuilder(CouponSearchCategoryType::class)->getForm();
  203.         $viewParameters = [
  204.             'searchProductModalForm' => $searchProductModalForm->createView(),
  205.             'searchCategoryModalForm' => $searchCategoryModalForm->createView(),
  206.         ];
  207.         $viewParameters += $parameters;
  208.         return $this->render('@Coupon42/admin/regist.twig'$viewParameters);
  209.     }
  210. }