app/Plugin/HsdRelatedProduct/Controller/Block/HsdRelatedProductController.php line 75

Open in your IDE?
  1. <?php
  2. namespace Plugin\HsdRelatedProduct\Controller\Block;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Entity\Product;
  5. use Eccube\Repository\ProductRepository;
  6. use Eccube\Service\ProductReviewService;
  7. use Plugin\HsdRelatedProduct\Repository\ConfigRepository;
  8. use Plugin\HsdRelatedProduct\Entity\HsdRelatedProduct;
  9. use Plugin\ProductReview4\Repository\ProductReviewRepository;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\Routing\RouterInterface;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. class HsdRelatedProductController extends AbstractController
  18. {
  19.     //タイトル
  20.     private $_title 'この商品をみた人はこんな商品もみています';
  21.     // 表示個数
  22.     private $_show_count 4//初期値4
  23.     // 関連商品用データ
  24.     private $_rp = array();
  25.     // 価格の表示/非表示
  26.     private $_show_price null;
  27.     // 表示タイプ
  28.     private $_show_type 'normal'//初期値 スライダーなし
  29.     // ページネーション
  30.     private $_pagination 'true'//初期値 あり
  31.     // ナビゲーション
  32.     private $_navbuttons 'true'//初期値 あり
  33.     // 表示の自動ループ
  34.     private $_showloop 'true'//初期値 あり
  35.     // Anna用
  36.     private $_save_from_id 0;
  37.     private $_save_to_id 0;
  38.     private $_save_from_product_name '';
  39.     private $_save_to_product_name '';
  40.     private $productRepository;
  41.     private $productReviewRepository;
  42.     private $productReviewService;
  43.     private $_def_max_row 1000//データの最大保持数の初期値
  44.     /**
  45.      * HsdRelatedProductController constructor.
  46.      *
  47.      * @param ConfigRepository $configRepository
  48.      */
  49.     public function __construct(ConfigRepository $configRepositoryRequestStack $requestStackProductRepository $productRepositoryProductReviewRepository $productReviewRepositoryProductReviewService $productReviewService)
  50.     {
  51.         $this->_configRepository $configRepository;
  52.         $this->requestStack $requestStack;
  53.         $this->productRepository $productRepository;
  54.         $this->productReviewRepository $productReviewRepository;
  55.         $this->productReviewService $productReviewService;
  56.     }
  57.     /**
  58.      * @Route("block_hsd_related_product", name="block_hsd_related_product")
  59.      * @Template("@Block/hsd_related_product.twig")
  60.      */
  61.     public function index(Request $request)
  62.     {
  63.         /*
  64.          * 詳細画面の場合のみ動作
  65.          * /products/detail/[product_id] を想定
  66.          */
  67.         $request_stack $this->get('request_stack')->getMasterRequest();
  68.         $chkpath explode('/'$request_stack->getPathInfo());
  69.         //セット商品プラグインの商品ページにも対応 20211123 kikuzawa
  70.         if( ($chkpath[1] == 'products' && $chkpath[2] == 'detail') || ($chkpath[1] == 'products' && $chkpath[2] == 'select') ){
  71.             if($chkpath[2] == 'detail'){
  72.                 $id $chkpath[3];
  73.                 $isSlug = !is_numeric($id);
  74.                 if ($isSlug) {
  75.                     $em $this->entityManager;
  76.                     $stmt $em->getConnection()->prepare(
  77.                         "select id from dtb_product p where p.slug_variant = '$id'"
  78.                     );
  79.                     $stmt->execute();
  80.                     $rs $stmt->fetch();
  81.                     $id $rs['id'];
  82.                 }
  83.             }
  84.             else{
  85.                 $id $chkpath[4];
  86.             }
  87.             $max_row $this->_def_max_row// 初期値1000
  88.             // 保持する最大データ数を取得し、1回の削除数を設定(20%を設定)
  89.             $setting $this->_configRepository->get();
  90.             if( $setting != null ){
  91.                 if( !empty($setting->getMaxNum()) && is_numeric($setting->getMaxNum()) ) {
  92.                     $this->_show_count $setting->getMaxNum();
  93.                 }
  94.                 if( !empty($setting->getMaxRowNum()) && is_numeric($setting->getMaxRowNum()) ) {
  95.                     $max_row $setting->getMaxRowNum();
  96.                     $del_rows intval($max_row 0.2);
  97.                 }else{
  98.                     $del_rows 200;
  99.                 }
  100.                 if( !empty($setting->getTitle()) ) {
  101.                     $this->_title $setting->getTitle();
  102.                 }
  103.                 if( !empty($setting->getShowPrice()) ) {
  104.                     $this->_show_price $setting->getShowPrice();
  105.                 }
  106.                 if( !empty($setting->getShowType()) ) {
  107.                     $this->_show_type $setting->getShowType();
  108.                 }
  109.                 if( !empty($setting->getPagination()) ) {
  110.                     $this->_pagination $setting->getPagination();
  111.                 }
  112.                 if( !empty($setting->getNavbuttons()) ) {
  113.                     $this->_navbuttons $setting->getNavbuttons();
  114.                 }
  115.                 if( !empty($setting->getShowloop()) ) {
  116.                     $this->_showloop $setting->getShowloop();
  117.                 }
  118.             }
  119.             $em $this->entityManager;
  120.             $con_db_type $em->getConnection()->getDatabasePlatform()->getName(); // postgresql or mysql
  121.             // データ保持数に達していたら削除
  122.             $stmt $em->getConnection()->prepare(
  123.                     'select count(rp.id) cn from plg_hsd_related_product rp'
  124.                 );
  125.             $stmt->execute();
  126.             $rs $stmt->fetchAll();
  127.             if($rs[0]['cn'] > $max_row){
  128.                 // 古いrowを削除
  129.                 if($rs[0]['cn'] > $max_row){
  130.                     $del_rows $rs[0]['cn'] - $max_row;
  131.                 }
  132.                 $stmt $em->getConnection()->prepare('delete from plg_hsd_related_product order by updated_at asc limit ' $del_rows);
  133.                 $stmt->execute();
  134.             }
  135.             /*
  136.             * もしセッションにsave_pr_idが保持されていたら処理を行う
  137.             */
  138.             if( isset($_SESSION['ec_save_pr_id']) ) {
  139.                 $_from_id $_SESSION['ec_save_pr_id'];
  140.                 // DB更新:もしfromとtoが異なる場合は保持
  141.                 if ($_from_id != $id) {
  142.                     $rp_obj = new HsdRelatedProduct();
  143.                     $rp_obj->setId(uniqid('rp_'))
  144.                         ->setFromId($_from_id)
  145.                         ->setToId($id)
  146.                         ->setUpdatedAt(new \DateTime());
  147.                     $em->persist($rp_obj);
  148.                     $em->flush($rp_obj);
  149.                     // Anna用
  150.                     $this->_save_from_id $_from_id;
  151.                     $stmt_from $em->getConnection()->prepare("select name from dtb_product where id = " $_from_id);
  152.                     $stmt_from->execute();
  153.                     $rs_from $stmt_from->fetchAll();
  154.                     $this->_save_from_product_name $rs_from[0]['name'];
  155.                     $this->_save_to_id $id;
  156.                     $stmt_to $em->getConnection()->prepare("select name from dtb_product where id = " $id);
  157.                     $stmt_to->execute();
  158.                     $rs_to $stmt_to->fetchAll();
  159.                     $this->_save_to_product_name $rs_to[0]['name'];
  160.                 }
  161.             }
  162.             // 現在の商品IDをもとに、次の商品IDを取得
  163.             $stmt $em->getConnection()->prepare("
  164.                 SELECT count(rp.to_id) cn, rp.to_id FROM plg_hsd_related_product rp, dtb_product as p WHERE rp.from_id='" $id "' AND rp.to_id = p.id AND p.product_status_id = 1 GROUP BY rp.from_id, rp.to_id ORDER BY cn DESC
  165.                 ");
  166.             $stmt->execute();
  167.             $rs $stmt->fetchAll();
  168.             // 関連商品自動表示ブロックの設定
  169.             $or_str '';
  170.             foreach($rs as $item){
  171.                 $or_str .= '(ecp.id=' $item['to_id'] . ' AND ecp.id = ecpi.product_id) or ';
  172.             }
  173.             $or_str substr($or_str0strlen($or_str)-4);
  174.             if(strlen($or_str) > 1) {
  175.                 $sql '';
  176.                 if( $con_db_type == 'postgresql' ){
  177.                     $sql 'SELECT ecp.id, ecp.name, ecp.description_detail, (select in_ecpi.file_name FROM dtb_product_image in_ecpi WHERE in_ecpi.product_id = ecp.id AND in_ecpi.sort_no = 1 ) file_name, (select MIN(in_pcl.price02) FROM dtb_product_class in_pcl WHERE in_pcl.product_id = ecp.id and in_pcl.visible::int = 1 GROUP BY in_pcl.product_id) min_price, (select MAX(in_pcl.price02) FROM dtb_product_class in_pcl WHERE in_pcl.product_id = ecp.id and in_pcl.visible::int = 1 GROUP BY in_pcl.product_id) max_price FROM dtb_product ecp, dtb_product_image ecpi WHERE ' $or_str ' GROUP BY ecp.id';
  178.                 }else{
  179.                     $sql 'SELECT ecp.id, ecp.name, ecp.description_detail, (select in_ecpi.file_name FROM dtb_product_image in_ecpi WHERE in_ecpi.product_id = ecp.id AND in_ecpi.sort_no = 1 ) file_name, (select MIN(in_pcl.price02) FROM dtb_product_class in_pcl WHERE in_pcl.product_id = ecp.id and in_pcl.visible = 1 GROUP BY in_pcl.product_id) min_price, (select MAX(in_pcl.price02) FROM dtb_product_class in_pcl WHERE in_pcl.product_id = ecp.id and in_pcl.visible = 1 GROUP BY in_pcl.product_id) max_price FROM dtb_product ecp, dtb_product_image ecpi WHERE ' $or_str ' GROUP BY ecp.id';
  180.                 }
  181.                 $stmt $em->getConnection()->prepare($sql);
  182.                 $stmt->execute();
  183.                 $this->_rp $stmt->fetchAll();
  184.             }
  185.             foreach($this->_rp as $k=>$product){
  186.                 $pr $this->productRepository->find($product["id"]);
  187.                 $rate $this->productReviewService->getReview($pr);
  188.                 $product["review_ave"] = round($rate['RecommendAvg'], 1);
  189.                 $product["review_cnt"] = intval($rate['ReviewCntList']);
  190.                 $this->_rp[$k] = $product;
  191.             }
  192.             /*
  193.              * 現在の商品id をセッションに保持
  194.              */
  195.             $_SESSION['ec_save_pr_id'] = $id;
  196.             return $this->render("Block/hsd_related_product.twig", array(
  197.                 'title' => $this->_title,
  198.                 'max_count' => $this->_show_count,
  199.                 'rp_count' => count($this->_rp),
  200.                 'hsd_related_product' => $this->_rp,
  201.                 'show_price' => $this->_show_price,
  202.                 'show_type' => $this->_show_type,
  203.                 'pagination' => $this->_pagination,
  204.                 'navbuttons' => $this->_navbuttons,
  205.                 'showloop' => $this->_showloop,
  206.                 'from_id' => $this->_save_from_id,
  207.                 'from_product_name' => $this->_save_from_product_name,
  208.                 'to_id' => $this->_save_to_id,
  209.                 'to_product_name' => $this->_save_to_product_name
  210.             ));
  211.         }
  212.         return $this->render("Block/hsd_related_product.twig", array(
  213.             'title' => $this->_title,
  214.             'max_count' => $this->_show_count,
  215.             'rp_count' => count($this->_rp),
  216.             'hsd_related_product' => $this->_rp,
  217.             'show_price' => $this->_show_price,
  218.             'show_type' => $this->_show_type,
  219.             'pagination' => $this->_pagination,
  220.             'navbuttons' => $this->_navbuttons,
  221.             'showloop' => $this->_showloop,
  222.             'from_id' => $this->_save_from_id,
  223.             'from_product_name' => $this->_save_from_product_name,
  224.             'to_id' => $this->_save_to_id,
  225.             'to_product_name' => $this->_save_to_product_name
  226.         ));
  227.     }
  228. }