app/Customize/Controller/TopControllerCustomizer.php line 72

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\Repository\ProductRepository;
  14. use Eccube\Repository\CategoryRepository;
  15. use Eccube\Repository\CustomerFavoriteProductRepository;
  16. use Eccube\Entity\Category;
  17. use Eccube\Form\Type\AddCartType;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Eccube\Controller\TopController;
  21. class TopControllerCustomizer extends TopController
  22. {
  23.     /**
  24.      * @var CustomerFavoriteProductRepository
  25.      */
  26.     protected $customerFavoriteProductRepository;
  27.     /**
  28.      * @var ProductRepository
  29.      */
  30.     protected $productRepository;
  31.     /**
  32.      * @var CategoryRepository
  33.      */
  34.     protected $categoryRepository;
  35.     /**
  36.      * TopController constructor.
  37.      *
  38.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  39.      * @param ProductRepository $productRepository
  40.      * @param CategoryRepository $categoryRepository
  41.      */
  42.     public function __construct(
  43.       CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  44.       ProductRepository $productRepository,
  45.       CategoryRepository $categoryRepository,
  46.     ) {
  47.       $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  48.       $this->productRepository $productRepository;
  49.       $this->categoryRepository $categoryRepository;
  50.     }
  51.     /**
  52.      * @Route("/", name="homepage", methods={"GET"})
  53.      * @Template("index.twig")
  54.      */
  55.     public function index()
  56.     {
  57.         $Customer null;
  58.         if ($this->isGranted('ROLE_USER')) {
  59.           $Customer $this->getUser();
  60.         }
  61.         $searchData = array();
  62.         $Category $this->categoryRepository->find(187);
  63.         $searchData['category_id'] = $Category;
  64.         $qb $this->productRepository->getQueryBuilderBySearchData($searchData);
  65.         $pagination $qb->getQuery()->getResult();
  66.         shuffle($pagination);
  67.         foreach ($pagination as $Product) {
  68.           if ($Customer) {
  69.             $Product->setSearchWord($this->customerFavoriteProductRepository->isFavorite($Customer$Product));
  70.           }
  71.           else {
  72.             $Product->setSearchWord('');
  73.           }
  74.         }
  75.         $arrProducts $pagination;
  76.         $pagination = array();
  77.         /*$products = $this->productRepository->findBy([], ['create_date' => 'DESC']);
  78.         for ($i=0; $i<10; $i++) {
  79.           $Product = $products[$i];
  80.           $pagination[] = $Product;
  81.         }*/
  82.         $qb $this->productRepository->createQueryBuilder('p')
  83.           ->andWhere('p.Status = 1')              // ← 非公開を除外
  84.           ->orderBy('p.create_date''DESC')      // 新着順
  85.           ->setMaxResults(20);                    // 最新20件だけ
  86.           $pagination $qb->getQuery()->getResult();
  87.         foreach ($pagination as $Product) {
  88.           if ($Customer) {
  89.             $Product->setSearchWord($this->customerFavoriteProductRepository->isFavorite($Customer$Product));
  90.           }
  91.           else {
  92.             $Product->setSearchWord('');
  93.           }
  94.         }
  95.         $arrNewItems $pagination;
  96.         $rss simplexml_load_file('https://ppplot.com/category/props/feed/');
  97.         $arrNews = array();
  98.         $count 0;
  99.         foreach($rss ->channel ->item as $item){
  100.           ++$count;
  101.           if ($count <= 8) {
  102.             $cates $item->category;
  103.             $title $item->title;
  104.             $date date("Y.m.d" strtotime($item->pubDate));
  105.             $link $item->link;
  106.             $desc $item->children('http://purl.org/rss/1.0/modules/content/');
  107.             $img $this->get_thumbnail_image($desc);
  108.             $cate '';
  109.             if ($cates) {
  110.               foreach((array)$cates as $cat) {
  111.                 $cate $cat;
  112.               }
  113.             }
  114.             $arrNews[] = array(
  115.               'category' => $cate,
  116.               'date' => $date,
  117.               'title' => $title,
  118.               'link' => $link,
  119.               'img' => $img,
  120.             );
  121.           }
  122.         }
  123.         
  124.         return [
  125.           'mainProducts' => $arrProducts,
  126.           'news' => $arrNews,
  127.           'newProducts' => $arrNewItems,
  128.         ];
  129.     }
  130.     private function get_thumbnail_image($content) {
  131.       if( preg_match_all('/<img([\s\S]+?)>/is'$content$matches) ){
  132.         foreach( $matches[0] as $img ){
  133.           if ($img === reset($matches[0])) {
  134.             if( preg_match('/<img.*src\s*=\s*[\"|\'](.*?)[\"|\'].*>/i'$img$m) ) {
  135.               return $m[1];
  136.             }
  137.           }
  138.         }
  139.       }
  140.       return '';
  141.     }
  142.     
  143. }