app/Customize/Controller/CustomTopController.php line 43

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 Customize\Controller;
  13. use Eccube\Controller\AbstractController;
  14. use Eccube\Service\CartService;
  15. use Eccube\Event\EventArgs as CacheClearEvent;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  22. class CustomTopController extends AbstractController
  23. {
  24.     public function __construct(
  25.         TokenStorageInterface $tokenStorage,
  26.         CartService $cartService,
  27.         EventDispatcherInterface $eventDispatcher
  28.     ) {
  29.         $this->tokenStorage $tokenStorage;
  30.         $this->cartService $cartService;
  31.         $this->eventDispatcher $eventDispatcher;
  32.     }
  33.     /**
  34.      * @Route("/", name="homepage")
  35.      * @Template("index.twig")
  36.      */
  37.     public function index(Request $request)
  38.     {
  39.         $empty_cookies $request->query->get('empty_cookie');
  40.         if ($empty_cookies == '1') {
  41.             $response = new RedirectResponse($this->generateUrl('homepage'));
  42.             $response->headers->clearCookie('en_user');
  43.             //カートを空にする
  44.             $this->cartService->clear();
  45.             //キャッシュをクリアする
  46.             $this->eventDispatcher->dispatch(new CacheClearEvent());
  47.             return $response;
  48.         }
  49.         $LoginTypeInfo $this->getLoginTypeInfo();
  50.         $LoginType $LoginTypeInfo['LoginType'];
  51.         if ( $LoginType == ) {
  52.             return $this->redirectToRoute('product_list');
  53.         }
  54.         // ブログ情報を取得
  55.         $response wp_remote_get($request->getSchemeAndHttpHost().$request->getBasePath().'/shop/wp-json/wp/v2/posts?per_page=3&_embed');
  56.         if ($response) {
  57.             if (is_wp_error($response)) {
  58.                 $posts false;
  59.             } else {
  60.                 $posts json_decode($response["body"]);
  61.             }
  62.         } else {
  63.             $posts false;
  64.         }
  65.         $blogDatas = [];
  66.         if ($posts) {
  67.             foreach ($posts as $data) {
  68.                 $item = [];
  69.                 $item['title'] = $data->title;
  70.                 $item['date'] = $data->date;
  71.                 $item['link'] = $data->link;
  72.                 $name 'wp:featuredmedia';
  73.                 if (isset($data->_embedded->{$name})) {
  74.                     $item['attachment'] = $data->_embedded->{$name}[0];
  75.                 }
  76.                 $name 'wp:term';
  77.                 if (isset($data->_embedded->{$name})) {
  78.                     $item['category'] = $data->_embedded->{$name}[0];
  79.                 }
  80.                 $blogDatas[] = $item;
  81.             }
  82.         }
  83.         return [
  84.             'blogDatas' => $blogDatas,
  85.         ];
  86.     }
  87.     function getLoginTypeInfo()
  88.     {
  89.         $LoginType 1;         //Default is guest
  90.         $Customer $this->getCurrentUser();
  91.         $ChainStore null;
  92.         $ContractType null;
  93.         if (is_object($Customer)) {
  94.             $ChainStore $Customer->getChainStore();
  95.             if(is_object($ChainStore)){
  96.                 $LoginType 3;         //ChainStore member
  97.                 $ContractType $ChainStore->getContractType();
  98.             }else{
  99.                 $LoginType 2;         //Normal member
  100.             }
  101.         }else{
  102.             $Customer null;
  103.         }
  104.         return [
  105.             'LoginType' => $LoginType,
  106.             'Customer' => $Customer,
  107.             'ChainStore' => $ChainStore,
  108.             'ContractType' => $ContractType,
  109.         ];
  110.     }
  111.     function getCurrentUser()
  112.     {
  113.         if(!$this->tokenStorage){
  114.             return null;
  115.         }
  116.         if (!$token $this->tokenStorage->getToken()) {
  117.             return null;
  118.         }
  119.         if (!$token->isAuthenticated()) {
  120.             return null;
  121.         }
  122.         if(!$user $token->getUser()){
  123.             return null;
  124.         }
  125.         if(is_object($user)){
  126.             return $user;
  127.         }
  128.         return null;
  129.     }
  130. }