開発メモ

開発用のメモです。

EC CUBE のプラグインの骨格

ディレクトリ構成

ファイル内容

config.php

<?php

require_once PLUGIN_UPLOAD_REALDIR . 'PluginName/LC_Page_Plugin_PluginName_Config.php';
$objPage = new LC_Page_Plugin_PluginName_Config();
register_shutdown_function(array(
    $objPage,
    'destroy'
));
$objPage -> init();
$objPage -> process();

LC_Page_Plugin_PluginName_Config.php

<?php
// {{{ requires
require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php';

class LC_Page_Plugin_PluginName_Config extends LC_Page_Admin_Ex {

    var $plugin_name = "PluginName";

    var $arrForm = array();

    /**
    * 初期化する.
    *
    * @return void
    */
    function init() {
        parent::init();
        $this -> tpl_mainpage = PLUGIN_UPLOAD_REALDIR . $this -> plugin_name . "/templates/config/index.tpl";
        $this -> tpl_subtitle = "設定画面";
        $this -> tpl_field1 = "項目1";
        $this -> tpl_field2 = "項目2";
        $this -> tpl_field3 = "項目3";
        $this -> tpl_field4 = "項目4";
        $this -> btn_regist = "登録";
        $this -> th_label = "項目名";
        $this -> th_input = "設定値";

        // 任意
    }

    /**
    * プロセス.
    *
    * @return void
    */
    function process() {
        $this -> action();
        $this -> sendResponse();
    }

    /**
    * Page のアクション.
    *
    * @return void
    */
    function action() {
        $objFormParam = new SC_FormParam_Ex();
        $this -> lfInitParam($objFormParam);
        $objFormParam -> setParam($_POST);
        $objFormParam -> convParam();

        $arrForm = array();
        switch ($this->getMode()) {
            case 'edit' :
                $arrForm = $objFormParam -> getHashArray();
                $this -> arrErr = $objFormParam -> checkError();
                // エラーなしの場合にはデータを更新
                if (count($this -> arrErr) == 0) {
                    // データ更新
                    $this -> arrErr = $this -> updateData($arrForm);
                    if (count($this -> arrErr) == 0) {
                        $this -> tpl_onload = "";
                        $this -> tpl_onload .= "alert('登録が完了しました。');";
                        $this -> tpl_onload .= 'window.close();';
                    }
                }
                break;
            default :
                // プラグイン情報を取得.
                $plugin = SC_Plugin_Util_Ex::getPluginByPluginCode($this -> plugin_name);
                // 必要フィールド数分定義
                $arrForm['free_field1'] = $plugin['free_field1'];
                $arrForm['free_field2'] = $plugin['free_field2'];
                $arrForm['free_field3'] = $plugin['free_field3'];
                $arrForm['free_field4'] = $plugin['free_field4'];
                break;
        }
        $this -> arrForm = $arrForm;
        $this -> setTemplate($this -> tpl_mainpage);
    }

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

    /**
    * パラメーター情報の初期化
    *
    * @param object $objFormParam SC_FormParamインスタンス
    * @return void
    */
    function lfInitParam(&$objFormParam) {
        $objFormParam -> addParam('項目1', 'free_field1', INT_LEN, 'n', array('MAX_LENGTH_CHECK'));
        $objFormParam -> addParam('項目2', 'free_field2', INT_LEN, 'n', array('MAX_LENGTH_CHECK'));
        $objFormParam -> addParam('項目3', 'free_field3', INT_LEN, 'n', array('MAX_LENGTH_CHECK'));
        $objFormParam -> addParam('項目4', 'free_field4', INT_LEN, 'n', array('MAX_LENGTH_CHECK'));
    }

    /**
    *
    * @param type $arrData
    * @return type
    */
    function updateData($arrData) {
        $arrErr = array();

        $objQuery = &SC_Query_Ex::getSingletonInstance();
        $objQuery -> begin();
        // UPDATEする値を作成する。
        $sqlval = array();
        $sqlval['free_field1'] = $arrData['free_field1'];
        $sqlval['free_field2'] = $arrData['free_field2'];
        $sqlval['free_field3'] = $arrData['free_field3'];
        $sqlval['free_field4'] = $arrData['free_field4'];
        $sqlval['update_date'] = 'CURRENT_TIMESTAMP';
        $where = "plugin_code = ?";
        $whereVal = array($this -> plugin_name);

        // UPDATEの実行
        $objQuery -> update('dtb_plugin', $sqlval, $where, $whereVal);
        $objQuery -> commit();
        return $arrErr;
    }

}

PluginName.php

<?php
class OrderPoint extends SC_Plugin_Base {

    public function __construct(array $arrSelfInfo) {
/*
       if (!isset($arrSelfInfo)) {
           $arrSelfInfo = array("plugin_hook_point" => array());
       } elseif (!isset($arrSelfInfo["plugin_hook_point"])) {
           $arrSelfInfo["plugin_hook_point"] = array();
       }
       array_push($arrSelfInfo["plugin_hook_point"], array(
           'hook_point' => 'prefilterTransform',
           'callback' => 'prefilter'
       ));
       array_push($arrSelfInfo["plugin_hook_point"], array(
           'hook_point' => 'LC_Page_Admin_Products_Product_action_after',
           'callback' => 'admin_products_product_after'
       ));
       array_push($arrSelfInfo["plugin_hook_point"], array(
           'hook_point' => 'LC_Page_Admin_Products_ProductClass_action_after',
           'callback' => 'admin_products_product_class_after'
       ));
       array_push($arrSelfInfo["plugin_hook_point"], array(
           'hook_point' => 'LC_Page_Admin_Home_action_after',
           'callback' => 'admin_home_after'
       ));
*/
        parent::__construct($arrSelfInfo);
    }

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

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

    function prefilter(&$source, LC_Page_Ex $objPage, $filename) {
        $objTransform = new SC_Helper_Transform($source);

        $source = $objTransform -> getHTML();
    }

    function admin_products_product_after($objectPage) {
    }

    function admin_products_product_class_after($objectPage) {
    }

    function admin_home_after($objectPage) {
    }


}

plugin_info.php

<?php
class plugin_info {

    static $PLUGIN_CODE = "PluginName";
    static $PLUGIN_NAME = "PluginName";
    static $PLUGIN_VERSION = "0.1";
    static $COMPLIANT_VERSION = "2.13.x";
    static $AUTHOR = "MyName";
    static $DESCRIPTION = "プラグインの説明";
    static $PLUGIN_SITE_URL = "/#ダウンロードサイト";
    static $AUTHOR_SITE_URL = "/#作成者のサイト";

    static $CLASS_NAME = "PluginName";
/*
   static $HOOK_POINTS = array(
       "prefilterTransform",
       "LC_Page_Admin_Products_Product_action_after",
       "LC_Page_Admin_Products_ProductClass_action_after",
       "LC_Page_Admin_Home_action_after"
   );
*/
    static $HOOK_POINTS = array(
        array(
            "prefilterTransform",
            "prefilter"
        ),
        array(
            "LC_Page_Admin_Products_Product_action_after",
            "admin_products_product_action_after"
        ),
        array(
            "LC_Page_Admin_Products_ProductClass_action_after",
            "admin_products_product_class_after"
        ),
        array(
            "LC_Page_Admin_Home_action_after",
            "admin_home_after"
        )
    );
    static $LICENSE = "LGPL";
}
Twitter: @asahina_alice