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

Revision 21481, 21.5 KB checked in by Seasoft, 12 years ago (diff)

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

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