app/Customize/Controller/TopController.php line 28

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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Eccube\Controller\AbstractController;
  16. use Doctrine\DBAL\Configuration;
  17. use Doctrine\DBAL\DriverManager;
  18. class TopController extends AbstractController
  19. {
  20.     /**
  21.      * @Route("/", name="homepage")
  22.      * @Template("index.twig")
  23.      */
  24.     public function index()
  25.     {
  26.         $config = new Configuration();
  27.         $connectionParams = array(
  28.             'dbname' => 'yasashiite_wp',
  29.             'user' => 'yasashiite_wp',
  30.             'password' => 'r8iMjLJWZD.4YRb',
  31.             'host' => 'localhost',
  32.             'port' => 3306,
  33.             'charset' => 'utf8',
  34.             'driver' => 'pdo_mysql',
  35.         );
  36.         // 接続
  37.         $conn DriverManager::getConnection($connectionParams$config);
  38.         $posts $this->getPosts($conn);
  39.         // echo "<div style='display:none;>";
  40.         // print_r($posts);
  41.         // echo "</div>";
  42.         $specialPosts $this->getPosts($conn,4,8);
  43.         // print_r($specialPosts);
  44.         $termsQb $conn->createQueryBuilder();
  45.         $terms $termsQb
  46.             ->select('*')
  47.             ->from('yt_term_taxonomy','tt')
  48.             ->join('tt','yt_terms','t','tt.term_id = t.term_id')
  49.             ->where('tt.taxonomy = "category"')
  50.             ->execute()
  51.             ->fetchAll()
  52.         ;
  53.         return [
  54.             'Posts' => $posts,
  55.             'Terms' => $terms,
  56.             'SpecialPosts' => $specialPosts,
  57.         ];
  58.     }
  59.     
  60.     private function getPosts($conn$limit 5$category null){
  61.             $postsQb $conn->createQueryBuilder();
  62.             $postsResults $postsQb
  63.                 ->select('*')
  64.                 ->from('yt_posts','p')
  65.                 ->join('p','yt_term_relationships','r','p.id = r.object_id')
  66.                 ->join('r','yt_term_taxonomy','tt','r.term_taxonomy_id = tt.term_taxonomy_id')
  67.                 ->join('tt','yt_terms','t','tt.term_id = t.term_id')
  68.                 ->leftJoin('p','yt_postmeta','pm','p.id = pm.post_id AND pm.meta_key = "_thumbnail_id"')
  69.                 ->where('p.post_status = "publish"')
  70.                 ->andWhere('p.post_type = "post"')
  71.                 ->orderBy('p.post_date',"DESC")
  72.                 ->execute()
  73.                 ->fetchAll()
  74.             ;
  75.             $posts = [];
  76.             foreach($postsResults as $key => $post){
  77.                 $post['terms'][] = [
  78.                     'term_id' => $post['term_id'],
  79.                     'name' => $post['name'],
  80.                     'taxonomy' => $post['taxonomy'],
  81.                     'slug' => $post['slug']
  82.                 ];
  83.                 // echo ($key - 1);
  84.                 if($key != && $post['ID'] == $posts[array_key_last($posts)]['ID']){
  85.                     $posts[array_key_last($posts)]['terms'][] = $post['terms'][0];
  86.                 }else{
  87.                     if(isset($post['meta_value']) && !empty($post['meta_value'])){
  88.                         $postMetaQb $conn->createQueryBuilder();
  89.                         $postMetaResults $postMetaQb
  90.                             ->select('*')
  91.                             ->from('yt_posts','p')
  92.                             ->where('p.ID = :metavalue')
  93.                             ->setParameter('metavalue',$post['meta_value'])
  94.                             ->execute()
  95.                             ->fetchAll()
  96.                             ;
  97.                             // print_r($postMetaResults);
  98.                         if(isset($postMetaResults[0]['guid'])){
  99.                             $post['thumbnail'] = $postMetaResults[0]['guid'];
  100.                         }else{
  101.                             $post['thumbnail'] = $this->get_first_image($post['post_content']);
  102.                         }
  103.                     }else{
  104.                         $post['thumbnail'] = $this->get_first_image($post['post_content']);
  105.                     }
  106.                     $posts[] = $post;
  107.                 }
  108.             }
  109.             if($category != null){
  110.                 $feature_posts = [];
  111.                 foreach($posts as $key => $post){
  112.                     $has_category false;
  113.                     foreach($post['terms'] as $term){
  114.                         if($term['term_id'] == $category$has_category true;
  115.                         if($has_category) break;
  116.                     }
  117.                     if($has_category$feature_posts[] = $post;
  118.                 }
  119.                 return array_slice($feature_posts,0,$limit);
  120.             }
  121.             return array_slice($posts,0,$limit);
  122.     }
  123.     private function get_first_image($content) {
  124.         $first_img '/column/img/common/no_image.png';
  125.         preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*>/i'$content$matches);
  126.         if(isset($matches[1][0])){
  127.           $first_img $matches[1][0];
  128.         }
  129.         return $first_img;
  130.     }
  131. }