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

Revision 22857, 21.5 KB checked in by Seasoft, 11 years ago (diff)

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

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