app/Customize/Service/RecentGoodsService.php line 92

Open in your IDE?
  1. <?php
  2. /**
  3.  *
  4.  * app/Customize/Service/RecentGoodsService.php
  5.  * 最近チェックした商品
  6.  *
  7.  ******************************************************/
  8. namespace Customize\Service;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Lm\Service\Db\SqlService;
  11. use Customize\Converter\DynamicConverter;
  12. class RecentGoodsService {
  13.     /**
  14.      * @var SqlService $Sql
  15.      */
  16.     protected $Sql;
  17.     /**
  18.      * @var DynamicConverter $dynamicConverter
  19.      */
  20.     protected $dynamicConverter;
  21.     public function __construct(
  22.         DynamicConverter  $dynamicConverter
  23.     )
  24.     {
  25.         $this->Sql = new SqlService();
  26.         $this->dynamicConverter $dynamicConverter;
  27.     }
  28.     /**
  29.      * cookieに商品IDをセットする
  30.      * @param Request $request
  31.      * @param cookies $cookies
  32.      * @param int $id 商品ID
  33.      */
  34.     public function setRecentGoodsCookie($cookies$id) {
  35.         $recent_goods_list = [];
  36.         $recentviews = array($id);
  37.         if(!empty($cookies)){
  38.             $recentviews array_merge($recentviews$cookies);
  39.         }
  40.         $recent_goods_list =array_unique($recentviews);
  41.         $setTime=60*60*24*365//365日
  42.         setcookie('recent_goods_list'serialize($recent_goods_list), time() + $setTime"/");
  43.     }
  44.     /**
  45.      * cookieから商品IDを取得する
  46.      * @param Request $request
  47.      */
  48.     public function getRecentView() {
  49.         return isset($_COOKIE['recent_goods_list']) ? unserialize($_COOKIE['recent_goods_list']) : [];
  50.     }
  51.     /**
  52.      * cookie登録~取得
  53.      * @param Request $request
  54.      * @param int $id 商品ID
  55.      */
  56.     public function getRecentList($id) {
  57.         //cookie取得
  58.         $cookies $this->getRecentView();
  59.         //cookie設定
  60.         $this->setRecentGoodsCookie($cookies$id);
  61.         //現在のcookieを取得
  62.         $cookies $this->getRecentView();
  63.         return $cookies;
  64.     }
  65.     public function LM_getRecentViews2($cookies$limit 14) {
  66.         // SQLインジェクション対策(cookieの中身を確認し、数字のみの配列に詰め替える)
  67.         $goodsIdList = array();
  68.         if($cookies){
  69.             foreach($cookies as $goodsId){
  70.                 if(ctype_digit($goodsId)){
  71.                     $goodsIdList[] = $goodsId;
  72.                 }
  73.             }
  74.         }
  75.         // 商品情報の取得
  76.         $goodsIdList array_slice($goodsIdList,0$limit);
  77.         $recentGoodsList $this->dynamicConverter->getGoodsDataForRecentviews2($goodsIdList);
  78.         $recentviews = array();
  79.         if($recentGoodsList){
  80.             // 閲覧順にソートするためのキー配列を作成
  81.             $flip array_flip($goodsIdList);
  82.             $sortKey = array();
  83.             foreach ($recentGoodsList as $index => $goods) {
  84.                 $goodsId $goods['goods_id'];
  85.                 $dir substr($goodsId, -1);
  86.                 $goodsDir "/goods.img/{$dir}/{$goodsId}/";
  87.                 $item_id = !empty($goods['goods_canonical_hinban']) ? $goods['goods_canonical_hinban'] : $goodsId;
  88.                 $sortKey[] = $flip[$goodsId];
  89.                 $recentviews[$goodsId]['id'] = $goodsId;
  90.                 $recentviews[$goodsId]['item_id'] = $item_id;
  91.                 $recentviews[$goodsId]['img'] = $goodsDir "main.jpg";
  92.                 $recentviews[$goodsId]['goods_name'] = $goods['goods_name'];
  93.                 $recentviews[$goodsId]['min_gp_price'] = $goods['min_gp_price'];
  94.                 $recentviews[$goodsId]['min_gp_price_inc_tax'] = round($goods['min_gp_price'] * 1.1);
  95.                 $recentviews[$goodsId]['count_gp_price'] = $goods['count_gp_price'];
  96.                 // 口コミの取得
  97.                 $average $goods['review_average'];
  98.                 $image_name = (int)($average 2) / 10;
  99.                 if ($average 0) {
  100.                     $recentviews[$goodsId]['average'] = $average;
  101.                     $recentviews[$goodsId]['average_img'] = "<img width='90px' height='18px' class=\"history-average-img\" alt=\"お客様からの口コミレビュー評価の星の数".$average."\" src=\"/images/review/star_m".$image_name.".gif\">";
  102.                     $recentviews[$goodsId]['review'] = $goods['review_count'];
  103.                 }
  104.             }
  105.         // 閲覧順にソート
  106.         array_multisort($sortKeySORT_NUMERIC$recentviews);
  107.         }
  108.         return $recentviews;
  109.     }
  110. }