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.         foreach ($pagination as $Product) {
  83.           if ($Customer) {
  84.             $Product->setSearchWord($this->customerFavoriteProductRepository->isFavorite($Customer$Product));
  85.           }
  86.           else {
  87.             $Product->setSearchWord('');
  88.           }
  89.         }
  90.         $arrNewItems $pagination;
  91.         $rss simplexml_load_file('https://ppplot.com/category/props/feed/');
  92.         $arrNews = array();
  93.         $count 0;
  94.         foreach($rss ->channel ->item as $item){
  95.           ++$count;
  96.           if ($count <= 8) {
  97.             $cates $item->category;
  98.             $title $item->title;
  99.             $date date("Y.m.d" strtotime($item->pubDate));
  100.             $link $item->link;
  101.             $desc $item->children('http://purl.org/rss/1.0/modules/content/');
  102.             $img $this->get_thumbnail_image($desc);
  103.             $cate '';
  104.             if ($cates) {
  105.               foreach((array)$cates as $cat) {
  106.                 $cate $cat;
  107.               }
  108.             }
  109.             $arrNews[] = array(
  110.               'category' => $cate,
  111.               'date' => $date,
  112.               'title' => $title,
  113.               'link' => $link,
  114.               'img' => $img,
  115.             );
  116.           }
  117.         }
  118.         
  119.         return [
  120.           'mainProducts' => $arrProducts,
  121.           'news' => $arrNews,
  122.           'newProducts' => $arrNewItems,
  123.         ];
  124.     }
  125.     private function get_thumbnail_image($content) {
  126.       if( preg_match_all('/<img([\s\S]+?)>/is'$content$matches) ){
  127.         foreach( $matches[0] as $img ){
  128.           if ($img === reset($matches[0])) {
  129.             if( preg_match('/<img.*src\s*=\s*[\"|\'](.*?)[\"|\'].*>/i'$img$m) ) {
  130.               return $m[1];
  131.             }
  132.           }
  133.         }
  134.       }
  135.       return '';
  136.     }
  137.     
  138. }