<?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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Eccube\Controller\AbstractController;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
class TopController extends AbstractController
{
/**
* @Route("/", name="homepage")
* @Template("index.twig")
*/
public function index()
{
$config = new Configuration();
$connectionParams = array(
'dbname' => 'yasashiite_wp',
'user' => 'yasashiite_wp',
'password' => 'r8iMjLJWZD.4YRb',
'host' => 'localhost',
'port' => 3306,
'charset' => 'utf8',
'driver' => 'pdo_mysql',
);
// 接続
$conn = DriverManager::getConnection($connectionParams, $config);
$posts = $this->getPosts($conn);
// echo "<div style='display:none;>";
// print_r($posts);
// echo "</div>";
$specialPosts = $this->getPosts($conn,4,8);
// print_r($specialPosts);
$termsQb = $conn->createQueryBuilder();
$terms = $termsQb
->select('*')
->from('yt_term_taxonomy','tt')
->join('tt','yt_terms','t','tt.term_id = t.term_id')
->where('tt.taxonomy = "category"')
->execute()
->fetchAll()
;
return [
'Posts' => $posts,
'Terms' => $terms,
'SpecialPosts' => $specialPosts,
];
}
private function getPosts($conn, $limit = 5, $category = null){
$postsQb = $conn->createQueryBuilder();
$postsResults = $postsQb
->select('*')
->from('yt_posts','p')
->join('p','yt_term_relationships','r','p.id = r.object_id')
->join('r','yt_term_taxonomy','tt','r.term_taxonomy_id = tt.term_taxonomy_id')
->join('tt','yt_terms','t','tt.term_id = t.term_id')
->leftJoin('p','yt_postmeta','pm','p.id = pm.post_id AND pm.meta_key = "_thumbnail_id"')
->where('p.post_status = "publish"')
->andWhere('p.post_type = "post"')
->orderBy('p.post_date',"DESC")
->execute()
->fetchAll()
;
$posts = [];
foreach($postsResults as $key => $post){
$post['terms'][] = [
'term_id' => $post['term_id'],
'name' => $post['name'],
'taxonomy' => $post['taxonomy'],
'slug' => $post['slug']
];
// echo ($key - 1);
if($key != 0 && $post['ID'] == $posts[array_key_last($posts)]['ID']){
$posts[array_key_last($posts)]['terms'][] = $post['terms'][0];
}else{
if(isset($post['meta_value']) && !empty($post['meta_value'])){
$postMetaQb = $conn->createQueryBuilder();
$postMetaResults = $postMetaQb
->select('*')
->from('yt_posts','p')
->where('p.ID = :metavalue')
->setParameter('metavalue',$post['meta_value'])
->execute()
->fetchAll()
;
// print_r($postMetaResults);
if(isset($postMetaResults[0]['guid'])){
$post['thumbnail'] = $postMetaResults[0]['guid'];
}else{
$post['thumbnail'] = $this->get_first_image($post['post_content']);
}
}else{
$post['thumbnail'] = $this->get_first_image($post['post_content']);
}
$posts[] = $post;
}
}
if($category != null){
$feature_posts = [];
foreach($posts as $key => $post){
$has_category = false;
foreach($post['terms'] as $term){
if($term['term_id'] == $category) $has_category = true;
if($has_category) break;
}
if($has_category) $feature_posts[] = $post;
}
return array_slice($feature_posts,0,$limit);
}
return array_slice($posts,0,$limit);
}
private function get_first_image($content) {
$first_img = '/column/img/common/no_image.png';
preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
if(isset($matches[1][0])){
$first_img = $matches[1][0];
}
return $first_img;
}
}