<?php
namespace Customize\Controller\Block;
use Eccube\Controller\AbstractController;
use Eccube\Repository\NewsRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class TopNewsController extends AbstractController {
/**
* @var NewsRepository
*/
protected $newsRepository;
/**
* NewsController constructor.
*
* @param NewsRepository $newsRepository
*/
public function __construct(NewsRepository $newsRepository) {
$this->newsRepository = $newsRepository;
}
private $_htImg;
/**
* @Route("/block/top_news", name="block_top_news")
* @Template("Block/top_news.twig")
*/
public function index(Request $request) {
$rows1 = $this->newsRepository->getList("1",4);
$rows2 = $this->newsRepository->getList("2",4);
$this->_getHashImg($rows1);
$this->_getHashImg($rows2);
return [
'rows1' => $rows1,
'rows2' => $rows2,
'htImg' => $this->_htImg,
];
}
/**
* 画像ハッシュ一覧
* @param array $rows
* @return hash
*/
private function _getHashImg($rows){
if(!empty($rows)){
foreach($rows as $row){
$id = $row->getId();
$pattern = '/<img.*?src\s*=\s*[\"|\'](.*?)[\"|\'].*?>/i';
$images = array();
preg_match( $pattern, $row->getDescription(), $images );
$this->_htImg[$id] = empty($images[1]) ? "" : $images[1];
}
}
}
}