<?php
namespace Customize\Controller;
use Customize\Controller\LM\LmAbstractController;
use Lm\Service\Db\DbManager;
use Customize\Service\LmHelper;
use Customize\Service\CommonService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Customize\Service\MessageService;
use Customize\Service\GoodsService;
class MessageController extends LmAbstractController
{
const ITEMS_PER_PAGE = 10;
const MAX_PAGING_LINKS = 10;
protected $MessageService;
protected $GoodsService;
protected $MetaTags=[];
protected $PageName;
public function __construct(
CommonService $commonService
,LmHelper $LmHelper
,MessageService $MessageService
,GoodsService $GoodsService
){
parent::__construct(
$commonService
,$LmHelper);
$this->MessageService = $MessageService;
$this->GoodsService = $GoodsService;
}
/**
* お客様の声一覧画面
*
* @Route("/message/", name="message/index", methods={"GET"})
* @Template("Message/index.twig")
*/
public function index(Request $request)
{
// 不正なパラメータの場合、404
$availableParameterKeys = [
'category',
'goods_id',
'page',
];
$parameterKeys = array_keys($request->query->all());
$diff = array_diff($parameterKeys, $availableParameterKeys);
if (!empty($diff)) {
throw new NotFoundHttpException();
}
$category_id = $request->query->get('category');
$goods_id = $request->query->get('goods_id');
# $basic_id = $request->query->get('id', ''); テンプレートで未使用 20220817 KAWAI
$page = $request->query->get('page', 1);
$itemsPerPage = self::ITEMS_PER_PAGE; // TODO: 1ページあたりの表示件数はユーザーに選択させると尚可(cookieの活用も検討)
$kataban = [];
$goods = [];
$breadCrumbList =[];
// 不正パラメータの場合、404
if (( $goods_id && !ctype_digit($goods_id)) || ( $category_id && !ctype_digit($category_id))) {
// "Page Not Found"という文言は、エラーコントローラーで使用していないので任意の文字列
throw new NotFoundHttpException();
}
//$ dbManager = new DbManager();
//$ db = $dbManager->get();
if (!$msgList = $this->MessageService->GetMessage($goods_id,$category_id)){
throw new NotFoundHttpException();
}
$pager = LmHelper::paging($page, $msgList, $itemsPerPage, self::MAX_PAGING_LINKS, $request->query->all());
$count = $pager['total_count'];
$pages = $pager['pages'];
$page_offset = $pager['page_offset'];
$msgList = array_slice($msgList, $page_offset, $itemsPerPage);
foreach((array)$msgList as $row_idx => $row){
/**
* 古い画像のパス置換対応
* 古い画像は「http://www.l-m.co.jp/koe/m-482131.jpg」のような画像パスになっている。
*
* ややこしいが、ここで画像のパスが/koe/で開始されるように置換し、viewで/images/koe_old/に置換され
* default/views/filters/ImgPath.php でドメインを含むパスに置換される。
*/
$msgList[$row_idx]['comment'] = preg_replace('!src=(["\'])https?://www\.l-m\.co\.jp/!', 'src=$1/', $row['comment']);
$goodlist = $this->MessageService->GetGoodsList($row['cm_id']);
foreach((array)$goodlist as $goods){
// 型番を検索
$katabanList = array();
/*$strSql = "SELECT DISTINCT gp_kataban FROM goods_price_table WHERE gp_goods = {$goods['goods_id']} AND NOT(gp_display <=> 99) LIMIT 0,3";
$sth = $ db->prepare($strSql);
$sth->execute();
$rskata = $sth->fetchAll(\PDO::FETCH_ASSOC);*/
$rskata = $this->GoodsService->getGoodsKataban($goods['goods_id']);
foreach ($rskata as $_rowkata) {
$katabanList[] = $_rowkata['gp_kataban'];
}
$kataban[$row['cm_id']][] = array( 'kataban' => implode(',', array_unique($katabanList)), 'name' => $goods['goods_name'], 'goods_id' => $goods['goods_id']);
}
}
// カテゴリ取得(お客様の声数でソート)
/* $strSql = "SELECT * FROM customer_message_category_table WHERE `delete` = '0'";
$sth = $ db->prepare($strSql);
$sth->execute();
$tmp_list = $sth->fetchAll(\PDO::FETCH_ASSOC);
*/
$tmp_list = $this->MessageService->GetCategoryList();
$categoryList = $sort = [];
foreach((array)$tmp_list as $row){
$categoryList['cmc_id_'.$row['cmc_id']] = $row;
$sort[] = $row['cmc_count'];
}
array_multisort($sort, SORT_DESC, $categoryList);
if($goods_id){
// 商品情報を取得
$goods = $this->GoodsService->getGoodsById($goods_id);
$breadCrumbList[] = [
'label' => LmHelper::display_text($goods['goods_name']),
];
$breadCrumbList[]=['label'=>'一覧'];
// カテゴリをランダムで1つ表示
/*$viewStrReview = array();
foreach((array)$displayCategory['viewStrReviewList'] as $data){
if(strpos($data, $displayCategory['canonicalPankuzuMainCategory']['main_name']) === false){
$viewStrReview[] = $data;
}
}
$this->view->displayCategory = $viewStrReview[0];*/
}
//elseif (!is_null($category_id)) {
//goods_idが取得できない場合
//}
// 用途取得
/* $strSql = "SELECT * FROM customer_message_use_table WHERE `delete` = '0'";
$sth = $ db->prepare($strSql);
$sth->execute();
$tmp_list = $sth->fetchAll(\PDO::FETCH_ASSOC);
$useList = array();
foreach((array)$tmp_list as $row){
$useList[$row['cmu_id']] = $row['use_name'];
}*/
// 不正パラメータの場合、404
if (!$msgList) {
// "Page Not Found"という文言は、エラーコントローラーで使用していないので任意の文字列
throw new NotFoundHttpException();
exit;
}
$category = null;
if( $category_id && preg_match('/^[0-9]+$/', $category_id) ){
$anotation_querystring = '?category='.$category_id;
$canonical_querystring = '?category='.$category_id;
}
else if( $page && preg_match('/^[0-9]+$/', $page) ){
$anotation_querystring = '?page='.$page;
$canonical_querystring = '?page='.$page;
}
else{
$anotation_querystring = '/';
$canonical_querystring = '/';
}
if(!is_null($category_id)) {
$category = $categoryList[ 'cmc_id_'.$category_id] ;
$breadCrumbList[] = [
'label' => "「{$category['category_name']}」のお客様の声",
];
}
foreach ($msgList as $key => $message) {
$msgList[$key]['img_paths'] = $this->get_image_path($message['file'], $message['cm_id'], $this->lmHelper->getConfig('BACK_URL'), $message['customer_name']);
$msgList[$key]['comment'] = preg_replace('@/koe/@', '/images/koe_old/', strip_tags($message['comment'], '<img><br><a><p><center><b><em><s>'));
if (!empty($message['customer_name']) && $message['customer_name'] != "#VALUE!") {
$msgList[$key]['customer_name'] = $message['customer_name'] . (!preg_match("/お客$/", $message['customer_name']) ? " " : "") ;
}
}
foreach ($kataban as $key1 => $rows) {
$rows = array_unique((array)$rows, SORT_REGULAR);
$kataban[$key1] = $rows;
foreach ($rows as $key2 => $row) {
$kataban[$key1][$key2]['kataban'] = LmHelper::display_text($row['kataban']);
$kataban[$key1][$key2]['name'] = LmHelper::display_text($row['name']);
}
}
return [
# 'g_basic_id' => $basic_id, テンプレートで未使用 20220817 KAWAI
'g_goods_id' => $goods_id,
'g_category_id' => $category_id,
'g_category' => $category,
'g_goods' => $goods,
'BreadCrumbs' => $breadCrumbList,
'g_page' => $page,
'g_pages' => $pages,
'g_pager' => $pager,
'g_msg_list' => $msgList,
'g_total_count' => $count,
'g_kataban' => $kataban,
'g_category_list' => $categoryList,
# 'g_use_list' => $useList, テンプレートで未使用 20220817 KAWAI
'g_anotation_querystring' => $anotation_querystring,
'g_canonical_querystring' => $canonical_querystring,
'MetaTags' => [
'message' => $goods_id ? ($goods['goods_name'] ?? null) : ($category['category_name'] ?? null)
], #20220730kawai
];
}
protected function get_image_path($data, $id, $path, $customer_name) {
$disp_data = unserialize($data);
$return_data = array();
$alt = $customer_name;
if (!preg_match("/お客$/", $customer_name)){
$alt .= " 様からの声の写真";
}
if(is_array($disp_data)){
$idx = 0;
foreach($disp_data as $disp){
$idx++;
$tmp_alt = '';
$return_data[$disp]['path'] = $this->lmHelper->getConfig('CFIMG_BACK_URL'). '/images/koe_admin/'.$id. '/'. $disp;
$n = "";
if($idx > 1){
$n .= "{$idx}枚目";
}
$tmp_alt .= $n;
$return_data[$disp]['alt'] = $alt.$tmp_alt;
}
$return_data = array_values($return_data);
} elseif($disp_data){
$return_data[]['path'] = $this->lmHelper->getConfig('CFIMG_BACK_URL'). '/images/koe_admin/'.$id. '/'. $disp_data;
$return_data[]['alt'] = $alt;
}
return $return_data;
}
}