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

Revision 22567, 21.7 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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