<?php
/**
* @version EC=CUBE4
* @copyright 株式会社 翔 kakeru.co.jp
* @author
* 2022年08月19日作成
*
* app/Customize/Service/CategoryService.php
*
*
* カテゴリーサービス
*
*
* C= C= C= ┌(;・_・)┘トコトコ
******************************************************/
namespace Customize\Service;
use Lm\Service\Db\SqlService;
class CategoryService extends LmAbstractService
{
const CATEGORY_LIST_PAGING_COUNT_IN_PAGER_PC = 5;
const CATEGORY_LIST_PAGING_COUNT_IN_PAGER_SP = 3;
/**
* @var LmHelper $lmHelper
*/
protected $lmHelper;
/**
* @param LmHelper $lmHelper
*/
public function __construct(LmHelper $lmHelper)
{
$this->lmHelper = $lmHelper;
}
public function getCategory($ct = null, $mc = null, $mcMc = null)
{
return $this->getCached(function () use ($ct, $mc, $mcMc) {
//
$query = (new SqlService())
->Select('T.*, T1.*')
->Table('category_table')
->Join('main_category_table', 'T1.main_category_id = T.category_main_category');
if (!empty($mcMc)) {
$query->Set('T1.main_category_main_category_webname', $mcMc);
}
if (!empty($mc)) {
$query->Set('T1.main_category_webname', $mc);
}
if (!empty($ct)) {
$query->Set('T.category_webname', $ct);
}
//
$result = $query->Find();
//
return $result;
});
}
/**
* @param $ct
* @param $mc
* @param $mcMc
* @return array
*/
public function getBreadCrumbs($ct = null, $mc = null, $mcMc = null)
{
//
$category = $this->getCategory($ct, $mc, $mcMc);
//
$BreadCrumbs = [];
//
foreach ([
[
'f' => $ct,
'fn' => 'category',
],
[
'f' => $mc,
'fn' => 'main_category',
],
[
'f' => $mcMc,
'fn' => 'main_category_main_category',
],
] as $i => $obj) {
for ($j = 0; $j <= $i; $j++) {
if (!empty($obj['f'])) {
if (!isset($BreadCrumbs[$i]['label'])) {
$BreadCrumbs[$i]['label'] = $category["{$obj['fn']}_name"];
}
if (!isset($BreadCrumbs[$j]['href'])) {
$BreadCrumbs[$j]['href'] = "/{$category["{$obj['fn']}_webname"]}";
} else {
$BreadCrumbs[$j]['href'] = "/{$category["{$obj['fn']}_webname"]}{$BreadCrumbs[$j]['href']}";
}
}
}
}
//
return $BreadCrumbs;
}
/**
* Moved from front/app/html/application/modules/default/controllers/commons/getcategorydata.php#getPagerData
*
* @param $main_category_name
* @param $category_name
* @param $display_count
* @param $item_count
* @param $item_num_from
* @param $item_num_to
* @param $disp
* @param $type
* @param false $is_sp
* @return array
*/
public function getPagerData($main_category_name, $category_name, $display_count, $item_count, $item_num_from, $item_num_to, $disp, $type, $is_sp = false)
{
$pager_data = array(
'prev_link' => false,
'pager' => array(),
'next_link' => false
);
$page_list = array();
$ellipses_pager = array(
'num' => '...',
'link' => false,
'is_ellipses' => true
);
$base_link = $category_name === 'allItem' ? $main_category_name : "{$main_category_name}/{$category_name}";
if ($item_num_from > 1) {
$no = max(0, ($item_num_from - $display_count) - 1);
$pager_data['prev_link'] = "/{$base_link}/?no={$no}&disp={$disp}&type={$type}";
}
if (($item_num_from + $display_count) <= $item_count) {
$no = $item_num_from + $display_count - 1;
$pager_data['next_link'] = "/{$base_link}/?no={$no}&disp={$disp}&type={$type}";
}
$current_idx = 0;
$idx = 0;
$current_num = (int)floor($item_num_to / $display_count);
// 最後のページだったら+1する
if ($item_num_to % $display_count) $current_num += 1;
for ($i = 1; $i <= $item_count; $i += $display_count) {
$no = ($i - 1);
$num = intval($i / $display_count) + 1;
$is_current = $num === $current_num;
$pager = array(
'num' => $num,
'link' => "/{$base_link}/?no={$no}&disp={$disp}&type={$type}",
'is_current' => $is_current,
'idx' => $idx
);
$page_list[] = $pager;
if ($is_current) $current_idx = $idx;
$idx++;
}
$CATEGORY_LIST_PAGING_COUNT_IN_PAGER = $is_sp ? self::CATEGORY_LIST_PAGING_COUNT_IN_PAGER_SP : self::CATEGORY_LIST_PAGING_COUNT_IN_PAGER_PC;
// ページング数がCATEGORY_LIST_PAGING_COUNT_IN_PAGER以下だったらページャーの調整不要
if (count($page_list) <= $CATEGORY_LIST_PAGING_COUNT_IN_PAGER) {
$pager_data['pager'] = $page_list;
return $pager_data;
}
/**
* ページャーから不要なページを除去する。
* ページャーが連続する数はCATEGORY_LIST_PAGING_COUNT_IN_PAGERで定義。
* 仕様は以下(例は10ページのケース)。
*
* 先頭ページ選択中
* [1] [2] [3] [4] [5]...[10]
*
* 中間ページ選択中
* [1]...[3] [4] [5] [6] [7]...[10]
*
* 最終ページ選択中
* [1]...[6] [7] [8] [9] [10]
*/
// 最初と最後のページ
$page_count = count($page_list);
$first = $page_list[0];
$last = $page_list[$page_count - 1];
// CATEGORY_LIST_PAGING_COUNT_IN_PAGERで現在のページからの表示範囲を取得
$dist_num = floor($CATEGORY_LIST_PAGING_COUNT_IN_PAGER / 2);
$offset = $current_idx - $dist_num;
if ($offset < 0) {
$offset = 0;
} else if (($offset + $CATEGORY_LIST_PAGING_COUNT_IN_PAGER) > $page_count) {
$offset -= ($offset + $CATEGORY_LIST_PAGING_COUNT_IN_PAGER) - $page_count;
}
$pager_data['pager'] = array_slice($page_list, $offset, $CATEGORY_LIST_PAGING_COUNT_IN_PAGER);
// ページャの最初と最後と、$first、$lastを比較
$pager_first = $pager_data['pager'][0];
$pager_last = $pager_data['pager'][count($pager_data['pager']) - 1];
// first
if ($first['num'] !== $pager_first['num']) {
$pager_data['pager'] = array_merge(array($first, $ellipses_pager), $pager_data['pager']);
}
// last
if ($last['num'] !== $pager_last['num']) {
$pager_data['pager'] = array_merge($pager_data['pager'], array($ellipses_pager, $last));
}
return $pager_data;
}
function getCategoryImageUrl($categoryId)
{
return 'https://' . $this->lmHelper->getConfig('CFIMG_DOMAIN') . '/category_images/' . $categoryId . '.webp';
}
function getGoodsImageUrlByCatetoryId($categoryId)
{
$goods = (new SqlService())
->Select('goods_id')
->Table('goods_table')
->Join('goods_category_table', 'T1.gc_goods = T.goods_id', 'INNER')
->Join('category_table', 'T2.category_id = T1.gc_category', 'INNER')
->Join('goods_price_table', 'T3.gp_goods = T.goods_id')
->Join('rank_table', 'T4.rank_goods = T.goods_id')
->Set('T.goods_status', 1)
->Set('T.goods_ddate', null)
->Set('T2.category_id', $categoryId)
->GroupBy('T.goods_id, T4.rank_money')
->OrderBy('ISNULL(T4.rank_money), T4.rank_money DESC, MIN(T3.gp_price2)')
->Limit(1)
->Find();
if (empty($goods['goods_id'])) {
return null;
}
return 'https://' . $this->lmHelper->getConfig('CFIMG_DOMAIN') . '/goods.img/'
. substr($goods['goods_id'], -1) . '/' . $goods['goods_id'] . "/main_bsum.jpg";
}
/**
* Get Main category group data by id
*
* @param $mainCategoryId
* @return array|null
*/
public function getMainCategoryGroupById($mainCategoryId)
{
$mainCategories = (new SqlService())
->Sql("SELECT main_category_webname AS mc, category_webname AS ct, main_category_id, category_id
, category_group_noshowdb, category_group_only_main
, main_category_name, category_name
FROM main_category_table
INNER JOIN category_group_table ON category_group_id = main_category_group
LEFT JOIN category_table ON main_category_id = category_main_category
WHERE main_category_webname = :mainCategoryId")
->Param('mainCategoryId', $mainCategoryId)
->Limit(1)
->Fetch();
if (empty($mainCategories)) {
return null;
}
return $mainCategories;
}
/**
* Get Main category by id
*
* @param $mainCategoryId
* @return array|null
*/
public function getMainCategory($mainCategoryId)
{
$mainCategories = (new SqlService())
->Sql("SELECT category_group_noshowdb,main_category_group FROM main_category_table
LEFT JOIN category_group_table ON category_group_id = main_category_group
WHERE main_category_id = :main_category_id")
->Param('main_category_id', $mainCategoryId)
->Limit(1)
->Fetch();
if (empty($mainCategories)) {
return null;
}
return $mainCategories;
}
}