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

Revision 23605, 17.5 KB checked in by kimoto, 10 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

Scrutinizer Auto-Fixes

This patch was automatically generated as part of the following inspection:
 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/d8722894-69a6-4b1b-898d-43618035c60d

Enabled analysis tools:

  • PHP Analyzer
  • PHP PDepend
  • PHP Similarity Analyzer
  • PHP Change Tracking Analyzer
  • 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-2014 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    public $arrErr;
39
40    /** 表示用項目 **/
41    public $arrTitle;
42
43    /** 結果行情報 **/
44    public $arrRowResult;
45
46    /** エラー行情報 **/
47    public $arrRowErr;
48
49    /** TAGエラーチェックフィールド情報 */
50    public $arrTagCheckItem;
51
52    /** テーブルカラム情報 (登録処理用) **/
53    public $arrRegistColumn;
54
55    /** 登録フォームカラム情報 **/
56    public $arrFormKeyList;
57    /** @var string メインタイトル */
58    public $tpl_maintitle;
59    /** @var string サブタイトル */
60    public $tpl_subtitle;
61    /** @var string サブテンプレートナンバー */
62    public $tpl_subno;
63
64    /** @var array 許可タグ情報 */
65    private $arrAllowedTag;
66    /** @var int CSV ID */
67    private $csv_id;
68    /** @var  bool */
69    private $tpl_is_format_default;
70    /** @var  bool */
71    private $tpl_is_update;
72    /** @var  int */
73    private $max_upload_csv_size;
74
75    /**
76     * Page を初期化する.
77     *
78     * @return void
79     */
80    public function init()
81    {
82        parent::init();
83        $this->tpl_mainpage = 'products/upload_csv_category.tpl';
84        $this->tpl_mainno   = 'products';
85        $this->tpl_subno    = 'upload_csv_category';
86        $this->tpl_maintitle = '商品管理';
87        $this->tpl_subtitle = 'カテゴリ登録CSV';
88        $this->csv_id = '5';
89
90        $masterData = new SC_DB_MasterData_Ex();
91        $this->arrAllowedTag = $masterData->getMasterData('mtb_allowed_tag');
92        $this->arrTagCheckItem = array();
93    }
94
95    /**
96     * Page のプロセス.
97     *
98     * @return void
99     */
100    public function process()
101    {
102        $this->action();
103        $this->sendResponse();
104    }
105
106    /**
107     * Page のアクション.
108     *
109     * @return void
110     */
111    public function action()
112    {
113        // CSV管理ヘルパー
114        $objCSV = new SC_Helper_CSV_Ex();
115        // CSV構造読み込み
116        $arrCSVFrame = $objCSV->sfGetCsvOutput($this->csv_id);
117
118        // CSV構造がインポート可能かのチェック
119        if (!$objCSV->sfIsImportCSVFrame($arrCSVFrame)) {
120            // 無効なフォーマットなので初期状態に強制変更
121            $arrCSVFrame = $objCSV->sfGetCsvOutput($this->csv_id, '', array(), 'no');
122            $this->tpl_is_format_default = true;
123        }
124        // CSV構造は更新可能なフォーマットかのフラグ取得
125        $this->tpl_is_update = $objCSV->sfIsUpdateCSVFrame($arrCSVFrame);
126
127        // CSVファイルアップロード情報の初期化
128        $objUpFile = new SC_UploadFile_Ex(IMAGE_TEMP_REALDIR, IMAGE_SAVE_REALDIR);
129        $this->lfInitFile($objUpFile);
130
131        // パラメーター情報の初期化
132        $objFormParam = new SC_FormParam_Ex();
133        $this->lfInitParam($objFormParam, $arrCSVFrame);
134
135        $this->max_upload_csv_size = SC_Utils_Ex::getUnitDataSize(CSV_SIZE);
136
137        $objFormParam->setHtmlDispNameArray();
138        $this->arrTitle = $objFormParam->getHtmlDispNameArray();
139
140        switch ($this->getMode()) {
141            case 'csv_upload':
142                $this->doUploadCsv($objFormParam, $objUpFile);
143                break;
144            default:
145                break;
146        }
147
148    }
149
150    /**
151     * 登録/編集結果のメッセージをプロパティへ追加する
152     *
153     * @param  integer $line_count 行数
154     * @param  string  $message    メッセージ
155     * @return void
156     */
157    public function addRowResult($line_count, $message)
158    {
159        $this->arrRowResult[] = $line_count . '行目:' . $message;
160    }
161
162    /**
163     * 登録/編集結果のエラーメッセージをプロパティへ追加する
164     *
165     * @param  integer $line_count 行数
166     * @param  string  $message    メッセージ
167     * @return void
168     */
169    public function addRowErr($line_count, $message)
170    {
171        $this->arrRowErr[] = $line_count . '行目:' . $message;
172    }
173
174    /**
175     * CSVアップロードを実行する
176     *
177     * @param  SC_FormParam  $objFormParam
178     * @param  SC_UploadFile $objUpFile
179     * @return void
180     */
181    public function doUploadCsv(&$objFormParam, &$objUpFile)
182    {
183        // ファイルアップロードのチェック
184        $objUpFile->makeTempFile('csv_file');
185        $arrErr = $objUpFile->checkExists();
186        if (count($arrErr) > 0) {
187            $this->arrErr = $arrErr;
188
189            return;
190        }
191        // 一時ファイル名の取得
192        $filepath = $objUpFile->getTempFilePath('csv_file');
193        // CSVファイルの文字コード変換
194        $enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath, CHAR_CODE, CSV_TEMP_REALDIR);
195        // CSVファイルのオープン
196        $fp = fopen($enc_filepath, 'r');
197        // 失敗した場合はエラー表示
198        if (!$fp) {
199            SC_Utils_Ex::sfDispError('');
200        }
201
202        // 登録先テーブル カラム情報の初期化
203        $this->lfInitTableInfo();
204
205        // 登録フォーム カラム情報
206        $this->arrFormKeyList = $objFormParam->getKeyList();
207
208        // 登録対象の列数
209        $col_max_count = $objFormParam->getCount();
210        // 行数
211        $line_count = 0;
212
213        $objQuery =& SC_Query_Ex::getSingletonInstance();
214        $objQuery->begin();
215
216        $errFlag = false;
217
218        while (!feof($fp)) {
219            $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
220            // 行カウント
221            $line_count++;
222            // ヘッダ行はスキップ
223            if ($line_count == 1) {
224                continue;
225            }
226            // 空行はスキップ
227            if (empty($arrCSV)) {
228                continue;
229            }
230            // 列数が異なる場合はエラー
231            $col_count = count($arrCSV);
232            if ($col_max_count != $col_count) {
233                $this->addRowErr($line_count, '※ 項目数が' . $col_count . '個検出されました。項目数は' . $col_max_count . '個になります。');
234                $errFlag = true;
235                break;
236            }
237            // シーケンス配列を格納する。
238            $objFormParam->setParam($arrCSV, true);
239            // 入力値の変換
240            $objFormParam->convParam();
241            // <br>なしでエラー取得する。
242            $arrCSVErr = $this->lfCheckError($objFormParam);
243
244            // 入力エラーチェック
245            if (count($arrCSVErr) > 0) {
246                foreach ($arrCSVErr as $err) {
247                    $this->addRowErr($line_count, $err);
248                }
249                $errFlag = true;
250                break;
251            }
252
253            $category_id = $this->lfRegisterCategory($line_count, $objFormParam);
254            $this->addRowResult($line_count, 'カテゴリID:'.$category_id . ' / カテゴリ名:' . $objFormParam->getValue('category_name'));
255        }
256
257        // 実行結果画面を表示
258        $this->tpl_mainpage = 'products/upload_csv_category_complete.tpl';
259
260        fclose($fp);
261
262        if ($errFlag) {
263            $objQuery->rollback();
264
265            return;
266        }
267
268        $objQuery->commit();
269
270        // カテゴリ件数を更新
271        $objDb = new SC_Helper_DB_Ex();
272        $objDb->sfCountCategory($objQuery);
273
274        return;
275    }
276
277    /**
278     * ファイル情報の初期化を行う.
279     *
280     * @param SC_UploadFile $objUpFile
281     * @return void
282     */
283    public function lfInitFile(SC_UploadFile &$objUpFile)
284    {
285        $objUpFile->addFile('CSVファイル', 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false);
286    }
287
288    /**
289     * 入力情報の初期化を行う.
290     *
291     * @param SC_FormParam $objFormParam
292     * @param array $arrCSVFrame CSV構造設定配列
293     * @return void
294     */
295    public function lfInitParam(SC_FormParam &$objFormParam, &$arrCSVFrame)
296    {
297        // 固有の初期値調整
298        $arrCSVFrame = $this->lfSetParamDefaultValue($arrCSVFrame);
299        // CSV項目毎の処理
300        foreach ($arrCSVFrame as $item) {
301            if ($item['status'] == CSV_COLUMN_STATUS_FLG_DISABLE) continue;
302            //サブクエリ構造の場合は AS名 を使用
303            if (preg_match_all('/\(.+\) as (.+)$/i', $item['col'], $match, PREG_SET_ORDER)) {
304                $col = $match[0][1];
305            } else {
306                $col = $item['col'];
307            }
308            // HTML_TAG_CHECKは別途実行なので除去し、別保存しておく
309            if (strpos(strtoupper($item['error_check_types']), 'HTML_TAG_CHECK') !== FALSE) {
310                $this->arrTagCheckItem[] = $item;
311                $error_check_types = str_replace('HTML_TAG_CHECK', '', $item['error_check_types']);
312            } else {
313                $error_check_types = $item['error_check_types'];
314            }
315            $arrErrorCheckTypes = explode(',', $error_check_types);
316            foreach ($arrErrorCheckTypes as $key => $val) {
317                if (trim($val) == '') {
318                    unset($arrErrorCheckTypes[$key]);
319                } else {
320                    $arrErrorCheckTypes[$key] = trim($val);
321                }
322            }
323            // パラメーター登録
324            $objFormParam->addParam(
325                    $item['disp_name'],
326                    $col,
327                    constant($item['size_const_type']),
328                    $item['mb_convert_kana_option'],
329                    $arrErrorCheckTypes,
330                    $item['default'],
331                    $item['rw_flg'] != CSV_COLUMN_RW_FLG_READ_ONLY
332                    );
333        }
334    }
335
336    /**
337     * 入力チェックを行う.
338     *
339     * @param SC_FormParam $objFormParam
340     * @return array
341     */
342    public function lfCheckError(SC_FormParam &$objFormParam)
343    {
344        // 入力データを渡す。
345        $arrRet =  $objFormParam->getHashArray();
346        $objErr = new SC_CheckError_Ex($arrRet);
347        $objErr->arrErr = $objFormParam->checkError(false);
348        // HTMLタグチェックの実行
349        foreach ($this->arrTagCheckItem as $item) {
350            $objErr->doFunc(array($item['disp_name'], $item['col'], $this->arrAllowedTag), array('HTML_TAG_CHECK'));
351        }
352        // このフォーム特有の複雑系のエラーチェックを行う
353        if (count($objErr->arrErr) == 0) {
354            $objErr->arrErr = $this->lfCheckErrorDetail($arrRet, $objErr->arrErr);
355        }
356
357        return $objErr->arrErr;
358    }
359
360    /**
361     * 保存先テーブル情報の初期化を行う.
362     *
363     * @return void
364     */
365    public function lfInitTableInfo()
366    {
367        $objQuery =& SC_Query_Ex::getSingletonInstance();
368        $this->arrRegistColumn = $objQuery->listTableFields('dtb_category');
369    }
370
371    /**
372     * カテゴリ登録を行う.
373     *
374     * FIXME: 登録の実処理自体は、LC_Page_Admin_Products_Categoryと共通化して欲しい。
375     *
376     * @param  integer $line 処理中の行数
377     * @param SC_FormParam $objFormParam
378     * @return integer        カテゴリID
379     */
380    public function lfRegisterCategory($line, SC_FormParam &$objFormParam)
381    {
382        // 登録データ対象取得
383        $arrList = $objFormParam->getDbArray();
384
385        // 登録情報を生成する。
386        // テーブルのカラムに存在しているもののうち、Form投入設定されていないデータは上書きしない。
387        $sqlval = SC_Utils_Ex::sfArrayIntersectKeys($arrList, $this->arrRegistColumn);
388
389        // 必須入力では無い項目だが、空文字では問題のある特殊なカラム値の初期値設定
390        $sqlval = $this->lfSetCategoryDefaultData($sqlval);
391
392        $objCategory = new SC_Helper_Category_Ex();
393        $category_id = $objCategory->save($sqlval);
394
395        return $category_id;
396    }
397
398    /**
399     * 初期値の設定
400     *
401     * @param  array $arrCSVFrame CSV構造配列
402     * @return array $arrCSVFrame CSV構造配列
403     */
404    public function lfSetParamDefaultValue(&$arrCSVFrame)
405    {
406        foreach ($arrCSVFrame as $key => $val) {
407            switch ($val['col']) {
408                case 'parent_category_id':
409                    $arrCSVFrame[$key]['default'] = '0';
410                    break;
411                case 'del_flg':
412                    $arrCSVFrame[$key]['default'] = '0';
413                    break;
414                default:
415                    break;
416            }
417        }
418
419        return $arrCSVFrame;
420    }
421
422    /**
423     * データ登録前に特殊な値の持ち方をする部分のデータ部分の初期値補正を行う
424     *
425     * @param array $sqlval 商品登録情報配列
426     * @return array $sqlval 登録情報配列
427     */
428    public function lfSetCategoryDefaultData(&$sqlval)
429    {
430        if ($sqlval['del_flg'] == '') {
431            $sqlval['del_flg'] = '0'; //有効
432        }
433        if ($sqlval['creator_id'] == '') {
434            $sqlval['creator_id'] = $_SESSION['member_id'];
435        }
436        if ($sqlval['parent_category_id'] == '') {
437            $sqlval['parent_category_id'] = (string) '0';
438        }
439
440        return $sqlval;
441    }
442
443    /**
444     * このフォーム特有の複雑な入力チェックを行う.
445     *
446     * @param array $item 確認対象データ
447     * @param array $arrErr エラー配列
448     * @return array エラー配列
449     */
450    public function lfCheckErrorDetail($item, $arrErr)
451    {
452        $objCategory = new SC_Helper_Category_Ex();
453        // スタティック変数を初期化
454        $objCategory->getTree(true);
455        /*
456        // カテゴリIDの存在チェック
457        if (!$this->lfIsDbRecord('dtb_category', 'category_id', $item)) {
458            $arrErr['category_id'] = '※ 指定のカテゴリIDは、登録されていません。';
459        }
460        */
461        // 親カテゴリIDの存在チェック
462        if (array_search('parent_category_id', $this->arrFormKeyList) !== FALSE
463            && $item['parent_category_id'] != ''
464            && $item['parent_category_id'] != '0'
465            && !$objCategory->get($item['parent_category_id'])
466        ) {
467            $arrErr['parent_category_id'] = '※ 指定の親カテゴリID(' . $item['parent_category_id'] . ')は、存在しません。';
468        }
469        // 削除フラグのチェック
470        if (array_search('del_flg', $this->arrFormKeyList) !== FALSE
471            && $item['del_flg'] != ''
472        ) {
473            if (!($item['del_flg'] == '0' or $item['del_flg'] == '1')) {
474                $arrErr['del_flg'] = '※ 削除フラグは「0」(有効)、「1」(削除)のみが有効な値です。';
475            }
476        }
477        // 重複チェック 同じカテゴリ内に同名の存在は許可されない
478        if (array_search('category_name', $this->arrFormKeyList) !== FALSE
479            && $item['category_name'] != ''
480        ) {
481            $exists = false;
482            $arrBrother = $objCategory->getTreeBranch($item['parent_category_id']);
483            foreach ($arrBrother as $brother) {
484                if ($brother['category_name'] == $item['category_name'] && $brother['category_id'] != $item['category_id']) {
485                    $exists = true;
486                }
487            }
488            if ($exists) {
489                $arrErr['category_name'] = '※ 既に同名のカテゴリが存在します。';
490            }
491        }
492        // 登録数上限チェック
493        $count = count($objCategory->getList());
494        if ($count >= CATEGORY_MAX) {
495            $item['category_name'] = '※ カテゴリの登録最大数を超えました。';
496        }
497
498        if (array_search('parent_category_id', $this->arrFormKeyList) !== FALSE
499                and $item['parent_category_id'] != '') {
500            // 階層上限チェック
501            $arrParent = $objCategory->get($item['parent_category_id']);
502            if ($arrParent['level'] >= LEVEL_MAX) {
503                $arrErr['parent_category_id'] = '※ ' . LEVEL_MAX . '階層以上の登録はできません。';
504            }
505            // 親カテゴリー論理チェック
506            if (array_search('category_id', $this->arrFormKeyList) !== FALSE
507                and $item['category_id'] != '') {
508                $arrTrail = $objCategory->getTreeTrail($item['parent_category_id'], true);
509                foreach ($arrTrail as $trailId) {
510                    if ($trailId == $item['category_id']) {
511                        $arrErr['parent_category_id'] = '※ 再帰的な親カテゴリーの指定はできません。';
512                    }
513                }
514            }
515        }
516
517
518        return $arrErr;
519    }
520}
Note: See TracBrowser for help on using the repository browser.