CS-CART表示期限付きメンテナンス表示アドオン

らら
らら

はじめに

過去に作成したものを現在のバージョンcscart_v4.11系で移行したら仕様が変わっていたので変更しました。

仕様

cscart_v4.11系

表示メッセージの変更もあると便利なので対応する

開始日、終了日、表示メッセージ、表示・非表示の機能を付ける

store_closed.htmlはそのまま使う。

管理画面

管理画面

メンテナンス表示

メンテナンス表示

作成ファイル一式

app\addons\ccv_shopclose\controllers\addon.xml

app\addons\ccv_shopclose\schemas\menu\menu.post.php

app\addons\ccv_shopclose\controllers\frontend\init.pre.php

app\addons\ccv_shopclose\controllers\backend\closeshop.php

管理画面用テンプレート

design\backend\templates\addons\ccv_shopclose\views\closeshop\update.tpl

別途、既存のstore_closed.html

各コード

アドオン定義ファイルaddon.xml

言語ファイル面倒なのでここで定義、英語は今回は除外してます。


<?xml version="1.0"?>
<addon scheme="3.0">
    <id>ccv_shopclose</id>
    <name>期限付サイトメンテナンス</name>
    <description>期限付サイトメンテナンス</description>
    <version>0.2</version>
    <priority>1005</priority>
    <position>0</position>
    <default_language>ja</default_language>
    <status>active</status>
    <language_variables>
        <item id="open_store" lang="ja">メンテナンスOFF</item>
        <item id="preview_store" lang="ja">プレビュー</item>
        <item id="priod_close" lang="ja">期限付きメンテナンス</item>
        <item id="closeshop.eidt" lang="ja">期限付きショップ閉鎖</item>
        <item id="maintance" lang="ja">メンテナンス</item>
        <item id="close_sdate" lang="ja">開始日時</item>
        <item id="close_edate" lang="ja">終了日時</item>
    </language_variables>
    <queries>
        <item editions="MULTIVENDOR,ULTIMATE">REPLACE INTO ?:privileges (privilege, is_default, section_id) VALUES ('manage_closeshop', 'Y', 'design');</item>
        <item editions="MULTIVENDOR,ULTIMATE">REPLACE INTO ?:usergroup_privileges (usergroup_id, privilege) VALUES ('4', 'manage_closeshop');</item>
        <item>DROP TABLE IF EXISTS ?:closeshop;</item>
        <item>
            CREATE TABLE `?:closeshop` (
                `closeshop_id` mediumint(8) unsigned NOT NULL auto_increment,
                `company_id` int(11) unsigned NOT NULL default '0',
                `status` char(1) NOT NULL default 'A',
                `closeshop_sdate` datetime ,
                `closeshop_edate` datetime ,
                `description` mediumtext NOT NULL,
                PRIMARY KEY  (`closeshop_id`),
                KEY `status` (`status`)
            ) Engine=MyISAM DEFAULT CHARSET UTF8;
        </item>
        <item editions="MULTIVENDOR,ULTIMATE">REPLACE INTO  ?:privileges (privilege, is_default, section_id) VALUES ('ccv_shopclose', 'Y', 'addons');</item>
        <item for="uninstall" editions="MULTIVENDOR,ULTIMATE">DELETE FROM ?:privileges WHERE privilege = 'manage_closeshop'</item>
        <item for="uninstall" editions="MULTIVENDOR,ULTIMATE">DELETE FROM ?:usergroup_privileges WHERE privilege = 'manage_closeshop'</item>
    </queries>
</addon>

管理画面メニューに追加するmenu.post.php

キャッシュクリアのリンクもついています。4.3ぐらいではstorage.open_storeとstorage.close_storeで既存のメンテナンスがGETで出来ていたが、POSTになったのでやめた。


<?php
use \Tygh\Registry;
$schema['central']['maintance'] = array(
            'items' => array(
                'clear_cache' => array(
                    'href' => 'storage.clear_cache?redirect_url=%CURRENT_URL',
                    'position' => 100,
                ),
                'clear_thumbnails' => array(
                    'href' => 'storage.clear_thumbnails?redirect_url=%CURRENT_URL',
                    'position' => 200,
                ),
                'priod_close' => array(
                    'href' => 'closeshop.update',
                    'position' => 300,
                ),
                'preview_store' => array(
                    'href' => Registry::get('config.current_location') . '/' . Registry::get('config.customer_index') . '?store_access_key=' . Registry::get('settings.General.store_access_key'),
                    'position' => 500,
                    'attrs' => array(
                        'class' => 'menu-target',
                    ),
                ),
            ),
            'position' => 1000,
);
return $schema;

ユーザー画面の表示する部分init.pre.php

日付判定でcloseshop_sdateが空だったら、開始・終了関係なく表示などカスタマイズ可能?

標準の機能が走る前に.preで


<?php
use Tygh\Development;
use Tygh\Registry;
use Tygh\Session;
use Tygh\BlockManager\Location;
use Tygh\BlockManager\Layout;
use Tygh\Enum\StorefrontStatuses;
if (!defined('BOOTSTRAP')) { die('Access denied'); }
//
// 店舗閉店処理
//
$storefront = Tygh::$app['storefront'];
$company_id=fn_get_runtime_company_id();
$closeshop = db_get_row('SELECT * FROM ?:closeshop WHERE company_id = ?i', $company_id);
if(!empty($closeshop)){
    if ($closeshop['status'] == 'A') {
        if (time() >= strtotime($closeshop['closeshop_sdate']) && time() < strtotime($closeshop['closeshop_edate'])) {
            if (empty($closeshop['description'])) {
                $closeshop['description']=__('text_store_closed');
            }
            if (!empty($_REQUEST['store_access_key'])) {
                Tygh::$app['session']['store_access_key'] = $_GET['store_access_key'];
            }
            if (!fn_check_permissions(Registry::get('runtime.controller'), Registry::get('runtime.mode'), 'trusted_controllers')) {
                if (empty(Tygh::$app['session']['store_access_key']) || Tygh::$app['session']['store_access_key'] !== $storefront->access_key) {
                    if (defined('AJAX_REQUEST')) {
                        fn_set_notification('E', __('notice'), __('text_store_closed'));
                        exit;
                    }
                    $placeholders = array(
                        '[title]' => __('store_closed'),
                        '[banner]' =>  __('store_closed_banner'),
                        '[message]' => $closeshop['description']
                    );
                    Development::showStub($placeholders);
                }
            }
        }
    }
}

管理画面登録のコードcloseshop.php


<?php
use Tygh\Registry;
if (!defined('BOOTSTRAP')) { die('Access denied'); }
    fn_trusted_vars (
        'description'
    );
fn_trusted_vars('close_data');        #エンコードをしないように
$company_id = Registry::get('runtime.company_id');
$company_id=fn_get_runtime_company_id();
if ($_SERVER['REQUEST_METHOD']    == 'POST') {
    $suffix = '';
    if ($mode == 'update') {
        $_REQUEST['close_data']['closeshop_sdate'] = sprintf("%s %02d:%02d",$_REQUEST['close_data']['closeshop_sdate'],$_REQUEST['close_data']['closeshop_stime']['Hour'],$_REQUEST['close_data']['closeshop_stime']['Minute']);
        $_REQUEST['close_data']['closeshop_edate'] = sprintf("%s %02d:%02d",$_REQUEST['close_data']['closeshop_edate'],$_REQUEST['close_data']['closeshop_etime']['Hour'],$_REQUEST['close_data']['closeshop_etime']['Minute']);
        $_data = array(
            'closeshop_id' => $company_id,
            'company_id' => $company_id,
            'status' => $_REQUEST['close_data']['status'],
            'closeshop_sdate' => $_REQUEST['close_data']['closeshop_sdate'],
            'closeshop_edate' => $_REQUEST['close_data']['closeshop_edate'],
            'description' => $_REQUEST['close_data']['description'],
        );
        db_query("REPLACE INTO ?:closeshop ?e", $_data);
        $suffix = ".update";
    }
    return array(CONTROLLER_STATUS_OK, "closeshop$suffix");
}
if ($mode == 'update') {
    $closeshop = db_get_row('SELECT * FROM ?:closeshop WHERE company_id = ?i', $company_id);
    list($sdate, $stime)= explode(' ', $closeshop['closeshop_sdate']);
    list($edate, $etime)= explode(' ', $closeshop['closeshop_edate']);
    list($shour,$smin,$sss)= explode(':', $stime);
    list($ehour,$emin,$ess)= explode(':', $etime);
    $sdate    = preg_replace('/-/',"/",$sdate);
    $edate    = preg_replace('/-/',"/",$edate);
    $closeshop['closeshop_sdate']    = $sdate;
    $closeshop['closeshop_edate']    = $edate;
    $closeshop['closeshop_stime']['Hour']    = $shour;
    $closeshop['closeshop_stime']['Minute']    = $smin;
    $closeshop['def_stime']                    = sprintf("%02d:%02d",$shour,$smin);
    $closeshop['closeshop_etime']['Hour']    = $ehour;
    $closeshop['closeshop_etime']['Minute']    = $emin;
    $closeshop['def_etime']                    = sprintf("%02d:%02d",$ehour,$emin);
    Registry::set('navigation.tabs', array (
        'general' => array (
            'title' => __('general'),
            'js' => true
        ),
    ));
    Registry::get('view')->assign('closeshop', $closeshop);
}

管理画面登録のテンプレートupdate.tpl


{assign var="allow_save" value=$closeshop|fn_allow_save_object:"closeshop"}
{** closeshop section **}
{capture name="mainbox"}
<form action="{""|fn_url}" method="post" class="form-horizontal form-edit  {if !$allow_save} cm-hide-inputs{/if}" name="closeshop_form" enctype="multipart/form-data">
<input type="hidden" class="cm-no-hide-input" name="fake" value="1" />
{capture name="tabsbox"}
<div id="content_general">
    <div class="control-group">
        <label class="control-label" for="elm_close_sdate">{__("close_sdate")}</label>
        <div class="controls">
        {include file="common/calendar.tpl" date_id="elm_close_sdate" date_name="close_data[closeshop_sdate]" date_val=$closeshop.closeshop_sdate|default:$smarty.const.TIME start_year=$settings.Company.company_start_year}
    時間
        {html_select_time prefix="" field_array="close_data[closeshop_stime]" time=$closeshop.def_stime  display_seconds=false minute_interval="5" all_extra='style="width:100px"' field_separator = ":"}
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="elm_close_edate">{__("close_edate")}</label>
        <div class="controls">
        {include file="common/calendar.tpl" date_id="elm_close_edate" date_name="close_data[closeshop_edate]" date_val=$closeshop.closeshop_edate|default:$smarty.const.TIME start_year=$settings.Company.company_start_year}
    時間
        {html_select_time prefix="" field_array="close_data[closeshop_etime]" time=$closeshop.def_etime display_seconds=false minute_interval="5" all_extra='style="width:100px"' field_separator = ":"}
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="elm_news_description">{__("description")}</label>
        <div class="controls">
            <textarea id="elm_news_description" name="close_data[description]" cols="35" rows="8" class="cm-wysiwyg input-large">{$closeshop.description}</textarea>
        </div>
    </div>
    {include file="common/select_status.tpl" input_name="close_data[status]" id="elm_banner_status" obj_id=$id obj=$closeshop hidden=true}
</div>
{/capture}
{include file="common/tabsbox.tpl" content=$smarty.capture.tabsbox active_tab=$smarty.request.selected_section track=true}
{capture name="buttons"}
    {if !$id}
        {include file="buttons/save_cancel.tpl" but_role="submit-link" but_target_form="closeshop_form" but_name="dispatch[closeshop.update]" hide_second_button=ture}
    {else}
        {if "ULTIMATE"|fn_allowed_for && !$allow_save}
            {assign var="hide_first_button" value=true}
            {assign var="hide_second_button" value=true}
        {/if}
        {include file="buttons/save_cancel.tpl" but_name="dispatch[closeshop.update]" but_role="submit-link" but_target_form="closeshop_form" hide_first_button=$hide_first_button hide_second_button=$hide_second_button}
    {/if}
{/capture}
</form>
{/capture}
{assign var="title" value=__("closeshop.eidt")}
{include file="common/mainbox.tpl" title=$title content=$smarty.capture.mainbox buttons=$smarty.capture.buttons select_languages=true}
{** closeshop section **}

さいごに

このプラグインを検証するのに、cscart_v4.11を入れた、試用期間きれてますた。。。めんどい・・

ご自由にどうぞ!サポートはしていませんよ。

プラグイン名、テーブル名は変更しておきましょう!

では。

関連記事