src/Eccube/Controller/Admin/Setting/Shop/PaymentController.php line 57

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 Eccube\Controller\Admin\Setting\Shop;
  13. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  14. use Eccube\Controller\AbstractController;
  15. use Eccube\Entity\Payment;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Form\Type\Admin\PaymentRegisterType;
  19. use Eccube\Repository\PaymentRepository;
  20. use Eccube\Service\Payment\Method\Cash;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  22. use Symfony\Component\Filesystem\Filesystem;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  26. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  27. use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. /**
  30.  * Class PaymentController
  31.  */
  32. class PaymentController extends AbstractController
  33. {
  34.     /**
  35.      * @var PaymentRepository
  36.      */
  37.     protected $paymentRepository;
  38.     /**
  39.      * PaymentController constructor.
  40.      *
  41.      * @param PaymentRepository $paymentRepository
  42.      */
  43.     public function __construct(PaymentRepository $paymentRepository)
  44.     {
  45.         $this->paymentRepository $paymentRepository;
  46.     }
  47.     /**
  48.      * @Route("/%eccube_admin_route%/setting/shop/payment", name="admin_setting_shop_payment", methods={"GET"})
  49.      * @Template("@admin/Setting/Shop/payment.twig")
  50.      */
  51.     public function index(Request $request)
  52.     {
  53.         $Payments $this->paymentRepository
  54.             ->findBy(
  55.                 [],
  56.                 ['sort_no' => 'DESC']
  57.             );
  58.         $event = new EventArgs(
  59.             [
  60.                 'Payments' => $Payments,
  61.             ],
  62.             $request
  63.         );
  64.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_INDEX_COMPLETE);
  65.         return [
  66.             'Payments' => $Payments,
  67.         ];
  68.     }
  69.     /**
  70.      * @Route("/%eccube_admin_route%/setting/shop/payment/new", name="admin_setting_shop_payment_new", methods={"GET", "POST"})
  71.      * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/edit", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_edit", methods={"GET", "POST"})
  72.      * @Template("@admin/Setting/Shop/payment_edit.twig")
  73.      */
  74.     public function edit(Request $requestPayment $Payment null)
  75.     {
  76.         if (is_null($Payment)) {
  77.             $Payment $this->paymentRepository->findOneBy([], ['sort_no' => 'DESC']);
  78.             $sortNo 1;
  79.             if ($Payment) {
  80.                 $sortNo $Payment->getSortNo() + 1;
  81.             }
  82.             $Payment = new \Eccube\Entity\Payment();
  83.             $Payment
  84.                 ->setSortNo($sortNo)
  85.                 ->setFixed(true)
  86.                 ->setVisible(true);
  87.         }
  88.         $builder $this->formFactory
  89.             ->createBuilder(PaymentRegisterType::class, $Payment);
  90.         $event = new EventArgs(
  91.             [
  92.                 'builder' => $builder,
  93.                 'Payment' => $Payment,
  94.             ],
  95.             $request
  96.         );
  97.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_EDIT_INITIALIZE);
  98.         $form $builder->getForm();
  99.         // 既に画像保存されてる場合は取得する
  100.         $oldPaymentImage $Payment->getPaymentImage();
  101.         $form->setData($Payment);
  102.         $form->handleRequest($request);
  103.         // 登録ボタン押下
  104.         if ($form->isSubmitted() && $form->isValid()) {
  105.             $Payment $form->getData();
  106.             // ファイルアップロード
  107.             $file $form['payment_image']->getData();
  108.             $fs = new Filesystem();
  109.             if ($file && strpos($file'..') === false && $fs->exists($this->getParameter('eccube_temp_image_dir').'/'.$file)) {
  110.                 $fs->rename(
  111.                     $this->getParameter('eccube_temp_image_dir').'/'.$file,
  112.                     $this->getParameter('eccube_save_image_dir').'/'.$file
  113.                 );
  114.             }
  115.             // Payment method class of Cash to default.
  116.             if (!$Payment->getMethodClass()) {
  117.                 $Payment->setMethodClass(Cash::class);
  118.             }
  119.             $this->entityManager->persist($Payment);
  120.             $this->entityManager->flush();
  121.             $event = new EventArgs(
  122.                 [
  123.                     'form' => $form,
  124.                     'Payment' => $Payment,
  125.                 ],
  126.                 $request
  127.             );
  128.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_EDIT_COMPLETE);
  129.             $this->addSuccess('admin.common.save_complete''admin');
  130.             return $this->redirectToRoute('admin_setting_shop_payment_edit', ['id' => $Payment->getId()]);
  131.         }
  132.         return [
  133.             'form' => $form->createView(),
  134.             'payment_id' => $Payment->getId(),
  135.             'Payment' => $Payment,
  136.             'oldPaymentImage' => $oldPaymentImage,
  137.         ];
  138.     }
  139.     /**
  140.      * 画像アップロード時にリクエストされるメソッド.
  141.      *
  142.      * @see https://pqina.nl/filepond/docs/api/server/#process
  143.      * @Route("/%eccube_admin_route%/setting/shop/payment/image/process", name="admin_payment_image_process", methods={"POST"})
  144.      */
  145.     public function imageProcess(Request $request)
  146.     {
  147.         if (!$request->isXmlHttpRequest() && $this->isTokenValid()) {
  148.             throw new BadRequestHttpException();
  149.         }
  150.         $images $request->files->get('payment_register');
  151.         $allowExtensions = ['gif''jpg''jpeg''png'];
  152.         $filename null;
  153.         if (isset($images['payment_image_file'])) {
  154.             $image $images['payment_image_file'];
  155.             // ファイルフォーマット検証
  156.             $mimeType $image->getMimeType();
  157.             if (!== strpos($mimeType'image')) {
  158.                 throw new UnsupportedMediaTypeHttpException();
  159.             }
  160.             // 拡張子
  161.             $extension $image->getClientOriginalExtension();
  162.             if (!in_array(strtolower($extension), $allowExtensions)) {
  163.                 throw new UnsupportedMediaTypeHttpException();
  164.             }
  165.             $filename date('mdHis').uniqid('_').'.'.$extension;
  166.             $image->move($this->getParameter('eccube_temp_image_dir'), $filename);
  167.         }
  168.         $event = new EventArgs(
  169.             [
  170.                 'images' => $images,
  171.                 'filename' => $filename,
  172.             ],
  173.             $request
  174.         );
  175.         $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_IMAGE_ADD_COMPLETE);
  176.         $filename $event->getArgument('filename');
  177.         return new Response($filename);
  178.     }
  179.     /**
  180.      * アップロード画像を取得する際にコールされるメソッド.
  181.      *
  182.      * @see https://pqina.nl/filepond/docs/api/server/#load
  183.      * @Route("/%eccube_admin_route%/setting/shop/payment/image/load", name="admin_payment_image_load", methods={"GET"})
  184.      */
  185.     public function imageLoad(Request $request)
  186.     {
  187.         if (!$request->isXmlHttpRequest()) {
  188.             throw new BadRequestHttpException();
  189.         }
  190.         $dirs = [
  191.             $this->eccubeConfig['eccube_save_image_dir'],
  192.             $this->eccubeConfig['eccube_temp_image_dir'],
  193.         ];
  194.         foreach ($dirs as $dir) {
  195.             $image = \realpath($dir.'/'.$request->query->get('source'));
  196.             $dir = \realpath($dir);
  197.             if (\is_file($image) && \str_starts_with($image$dir)) {
  198.                 $file = new \SplFileObject($image);
  199.                 return $this->file($file$file->getBasename());
  200.             }
  201.         }
  202.         throw new NotFoundHttpException();
  203.     }
  204.     /**
  205.      * アップロード画像をすぐ削除する際にコールされるメソッド.
  206.      *
  207.      * @see https://pqina.nl/filepond/docs/api/server/#revert
  208.      * @Route("/%eccube_admin_route%/setting/shop/payment/image/revert", name="admin_payment_image_revert", methods={"DELETE"})
  209.      */
  210.     public function imageRevert(Request $request)
  211.     {
  212.         if (!$request->isXmlHttpRequest() && $this->isTokenValid()) {
  213.             throw new BadRequestHttpException();
  214.         }
  215.         $tempFile $this->eccubeConfig['eccube_temp_image_dir'].'/'.$request->getContent();
  216.         if (is_file($tempFile) && stripos(realpath($tempFile), $this->eccubeConfig['eccube_temp_image_dir']) === 0) {
  217.             $fs = new Filesystem();
  218.             $fs->remove($tempFile);
  219.             return new Response(nullResponse::HTTP_NO_CONTENT);
  220.         }
  221.         throw new NotFoundHttpException();
  222.     }
  223.     /**
  224.      * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_delete", methods={"DELETE"})
  225.      *
  226.      * @param Request $request
  227.      * @param Payment $TargetPayment
  228.      *
  229.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  230.      */
  231.     public function delete(Request $requestPayment $TargetPayment)
  232.     {
  233.         $this->isTokenValid();
  234.         $sortNo 1;
  235.         $Payments $this->paymentRepository->findBy([], ['sort_no' => 'ASC']);
  236.         foreach ($Payments as $Payment) {
  237.             $Payment->setSortNo($sortNo++);
  238.         }
  239.         try {
  240.             $this->paymentRepository->delete($TargetPayment);
  241.             $this->entityManager->flush();
  242.             $event = new EventArgs(
  243.                 [
  244.                     'Payment' => $TargetPayment,
  245.                 ],
  246.                 $request
  247.             );
  248.             $this->eventDispatcher->dispatch($eventEccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_DELETE_COMPLETE);
  249.             $this->addSuccess('admin.common.delete_complete''admin');
  250.         } catch (ForeignKeyConstraintViolationException $e) {
  251.             $this->entityManager->rollback();
  252.             $message trans('admin.common.delete_error_foreign_key', ['%name%' => $TargetPayment->getMethod()]);
  253.             $this->addError($message'admin');
  254.         }
  255.         return $this->redirectToRoute('admin_setting_shop_payment');
  256.     }
  257.     /**
  258.      * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/visible", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_visible", methods={"PUT"})
  259.      */
  260.     public function visible(Payment $Payment)
  261.     {
  262.         $this->isTokenValid();
  263.         $Payment->setVisible(!$Payment->isVisible());
  264.         $this->entityManager->flush();
  265.         if ($Payment->isVisible()) {
  266.             $this->addSuccess(trans('admin.common.to_show_complete', ['%name%' => $Payment->getMethod()]), 'admin');
  267.         } else {
  268.             $this->addSuccess(trans('admin.common.to_hide_complete', ['%name%' => $Payment->getMethod()]), 'admin');
  269.         }
  270.         return $this->redirectToRoute('admin_setting_shop_payment');
  271.     }
  272.     /**
  273.      * @Route("/%eccube_admin_route%/setting/shop/payment/sort_no/move", name="admin_setting_shop_payment_sort_no_move", methods={"POST"})
  274.      *
  275.      * @param Request $request
  276.      *
  277.      * @return Response
  278.      */
  279.     public function moveSortNo(Request $request)
  280.     {
  281.         if (!$request->isXmlHttpRequest()) {
  282.             throw new BadRequestHttpException();
  283.         }
  284.         if ($this->isTokenValid()) {
  285.             $sortNos $request->request->all();
  286.             foreach ($sortNos as $paymentId => $sortNo) {
  287.                 /** @var Payment $Payment */
  288.                 $Payment $this->paymentRepository
  289.                     ->find($paymentId);
  290.                 $Payment->setSortNo($sortNo);
  291.                 $this->entityManager->persist($Payment);
  292.             }
  293.             $this->entityManager->flush();
  294.             return new Response();
  295.         }
  296.         throw new BadRequestHttpException();
  297.     }
  298. }