<?php
// /app/Customize/EventListener/TwigAddParamsListener.php
namespace Customize\EventListener;
use Customize\Repository\RankingRepository;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\ProductRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TwigAddParamsListener implements EventSubscriberInterface
{
/**
* コンストラクタ
*
* @param RankingRepository $rankingRepository 売上ランキングのリポジトリ
* @param ProductRepository $productRepository 商品リポジトリ
*/
public function __construct(
RankingRepository $rankingRepository,
ProductRepository $productRepository
) {
$this->rankingRepository = $rankingRepository;
$this->productRepository = $productRepository;
}
/**
* テンプレートイベントのリスナー
* 売上ランキングをテンプレートに渡すための処理
*
* @param TemplateEvent $event テンプレートイベント
*/
public function onRanking(TemplateEvent $event)
{
// 現在のテンプレートのパラメータを取得
$parameters = $event->getParameters();
// 売上ランキングのデータを取得
$ProductClasses = $this->rankingRepository->getProductsRanking(365);
$event->setParameters($parameters);
$sums = [];
$Products = [];
if (!empty($ProductClasses)) {
foreach ($ProductClasses as $item) {
if (!isset($sums[$item['product_id']])) {
$sums[$item['product_id']] = 0;
}
$sums[$item['product_id']] += $item['total_price'];
}
// 合計をtotal_priceで並び替え
arsort($sums);
// 結果を表示(必要に応じて)
foreach ($sums as $productId => $totalPrice) {
// 商品IDに基づいて商品情報を取得
$Products[] = current($this->productRepository->findBy(['id' => $productId]));
}
// メッセージを空にする
$parameters['rankingMessage'] = '';
} else {
// 売上がない場合はメッセージを設定
$parameters['rankingMessage'] = '現在、売上ランキングはありません。';
}
// // テンプレートにランキング商品を渡す
$parameters['rankingProducts'] = $Products;
$event->setParameters($parameters);
}
/**
* テンプレートイベントのリスナー02
* 売上個数、カテゴリ取得に適応した形に
* 売上ランキングをテンプレートに渡すための処理
*
* @param TemplateEvent $event テンプレートイベント
*/
public function onRanking02(TemplateEvent $event)
{
// 現在のテンプレートのパラメータを取得
$parameters = $event->getParameters();
// 売上ランキングのデータを取得
// $ProductClasses = $this->rankingRepository->getProductsRanking02(365);
// 年間ランキングのデータを取得
// $yearlyProductClasses = $this->rankingRepository->getProductsRanking02(365);
$yearlyProductClasses = $this->rankingRepository->getProductsRanking03(365, null);
// 月間ランキングのデータを取得
$monthlyProductClasses = $this->rankingRepository->getProductsRanking03(30, null);
// リングのデータを取得
$ringProductClasses = $this->rankingRepository->getProductsRanking03(365, 113);
// ネックレスのデータを取得
$necklaceProductClasses = $this->rankingRepository->getProductsRanking03(365, 14);
// ピアスのデータを取得
$pierceProductClasses = $this->rankingRepository->getProductsRanking03(365, 7);
// イヤリングのデータを取得
$earringProductClasses = $this->rankingRepository->getProductsRanking03(365, 137);
// ブレスレットのデータを取得
$braceletProductClasses = $this->rankingRepository->getProductsRanking03(365, 115);
// $NecklaceProductClasses = $this->rankingRepository->getProductsRanking03(365,14);
$event->setParameters($parameters);
// $sums = [];
// $quantities = [];
// $Products = [];
// 年間ランキングの集計
$yearlyQuantities = [];
$yearlyProducts = [];
if (!empty($yearlyProductClasses)) {
foreach ($yearlyProductClasses as $item) {
if (!isset($yearlyQuantities[$item['product_id']])) {
$yearlyQuantities[$item['product_id']] = 0;
}
$yearlyQuantities[$item['product_id']] += $item['total_quantity'];
}
// echo '<pre>'; print_r(array_slice($yearlyQuantities, 0, 10, true)); echo '</pre>';
// 合計をtotal_priceで並び替え
arsort($yearlyQuantities);
// 結果を表示(必要に応じて)
// foreach ($yearlyQuantities as $productId => $totalQuantity) {
// // 商品IDに基づいて商品情報を取得
// $yearlyProducts[] = current($this->productRepository->findBy(['id' => $productId]));
// }
// 除外するID
$excludedProductIds = [73, 74, 75];
foreach ($yearlyQuantities as $productId => $totalQuantity) {
if (!in_array($productId, $excludedProductIds)) {
// $yearlyProducts[] = current($this->productRepository->findBy(['id' => $productId]));
$product = current($this->productRepository->findBy(['id' => $productId]));
$product->total_quantity = $totalQuantity; // 合計数量を追加
$yearlyProducts[] = $product;
}
}
// メッセージを空にする
$parameters['rankingMessage'] = '';
} else {
// 売上がない場合はメッセージを設定
$parameters['rankingMessage'] = '現在、売上ランキングはありません。';
}
// 月間ランキングの集計
$monthlyProducts = [];
$monthlyQuantities = [];
if (!empty($monthlyProductClasses)) {
foreach ($monthlyProductClasses as $item) {
if (!isset($monthlyQuantities[$item['product_id']])) {
$monthlyQuantities[$item['product_id']] = 0;
}
$monthlyQuantities[$item['product_id']] += $item['total_quantity'];
}
arsort($monthlyQuantities);
// foreach ($monthlyQuantities as $productId => $totalQuantity) {
// $monthlyProducts[] = current($this->productRepository->findBy(['id' => $productId]));
// }
// 除外するID
$excludedProductIds = [73, 74, 75];
foreach ($monthlyQuantities as $productId => $totalQuantity) {
if (!in_array($productId, $excludedProductIds)) {
$monthlyProducts[] = current($this->productRepository->findBy(['id' => $productId]));
}
}
$parameters['monthlyRankingMessage'] = '';
} else {
$parameters['monthlyRankingMessage'] = '現在、月間売上ランキングはありません。';
}
// ringランキングの集計
$ringQuantities = [];
$ringProducts = [];
if (!empty($ringProductClasses)) {
foreach ($ringProductClasses as $item) {
if (!isset($ringQuantities[$item['product_id']])) {
$ringQuantities[$item['product_id']] = 0;
}
$ringQuantities[$item['product_id']] += $item['total_quantity'];
}
arsort($ringQuantities);
foreach ($ringQuantities as $productId => $totalQuantity) {
$ringProducts[] = current($this->productRepository->findBy(['id' => $productId]));
}
$parameters['ringRankingMessage'] = '';
} else {
$parameters['ringRankingMessage'] = '現在、リング売上ランキングはありません。';
}
// ネックレスランキングの集計
$necklaceQuantities = [];
$necklaceProducts = [];
if (!empty($necklaceProductClasses)) {
foreach ($necklaceProductClasses as $item) {
if (!isset($necklaceQuantities[$item['product_id']])) {
$necklaceQuantities[$item['product_id']] = 0;
}
$necklaceQuantities[$item['product_id']] += $item['total_quantity'];
}
// echo '<pre>'; print_r(array_slice($necklaceQuantities, 0, 10, true)); echo '</pre>';
arsort($necklaceQuantities);
foreach ($necklaceQuantities as $productId => $totalQuantity) {
$necklaceProducts[] = current($this->productRepository->findBy(['id' => $productId]));
// echo '<pre>'; print_r($totalQuantity); echo '</pre>';
}
// 除外するID
// $excludedProductIds = [73, 74, 75];
// foreach ($necklaceQuantities as $productId => $totalQuantity) {
// if (!in_array($productId, $excludedProductIds)) {
// $necklaceProducts[] = current($this->productRepository->findBy(['id' => $productId]));
// }
// }
$parameters['necklaceRankingMessage'] = '';
} else {
$parameters['necklaceRankingMessage'] = '現在、ネックレス売上ランキングはありません。';
}
// ピアスランキングの集計
$pierceQuantities = [];
$pierceProducts = [];
if (!empty($pierceProductClasses)) {
foreach ($pierceProductClasses as $item) {
if (!isset($pierceQuantities[$item['product_id']])) {
$pierceQuantities[$item['product_id']] = 0;
}
$pierceQuantities[$item['product_id']] += $item['total_quantity'];
}
arsort($pierceQuantities);
foreach ($pierceQuantities as $productId => $totalQuantity) {
$pierceProducts[] = current($this->productRepository->findBy(['id' => $productId]));
}
$parameters['pierceRankingMessage'] = '';
} else {
$parameters['pierceRankingMessage'] = '現在、ピアス売上ランキングはありません。';
}
// イヤリングランキングの集計
$earringProducts = [];
$earringQuantities = [];
if (!empty($earringProductClasses)) {
foreach ($earringProductClasses as $item) {
if (!isset($earringQuantities[$item['product_id']])) {
$earringQuantities[$item['product_id']] = 0;
}
$earringQuantities[$item['product_id']] += $item['total_quantity'];
}
arsort($earringQuantities);
foreach ($earringQuantities as $productId => $totalQuantity) {
$earringProducts[] = current($this->productRepository->findBy(['id' => $productId]));
}
$parameters['earringRankingMessage'] = '';
} else {
$parameters['earringRankingMessage'] = '現在、イヤリング売上ランキングはありません。';
}
// ブレスレットの集計
$braceletQuantities = [];
$braceletProducts = [];
if (!empty($braceletProductClasses)) {
foreach ($braceletProductClasses as $item) {
if (!isset($braceletQuantities[$item['product_id']])) {
$braceletQuantities[$item['product_id']] = 0;
}
$braceletQuantities[$item['product_id']] += $item['total_quantity'];
}
arsort($braceletQuantities);
foreach ($braceletQuantities as $productId => $totalQuantity) {
$braceletProducts[] = current($this->productRepository->findBy(['id' => $productId]));
}
$parameters['braceletRankingMessage'] = '';
} else {
$parameters['braceletRankingMessage'] = '現在、ブレスレット売上ランキングはありません。';
}
// テンプレートにランキング商品を渡す
$parameters['rankingProducts'] = $yearlyProducts;
$parameters['monthlyRankingProducts'] = $monthlyProducts;
$parameters['ringRankingProducts'] = $ringProducts;
$parameters['necklaceRankingProducts'] = $necklaceProducts;
$parameters['pierceRankingProducts'] = $pierceProducts;
$parameters['earringRankingProducts'] = $earringProducts;
$parameters['braceletRankingProducts'] = $braceletProducts;
$event->setParameters($parameters);
}
/**
* テンプレートイベントのリスナー03
* クエリを一回に限定する
* 売上個数、カテゴリ取得に適応した形に
* 売上ランキングをテンプレートに渡すための処理
*
* @param TemplateEvent $event テンプレートイベント
*/
public function onRanking03(TemplateEvent $event)
{
// 現在のテンプレートのパラメータを取得
$parameters = $event->getParameters();
// 全てのデータ取得へ
$allProductClasses = $this->rankingRepository->getProductsRanking03(365);
// 月間データ取得(日付範囲が異なるため)
$monthlyProductClasses = $this->rankingRepository->getProductsRanking03(30);
// // 配列の最初の5件を抽出
// $slicedData = array_slice($allProductClasses, 0, 50, true);
// // 抽出したデータを表示
// echo '<pre>取得したアイテム</pre>';
// print_r($slicedData);
// 初期化
$yearlyQuantities = [];
$monthlyQuantities = [];
$ringQuantities = [];
$necklaceQuantities = [];
$pierceQuantities = [];
$earringQuantities = [];
$braceletQuantities = [];
// 年間ランキングの集計(カテゴリIDを指定しない)
foreach ($allProductClasses as $item) {
if (!isset($yearlyQuantities[$item['product_id']])) {
$yearlyQuantities[$item['product_id']] = 0;
}
$yearlyQuantities[$item['product_id']] += (int)$item['total_quantity'];
}
arsort($yearlyQuantities);
// // 最初の10件だけ抽出
// $top10YearlyQuantities = array_slice($yearlyQuantities, 0, 10, true);
// // 結果の確認
// echo '<pre>Top 10 Necklace Quantities:</pre>';
// print_r($top10YearlyQuantities);
// // 重複調査
// $pCounts = [];
// foreach($allProductClasses as $item) {
// if(!isset($productCounts[$item['product_id']])) {
// $pCounts[$item['product_id']] = 0;
// }
// $pCounts[$item['product_id']]++;
// }
// arsort($pCounts);
// $limitcount = array_slice($pCounts, 0, 10, true);
// echo '<pre>重複チェック</pre>';
// print_r($limitcount);
//
$excludedProductIds = [73, 74, 75];
$yearlyProducts = [];
foreach ($yearlyQuantities as $productId => $totalQuantity) {
if (!in_array($productId, $excludedProductIds)) {
$product = current($this->productRepository->findBy(['id' => $productId]));
$product->total_quantity = $totalQuantity;
$yearlyProducts[] = $product;
}
}
$parameters['yearlyRankingProducts'] = $yearlyProducts;
// 月間ランキングの集計(カテゴリIDを指定しない)
foreach ($monthlyProductClasses as $item) {
if (!isset($monthlyQuantities[$item['product_id']])) {
$monthlyQuantities[$item['product_id']] = 0;
}
$monthlyQuantities[$item['product_id']] += (int)$item['total_quantity'];
}
arsort($monthlyQuantities);
$monthlyProducts = [];
foreach ($monthlyQuantities as $productId => $totalQuantity) {
if (!in_array($productId, $excludedProductIds)) {
$product = current($this->productRepository->findBy(['id' => $productId]));
$product->total_quantity = $totalQuantity;
$monthlyProducts[] = $product;
}
}
$parameters['monthlyRankingProducts'] = $monthlyProducts;
// リングの集計
$categoryRingMap = [];
foreach ($allProductClasses as $item) {
$product_id = $item['product_id'];
$category_id = $item['category_id'];
if (!isset($ringQuantities[$item['product_id']])) {
$ringQuantities[$item['product_id']] = 0;
$categoryRingMap[$product_id] = [];
}
$ringQuantities[$item['product_id']] += (int)$item['total_quantity'];
$categoryRingMap[$product_id][] = $category_id;
}
// 重複カテゴリを削除し、カテゴリidごとに。
foreach ($categoryRingMap as $pid => $categories) {
$categoryRingMap[$pid] = array_unique($categories);
}
// カテゴリ選別
$RingId = 113;
$ringProducts = [];
arsort($ringQuantities);
foreach ($ringQuantities as $productId => $totalQuantity) {
if (in_array($RingId, $categoryRingMap[$productId])) {
$product = current($this->productRepository->findBy(['id' => $productId]));
$product->total_quantity = $totalQuantity;
$ringProducts[] = $product;
}
}
// $ringProducts = [];
// foreach ($ringQuantities as $productId => $totalQuantity) {
// $product = current($this->productRepository->findBy(['id' => $productId]));
// $product->total_quantity = $totalQuantity;
// $ringProducts[] = $product;
// }
$parameters['ringRankingProducts'] = $ringProducts;
// ネックレスの集計 id14
$categoryMap = [];
foreach ($allProductClasses as $item) {
$product_id = $item['product_id'];
$category_id = $item['category_id'];
if (!isset($necklaceQuantities[$item['product_id']])) {
$necklaceQuantities[$item['product_id']] = 0;
$categoryMap[$product_id] = [];
}
$necklaceQuantities[$item['product_id']] += (int)$item['total_quantity'];
$categoryMap[$product_id][] = $category_id;
}
// 重複カテゴリを削除し、カテゴリidごとに。
foreach ($categoryMap as $pid => $categories) {
$categoryMap[$pid] = array_unique($categories);
}
// カテゴリ選別
$necklaceId = 14;
$necklaceProducts = [];
arsort($necklaceQuantities);
foreach ($necklaceQuantities as $productId => $totalQuantity) {
if (in_array($necklaceId, $categoryMap[$productId])) {
$product = current($this->productRepository->findBy(['id' => $productId]));
$product->total_quantity = $totalQuantity;
$necklaceProducts[] = $product;
}
}
// $necklaceProducts = [];
// foreach ($necklaceQuantities as $productId => $totalQuantity) {
// $product = current($this->productRepository->findBy(['id' => $productId]));
// $product->total_quantity = $totalQuantity;
// $necklaceProducts[] = $product;
// }
$parameters['necklaceRankingProducts'] = $necklaceProducts;
// ピアスの集計
$categoryPierceMap = [];
foreach ($allProductClasses as $item) {
$product_id = $item['product_id'];
$category_id = $item['category_id'];
if (!isset($pierceQuantities[$item['product_id']])) {
$pierceQuantities[$item['product_id']] = 0;
$categoryPierceMap[$product_id] = [];
}
$pierceQuantities[$item['product_id']] += (int)$item['total_quantity'];
$categoryPierceMap[$product_id][] = $category_id;
}
// 重複カテゴリを排除
foreach ($categoryPierceMap as $productId => $categories) {
$categoryPierceMap[$productId] = array_unique($categories);
}
arsort($pierceQuantities);
$pierceId = 7;
$pierceProducts = [];
foreach ($pierceQuantities as $productId => $totalQuantity) {
if (in_array($pierceId, $categoryPierceMap[$productId])) {
$product = current($this->productRepository->findBy(['id' => $productId]));
$product->total_quantity = $totalQuantity;
$pierceProducts[] = $product;
}
}
$parameters['pierceRankingProducts'] = $pierceProducts;
// イヤリングの集計 137
$categoryEarringMap = [];
foreach ($allProductClasses as $item) {
$product_id = $item['product_id'];
$category_id = $item['category_id'];
if (!isset($earringQuantities[$item['product_id']])) {
$earringQuantities[$item['product_id']] = 0;
$categoryEarringMap[$product_id] = [];
}
$earringQuantities[$item['product_id']] += (int)$item['total_quantity'];
$categoryEarringMap[$product_id][] = $category_id;
}
// 重複カテゴリを排除
foreach ($categoryEarringMap as $productId => $categories) {
$categoryEarringMap[$productId] = array_unique($categories);
}
arsort($earringQuantities);
$earringId = 137;
$earringProducts = [];
foreach ($earringQuantities as $productId => $totalQuantity) {
if (in_array($earringId, $categoryEarringMap[$productId])) {
$product = current($this->productRepository->findBy(['id' => $productId]));
$product->total_quantity = $totalQuantity;
$earringProducts[] = $product;
}
}
$parameters['earringRankingProducts'] = $earringProducts;
// ブレスレットの集計 115
$categoryBraceletMap = [];
foreach ($allProductClasses as $item) {
$product_id = $item['product_id'];
$category_id = $item['category_id'];
if (!isset($braceletQuantities[$item['product_id']])) {
$braceletQuantities[$item['product_id']] = 0;
$categoryBraceletMap[$product_id] = [];
}
$braceletQuantities[$item['product_id']] += (int)$item['total_quantity'];
$categoryBraceletMap[$product_id][] = $category_id;
}
// 重複カテゴリを排除
foreach($categoryBraceletMap as $productId => $categories) {
$categoryBraceletMap[$productId] = array_unique($categories);
}
arsort($braceletQuantities);
$braceletId = 115;
$braceletProducts = [];
foreach ($braceletQuantities as $productId => $totalQuantity) {
if(in_array($braceletId, $categoryBraceletMap[$productId])) {
$product = current($this->productRepository->findBy(['id' => $productId]));
$product->total_quantity = $totalQuantity;
$braceletProducts[] = $product;
}
}
$parameters['braceletRankingProducts'] = $braceletProducts;
// パラメータをテンプレートに設定
$event->setParameters($parameters);
}
/**
* イベントサブスクライバーを定義
*
* @return array イベントとメソッドのマッピング
*/
public static function getSubscribedEvents()
{
return [
// 'Block/ranking.twig'テンプレートに対するイベントリスナーを登録
'Block/ranking.twig' => 'onRanking03',
];
}
}