<?php 
 
namespace Lm\Engine\EC\Entity; 
 
use Lm\Entity\Category; 
use Lm\Entity\Site; 
use Lm\Service\Cache\CacheService; 
use Lm\Service\Db\SqlService; 
 
class CategoryExtended extends Category 
{ 
 
    /** 
     * @var bool 
     */ 
    protected $isMobile; 
 
    /** 
     * @var int 
     */ 
    protected $siteType; 
 
    /** 
     * @return bool 
     */ 
    public function isMobile() 
    { 
        return $this->isMobile; 
    } 
 
    /** 
     * @return int 
     */ 
    public function getSiteType() 
    { 
        return (int)$this->siteType; 
    } 
 
    /** 
     * @return bool 
     */ 
    public function isMainCategory() 
    { 
        return !empty($this->getMainCategoryId()) && empty($this->getCategoryId()); 
    } 
 
    /** 
     * @return bool 
     */ 
    public function isSubCategory() 
    { 
        return !empty($this->getMainCategoryId()) && !empty($this->getCategoryId()); 
    } 
 
    /** 
     * @return bool 
     */ 
    public function hasMainCategoryMainCategory() 
    { 
        return !empty($this->getMainCategoryMainCategory()); 
    } 
 
    /** 
     * @return bool 
     */ 
    public function isShowSmallFooter() 
    { 
        return $this->isSubCategory() || $this->getMainCategorySmallFooterFlg() === 1; 
    } 
 
    /** 
     * @return bool 
     */ 
    public function hasMainCategories() 
    { 
        return $this->getMainCategoryHasMainCategories() === 1; 
    } 
 
    /** 
     * @return bool 
     */ 
    public function isNoShowDb() 
    { 
        return (int)$this->data['category_group_noshowdb'] === 1; 
    } 
 
    /** 
     * @return bool 
     */ 
    public function isOnlyMain() 
    { 
        return (int)$this->data['category_group_only_main'] === 1; 
    } 
 
    /** 
     * @return bool 
     */ 
    public function isItemSeries() 
    { 
        return (int)$this->data['category_group_is_item_series'] === 1; 
    } 
 
    public function getCategory($mc, $ct = null, $mcMc = null) 
    { 
        return $this->getCached(function () use ($mc, $ct, $mcMc) { 
            // 
            $query = (new SqlService()) 
                ->Select('T.*, T1.*, T2.*') 
                ->Table('main_category_table') 
            ; 
            if (!empty($mcMc)) { 
                $query->Set('T.main_category_main_category_webname', $mcMc); 
            } 
            if (!empty($mc)) { 
                $query->Set('T.main_category_webname', $mc); 
            } 
            if (!empty($ct)) { 
                $query 
                    ->Join('category_table', 'T.main_category_id = T1.category_main_category') 
                    ->Set('T1.category_webname', $ct) 
                ; 
            } else { 
                $query 
                    ->Join('category_table', 'T1.category_id = :ct') 
                    ->Param('ct', $ct) 
                ; 
            } 
            // 
            $query->Join('category_group_table', 'T2.category_group_id = T.main_category_group'); 
 
            // 
            $result = $query->Find(); 
            // 
            return $result; 
        }); 
    } 
 
    /** 
     * @param string $mc 
     * @param string $ct 
     * @param string $mcMc 
     * @param bool $isMobile 
     * @param int $siteType 
     * @throws \Exception 
     */ 
    public function __construct($mc, $ct = null, $mcMc = null, $isMobile = false, $siteType = Site::TYPE_UT) 
    { 
        /** 
         * implement as LmCacheTrait. 
         */ 
        $this->__constructLmCacheTrait(new CacheService()); 
        /** 
         * implement as Entity; 
         */ 
        // 
        $this->isMobile = $isMobile; 
        // 
        if (Site::TYPE_LIST[$siteType] === null) { 
            // 
            throw new \Exception('指定されたサイトが見つかりません'); 
        } 
        // 
        $this->siteType = $siteType; 
        // 
        if ($data = $this->getCategory($mc, $ct, $mcMc)) { 
            // 
            $this->data = $data; 
            // 
            if (!empty($ct) && $this->getCategoryStatus() !== Category::STATUS_AVAILABLE) { 
                // 
                throw new \Exception('非表示設定のカテゴリページが指定されました'); 
            } else if (!empty($mc) && $this->getMainCategoryStatus() !== Category::STATUS_AVAILABLE) { 
                // 
                throw new \Exception('非表示設定のカテゴリページが指定されました'); 
            } 
        } else { 
            // 
            throw new \Exception('指定されたカテゴリページが見つかりません'); 
        } 
    } 
 
}