開発メモ

開発用のメモです。

EC CUBE でファイルアップロード (サンプル:カテゴリ管理)

LC_Page

<?php
/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

require_once CLASS_REALDIR . 'pages/admin/products/LC_Page_Admin_Products_Category.php';

/**
 * カテゴリ管理 のページクラス(拡張).
 *
 * LC_Page_Admin_Products_Category をカスタマイズする場合はこのクラスを編集する.
 *
 * @package Page
 * @author LOCKON CO.,LTD.
 * @version $Id: LC_Page_Admin_Products_Category_Ex.php 22926 2013-06-29 16:24:23Z Seasoft $
 */
class LC_Page_Admin_Products_Category_Ex extends LC_Page_Admin_Products_Category {

    /**
     * Page のアクション.
     *
     * @return void
     */
    public function action ( ) {
        $this -> objFormParam = new SC_FormParam_Ex ( );
        $this -> objCategory = new SC_Helper_Category_Ex ( );

        // 入力パラメーター初期化
        $this -> initParam ( $this -> objFormParam );
        $this -> objFormParam -> setParam ( $_POST );
        $this -> objFormParam -> convParam ( );

        // ファイル管理クラス
        $this -> objUpFile = new SC_UploadFile_Ex ( IMAGE_TEMP_REALDIR , IMAGE_SAVE_REALDIR );
        // ファイル情報の初期化
        $this -> objUpFile -> addFile ( 'カテゴリ画像' , 'image_path_f' , array (
            'gif' ,
            'jpg' ,
            'png'
        ) , IMAGE_SIZE , false , 200 , 200 );
        // Hiddenからのデータを引き継ぐ
        $this -> objUpFile -> setHiddenFileList ( $_POST );

        switch ($this->getMode()) {
            case 'upload_image' :
            case 'delete_image' :
                $this -> doImage ( $this -> objFormParam , $this -> getMode ( ) );
        }
        parent :: action ( );

        // FORM表示用配列を渡す。
        $this -> arrFile = $this -> objUpFile -> getFormFileList ( IMAGE_TEMP_URLPATH , IMAGE_SAVE_URLPATH );
        // HIDDEN用に配列を渡す。
        $this -> arrHidden = (array)$this -> objUpFile -> getHiddenFileList ( );
    }

    /**
     * パラメーターの初期化を行う
     *
     * @param  SC_FormParam $objFormParam
     * @return void
     */
    public function initParam ( &$objFormParam ) {
        parent :: initParam ( $objFormParam );
        $objFormParam -> addParam ( '画像アップロードキー' , 'image_key' );
        $objFormParam -> addParam ( 'カテゴリ画像' , 'image_path' );
        $objFormParam -> addParam ( 'カテゴリ画像' , 'image_path_f' );
        $objFormParam -> addParam ( 'カテゴリ画像' , 'temp_image_path_f' );
        $objFormParam -> addParam ( '詳細' , 'description' );
    }

    /**
     * カテゴリを更新する
     *
     * @param  SC_FormParam $objFormParam SC_FormParam インスタンス
     * @return void
     */
    public function updateCategory ( $category_id , $arrCategory ) {
        $post = $this -> objFormParam -> getHashArray ( );
        if ( strlen ( $post [ "temp_image_path_f" ] ) > 0 ) {
            $this -> objUpFile -> moveTempFile ( );
            $arrCategory [ "image_path" ] = $this -> objUpFile -> temp_file [ 0 ];
        }
        parent :: updateCategory ( $category_id , $arrCategory );
    }

    /**
     * カテゴリを登録する
     *
     * @param  SC_FormParam $objFormParam SC_FormParam インスタンス
     * @return void
     */
    public function registerCategory ( $arrCategory ) {
        $post = $this -> objFormParam -> getHashArray ( );
        if ( strlen ( $post [ "temp_image_path_f" ] ) > 0 ) {
            $this -> objUpFile -> moveTempFile ( );
            $arrCategory [ "image_path" ] = $this -> objUpFile -> temp_file [ 0 ];
        }
        parent :: registerCategory ( $arrCategory );
    }

    public function doImage ( &$objFormParam , $mode ) {
        $post = $this -> objFormParam -> getHashArray ( );
        switch($mode) {
            // 画像のアップロード
            case 'upload_image' :
                // ファイル存在チェック
                $this -> arrErr = $this -> objUpFile -> checkExists ( $post [ 'image_key' ] );
                // 画像保存処理
                $this -> arrErr [ $post [ 'image_key' ] ] = $this -> objUpFile -> makeTempFile ( $post [ 'image_key' ] );
                break;
            // 画像の削除
            case 'delete_image' :
                if ( count ( $this -> arrErr ) == 0 ) {
                    $this -> objUpFile -> deleteFile ( $post [ 'image_key' ] );
                }
                break;
        }
        $this -> arrForm = $objFormParam -> getHashArray ( );
    }

}

tpl

        <!--{* ▼画面右 *}-->
        <div id="products-category-right">
            <div class="now_dir">
                <table>
                    <colgroup>
                        <col width="15%"/>
                        <col width="85%"/>
                    </colgroup>
                    <tbody>
                        <tr>
                            <th>カテゴリ名</th>
                            <td>
                                <div class="attention"><!--{$arrErr.category_name}--></div>
                                <input type="text" name="category_name" value="<!--{$arrForm.category_name|h}-->" class="box380" 
                                       maxlength="<!--{$smarty.const.STEXT_LEN}-->" style="<!--{$arrErr.category_name|sfGetErrorColor}-->" />
                                <div class="attention">
                                    <span>(上限<!--{$smarty.const.STEXT_LEN}-->文字)</span>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <th>カテゴリ画像</th>
                            <td>
                                <div>
                                    <div class="attention"><!--{$arrErr.image_path}--><!--{$arrErr.image_path_f}--></div>
                                    <input type="hidden" name="image_key" value="" />
                                    <!--{if $arrFile.image_path.filepath != ""}-->
                                    <img src="<!--{$arrFile.image_path.filepath}-->" alt="">
                                    <a href="" onclick="eccube.setModeAndSubmit('delete_image', 'image_key', 'image_path_f'); return false;">[画像の取り消し]</a>
                                    <!--{/if}-->
                                </div>
                                <!--{foreach key=key item=item from=$arrHidden}-->
                                    <input type="hidden" name="<!--{$key}-->" value="<!--{$item|h}-->" />
                                <!--{/foreach}-->
                                <input type="hidden" name="image_path"   value="<!--{$arrForm.image_path|h}-->" />
                                <input type="file" name="image_path_f" style="<!--{$arrErr.image_path|default:$arrErr.image_path_f|sfGetErrorColor}-->" /><br/>
                                <a href="" onclick="eccube.setModeAndSubmit('upload_image', 'image_key', 'image_path_f'); return false;">[アップロード]</a>
                            </td>
                        </tr>
                        <tr>
                            <th>詳細</th>
                            <td>
                                <textarea name="description" class="box320" rows="6" cols="80" style="<!--{$arrErr.description|sfGetErrorColor}-->" ><!--{$arrForm.description|h}--></textarea>
                                <div class="attention">
                                    <span>(上限<!--{$smarty.const.LTEXT_LEN}-->文字)</span>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <a class="btn-normal" href="javascript:;" onclick="eccube.setModeAndSubmit('edit','',''); return false;"><span class="btn-next">登録</span></a>
            </div>
Twitter: @asahina_alice