<?php
/**
*
* app/Customize/Service/RecentGoodsService.php
* 最近チェックした商品
*
******************************************************/
namespace Customize\Service;
use Symfony\Component\HttpFoundation\Request;
use Lm\Service\Db\SqlService;
use Customize\Converter\DynamicConverter;
class RecentGoodsService {
/**
* @var SqlService $Sql
*/
protected $Sql;
/**
* @var DynamicConverter $dynamicConverter
*/
protected $dynamicConverter;
public function __construct(
DynamicConverter $dynamicConverter
)
{
$this->Sql = new SqlService();
$this->dynamicConverter = $dynamicConverter;
}
/**
* cookieに商品IDをセットする
* @param Request $request
* @param cookies $cookies
* @param int $id 商品ID
*/
public function setRecentGoodsCookie($cookies, $id) {
$recent_goods_list = [];
$recentviews = array($id);
if(!empty($cookies)){
$recentviews = array_merge($recentviews, $cookies);
}
$recent_goods_list =array_unique($recentviews);
$setTime=60*60*24*365; //365日
setcookie('recent_goods_list', serialize($recent_goods_list), time() + $setTime, "/");
}
/**
* cookieから商品IDを取得する
* @param Request $request
*/
public function getRecentView() {
return isset($_COOKIE['recent_goods_list']) ? unserialize($_COOKIE['recent_goods_list']) : [];
}
/**
* cookie登録~取得
* @param Request $request
* @param int $id 商品ID
*/
public function getRecentList($id) {
//cookie取得
$cookies = $this->getRecentView();
//cookie設定
$this->setRecentGoodsCookie($cookies, $id);
//現在のcookieを取得
$cookies = $this->getRecentView();
return $cookies;
}
public function LM_getRecentViews2($cookies, $limit = 14) {
// SQLインジェクション対策(cookieの中身を確認し、数字のみの配列に詰め替える)
$goodsIdList = array();
if($cookies){
foreach($cookies as $goodsId){
if(ctype_digit($goodsId)){
$goodsIdList[] = $goodsId;
}
}
}
// 商品情報の取得
$goodsIdList = array_slice($goodsIdList,0, $limit);
$recentGoodsList = $this->dynamicConverter->getGoodsDataForRecentviews2($goodsIdList);
$recentviews = array();
if($recentGoodsList){
// 閲覧順にソートするためのキー配列を作成
$flip = array_flip($goodsIdList);
$sortKey = array();
foreach ($recentGoodsList as $index => $goods) {
$goodsId = $goods['goods_id'];
$dir = substr($goodsId, -1);
$goodsDir = "/goods.img/{$dir}/{$goodsId}/";
$item_id = !empty($goods['goods_canonical_hinban']) ? $goods['goods_canonical_hinban'] : $goodsId;
$sortKey[] = $flip[$goodsId];
$recentviews[$goodsId]['id'] = $goodsId;
$recentviews[$goodsId]['item_id'] = $item_id;
$recentviews[$goodsId]['img'] = $goodsDir . "main.jpg";
$recentviews[$goodsId]['goods_name'] = $goods['goods_name'];
$recentviews[$goodsId]['min_gp_price'] = $goods['min_gp_price'];
$recentviews[$goodsId]['min_gp_price_inc_tax'] = round($goods['min_gp_price'] * 1.1);
$recentviews[$goodsId]['count_gp_price'] = $goods['count_gp_price'];
// 口コミの取得
$average = $goods['review_average'];
$image_name = (int)($average * 2) / 2 * 10;
if ($average > 0) {
$recentviews[$goodsId]['average'] = $average;
$recentviews[$goodsId]['average_img'] = "<img width='90px' height='18px' class=\"history-average-img\" alt=\"お客様からの口コミレビュー評価の星の数".$average."\" src=\"/images/review/star_m".$image_name.".gif\">";
$recentviews[$goodsId]['review'] = $goods['review_count'];
}
}
// 閲覧順にソート
array_multisort($sortKey, SORT_NUMERIC, $recentviews);
}
return $recentviews;
}
}