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

Revision 22433, 21.7 KB checked in by kim, 11 years ago (diff)

#2060 r22372,r22381,r22382,r22386-r22390,r22397-r22400,r22407-r22411,r22416,r22417,r22420,r22421,r22423,r22425,r22426 を差し戻す。

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