開発メモ

開発用のメモです。

EC CUBE 時限公開プラグイン

追加カラム

dtb_products

カラム名 カラムタイプ 備考
auto_display_status int not null default 0 時限公開設定
auto_display_start_date timestamp 公開開始日
auto_display_end_date timestamp 公開終了日

追加マスタ

mtb_auto_display_start_date

その他マスターとほぼ同様の構成

<?php
require_once dirname ( __FILE__ ) . "/define.php";

class AutoDisplay extends SC_Plugin_Base {

    // TODO コンストラクタ
    public function __construct(array $arrSelfInfo) {

        parent::__construct ( $arrSelfInfo );

    }

    function install(array $arrPlugin, SC_Plugin_Installer $installer) {

        $objQuery = new SC_Query_Ex ();
        $listTable = $objQuery->listTables ();

        $installer->addColumn ( "dtb_products", "auto_display_status", "int not null default 0" );
        $installer->addColumn ( "dtb_products", "auto_display_start_date", "timestamp" );
        $installer->addColumn ( "dtb_products", "auto_display_end_date", "timestamp" );

        $table = "mtb_auto_display_start_date";
        $installer->sql ( "CREATE TABLE " . $table . " (id INT PRIMARY KEY, name TEXT, rank int)" );
        $installer->insert ( $table, array (
            "id" => 0,
            "name" => "常時公開",
            "rank" => 0
        ) );
        $installer->insert ( $table, array (
            "id" => 1,
            "name" => "時限公開",
            "rank" => 1
        ) );

    }

    function uninstall(array $arrPlugin, SC_Plugin_Installer $installer) {

        $installer->dropColumn ( "dtb_products", "auto_display_status" );
        $installer->dropColumn ( "dtb_products", "auto_display_start_date" );
        $installer->dropColumn ( "dtb_products", "auto_display_end_date" );

        $installer->sql ( "DROP TABLE " . $table );

    }

    // TODO [HOOKPOINT] 事前処理
    function preProcess(LC_Page $objPage) {


    }

    // TODO [HOOKPOINT] 事後処理
    function process(LC_Page $objPage) {


    }

    // TODO [HOOKPOINT] テンプレートコンパイル前前処理
    function prefilterTransform(&$source, LC_Page_Ex $objPage, $filename) {

        $objTransform = new SC_Helper_Transform_Ex ( $source );
        if (GC_Utils_Ex::isFrontFunction ()) {
            $device_type_id = SC_Display_Ex::detectDevice ();
            switch ($device_type_id) {
            case DEVICE_TYPE_PC :
                $filename = str_replace ( TEMPLATE_REALDIR, '', $filename );
                self::prefilterTransform_pc ( $source, $objPage, $filename, $objTransform );
                break;

            case DEVICE_TYPE_SMARTPHONE :
                $filename = str_replace ( SMARTPHONE_TEMPLATE_REALDIR, '', $filename );
                self::prefilterTransform_sp ( $source, $objPage, $filename, $objTransform );
                break;

            case DEVICE_TYPE_MOBILE :
                $filename = str_replace ( MOBILE_TEMPLATE_REALDIR, '', $filename );
                self::prefilterTransform_mb ( $source, $objPage, $filename, $objTransform );
                break;

            default :
                break;
            }
        } else {
            $filename = str_replace ( TEMPLATE_ADMIN_REALDIR, '', $filename );
            self::prefilterTransform_ad ( $source, $objPage, $filename, $objTransform );
        }

        $source = $objTransform->getHTML ();

    }

    // TODO [HOOKPOINT] テンプレートコンパイル前前処理 (PC用)
    function prefilterTransform_pc(&$source, LC_Page_Ex $objPage, $filename, SC_Helper_Transform &$objTransform) {


    }

    // TODO [HOOKPOINT] テンプレートコンパイル前前処理 (スマホ用)
    function prefilterTransform_sp(&$source, LC_Page_Ex $objPage, $filename, SC_Helper_Transform &$objTransform) {


    }

    // TODO [HOOKPOINT] テンプレートコンパイル前前処理 (携帯電話用)
    function prefilterTransform_mb(&$source, LC_Page_Ex $objPage, $filename, SC_Helper_Transform &$objTransform) {


    }

    // TODO [HOOKPOINT] テンプレートコンパイル前前処理 (管理画面)
    function prefilterTransform_ad(&$source, LC_Page_Ex $objPage, $filename, SC_Helper_Transform &$objTransform) {

        switch ($filename) {
        case "products/confirm.tpl" :
            $html = <<<'__EOL___'
<tr>
    <th>時限公開設定</th>
    <td><!--{$arrAutoDispStatus[$arrForm.auto_display_status]}--></td>
</tr>
<tr>
    <th>公開開始日時</th>
    <td>
        <!--{if $arrForm.auto_display_start_date_year}-->
        <!--{$arrForm.auto_display_start_date_year}-->年
        <!--{$arrForm.auto_display_start_date_month}-->月
        <!--{$arrForm.auto_display_start_date_day}-->日
        <!--{$arrForm.auto_display_start_date_hour}-->時
        <!--{$arrForm.auto_display_start_date_min}-->分
        <!--{else}-->(未設定)<!--{/if}-->
    </td>
</tr>
<tr>
    <th>公開終了日時</th>
    <td>
        <!--{if $arrForm.auto_display_end_date_year}-->
        <!--{$arrForm.auto_display_end_date_year}-->年
        <!--{$arrForm.auto_display_end_date_month}-->月
        <!--{$arrForm.auto_display_end_date_day}-->日
        <!--{$arrForm.auto_display_end_date_hour}-->時
        <!--{$arrForm.auto_display_end_date_min}-->分
        <!--{else}-->(未設定)<!--{/if}-->
    </td>
</tr>

__EOL___;

            $objTransform->select ( "table", 0 )->find ( "tr", 2 )->insertAfter ( $html );

            break;

        case "products/product.tpl" :
            $html = <<<'__EOL___'
<tr>
    <th>時限公開設定<span class='attention'>*</span></th>
    <td style="<!--{$arrErr.auto_display_status|sfGetErrorColor}-->">
        <!--{if $arrErr.auto_display_status}-->
        <div class="attention"><!--{$arrErr.auto_display_status}--></div>
        <!--{/if}-->
        <!--{html_radios options=$arrAutoDispStatus name="auto_display_status" selected=$arrForm.auto_display_status}-->
    </td>
</tr>
<tr>
    <th>公開開始日時</th>
    <td style="<!--{$arrErr.auto_display_start_date_year|sfGetErrorColor}-->">
        <!--{if $arrErr.auto_display_start_date_year}-->
        <div class="attention"><!--{$arrErr.auto_display_start_date_year}--></div>
        <!--{/if}-->
        <select name="auto_display_start_date_year" style="<!--{$arrErr.auto_display_start_date_year|sfGetErrorColor}-->">
            <option selected value="">----</option>
            <!--{html_options options=$arrDispYear selected=$arrForm.auto_display_start_date_year}-->
        </select>年
        <select name="auto_display_start_date_month" style="<!--{$arrErr.auto_display_start_date_year|sfGetErrorColor}-->">
            <option selected value="">----</option>
            <!--{html_options options=$arrDispMonth selected=$arrForm.auto_display_start_date_month}-->
        </select>月
        <select name="auto_display_start_date_day" style="<!--{$arrErr.auto_display_start_date_year|sfGetErrorColor}-->">
            <option selected value="">----</option>
            <!--{html_options options=$arrDispDay selected=$arrForm.auto_display_start_date_day}-->
        </select>日
        <select name="auto_display_start_date_hour" style="<!--{$arrErr.auto_display_start_date_year|sfGetErrorColor}-->">
            <option selected value="">----</option>
            <!--{html_options options=$arrDispHour selected=$arrForm.auto_display_start_date_hour}-->
        </select>時
        <select name="auto_display_start_date_min" style="<!--{$arrErr.auto_display_start_date_year|sfGetErrorColor}-->">
            <option selected value="">----</option>
            <!--{html_options options=$arrDispMinutes selected=$arrForm.auto_display_start_date_min}-->
        </select>分
        <button type="button" onclick="$(this).parents('td').children('select').val('')">クリア</button>
    </td>
</tr>
<tr>
    <th>公開終了日時</th>
    <td style="<!--{$arrErr.auto_display_end_date_year|sfGetErrorColor}-->">
        <!--{if $arrErr.auto_display_end_date_year}-->
        <div class="attention"><!--{$arrErr.auto_display_end_date_year}--></div>
        <!--{/if}-->
        <select name="auto_display_end_date_year" style="<!--{$arrErr.auto_display_end_date_year|sfGetErrorColor}-->">
            <option selected value="">----</option>
            <!--{html_options options=$arrDispYear selected=$arrForm.auto_display_end_date_year}-->
        </select>年
        <select name="auto_display_end_date_month" style="<!--{$arrErr.auto_display_end_date_year|sfGetErrorColor}-->">
            <option selected value="">----</option>
            <!--{html_options options=$arrDispMonth selected=$arrForm.auto_display_end_date_month}-->
        </select>月
        <select name="auto_display_end_date_day" style="<!--{$arrErr.auto_display_end_date_year|sfGetErrorColor}-->">
            <option selected value="">----</option>
            <!--{html_options options=$arrDispDay selected=$arrForm.auto_display_end_date_day}-->
        </select>日
        <select name="auto_display_end_date_hour" style="<!--{$arrErr.auto_display_end_date_year|sfGetErrorColor}-->">
            <option selected value="">----</option>
            <!--{html_options options=$arrDispHour selected=$arrForm.auto_display_end_date_hour}-->
        </select>時
        <select name="auto_display_end_date_min" style="<!--{$arrErr.auto_display_end_date_year|sfGetErrorColor}-->">
            <option selected value="">----</option>
            <!--{html_options options=$arrDispMinutes selected=$arrForm.auto_display_end_date_min}-->
        </select>分
        <button type="button" onclick="$(this).parents('td').children('select').val('')">クリア</button>
    </td>
</tr>

__EOL___;

            $objTransform->select ( ".form", 0 )->find ( "tr", 4 )->insertAfter ( $html );

            break;
        }

    }

    // TODO [HOOKPOINT] クラス読み込み
    function loadClassFileChange(&$classname, &$classpath) {


    }

    // TODO [HOOKPOINT] フォーム追加処理
    function SC_FormParam_construct($class_name, SC_FormParam $objFormParam) {

        if (strpos ( $class_name, "LC_Page_Admin_Products_Product" ) !== false) {

            $objFormParam->addParam ( "時限公開設定", "auto_display_status", INT_LEN, "a", array (
                "NUM_CHECK",
                "EXIST_CHECK",
                "MAX_LENGTH_CHECK"
            ) );

            switch ($_REQUEST ["mode"]) {
            case "pre_edit" :
            case "copy" :
                $objFormParam->overwriteParam ( "auto_display_status", "arrCheck", array (
                    "NUM_CHECK",
                    "MAX_LENGTH_CHECK"
                ) );

                break;

            default :
                break;
            }

            $objFormParam->addParam ( "公開開始日時(年)", "auto_display_start_date_year", INT_LEN, "a", array (
                "NUM_CHECK",
                "MAX_LENGTH_CHECK"
            ), '', false );
            $objFormParam->addParam ( "公開開始日時(月)", "auto_display_start_date_month", INT_LEN, "a", array (
                "NUM_CHECK",
                "MAX_LENGTH_CHECK"
            ), '', false );
            $objFormParam->addParam ( "公開開始日時(日)", "auto_display_start_date_day", INT_LEN, "a", array (
                "NUM_CHECK",
                "MAX_LENGTH_CHECK"
            ), '', false );
            $objFormParam->addParam ( "公開開始日時(時)", "auto_display_start_date_hour", INT_LEN, "a", array (
                "NUM_CHECK",
                "MAX_LENGTH_CHECK"
            ), '', false );
            $objFormParam->addParam ( "公開開始日時(分)", "auto_display_start_date_min", INT_LEN, "a", array (
                "NUM_CHECK",
                "MAX_LENGTH_CHECK"
            ), '', false );
            $objFormParam->addParam ( "公開開始日時(秒)", "auto_display_start_date_sec", INT_LEN, "a", array (
                "NUM_CHECK",
                "MAX_LENGTH_CHECK"
            ), 0, false );

            $objFormParam->addParam ( "公開終了日時(年)", "auto_display_end_date_year", INT_LEN, "a", array (
                "NUM_CHECK",
                "MAX_LENGTH_CHECK"
            ), '', false );
            $objFormParam->addParam ( "公開終了日時(月)", "auto_display_end_date_month", INT_LEN, "a", array (
                "NUM_CHECK",
                "MAX_LENGTH_CHECK"
            ), '', false );
            $objFormParam->addParam ( "公開終了日時(日)", "auto_display_end_date_day", INT_LEN, "a", array (
                "NUM_CHECK",
                "MAX_LENGTH_CHECK"
            ), '', false );
            $objFormParam->addParam ( "公開終了日時(時)", "auto_display_end_date_hour", INT_LEN, "a", array (
                "NUM_CHECK",
                "MAX_LENGTH_CHECK"
            ), '', false );
            $objFormParam->addParam ( "公開終了日時(分)", "auto_display_end_date_min", INT_LEN, "a", array (
                "NUM_CHECK",
                "MAX_LENGTH_CHECK"
            ), '', false );
            $objFormParam->addParam ( "公開終了日時(秒)", "auto_display_end_date_sec", INT_LEN, "a", array (
                "NUM_CHECK",
                "MAX_LENGTH_CHECK"
            ), 0, false );
        }

    }

    function LC_Page_Products_List_action_before(LC_Page_Products_List $objPage) {

        self::LC_Page_Products_Common_action_before ( $objPage );

    }

    function LC_Page_Products_Detail_action_before(LC_Page_Products_Detail $objPage) {

        self::LC_Page_Products_Common_action_before ( $objPage );

    }

    function LC_Page_Products_Common_action_before(LC_Page $objPage) {

        $objQuery = SC_Query_Ex::getSingletonInstance ();

        // 期間外(非公開)
        $objQuery->update ( "dtb_products", array (
            "status" => "2",
            "update_date" => "CURRENT_TIMESTAMP"
        ), "     auto_display_status = 1
             AND del_flg             = 0
             AND status              = 1
             AND CASE WHEN auto_display_start_date IS NOT NULL
                           THEN auto_display_start_date > CURRENT_TIMESTAMP
                      WHEN auto_display_end_date   IS NOT NULL
                           THEN auto_display_end_date   < CURRENT_TIMESTAMP
                      ELSE FALSE END" );

        // 期間内(公開)
        $objQuery->update ( "dtb_products", array (
            "status" => "1",
            "update_date" => "CURRENT_TIMESTAMP"
        ), "     auto_display_status = 1
             AND del_flg             = 0
             AND status              = 2
             AND CASE WHEN auto_display_start_date IS NOT NULL
                           THEN NOT auto_display_start_date > CURRENT_TIMESTAMP
                      WHEN auto_display_end_date   IS NOT NULL
                           THEN NOT auto_display_end_date   < CURRENT_TIMESTAMP
                      ELSE FALSE END" );

        // 商品数量を再計算
        $objDb = new SC_Helper_DB_Ex ();
        $objDb->sfCountCategory ( $objQuery );
        $objDb->sfCountMaker ( $objQuery );

    }

    function LC_Page_Admin_Products_Product_action_after(LC_Page_Admin_Products_Product $objPage) {

        $objQuery = new SC_Query_Ex ();
        $listTable = $objQuery->listTables ();

        $masterDate = new SC_DB_MasterData_Ex ();

        $table = "mtb_auto_display_start_date";
        $objPage->arrAutoDispStatus = $masterDate->getMasterData ( $table );

        $objDate = new SC_Date_Ex ( RELEASE_YEAR );

        $objPage->arrDispYear = $objDate->getYear ();
        $objPage->arrDispMonth = $objDate->getMonth ();
        $objPage->arrDispDay = $objDate->getDay ();
        $objPage->arrDispHour = $objDate->getHour ();
        $objPage->arrDispMinutes = $objDate->getMinutesInterval ();

        $objQuery = SC_Query_Ex::getSingletonInstance ();

        switch ($objPage->getMode ()) {
        case 'pre_edit' :
            if (is_numeric ( $objPage->arrForm ["product_id"] )) {
                $product_id = $objPage->arrForm ["product_id"];
                $product = $objQuery->getRow ( "*", "dtb_products", "product_id=?", ( array ) $product_id );
                // auto_display_status,auto_display_start_date,auto_display_end_date

                $objPage->arrForm ["auto_display_status"] = $product ["auto_display_status"];

                if (! empty ( $product ["auto_display_start_date"] )) {
                    $objPage->arrForm ["auto_display_start_date_year"] = date ( "Y", strtotime ( $product ["auto_display_start_date"] ) );
                    $objPage->arrForm ["auto_display_start_date_month"] = date ( "m", strtotime ( $product ["auto_display_start_date"] ) );
                    $objPage->arrForm ["auto_display_start_date_day"] = date ( "d", strtotime ( $product ["auto_display_start_date"] ) );
                    $objPage->arrForm ["auto_display_start_date_hour"] = date ( "h", strtotime ( $product ["auto_display_start_date"] ) );
                    $objPage->arrForm ["auto_display_start_date_min"] = date ( "i", strtotime ( $product ["auto_display_start_date"] ) );
                }

                if (! empty ( $product ["auto_display_end_date"] )) {
                    $objPage->arrForm ["auto_display_end_date_year"] = date ( "Y", strtotime ( $product ["auto_display_end_date"] ) );
                    $objPage->arrForm ["auto_display_end_date_month"] = date ( "m", strtotime ( $product ["auto_display_end_date"] ) );
                    $objPage->arrForm ["auto_display_end_date_day"] = date ( "d", strtotime ( $product ["auto_display_end_date"] ) );
                    $objPage->arrForm ["auto_display_end_date_hour"] = date ( "h", strtotime ( $product ["auto_display_end_date"] ) );
                    $objPage->arrForm ["auto_display_end_date_min"] = date ( "i", strtotime ( $product ["auto_display_end_date"] ) );
                }
            }
            break;

        case 'edit' :
            self::LC_Page_Admin_Products_Product_action_after_check ( $objPage );
            break;

        case 'complete' :
            if (SC_Utils_Ex::isBlank ( $objPage->arrErr )) {
                if (is_numeric ( $objPage->arrForm ["product_id"] )) {
                    $product_id = $objPage->arrForm ["product_id"];

                    $arrVal = array ();
                    $arrVal ["auto_display_status"] = $_POST ["auto_display_status"];

                    $year = $_POST ["auto_display_start_date_year"];
                    $month = $_POST ["auto_display_start_date_month"];
                    $day = $_POST ["auto_display_start_date_day"];
                    $hour = $_POST ["auto_display_start_date_hour"];
                    $minutes = $_POST ["auto_display_start_date_min"];
                    $arrVal ["auto_display_start_date"] = SC_Utils_Ex::sfGetTimestampistime ( $year, $month, $day, $hour, $minutes );

                    $year = $_POST ["auto_display_end_date_year"];
                    $month = $_POST ["auto_display_end_date_month"];
                    $day = $_POST ["auto_display_end_date_day"];
                    $hour = $_POST ["auto_display_end_date_hour"];
                    $minutes = $_POST ["auto_display_end_date_min"];
                    $arrVal ["auto_display_end_date"] = SC_Utils_Ex::sfGetTimestampistime ( $year, $month, $day, $hour, $minutes, true );

                    $objQuery->update ( "dtb_products", $arrVal, "product_id=?", ( array ) $product_id );
                }
            }
            break;
        }

    }

    function LC_Page_Admin_Products_Product_action_after_check(LC_Page_Admin_Products_Product $objPage) {

        $objErr = new SC_CheckError_Ex ( $objPage->arrForm );
        if (count ( $objPage->arrErr ) > 0) {
            $objErr->arrErr = $objPage->arrErr;
        }
        $objErr->doFunc ( array (
            "公開開始日時",
            "公開終了日時",
            "auto_display_start_date_year",
            "auto_display_start_date_month",
            "auto_display_start_date_day",
            "auto_display_start_date_hour",
            "auto_display_start_date_min",
            "auto_display_start_date_sec",
            "auto_display_end_date_year",
            "auto_display_end_date_month",
            "auto_display_end_date_day",
            "auto_display_end_date_hour",
            "auto_display_end_date_min",
            "auto_display_end_date_sec"
        ), array (
            "CHECK_SET_TERM2"
        ) );
        if (is_numeric ( $objPage->arrForm ["auto_display_start_date_year"] )) {
            $objErr->doFunc ( array (
                "公開開始日時",
                "auto_display_start_date_year",
                "auto_display_start_date_month",
                "auto_display_start_date_day",
                "auto_display_start_date_hour",
                "auto_display_start_date_min"
            ), array (
                "CHECK_DATE2"
            ) );
        }
        if (is_numeric ( $objPage->arrForm ["auto_display_end_date_year"] )) {
            $objErr->doFunc ( array (
                "公開終了日時",
                "auto_display_end_date_year",
                "auto_display_end_date_month",
                "auto_display_end_date_day",
                "auto_display_end_date_hour",
                "auto_display_end_date_min"
            ), array (
                "CHECK_DATE2"
            ) );
        }
        if ($objPage->arrForm ["auto_display_status"] == "1") {
            if (SC_Utils_Ex::isBlank ( $objErr->arrErr )) {
                $objErr->doFunc ( array (
                    "公開開始日時または公開終了日時",
                    "auto_display_start_date_year",
                    "auto_display_end_date_year"
                ), array (
                    "ONE_EXIST_CHECK"
                ) );
            }
        }

        $objPage->arrErr = $objErr->arrErr;
        if (count ( $objPage->arrErr ) > 0) {

            // アップロードファイル情報の初期化
            $objUpFile = new SC_UploadFile_Ex ( IMAGE_TEMP_REALDIR, IMAGE_SAVE_REALDIR );
            $objPage->lfInitFile ( $objUpFile );
            $objUpFile->setHiddenFileList ( $_POST );

            // ダウンロード販売ファイル情報の初期化
            $objDownFile = new SC_UploadFile_Ex ( DOWN_TEMP_REALDIR, DOWN_SAVE_REALDIR );
            $objPage->lfInitDownFile ( $objDownFile );
            $objDownFile->setHiddenFileList ( $_POST );

            // 入力画面表示設定
            $objPage->arrForm = $objPage->lfSetViewParam_InputPage ( $objUpFile, $objDownFile, $objPage->arrForm );
            // ページonload時のJavaScript設定
            $objPage->tpl_onload = $objPage->lfSetOnloadJavaScript_InputPage ();

            $objPage->tpl_mainpage = 'products/product.tpl';
            $objPage->tpl_subtitle = '商品登録';
        }

    }

}
Twitter: @asahina_alice