<?php 
 
/* 
 * This file is part of EC-CUBE 
 * 
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. 
 * 
 * http://www.ec-cube.co.jp/ 
 * 
 * For the full copyright and license information, please view the LICENSE 
 * file that was distributed with this source code. 
 */ 
 
 namespace Customize\Controller; 
 
use Eccube\Repository\ProductRepository; 
use Eccube\Repository\CategoryRepository; 
use Eccube\Repository\CustomerFavoriteProductRepository; 
use Eccube\Entity\Category; 
use Eccube\Form\Type\AddCartType; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; 
use Symfony\Component\Routing\Annotation\Route; 
use Eccube\Controller\TopController; 
 
class TopControllerCustomizer extends TopController 
{ 
    /** 
     * @var CustomerFavoriteProductRepository 
     */ 
    protected $customerFavoriteProductRepository; 
 
    /** 
     * @var ProductRepository 
     */ 
    protected $productRepository; 
 
    /** 
     * @var CategoryRepository 
     */ 
    protected $categoryRepository; 
 
    /** 
     * TopController constructor. 
     * 
     * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository 
     * @param ProductRepository $productRepository 
     * @param CategoryRepository $categoryRepository 
     */ 
    public function __construct( 
      CustomerFavoriteProductRepository $customerFavoriteProductRepository, 
      ProductRepository $productRepository, 
      CategoryRepository $categoryRepository, 
    ) { 
      $this->customerFavoriteProductRepository = $customerFavoriteProductRepository; 
      $this->productRepository = $productRepository; 
      $this->categoryRepository = $categoryRepository; 
    } 
 
    /** 
     * @Route("/", name="homepage", methods={"GET"}) 
     * @Template("index.twig") 
     */ 
    public function index() 
    { 
 
        $Customer = null; 
        if ($this->isGranted('ROLE_USER')) { 
          $Customer = $this->getUser(); 
        } 
 
        $searchData = array(); 
        $Category = $this->categoryRepository->find(187); 
        $searchData['category_id'] = $Category; 
        $qb = $this->productRepository->getQueryBuilderBySearchData($searchData); 
        $pagination = $qb->getQuery()->getResult(); 
        shuffle($pagination); 
        foreach ($pagination as $Product) { 
          if ($Customer) { 
            $Product->setSearchWord($this->customerFavoriteProductRepository->isFavorite($Customer, $Product)); 
          } 
          else { 
            $Product->setSearchWord(''); 
          } 
        } 
        $arrProducts = $pagination; 
 
        $pagination = array(); 
        $products = $this->productRepository->findBy([], ['create_date' => 'DESC']); 
        for ($i=0; $i<10; $i++) { 
          $Product = $products[$i]; 
          $pagination[] = $Product; 
        } 
        foreach ($pagination as $Product) { 
          if ($Customer) { 
            $Product->setSearchWord($this->customerFavoriteProductRepository->isFavorite($Customer, $Product)); 
          } 
          else { 
            $Product->setSearchWord(''); 
          } 
        } 
        $arrNewItems = $pagination; 
 
 
        $rss = simplexml_load_file('https://ppplot.com/category/props/feed/'); 
        $arrNews = array(); 
        $count = 0; 
        foreach($rss ->channel ->item as $item){ 
          ++$count; 
          if ($count <= 8) { 
            $cates = $item->category; 
            $title = $item->title; 
            $date = date("Y.m.d" , strtotime($item->pubDate)); 
            $link = $item->link; 
            $desc = $item->children('http://purl.org/rss/1.0/modules/content/'); 
            $img = $this->get_thumbnail_image($desc); 
            $cate = ''; 
            if ($cates) { 
              foreach((array)$cates as $cat) { 
                $cate = $cat; 
              } 
            } 
            $arrNews[] = array( 
              'category' => $cate, 
              'date' => $date, 
              'title' => $title, 
              'link' => $link, 
              'img' => $img, 
            ); 
          } 
        } 
         
        return [ 
          'mainProducts' => $arrProducts, 
          'news' => $arrNews, 
          'newProducts' => $arrNewItems, 
        ]; 
    } 
 
    private function get_thumbnail_image($content) { 
      if( preg_match_all('/<img([\s\S]+?)>/is', $content, $matches) ){ 
        foreach( $matches[0] as $img ){ 
          if ($img === reset($matches[0])) { 
            if( preg_match('/<img.*src\s*=\s*[\"|\'](.*?)[\"|\'].*>/i', $img, $m) ) { 
              return $m[1]; 
            } 
          } 
        } 
      } 
      return ''; 
    } 
     
}