source: branches/version-2_12-dev/data/class/pages/admin/products/LC_Page_Admin_Products_UploadCSVCategory.php @ 21684

Revision 21684, 21.9 KB checked in by Seasoft, 12 years ago (diff)

#1613 (typo修正・ソース整形・ソースコメントの改善)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24// {{{ requires
25require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php';
26
27/**
28 * カテゴリ登録CSVのページクラス
29 *
30 * LC_Page_Admin_Products_UploadCSV をカスタマイズする場合はこのクラスを編集する.
31 *
32 * @package Page
33 * @author LOCKON CO.,LTD.
34 * @version $$Id$$
35 */
36class LC_Page_Admin_Products_UploadCSVCategory extends LC_Page_Admin_Ex {
37
38    // {{{ properties
39    /** エラー情報 **/
40    var $arrErr;
41
42    /** 表示用項目 **/
43    var $arrTitle;
44
45    /** 結果行情報 **/
46    var $arrRowResult;
47
48    /** エラー行情報 **/
49    var $arrRowErr;
50
51    /** TAGエラーチェックフィールド情報 */
52    var $arrTagCheckItem;
53
54    /** テーブルカラム情報 (登録処理用) **/
55    var $arrRegistColumn;
56
57    /** 登録フォームカラム情報 **/
58    var $arrFormKeyList;
59
60    // }}}
61    // {{{ functions
62
63    /**
64     * Page を初期化する.
65     *
66     * @return void
67     */
68    function init() {
69        parent::init();
70        $this->tpl_mainpage = 'products/upload_csv_category.tpl';
71        $this->tpl_mainno   = 'products';
72        $this->tpl_subno    = 'upload_csv_category';
73        $this->tpl_maintitle = '商品管理';
74        $this->tpl_subtitle = 'カテゴリ登録CSV';
75        $this->csv_id = '5';
76
77        $masterData = new SC_DB_MasterData_Ex();
78        $this->arrAllowedTag = $masterData->getMasterData('mtb_allowed_tag');
79        $this->arrTagCheckItem = array();
80    }
81
82    /**
83     * Page のプロセス.
84     *
85     * @return void
86     */
87    function process() {
88        $this->action();
89        $this->sendResponse();
90    }
91
92    /**
93     * Page のアクション.
94     *
95     * @return void
96     */
97    function action() {
98        // フックポイント.
99        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance();
100        $objPlugin->doAction('lc_page_admin_products_uploadcsvcategory_action_start', array($this));
101
102        // CSV管理ヘルパー
103        $objCSV = new SC_Helper_CSV_Ex();
104        // CSV構造読み込み
105        $arrCSVFrame = $objCSV->sfGetCsvOutput($this->csv_id);
106
107        // CSV構造がインポート可能かのチェック
108        if (!$objCSV->sfIsImportCSVFrame($arrCSVFrame)) {
109            // 無効なフォーマットなので初期状態に強制変更
110            $arrCSVFrame = $objCSV->sfGetCsvOutput($this->csv_id, '', array(), 'no');
111            $this->tpl_is_format_default = true;
112        }
113        // CSV構造は更新可能なフォーマットかのフラグ取得
114        $this->tpl_is_update = $objCSV->sfIsUpdateCSVFrame($arrCSVFrame);
115
116        // CSVファイルアップロード情報の初期化
117        $objUpFile = new SC_UploadFile_Ex(IMAGE_TEMP_REALDIR, IMAGE_SAVE_REALDIR);
118        $this->lfInitFile($objUpFile);
119
120        // パラメーター情報の初期化
121        $objFormParam = new SC_FormParam_Ex();
122        $this->lfInitParam($objFormParam, $arrCSVFrame);
123
124        $objFormParam->setHtmlDispNameArray();
125        $this->arrTitle = $objFormParam->getHtmlDispNameArray();
126
127        switch ($this->getMode()) {
128            case 'csv_upload':
129                $this->doUploadCsv($objFormParam, $objUpFile);
130                break;
131            default:
132                break;
133        }
134        // フックポイント.
135        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance();
136        $objPlugin->doAction('lc_page_admin_products_uploadcsvcategory_action_end', array($this));
137    }
138
139    /**
140     * 登録/編集結果のメッセージをプロパティへ追加する
141     *
142     * @param integer $line_count 行数
143     * @param stirng $message メッセージ
144     * @return void
145     */
146    function addRowResult($line_count, $message) {
147        $this->arrRowResult[] = $line_count . '行目:' . $message;
148    }
149
150    /**
151     * 登録/編集結果のエラーメッセージをプロパティへ追加する
152     *
153     * @param integer $line_count 行数
154     * @param stirng $message メッセージ
155     * @return void
156     */
157    function addRowErr($line_count, $message) {
158        $this->arrRowErr[] = $line_count . '行目:' . $message;
159    }
160
161    /**
162     * CSVアップロードを実行する
163     *
164     * @param SC_FormParam  $objFormParam
165     * @param SC_UploadFile $objUpFile
166     * @param SC_Helper_DB  $objDb
167     * @return void
168     */
169    function doUploadCsv(&$objFormParam, &$objUpFile) {
170        // ファイルアップロードのチェック
171        $objUpFile->makeTempFile('csv_file');
172        $arrErr = $objUpFile->checkExists();
173        if (count($arrErr) > 0) {
174            $this->arrErr = $arrErr;
175            return;
176        }
177        // 一時ファイル名の取得
178        $filepath = $objUpFile->getTempFilePath('csv_file');
179        // CSVファイルの文字コード変換
180        $enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath, CHAR_CODE, CSV_TEMP_REALDIR);
181        // CSVファイルのオープン
182        $fp = fopen($enc_filepath, 'r');
183        // 失敗した場合はエラー表示
184        if (!$fp) {
185            SC_Utils_Ex::sfDispError('');
186        }
187
188        // 登録先テーブル カラム情報の初期化
189        $this->lfInitTableInfo();
190
191        // 登録フォーム カラム情報
192        $this->arrFormKeyList = $objFormParam->getKeyList();
193
194        // 登録対象の列数
195        $col_max_count = $objFormParam->getCount();
196        // 行数
197        $line_count = 0;
198
199        $objQuery =& SC_Query_Ex::getSingletonInstance();
200        $objQuery->begin();
201
202        $errFlag = false;
203
204        while (!feof($fp)) {
205            $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
206            // 行カウント
207            $line_count++;
208            // ヘッダ行はスキップ
209            if ($line_count == 1) {
210                continue;
211            }
212            // 空行はスキップ
213            if (empty($arrCSV)) {
214                continue;
215            }
216            // 列数が異なる場合はエラー
217            $col_count = count($arrCSV);
218            if ($col_max_count != $col_count) {
219                $this->addRowErr($line_count, '※ 項目数が' . $col_count . '個検出されました。項目数は' . $col_max_count . '個になります。');
220                $errFlag = true;
221                break;
222            }
223            // シーケンス配列を格納する。
224            $objFormParam->setParam($arrCSV, true);
225            $arrRet = $objFormParam->getHashArray();
226            $objFormParam->setParam($arrRet);
227            // 入力値の変換
228            $objFormParam->convParam();
229            // <br>なしでエラー取得する。
230            $arrCSVErr = $this->lfCheckError($objFormParam);
231
232            // 入力エラーチェック
233            if (count($arrCSVErr) > 0) {
234                foreach ($arrCSVErr as $err) {
235                    $this->addRowErr($line_count, $err);
236                }
237                $errFlag = true;
238                break;
239            }
240
241            $category_id = $this->lfRegistCategory($objQuery, $line_count, $objFormParam);
242            $this->addRowResult($line_count, 'カテゴリID:'.$category_id . ' / カテゴリ名:' . $objFormParam->getValue('category_name'));
243        }
244
245        // 実行結果画面を表示
246        $this->tpl_mainpage = 'products/upload_csv_category_complete.tpl';
247
248        fclose($fp);
249
250        if ($errFlag) {
251            $objQuery->rollback();
252            return;
253        }
254
255        $objQuery->commit();
256
257        // カテゴリ件数を更新
258        SC_Helper_DB_EX::sfCountCategory($objQuery);
259        return;
260    }
261
262    /**
263     * デストラクタ.
264     *
265     * @return void
266     */
267    function destroy() {
268        parent::destroy();
269    }
270
271    /**
272     * ファイル情報の初期化を行う.
273     *
274     * @return void
275     */
276    function lfInitFile(&$objUpFile) {
277        $objUpFile->addFile('CSVファイル', 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false);
278    }
279
280    /**
281     * 入力情報の初期化を行う.
282     *
283     * @param array CSV構造設定配列
284     * @return void
285     */
286    function lfInitParam(&$objFormParam, &$arrCSVFrame) {
287        // 固有の初期値調整
288        $arrCSVFrame = $this->lfSetParamDefaultValue($arrCSVFrame);
289        // CSV項目毎の処理
290        foreach ($arrCSVFrame as $item) {
291            if ($item['status'] == CSV_COLUMN_STATUS_FLG_DISABLE) continue;
292            //サブクエリ構造の場合は AS名 を使用
293            if (preg_match_all('/\(.+\) as (.+)$/i', $item['col'], $match, PREG_SET_ORDER)) {
294                $col = $match[0][1];
295            } else {
296                $col = $item['col'];
297            }
298            // HTML_TAG_CHECKは別途実行なので除去し、別保存しておく
299            if (strpos(strtoupper($item['error_check_types']), 'HTML_TAG_CHECK') !== FALSE) {
300                $this->arrTagCheckItem[] = $item;
301                $error_check_types = str_replace('HTML_TAG_CHECK', '', $item['error_check_types']);
302            } else {
303                $error_check_types = $item['error_check_types'];
304            }
305            $arrErrorCheckTypes = explode(',', $error_check_types);
306            foreach ($arrErrorCheckTypes as $key => $val) {
307                if (trim($val) == '') {
308                    unset($arrErrorCheckTypes[$key]);
309                } else {
310                    $arrErrorCheckTypes[$key] = trim($val);
311                }
312            }
313            // パラメーター登録
314            $objFormParam->addParam(
315                    $item['disp_name']
316                    , $col
317                    , constant($item['size_const_type'])
318                    , $item['mb_convert_kana_option']
319                    , $arrErrorCheckTypes
320                    , $item['default']
321                    , ($item['rw_flg'] != CSV_COLUMN_RW_FLG_READ_ONLY) ? true : false
322                    );
323        }
324    }
325
326    /**
327     * 入力チェックを行う.
328     *
329     * @return void
330     */
331    function lfCheckError(&$objFormParam) {
332        // 入力データを渡す。
333        $arrRet =  $objFormParam->getHashArray();
334        $objErr = new SC_CheckError_Ex($arrRet);
335        $objErr->arrErr = $objFormParam->checkError(false);
336        // HTMLタグチェックの実行
337        foreach ($this->arrTagCheckItem as $item) {
338            $objErr->doFunc(array($item['disp_name'], $item['col'], $this->arrAllowedTag), array('HTML_TAG_CHECK'));
339        }
340        // このフォーム特有の複雑系のエラーチェックを行う
341        if (count($objErr->arrErr) == 0) {
342            $objErr->arrErr = $this->lfCheckErrorDetail($arrRet, $objErr->arrErr);
343        }
344        return $objErr->arrErr;
345    }
346
347    /**
348     * 保存先テーブル情報の初期化を行う.
349     *
350     * @return void
351     */
352    function lfInitTableInfo() {
353        $objQuery =& SC_Query_Ex::getSingletonInstance();
354        $this->arrRegistColumn = $objQuery->listTableFields('dtb_category');
355    }
356
357    /**
358     * カテゴリ登録を行う.
359     *
360     * FIXME: 登録の実処理自体は、LC_Page_Admin_Products_Categoryと共通化して欲しい。
361     *
362     * @param SC_Query $objQuery SC_Queryインスタンス
363     * @param string|integer $line 処理中の行数
364     * @return integer カテゴリID
365     */
366    function lfRegistCategory($objQuery, $line, &$objFormParam) {
367        // 登録データ対象取得
368        $arrList = $objFormParam->getHashArray();
369        // 登録時間を生成(DBのCURRENT_TIMESTAMPだとcommitした際、すべて同一の時間になってしまう)
370        $arrList['update_date'] = $this->lfGetDbFormatTimeWithLine($line);
371
372        // 登録情報を生成する。
373        // テーブルのカラムに存在しているもののうち、Form投入設定されていないデータは上書きしない。
374        $sqlval = SC_Utils_Ex::sfArrayIntersectKeys($arrList, $this->arrRegistColumn);
375
376        // 必須入力では無い項目だが、空文字では問題のある特殊なカラム値の初期値設定
377        $sqlval = $this->lfSetCategoryDefaultData($sqlval);
378
379        if ($sqlval['category_id'] != '') {
380            // 同じidが存在すればupdate存在しなければinsert
381            $where = 'category_id = ?';
382            $category_exists = $objQuery->exists('dtb_category', $where, array($sqlval['category_id']));
383            if ($category_exists) {
384                // UPDATEの実行
385                $where = 'category_id = ?';
386                $objQuery->update('dtb_category', $sqlval, $where, array($sqlval['category_id']));
387            } else {
388                $sqlval['create_date'] = $arrList['update_date'];
389                // 新規登録
390                $category_id = $this->registerCategory($sqlval['parent_category_id'],
391                                        $sqlval['category_name'],
392                                        $_SESSION['member_id'],
393                                        $sqlval['category_id']);
394            }
395            $category_id = $sqlval['category_id'];
396            // TODO: 削除時処理
397        } else {
398            // 新規登録
399            $category_id = $this->registerCategory($sqlval['parent_category_id'],
400                                        $sqlval['category_name'],
401                                        $_SESSION['member_id']);
402        }
403        return $category_id;
404    }
405
406    /**
407     * 初期値の設定
408     *
409     * @param array $arrCSVFrame CSV構造配列
410     * @return array $arrCSVFrame CSV構造配列
411     */
412    function lfSetParamDefaultValue(&$arrCSVFrame) {
413        foreach ($arrCSVFrame as $key => $val) {
414            switch ($val['col']) {
415                case 'parent_category_id':
416                    $arrCSVFrame[$key]['default'] = '0';
417                    break;
418                case 'del_flg':
419                    $arrCSVFrame[$key]['default'] = '0';
420                    break;
421                default:
422                    break;
423            }
424        }
425        return $arrCSVFrame;
426    }
427
428    /**
429     * データ登録前に特殊な値の持ち方をする部分のデータ部分の初期値補正を行う
430     *
431     * @param array $sqlval 商品登録情報配列
432     * @return $sqlval 登録情報配列
433     */
434    function lfSetCategoryDefaultData(&$sqlval) {
435        if ($sqlval['del_flg'] == '') {
436            $sqlval['del_flg'] = '0'; //有効
437        }
438        if ($sqlval['creator_id'] == '') {
439            $sqlval['creator_id'] = $_SESSION['member_id'];
440        }
441        if ($sqlval['parent_category_id'] == '') {
442            $sqlval['parent_category_id'] = (string)'0';
443        }
444        return $sqlval;
445    }
446
447    /**
448     * このフォーム特有の複雑な入力チェックを行う.
449     *
450     * @param array 確認対象データ
451     * @param array エラー配列
452     * @return array エラー配列
453     */
454    function lfCheckErrorDetail($item, $arrErr) {
455        $objQuery =& SC_Query_Ex::getSingletonInstance();
456        /*
457        // カテゴリIDの存在チェック
458        if (!$this->lfIsDbRecord('dtb_category', 'category_id', $item)) {
459            $arrErr['category_id'] = '※ 指定のカテゴリIDは、登録されていません。';
460        }
461        */
462        // 親カテゴリIDの存在チェック
463        if (array_search('parent_category_id', $this->arrFormKeyList) !== FALSE
464            && $item['parent_category_id'] != ''
465            && $item['parent_category_id'] != '0'
466            && !SC_Helper_DB_Ex::sfIsRecord('dtb_category', 'category_id', array($item['parent_category_id']))
467        ) {
468            $arrErr['parent_category_id'] = '※ 指定の親カテゴリID(' . $item['parent_category_id'] . ')は、存在しません。';
469        }
470        // 削除フラグのチェック
471        if (array_search('del_flg', $this->arrFormKeyList) !== FALSE
472            && $item['del_flg'] != ''
473        ) {
474            if (!($item['del_flg'] == '0' or $item['del_flg'] == '1')) {
475                $arrErr['del_flg'] = '※ 削除フラグは「0」(有効)、「1」(削除)のみが有効な値です。';
476            }
477        }
478        // 重複チェック 同じカテゴリ内に同名の存在は許可されない
479        if (array_search('category_name', $this->arrFormKeyList) !== FALSE
480            && $item['category_name'] != ''
481        ) {
482            $parent_category_id = $item['parent_category_id'];
483            if ($parent_category_id == '') {
484                $parent_category_id = (string)'0';
485            }
486            $where = 'parent_category_id = ? AND category_id <> ? AND category_name = ?';
487            $exists = $objQuery->exists('dtb_category',
488                        $where,
489                        array($parent_category_id,
490                                $item['category_id'],
491                                $item['category_name']));
492            if ($exists) {
493                $arrErr['category_name'] = '※ 既に同名のカテゴリが存在します。';
494            }
495        }
496        // 登録数上限チェック
497        $where = 'del_flg = 0';
498        $count = $objQuery->count('dtb_category', $where);
499        if ($count >= CATEGORY_MAX) {
500            $item['category_name'] = '※ カテゴリの登録最大数を超えました。';
501        }
502        // 階層上限チェック
503        if (array_search('parent_category_id', $this->arrFormKeyList) !== FALSE
504                and $item['parent_category_id'] != '') {
505            $level = $objQuery->get('level', 'dtb_category', 'category_id = ?', array($parent_category_id));
506            if ($level >= LEVEL_MAX) {
507                $arrErr['parent_category_id'] = '※ ' . LEVEL_MAX . '階層以上の登録はできません。';
508            }
509        }
510        return $arrErr;
511    }
512
513    /**
514     * カテゴリを登録する
515     *
516     * @param integer 親カテゴリID
517     * @param string カテゴリ名
518     * @param integer 作成者のID
519     * @param integer 指定カテゴリID
520     * @return integer カテゴリID
521     */
522    function registerCategory($parent_category_id, $category_name, $creator_id, $category_id = null) {
523        $objQuery =& SC_Query_Ex::getSingletonInstance();
524
525        $rank = null;
526        if ($parent_category_id == 0) {
527            // ROOT階層で最大のランクを取得する。
528            $where = 'parent_category_id = ?';
529            $rank = $objQuery->max('rank', 'dtb_category', $where, array($parent_category_id)) + 1;
530        } else {
531            // 親のランクを自分のランクとする。
532            $where = 'category_id = ?';
533            $rank = $objQuery->get('rank', 'dtb_category', $where, array($parent_category_id));
534            // 追加レコードのランク以上のレコードを一つあげる。
535            $sqlup = 'UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?';
536            $objQuery->exec($sqlup, array($rank));
537        }
538
539        $where = 'category_id = ?';
540        // 自分のレベルを取得する(親のレベル + 1)
541        $level = $objQuery->get('level', 'dtb_category', $where, array($parent_category_id)) + 1;
542
543        $arrCategory = array();
544        $arrCategory['category_name'] = $category_name;
545        $arrCategory['parent_category_id'] = $parent_category_id;
546        $arrCategory['create_date'] = 'CURRENT_TIMESTAMP';
547        $arrCategory['update_date'] = 'CURRENT_TIMESTAMP';
548        $arrCategory['creator_id']  = $creator_id;
549        $arrCategory['rank']        = $rank;
550        $arrCategory['level']       = $level;
551        //カテゴリIDが指定されていればそれを利用する
552        if (isset($category_id)) {
553            $arrCategory['category_id'] = $category_id;
554            // シーケンスの調整
555            $seq_count = $objQuery->currVal('dtb_category_category_id');
556            if ($seq_count < $arrCategory['category_id']) {
557                $objQuery->setVal('dtb_category_category_id', $arrCategory['category_id'] + 1);
558            }
559        } else {
560            $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id');
561        }
562        $objQuery->insert('dtb_category', $arrCategory);
563
564        return $arrCategory['category_id'];
565    }
566
567    /**
568     * 指定された行番号をmicrotimeに付与してDB保存用の時間を生成する。
569     * トランザクション内のCURRENT_TIMESTAMPは全てcommit()時の時間に統一されてしまう為。
570     *
571     * @param string $line_no 行番号
572     * @return string $time DB保存用の時間文字列
573     */
574    function lfGetDbFormatTimeWithLine($line_no = '') {
575        $time = date('Y-m-d H:i:s');
576        // 秒以下を生成
577        if ($line_no != '') {
578            $microtime = sprintf('%06d', $line_no);
579            $time .= ".$microtime";
580        }
581        return $time;
582    }
583
584    /**
585     * 指定されたキーと値の有効性のDB確認
586     *
587     * @param string $table テーブル名
588     * @param string $keyname キー名
589     * @param array  $item 入力データ配列
590     * @return boolean true:有効なデータがある false:有効ではない
591     */
592    function lfIsDbRecord($table, $keyname, $item) {
593        if (array_search($keyname, $this->arrFormKeyList) !== FALSE  //入力対象である
594            && $item[$keyname] != ''   // 空ではない
595            && !SC_Helper_DB_EX::sfIsRecord($table, $keyname, (array)$item[$keyname]) //DBに存在するか
596        ) {
597            return false;
598        }
599        return true;
600    }
601
602}
Note: See TracBrowser for help on using the repository browser.