EC-CUBEでサイトマップページの追加

EC-CUBEの2系には、xmlサイトマップはありますが、HTML版のサイトマップページがありません。

そこで、カテゴリー一覧を流用して、サイトマップページを作ります。

HTMLディレクトリのファイルを作成

/html/sitemap/index.phpの作成。

<?php
// {{{ requires
require_once("../require.php");
require_once(CLASS_EX_PATH . "page_extends/sitemap/LC_Page_Sitemap_Ex.php");
// }}}
// {{{ generate page

$objPage = new LC_Page_Sitemap_Ex();
register_shutdown_function(array($objPage, "destroy"));
$objPage->init();
$objPage->process();
?>

拡張クラスのファイルを作成

/data/class_extends/page_extends/sitemap/LC_Page_Sitemap_Ex.phpの作成。

<?php

// {{{ requires
require_once(CLASS_PATH . "pages/sitemap/LC_Page_Sitemap.php");

class LC_Page_Sitemap_Ex extends LC_Page_Sitemap {

    // }}}
    // {{{ functions

    /**
     * Page を初期化する.
     *
     * @return void
     */
    function init() {
        parent::init();
    }

    /**
     * Page のプロセス.
     *
     * @return void
     */
    function process() {
        parent::process();
    }

    /**
     * デストラクタ.
     *
     * @return void
     */
    function destroy() {
        parent::destroy();
    }
}
?>

クラスファイルを作成

/data/class/pages/sitemap/LC_Page_Sitemap.phpの作成

<?php

// {{{ requires
require_once(CLASS_PATH . "pages/LC_Page.php");

class LC_Page_Sitemap extends LC_Page {

    // }}}
    // {{{ functions

    /**
     * Page を初期化する.
     *
     * @return void
     */
    function init() {
        parent::init();
        $this->tpl_mainpage = 'sitemap/index.tpl';
        $this->tpl_page_category = 'sitemap';
        $this->tpl_title = "サイトマップ";
    }

    /**
     * Page のプロセス.
     *
     * @return void
     */
    function process() {
        $objView = new SC_SiteView();
        $objDb = new SC_Helper_DB_Ex();

        // 選択中のカテゴリIDを判定する
        $arrCategory_id = $objDb->sfGetCategoryId($_GET['product_id'], $_GET['category_id']);

        // 選択中のカテゴリID
        $this->tpl_category_id = empty($arrCategory_id) ? array(0) : $arrCategory_id;;
        $this->lfGetCatTree($this->tpl_category_id, true, $this);

        // レイアウトデザインを取得
        $layout = new SC_Helper_PageLayout_Ex();
        $layout->sfGetPageLayout($this, false, DEF_LAYOUT);

        $objView->assignobj($this);
        $objView->display(SITE_FRAME);
    }

    /**
     * モバイルページを初期化する.
     *
     * @return void
     */
    function mobileInit() {
        $this->tpl_mainpage = MOBILE_TEMPLATE_DIR . "sitemap/index.tpl";
    }

    /**
     * Page のプロセス(モバイル).
     *
     * @return void
     */
    function mobileProcess() {
        $objSubView = new SC_MobileView();

       $this->lfGetMainCat(true, $this);

        $objSubView->assignobj($this);
        $objSubView->display($this->tpl_mainpage);
    }

    /**
     * デストラクタ.
     *
     * @return void
     */
    function destroy() {
        parent::destroy();
    }

    // カテゴリツリーの取得
    function lfGetCatTree($arrParent_category_id, $count_check = false) {
        $objQuery = new SC_Query();
        $objDb = new SC_Helper_DB_Ex();
        $col = "*";
        $from = "dtb_category left join dtb_category_total_count using (category_id)";
        // 登録商品数のチェック
        if($count_check) {
            $where = "del_flg = 0 AND product_count > 0";
        } else {
            $where = "del_flg = 0";
        }
        $objQuery->setoption("ORDER BY rank DESC");
        $arrRet = $objQuery->select($col, $from, $where);

        foreach ($arrParent_category_id as $category_id) {
            $arrParentID = $objDb->sfGetParents($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
            $arrBrothersID = SC_Utils_Ex::sfGetBrothersArray($arrRet, 'parent_category_id', 'category_id', $arrParentID);
            $arrChildrenID = SC_Utils_Ex::sfGetUnderChildrenArray($arrRet, 'parent_category_id', 'category_id', $category_id);

            $this->root_parent_id[] = $arrParentID[0];

            $arrDispID = array_merge($arrBrothersID, $arrChildrenID);

            foreach($arrRet as $key => $array) {
                foreach($arrDispID as $val) {
                    if($array['category_id'] == $val) {
                        $arrRet[$key]['display'] = 1;
                        break;
                    }
                }
            }
        }

        $this->arrTree = $arrRet;
    }

    // メインカテゴリーの取得
    function lfGetMainCat($count_check = false, &$objSubPage) {
        $objQuery = new SC_Query();
        $col = "*";
        $from = "dtb_category left join dtb_category_total_count using (category_id)";
        // メインカテゴリーとその直下のカテゴリーを取得する。
        $where = 'level <= 2 AND del_flg = 0';
        // 登録商品数のチェック
        if($count_check) {
            $where .= " AND product_count > 0";
        }
        $objQuery->setoption("ORDER BY rank DESC");
        $arrRet = $objQuery->select($col, $from, $where);

        // メインカテゴリーを抽出する。
        $arrMainCat = array();
        foreach ($arrRet as $cat) {
            if ($cat['level'] != 1) {
                continue;
            }

            // 子カテゴリーを持つかどうかを調べる。
            $arrChildrenID = SC_Utils_Ex::sfGetUnderChildrenArray($arrRet, 'parent_category_id', 'category_id', $cat['category_id']);
            $cat['has_children'] = count($arrChildrenID) > 0;
            $arrMainCat[] = $cat;
        }

        $objSubPage->arrCat = $arrMainCat;
        return $objSubPage;
    }
}
?>

テンプレートファイルを作成

/Smarty/templates/default/sitemap/index.tplを作成。

<!--▼CONTENTS-->
<div id="undercolumn">
<div id="listtitle">
<h2> サイトマップ</h2>
</div>
<div>
<ul>
<li><a href="<!--{$smarty.const.SITE_URL}-->">トップページ</a></li>
<div id="categoryarea">
  <ul id="categorytree">
  <!--{assign var=preLev value=1}-->
  <!--{assign var=firstdone value=0}-->
  <!--{section name=cnt loop=$arrTree}-->
    <!--{* 表示フラグがTRUEなら表示 *}-->
    <!--{if $arrTree[cnt].display == 1|| $arrTre[cnt].level <=1}-->
    <!--{assign var=level value=`$arrTree[cnt].level`}-->
    <!--{assign var=levdiff value=`$level-$preLev`}-->
      <!--{if $levdiff > 0}-->
          <ul>
      <!--{elseif $levdiff == 0 && $firstdone == 1}-->
          </li>
      <!--{elseif $levdiff < 0}-->
        <!--{section name=d loop=`$levdiff*-1`}-->
            </li>
          </ul>
        <!--{/section}-->
        </li>
      <!--{/if}-->
    <li class="level<!--{$level}--><!--{if in_array($arrTree[cnt].category_id, $tpl_category_id) }--> onmark<!--{/if}-->"><a href="<!--{$smarty.const.URL_DIR}-->products/list.php?category_id=<!--{$arrTree[cnt].category_id}-->"<!--{if in_array($arrTree[cnt].category_id, $tpl_category_id) }--> class="onlink"<!--{/if}-->><!--{$arrTree[cnt].category_name|escape}-->(<!--{$arrTree[cnt].product_count|default:0}-->)</a>
    <!--{if $firstdone == 0}--><!--{assign var=firstdone value=1}--><!--{/if}-->
    <!--{assign var=preLev value=`$level`}-->
    <!--{/if}-->
    <!--{* セクションの最後に閉じタグを追加 *}-->
    <!--{if $smarty.section.cnt.last}-->
      <!--{if $preLev-1 > 0 }-->
        <!--{section name=d loop=`$preLev-1`}-->
          </li>
        </ul>
        <!--{/section}-->
        </li>
      <!--{else}-->
        </li>
      <!--{/if}-->
    <!--{/if}-->
  <!--{/section}-->
  </ul>
</div>
<li><a href="<!--{$smarty.const.URL_DIR}-->abouts/index.php">当サイトについて</a></li>
<li><a href="<!--{$smarty.const.SSL_URL}-->contact/index.php">お問い合わせ</a></li>
<li><a href="<!--{$smarty.const.URL_DIR}-->order/index.php">特定商取引に関する法律</a></li>
<li><a href="<!--{$smarty.const.URL_DIR}-->sitemap/index.php">サイトマップ</a></li>
</ul>
</div>
</div>
<!--▲CONTENTS-->
This entry was posted in EC-CUBE and tagged . Bookmark the permalink. Comments are closed, but you can leave a trackback: Trackback URL.

One Trackback

  1. By 雨ときどき晴れ on 2010年8月20日 at 6:42 PM

    [ソフトウェア]EC-CUBE カスタマイズで参考になったサイトのまとめ(1)

    EC-CUBEでECサイトを作っていると、カスタマイズしたい部分がいろいろとでてきます。 今回は、カスタマイズする上で参考になったリンクと関連するファイルをまとめてみました。 サイト…