source: branches/version-2_13_3/data/class/pages/admin/products/LC_Page_Admin_Products_UploadCSV.php @ 23659

Revision 23659, 36.7 KB checked in by shinichi_takahashi, 9 years ago (diff)

rev23642 rev23642をマージ

#2634 アップロード先をCSV_TEMP_REALDIRに変更
#2634 インデント修正

  • 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 * @package Page
30 * @author LOCKON CO.,LTD.
31 * @version $Id:LC_Page_Admin_Products_UploadCSV.php 15532 2007-08-31 14:39:46Z nanasess $
32 *
33 * FIXME 同一商品IDで商品規格違いを登録できない。(更新は可能)
34 */
35class LC_Page_Admin_Products_UploadCSV extends LC_Page_Admin_Ex
36{
37    /** TAGエラーチェックフィールド情報 */
38    public $arrTagCheckItem;
39
40    /** 商品テーブルカラム情報 (登録処理用) **/
41    public $arrProductColumn;
42
43    /** 商品規格テーブルカラム情報 (登録処理用) **/
44    public $arrProductClassColumn;
45
46    /** 登録フォームカラム情報 **/
47    public $arrFormKeyList;
48
49    public $arrRowErr;
50
51    public $arrRowResult;
52
53    /**
54     * Page を初期化する.
55     *
56     * @return void
57     */
58    public function init()
59    {
60        parent::init();
61        $this->tpl_mainpage = 'products/upload_csv.tpl';
62        $this->tpl_mainno = 'products';
63        $this->tpl_subno = 'upload_csv';
64        $this->tpl_maintitle = '商品管理';
65        $this->tpl_subtitle = '商品登録CSV';
66        $this->csv_id = '1';
67
68        $masterData = new SC_DB_MasterData_Ex();
69        $this->arrDISP = $masterData->getMasterData('mtb_disp');
70        $this->arrSTATUS = $masterData->getMasterData('mtb_status');
71        $this->arrDELIVERYDATE = $masterData->getMasterData('mtb_delivery_date');
72        $this->arrProductType = $masterData->getMasterData('mtb_product_type');
73        $this->arrMaker = SC_Helper_Maker_Ex::getIDValueList();
74        $this->arrPayments = SC_Helper_Payment_Ex::getIDValueList();
75        $this->arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
76        $this->arrAllowedTag = $masterData->getMasterData('mtb_allowed_tag');
77        $this->arrTagCheckItem = array();
78    }
79
80    /**
81     * Page のプロセス.
82     *
83     * @return void
84     */
85    public function process()
86    {
87        $this->action();
88        $this->sendResponse();
89    }
90
91    /**
92     * Page のアクション.
93     *
94     * @return void
95     */
96    public function action()
97    {
98        $this->objDb = new SC_Helper_DB_Ex();
99
100        // CSV管理ヘルパー
101        $objCSV = new SC_Helper_CSV_Ex();
102        // CSV構造読み込み
103        $arrCSVFrame = $objCSV->sfGetCsvOutput($this->csv_id);
104
105        // CSV構造がインポート可能かのチェック
106        if (!$objCSV->sfIsImportCSVFrame($arrCSVFrame)) {
107            // 無効なフォーマットなので初期状態に強制変更
108            $arrCSVFrame = $objCSV->sfGetCsvOutput($this->csv_id, '', array(), 'no');
109            $this->tpl_is_format_default = true;
110        }
111        // CSV構造は更新可能なフォーマットかのフラグ取得
112        $this->tpl_is_update = $objCSV->sfIsUpdateCSVFrame($arrCSVFrame);
113
114        // CSVファイルアップロード情報の初期化
115        $objUpFile = new SC_UploadFile_Ex(CSV_TEMP_REALDIR, CSV_TEMP_REALDIR);
116        $this->lfInitFile($objUpFile);
117
118        // パラメーター情報の初期化
119        $objFormParam = new SC_FormParam_Ex();
120        $this->lfInitParam($objFormParam, $arrCSVFrame);
121
122        $this->max_upload_csv_size = SC_Utils_Ex::getUnitDataSize(CSV_SIZE);
123
124        $objFormParam->setHtmlDispNameArray();
125        $this->arrTitle = $objFormParam->getHtmlDispNameArray();
126
127        switch ($this->getMode()) {
128            case 'csv_upload':
129                $this->doUploadCsv($objFormParam, $objUpFile);
130                break;
131            default:
132                break;
133        }
134
135    }
136
137    /**
138     * 登録/編集結果のメッセージをプロパティへ追加する
139     *
140     * @param  integer $line_count 行数
141     * @param  stirng  $message    メッセージ
142     * @return void
143     */
144    public function addRowResult($line_count, $message)
145    {
146        $this->arrRowResult[] = $line_count . '行目:' . $message;
147    }
148
149    /**
150     * 登録/編集結果のエラーメッセージをプロパティへ追加する
151     *
152     * @param  integer $line_count 行数
153     * @param  stirng  $message    メッセージ
154     * @return void
155     */
156    public function addRowErr($line_count, $message)
157    {
158        $this->arrRowErr[] = $line_count . '行目:' . $message;
159    }
160
161    /**
162     * CSVファイルを読み込んで、保存処理を行う
163     *
164     * @param $objFormParam
165     * @param $fp CSVファイルポインタ
166     * @param $objQuery 保存を行うためのクエリ(指定がない場合、テストのみを行う)
167     * @return boolean errFlag. 読み込みに失敗した場合true
168     */
169    public function lfReadCSVFile(&$objFormParam, &$fp, $objQuery = null)
170    {
171        $dry_run = ($objQuery===null) ? true : false;
172        // 登録対象の列数
173        $col_max_count = $objFormParam->getCount();
174        // 行数
175        $line_count = 0;
176        // 処理に失敗した場合にtrue
177        $errFlag = false;
178
179        while (!feof($fp)) {
180            $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
181
182            // 行カウント
183            $line_count++;
184            // ヘッダ行はスキップ
185            if ($line_count == 1) {
186                continue;
187            }
188            // 空行はスキップ
189            if (empty($arrCSV)) {
190                continue;
191            }
192            // 列数が多すぎる場合はエラー、列数が少ない場合は未設定として配列を補う
193            $col_count = count($arrCSV);
194            if ($col_count > $col_max_count) {
195                $this->addRowErr($line_count, '※ 項目数が' . $col_count . '個検出されました。項目数は' . $col_max_count . '個になります。');
196                $errFlag = true;
197                break;
198            } elseif ($col_count < $col_max_count) {
199                $arrCSV = array_pad($arrCSV, $col_max_count, "");
200                if (!$dry_run) {
201                    $this->addRowResult($line_count, ($col_count + 1) . "項目以降を空欄として読み込みました");
202                }
203            }
204
205            // シーケンス配列を格納する。
206            $objFormParam->setParam($arrCSV, true);
207            // 入力値の変換
208            $objFormParam->convParam();
209
210            // 商品IDが設定されており、規格IDが設定されていなければ、既存の規格ID取得を試みる(product_class_idは必須入力項目ではない)
211            $product_id = $objFormParam->getValue('product_id');
212            $product_class_id = $objFormParam->getValue('product_class_id');
213            if ($product_class_id == '' && $product_id != '') {
214                $product_class_id = SC_Utils_Ex::sfGetProductClassId($product_id, $objFormParam->getValue('classcategory_id1'), $objFormParam->getValue('classcategory_id2'));
215                $objFormParam->setValue('product_class_id', $product_class_id);
216            }
217
218            // <br>なしでエラー取得する。
219            $arrCSVErr = $this->lfCheckError($objFormParam);
220            if (count($arrCSVErr) > 0) {
221                foreach ($arrCSVErr as $err) {
222                    $this->addRowErr($line_count, $err);
223                }
224                $errFlag = true;
225                break;
226            }
227
228            if (!$dry_run) {
229                $this->lfRegistProduct($objQuery, $line_count, $objFormParam);
230                $arrParam = $objFormParam->getHashArray();
231
232                $this->addRowResult($line_count, '商品ID:'.$arrParam['product_id'] . ' / 商品名:' . $arrParam['name']);
233            }
234            SC_Utils_Ex::extendTimeOut();
235        }
236
237        return $errFlag;
238    }
239
240    /**
241     * CSVアップロードを実行します.
242     *
243     * @return void
244     */
245    public function doUploadCsv(&$objFormParam, &$objUpFile)
246    {
247        // ファイルアップロードのチェック
248        $this->arrErr['csv_file'] = $objUpFile->makeTempFile('csv_file');
249        if (strlen($this->arrErr['csv_file']) >= 1) {
250            return;
251        }
252        $arrErr = $objUpFile->checkExists();
253        if (count($arrErr) > 0) {
254            $this->arrErr = $arrErr;
255
256            return;
257        }
258        // 一時ファイル名の取得
259        $filepath = $objUpFile->getTempFilePath('csv_file');
260        // CSVファイルの文字コード変換
261        $enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath, CHAR_CODE, CSV_TEMP_REALDIR);
262        // CSVファイルのオープン
263        $fp = fopen($enc_filepath, 'r');
264        // 失敗した場合はエラー表示
265        if (!$fp) {
266            SC_Utils_Ex::sfDispError('');
267        }
268
269        // 登録先テーブル カラム情報の初期化
270        $this->lfInitTableInfo();
271
272        // 登録フォーム カラム情報
273        $this->arrFormKeyList = $objFormParam->getKeyList();
274
275        $objQuery =& SC_Query_Ex::getSingletonInstance();
276        $objQuery->begin();
277
278        // CSVからの読み込み、入力エラーチェック
279        $errFlag = $this->lfReadCSVFile($objFormParam, $fp);
280        if (!$errFlag) {
281            rewind($fp);
282            // CSVからの読み込み、保存
283            $errFlag = $this->lfReadCSVFile($objFormParam, $fp, $objQuery);
284        }
285
286        // 実行結果画面を表示
287        $this->tpl_mainpage = 'products/upload_csv_complete.tpl';
288
289        fclose($fp);
290
291        if ($errFlag) {
292            $objQuery->rollback();
293
294            return;
295        }
296
297        $objQuery->commit();
298
299        // 商品件数カウント関数の実行
300        $this->objDb->sfCountCategory($objQuery);
301        $this->objDb->sfCountMaker($objQuery);
302    }
303
304    /**
305     * ファイル情報の初期化を行う.
306     *
307     * @return void
308     */
309    public function lfInitFile(&$objUpFile)
310    {
311        $objUpFile->addFile('CSVファイル', 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false);
312    }
313
314    /**
315     * 入力情報の初期化を行う.
316     *
317     * @param array CSV構造設定配列
318     * @return void
319     */
320    public function lfInitParam(&$objFormParam, &$arrCSVFrame)
321    {
322        // 固有の初期値調整
323        $arrCSVFrame = $this->lfSetParamDefaultValue($arrCSVFrame);
324        // CSV項目毎の処理
325        foreach ($arrCSVFrame as $item) {
326            if ($item['status'] == CSV_COLUMN_STATUS_FLG_DISABLE) continue;
327            //サブクエリ構造の場合は AS名 を使用
328            if (preg_match_all('/\(.+\)\s+as\s+(.+)$/i', $item['col'], $match, PREG_SET_ORDER)) {
329                $col = $match[0][1];
330            } else {
331                $col = $item['col'];
332            }
333            // HTML_TAG_CHECKは別途実行なので除去し、別保存しておく
334            if (strpos(strtoupper($item['error_check_types']), 'HTML_TAG_CHECK') !== FALSE) {
335                $this->arrTagCheckItem[] = $item;
336                $error_check_types = str_replace('HTML_TAG_CHECK', '', $item['error_check_types']);
337            } else {
338                $error_check_types = $item['error_check_types'];
339            }
340            $arrErrorCheckTypes = explode(',', $error_check_types);
341            foreach ($arrErrorCheckTypes as $key => $val) {
342                if (trim($val) == '') {
343                    unset($arrErrorCheckTypes[$key]);
344                } else {
345                    $arrErrorCheckTypes[$key] = trim($val);
346                }
347            }
348            // パラメーター登録
349            $objFormParam->addParam(
350                    $item['disp_name'],
351                    $col,
352                    constant($item['size_const_type']),
353                    $item['mb_convert_kana_option'],
354                    $arrErrorCheckTypes,
355                    $item['default'],
356                    $item['rw_flg'] != CSV_COLUMN_RW_FLG_READ_ONLY
357                    );
358        }
359    }
360
361    /**
362     * 入力チェックを行う.
363     *
364     * @return void
365     */
366    public function lfCheckError(&$objFormParam)
367    {
368        // 入力データを渡す。
369        $arrRet =  $objFormParam->getHashArray();
370        $objErr = new SC_CheckError_Ex($arrRet);
371        $objErr->arrErr = $objFormParam->checkError(false);
372        // HTMLタグチェックの実行
373        foreach ($this->arrTagCheckItem as $item) {
374            $objErr->doFunc(array($item['disp_name'], $item['col'], $this->arrAllowedTag), array('HTML_TAG_CHECK'));
375        }
376        // このフォーム特有の複雑系のエラーチェックを行う
377        if (count($objErr->arrErr) == 0) {
378            $objErr->arrErr = $this->lfCheckErrorDetail($arrRet, $objErr->arrErr);
379        }
380
381        return $objErr->arrErr;
382    }
383
384    /**
385     * 保存先テーブル情報の初期化を行う.
386     *
387     * @return void
388     */
389    public function lfInitTableInfo()
390    {
391        $objQuery =& SC_Query_Ex::getSingletonInstance();
392        $this->arrProductColumn = $objQuery->listTableFields('dtb_products');
393        $this->arrProductClassColumn = $objQuery->listTableFields('dtb_products_class');
394    }
395
396    /**
397     * 商品登録を行う.
398     *
399     * FIXME: 商品登録の実処理自体は、LC_Page_Admin_Products_Productと共通化して欲しい。
400     *
401     * @param  SC_Query       $objQuery SC_Queryインスタンス
402     * @param  string|integer $line     処理中の行数
403     * @return void
404     */
405    public function lfRegistProduct($objQuery, $line = '', &$objFormParam)
406    {
407        $objProduct = new SC_Product_Ex();
408        // 登録データ対象取得
409        $arrList = $objFormParam->getDbArray();
410
411        // 登録時間を生成(DBのCURRENT_TIMESTAMPだとcommitした際、全て同一の時間になってしまう)
412        $arrList['update_date'] = $this->lfGetDbFormatTimeWithLine($line);
413
414        // 商品登録情報を生成する。
415        // 商品テーブルのカラムに存在しているもののうち、Form投入設定されていないデータは上書きしない。
416        $sqlval = SC_Utils_Ex::sfArrayIntersectKeys($arrList, $this->arrProductColumn);
417
418        // 必須入力では無い項目だが、空文字では問題のある特殊なカラム値の初期値設定
419        $sqlval = $this->lfSetProductDefaultData($sqlval);
420
421        if ($sqlval['product_id'] != '') {
422            // 同じidが存在すればupdate存在しなければinsert
423            $where = 'product_id = ?';
424            $product_exists = $objQuery->exists('dtb_products', $where, array($sqlval['product_id']));
425            if ($product_exists) {
426                $objQuery->update('dtb_products', $sqlval, $where, array($sqlval['product_id']));
427            } else {
428                $sqlval['create_date'] = $arrList['update_date'];
429                // INSERTの実行
430                $objQuery->insert('dtb_products', $sqlval);
431                // シーケンスの調整
432                $seq_count = $objQuery->currVal('dtb_products_product_id');
433                if ($seq_count < $sqlval['product_id']) {
434                    $objQuery->setVal('dtb_products_product_id', $sqlval['product_id'] + 1);
435                }
436            }
437            $product_id = $sqlval['product_id'];
438        } else {
439            // 新規登録
440            $sqlval['product_id'] = $objQuery->nextVal('dtb_products_product_id');
441            $product_id = $sqlval['product_id'];
442            $sqlval['create_date'] = $arrList['update_date'];
443            // INSERTの実行
444            $objQuery->insert('dtb_products', $sqlval);
445        }
446
447        // カテゴリ登録
448        if (isset($arrList['category_ids'])) {
449            $arrCategory_id = explode(',', $arrList['category_ids']);
450            $this->objDb->updateProductCategories($arrCategory_id, $product_id);
451        }
452        // 商品ステータス登録
453        if (isset($arrList['product_statuses'])) {
454            $arrStatus_id = explode(',', $arrList['product_statuses']);
455            $objProduct->setProductStatus($product_id, $arrStatus_id);
456        }
457
458        // 商品規格情報を登録する
459        $this->lfRegistProductClass($objQuery, $arrList, $product_id, $arrList['product_class_id']);
460
461        // 関連商品登録
462        $this->lfRegistReccomendProducts($objQuery, $arrList, $product_id);
463    }
464
465    /**
466     * 商品規格登録を行う.
467     *
468     * FIXME: 商品規格登録の実処理自体は、LC_Page_Admin_Products_Productと共通化して欲しい。
469     *
470     * @param  SC_Query $objQuery         SC_Queryインスタンス
471     * @param  array    $arrList          商品規格情報配列
472     * @param  integer  $product_id       商品ID
473     * @param  integer  $product_class_id 商品規格ID
474     * @return void
475     */
476    public function lfRegistProductClass($objQuery, $arrList, $product_id, $product_class_id)
477    {
478        $objProduct = new SC_Product_Ex();
479
480        // FIXME: dtb_csvテーブルの中で古いカラム名(右辺)が設定されている。sfArrayIntersectKeysでフィルタされてしまうので、名称を変更する必要がある
481        if (array_key_exists('classcategory_id', $arrList) && $arrList['classcategory_id'] != '') {
482            $arrList['classcategory_id1'] = $arrList['classcategory_id'];
483        }
484        if (array_key_exists('parent_classcategory_id', $arrList) && $arrList['classcategory_id'] != '') {
485            $arrList['classcategory_id2'] = $arrList['parent_classcategory_id'];
486        }
487
488        // 商品規格登録情報を生成する。
489        // 商品規格テーブルのカラムに存在しているもののうち、Form投入設定されていないデータは上書きしない。
490        $sqlval = SC_Utils_Ex::sfArrayIntersectKeys($arrList, $this->arrProductClassColumn);
491
492        if ($product_class_id == '') {
493            // 新規登録
494            // 必須入力では無い項目だが、空文字では問題のある特殊なカラム値の初期値設定
495            $sqlval = $this->lfSetProductClassDefaultData($sqlval);
496            $sqlval['product_id'] = $product_id;
497            $sqlval['product_class_id'] = $objQuery->nextVal('dtb_products_class_product_class_id');
498            $sqlval['create_date'] = $arrList['update_date'];
499            // INSERTの実行
500            $objQuery->insert('dtb_products_class', $sqlval);
501            $product_class_id = $sqlval['product_class_id'];
502        } else {
503            // UPDATEの実行
504            // 必須入力では無い項目だが、空文字では問題のある特殊なカラム値の初期値設定
505            $sqlval = $this->lfSetProductClassDefaultData($sqlval, true);
506            $where = 'product_class_id = ?';
507            $objQuery->update('dtb_products_class', $sqlval, $where, array($product_class_id));
508        }
509    }
510
511    /**
512     * 関連商品登録を行う.
513     *
514     * FIXME: 商品規格登録の実処理自体は、LC_Page_Admin_Products_Productと共通化して欲しい。
515     *        DELETE/INSERT ではなく UPDATEへの変更も・・・
516     *
517     * @param  SC_Query $objQuery   SC_Queryインスタンス
518     * @param  array    $arrList    商品規格情報配列
519     * @param  integer  $product_id 商品ID
520     * @return void
521     */
522    public function lfRegistReccomendProducts($objQuery, $arrList, $product_id)
523    {
524        $objQuery->delete('dtb_recommend_products', 'product_id = ?', array($product_id));
525        for ($i = 1; $i <= RECOMMEND_PRODUCT_MAX; $i++) {
526            $keyname = 'recommend_product_id' . $i;
527            $comment_key = 'recommend_comment' . $i;
528            if ($arrList[$keyname] != '') {
529                $arrProduct = $objQuery->select('product_id', 'dtb_products', 'product_id = ?', array($arrList[$keyname]));
530                if ($arrProduct[0]['product_id'] != '') {
531                    $arrWhereVal = array();
532                    $arrWhereVal['product_id'] = $product_id;
533                    $arrWhereVal['recommend_product_id'] = $arrProduct[0]['product_id'];
534                    $arrWhereVal['comment'] = $arrList[$comment_key];
535                    $arrWhereVal['update_date'] = $arrList['update_date'];
536                    $arrWhereVal['create_date'] = $arrList['update_date'];
537                    $arrWhereVal['creator_id'] = $_SESSION['member_id'];
538                    $arrWhereVal['rank'] = RECOMMEND_PRODUCT_MAX - $i + 1;
539                    $objQuery->insert('dtb_recommend_products', $arrWhereVal);
540                }
541            }
542        }
543    }
544
545    /**
546     * 初期値の設定
547     *
548     * @param  array $arrCSVFrame CSV構造配列
549     * @return array $arrCSVFrame CSV構造配列
550     */
551    public function lfSetParamDefaultValue(&$arrCSVFrame)
552    {
553        foreach ($arrCSVFrame as $key => $val) {
554            switch ($val['col']) {
555                case 'status':
556                    $arrCSVFrame[$key]['default'] = DEFAULT_PRODUCT_DISP;
557                    break;
558                case 'del_flg':
559                    $arrCSVFrame[$key]['default'] = '0';
560                    break;
561                case 'point_rate':
562                    $arrCSVFrame[$key]['default'] = $this->arrInfo['point_rate'];
563                    break;
564                case 'product_type_id':
565                    $arrCSVFrame[$key]['default'] = DEFAULT_PRODUCT_DOWN;
566                    break;
567                case 'stock_unlimited':
568                    $arrCSVFrame[$key]['default'] = UNLIMITED_FLG_LIMITED;
569                default:
570                    break;
571            }
572        }
573
574        return $arrCSVFrame;
575    }
576
577    /**
578     * 商品データ登録前に特殊な値の持ち方をする部分のデータ部分の初期値補正を行う
579     *
580     * @param array $sqlval 商品登録情報配列
581     * @return $sqlval 登録情報配列
582     */
583    public function lfSetProductDefaultData(&$sqlval)
584    {
585        //新規登録時のみ設定する項目
586        if ($sqlval['product_id'] == '') {
587            if ($sqlval['status'] == '') {
588                $sqlval['status'] = DEFAULT_PRODUCT_DISP;
589            }
590        }
591        //共通で空欄時に上書きする項目
592        if ($sqlval['del_flg'] == '') {
593            $sqlval['del_flg'] = '0'; //有効
594        }
595        if ($sqlval['creator_id'] == '') {
596            $sqlval['creator_id'] = $_SESSION['member_id'];
597        }
598
599        return $sqlval;
600    }
601
602    /**
603     * 商品規格データ登録前に特殊な値の持ち方をする部分のデータ部分の初期値補正を行う
604     *
605     * @param array   $sqlval     商品登録情報配列
606     * @param boolean $upload_flg 更新フラグ(更新の場合true)
607     * @return $sqlval 登録情報配列
608     */
609    public function lfSetProductClassDefaultData(&$sqlval, $upload_flg = false)
610    {
611        //新規登録時のみ設定する項目
612        if ($sqlval['product_class_id'] == '') {
613            if ($sqlval['point_rate'] == '') {
614                $sqlval['point_rate'] = $this->arrInfo['point_rate'];
615            }
616            if ($sqlval['product_type_id'] == '') {
617                $sqlval['product_type_id'] = DEFAULT_PRODUCT_DOWN;
618            }
619        } else {
620            // 更新時に値を明示的に指定していない場合にそのままにする
621            if ($sqlval['point_rate'] == '') {
622                unset($sqlval['point_rate']);
623            }
624            if ($sqlval['product_type_id'] == '') {
625                unset($sqlval['product_type_id']);
626            }
627        }
628        //共通で設定する項目
629        if ($sqlval['del_flg'] == '') {
630            $sqlval['del_flg'] = '0'; //有効
631        }
632        if ($sqlval['creator_id'] == '') {
633            $sqlval['creator_id'] = $_SESSION['member_id'];
634        }
635
636        // 在庫無制限フラグ列を利用する場合、
637        if (array_key_exists('stock_unlimited', $sqlval) and $sqlval['stock_unlimited'] != '') {
638            // 在庫無制限フラグ = 無制限の場合、
639            if ($sqlval['stock_unlimited'] == UNLIMITED_FLG_UNLIMITED) {
640                $sqlval['stock'] = null;
641            }
642        } else {
643            // 初期登録の場合は、在庫数設定がされていない場合、在庫無制限フラグ = 無制限。
644            if (strlen($sqlval['stock']) === 0) {
645                //更新の場合は、sqlvalのキーにstockがある場合のみ対象
646                if (!$upload_flg or ($upload_flg and array_key_exists('stock', $sqlval))) {
647                    $sqlval['stock_unlimited'] = UNLIMITED_FLG_UNLIMITED;
648                }
649            // 在庫数を入力している場合、在庫無制限フラグ = 制限有り
650            } elseif (strlen($sqlval['stock']) >= 1) {
651                $sqlval['stock_unlimited'] = UNLIMITED_FLG_LIMITED;
652            // いずれにも該当しない場合、例外エラー
653            } else {
654                trigger_error('', E_USER_ERROR);
655            }
656        }
657
658        return $sqlval;
659    }
660
661    /**
662     * このフォーム特有の複雑な入力チェックを行う.
663     *
664     * @param array 確認対象データ
665     * @param array エラー配列
666     * @return array エラー配列
667     */
668    public function lfCheckErrorDetail($item, $arrErr)
669    {
670        // 規格IDの存在チェック
671        // FIXME 規格分類ID自体のが有効かを主眼においたチェックをすべきと感じる。
672        if (!$this->lfIsDbRecord('dtb_products_class', 'product_class_id', $item)) {
673            $arrErr['product_class_id'] = '※ 指定の商品規格IDは、登録されていません。';
674        }
675        // 商品ID、規格IDの組合せチェック
676        if (array_search('product_class_id', $this->arrFormKeyList) !== FALSE
677            && $item['product_class_id'] != ''
678        ) {
679            if ($item['product_id'] == '') {
680                $arrErr['product_class_id'] = '※ 商品規格ID指定時には商品IDの指定が必須です。';
681            } else {
682                if (!$this->objDb->sfIsRecord('dtb_products_class', 'product_id, product_class_id',
683                        array($item['product_id'], $item['product_class_id']))
684                ) {
685                    $arrErr['product_class_id'] = '※ 指定の商品IDと商品規格IDの組合せは正しくありません。';
686                }
687
688                // product_class_idは(product_id, classcategory_id1, classcategory_id2)に対して一意。既に異なるproduct_class_idが存在した場合はエラー
689                $classcategory_id1 = $item['classcategory_id'] ? $item['classcategory_id'] : 0;
690                $classcategory_id2 = $item['parent_classcategory_id'] ? $item['parent_classcategory_id'] : 0;
691                $product_class_id = SC_Utils_Ex::sfGetProductClassId($item['product_id'], $classcategory_id1, $classcategory_id2);
692                if ($product_class_id && $product_class_id != $item['product_class_id']) {
693                    $arrErr['product_class_id'] = '※ 指定の商品ID/規格分類と、商品規格IDの組合せは正しくありません。';
694                }
695            }
696        }
697        // 表示ステータスの存在チェック
698        if (!$this->lfIsArrayRecord($this->arrDISP, 'status', $item)) {
699            $arrErr['status'] = '※ 指定の表示ステータスは、登録されていません。';
700        }
701        // メーカーIDの存在チェック
702        if (!$this->lfIsArrayRecord($this->arrMaker, 'maker_id', $item)) {
703            $arrErr['maker_id'] = '※ 指定のメーカーIDは、登録されていません。';
704        }
705        // 発送日目安IDの存在チェック
706        if (!$this->lfIsArrayRecord($this->arrDELIVERYDATE, 'deliv_date_id', $item)) {
707            $arrErr['deliv_date_id'] = '※ 指定の発送日目安IDは、登録されていません。';
708        }
709        // 商品種別IDの存在チェック
710        if (!$this->lfIsArrayRecord($this->arrProductType, 'product_type_id', $item)) {
711            $arrErr['product_type_id'] = '※ 指定の商品種別IDは、登録されていません。';
712        }
713        // 関連商品IDのチェック
714        $arrRecommendProductUnique = array();
715        for ($i = 1; $i <= RECOMMEND_PRODUCT_MAX; $i++) {
716            $recommend_product_id_key = 'recommend_product_id' . $i;
717            if ((array_search($recommend_product_id_key, $this->arrFormKeyList) !== FALSE)
718             && ($item[$recommend_product_id_key] != '')) {
719                // 商品IDの存在チェック
720                if (!$this->objDb->sfIsRecord('dtb_products', 'product_id', (array) $item[$recommend_product_id_key])) {
721                    $arrErr[$recommend_product_id_key] = "※ 指定の関連商品ID($i)は、登録されていません。";
722                    continue;
723                }
724                // 商品IDの重複チェック
725                $recommend_product_id = $item[$recommend_product_id_key];
726                if (isset($arrRecommendProductUnique[$recommend_product_id])) {
727                    $arrErr[$recommend_product_id_key] = "※ 指定の関連商品ID($i)は、すでに登録されています。";
728                } else {
729                    $arrRecommendProductUnique[$recommend_product_id] = true;
730                }
731            }
732        }
733        // カテゴリIDの存在チェック
734        if (!$this->lfIsDbRecordMulti('dtb_category', 'category_id', 'category_ids', $item, ',')) {
735            $arrErr['category_ids'] = '※ 指定のカテゴリIDは、登録されていません。';
736        }
737        // 商品ステータスIDの存在チェック
738        if (!$this->lfIsArrayRecordMulti($this->arrSTATUS, 'product_statuses', $item, ',')) {
739            $arrErr['product_statuses'] = '※ 指定の商品ステータスIDは、登録されていません。';
740        }
741        // 削除フラグのチェック
742        if (array_search('del_flg', $this->arrFormKeyList) !== FALSE
743            && $item['del_flg'] != ''
744        ) {
745            if (!($item['del_flg'] == '0' or $item['del_flg'] == '1')) {
746                $arrErr['del_flg'] = '※ 削除フラグは「0」(有効)、「1」(削除)のみが有効な値です。';
747            }
748        }
749/*
750    TODO: 在庫数の扱いが2.4仕様ではぶれているのでどうするか・・
751        // 在庫数/在庫無制限フラグの有効性に関するチェック
752        if ($item['stock'] == '') {
753            if (array_search('stock_unlimited', $this->arrFormKeyList) === FALSE) {
754                $arrErr['stock'] = '※ 在庫数は必須です(無制限フラグ項目がある場合のみ空欄許可)。';
755            } elseif ($item['stock_unlimited'] != UNLIMITED_FLG_UNLIMITED) {
756                $arrErr['stock'] = '※ 在庫数または在庫無制限フラグのいずれかの入力が必須です。';
757            }
758        }
759*/
760        // ダウンロード商品チェック
761        if (array_search('product_type_id', $this->arrFormKeyList) !== FALSE
762            && $item['product_type_id'] != PRODUCT_TYPE_DOWNLOAD
763        ) {
764            //実商品の場合
765            if ($item['down_filename'] != '') {
766                $arrErr['down_filename'] = '※ ダウンロード商品ではない場合、ダウンロードファイル名は入力できません。';
767            }
768            if ($item['down_realfilename'] != '') {
769                $arrErr['down_realfilename'] = '※ ダウンロード商品ではない場合、ダウンロード商品用ファイルアップロードは入力できません。';
770            }
771        } elseif (array_search('product_type_id', $this->arrFormKeyList) !== FALSE
772                  && $item['product_type_id'] == PRODUCT_TYPE_DOWNLOAD
773        ) {
774            //ダウンロード商品の場合
775            if ($item['down_filename'] == '') {
776                $arrErr['down_filename'] = '※ ダウンロード商品の場合はダウンロードファイル名は必須です。';
777            }
778            if ($item['down_realfilename'] == '') {
779                $arrErr['down_realfilename'] = '※ ダウンロード商品の場合はダウンロード商品用ファイルアップロードは必須です。';
780            }
781        }
782
783        return $arrErr;
784    }
785
786    // TODO: ここから下のルーチンは汎用ルーチンとして移動が望ましい
787
788    /**
789     * 指定された行番号をmicrotimeに付与してDB保存用の時間を生成する。
790     * トランザクション内のCURRENT_TIMESTAMPは全てcommit()時の時間に統一されてしまう為。
791     *
792     * @param  string $line_no 行番号
793     * @return string $time DB保存用の時間文字列
794     */
795    public function lfGetDbFormatTimeWithLine($line_no = '')
796    {
797        $time = date('Y-m-d H:i:s');
798        // 秒以下を生成
799        if ($line_no != '') {
800            $microtime = sprintf('%06d', $line_no);
801            $time .= ".$microtime";
802        }
803
804        return $time;
805    }
806
807    /**
808     * 指定されたキーと複数値の有効性の配列内確認
809     *
810     * @param  string  $arr       チェック対象配列
811     * @param  string  $keyname   フォームキー名
812     * @param  array   $item      入力データ配列
813     * @param  string  $delimiter 分割文字
814     * @return boolean true:有効なデータがある false:有効ではない
815     */
816    public function lfIsArrayRecordMulti($arr, $keyname, $item, $delimiter = ',')
817    {
818        if (array_search($keyname, $this->arrFormKeyList) === FALSE) {
819            return true;
820        }
821        if ($item[$keyname] == '') {
822            return true;
823        }
824        $arrItems = explode($delimiter, $item[$keyname]);
825        //空項目のチェック 1つでも空指定があったら不正とする。
826        if (array_search('', $arrItems) !== FALSE) {
827            return false;
828        }
829        //キー項目への存在チェック
830        foreach ($arrItems as $item) {
831            if (!array_key_exists($item, $arr)) {
832                return false;
833            }
834        }
835
836        return true;
837    }
838
839    /**
840     * 指定されたキーと複数値の有効性のDB確認
841     *
842     * @param  string  $table     テーブル名
843     * @param  string  $tblkey    テーブルキー名
844     * @param  string  $keyname   フォームキー名
845     * @param  array   $item      入力データ配列
846     * @param  string  $delimiter 分割文字
847     * @return boolean true:有効なデータがある false:有効ではない
848     */
849    public function lfIsDbRecordMulti($table, $tblkey, $keyname, $item, $delimiter = ',')
850    {
851        if (array_search($keyname, $this->arrFormKeyList) === FALSE) {
852            return true;
853        }
854        if ($item[$keyname] == '') {
855            return true;
856        }
857        $arrItems = explode($delimiter, $item[$keyname]);
858        //空項目のチェック 1つでも空指定があったら不正とする。
859        if (array_search('', $arrItems) !== FALSE) {
860            return false;
861        }
862        $count = count($arrItems);
863        $where = $tblkey .' IN (' . SC_Utils_Ex::repeatStrWithSeparator('?', $count) . ')';
864
865        $objQuery =& SC_Query_Ex::getSingletonInstance();
866        $db_count = $objQuery->count($table, $where, $arrItems);
867        if ($count != $db_count) {
868            return false;
869        }
870
871        return true;
872    }
873
874    /**
875     * 指定されたキーと値の有効性のDB確認
876     *
877     * @param  string  $table   テーブル名
878     * @param  string  $keyname キー名
879     * @param  array   $item    入力データ配列
880     * @return boolean true:有効なデータがある false:有効ではない
881     */
882    public function lfIsDbRecord($table, $keyname, $item)
883    {
884        if (array_search($keyname, $this->arrFormKeyList) !== FALSE  //入力対象である
885            && $item[$keyname] != ''   // 空ではない
886            && !$this->objDb->sfIsRecord($table, $keyname, (array) $item[$keyname]) //DBに存在するか
887        ) {
888            return false;
889        }
890
891        return true;
892    }
893
894    /**
895     * 指定されたキーと値の有効性の配列内確認
896     *
897     * @param  string  $arr     チェック対象配列
898     * @param  string  $keyname キー名
899     * @param  array   $item    入力データ配列
900     * @return boolean true:有効なデータがある false:有効ではない
901     */
902    public function lfIsArrayRecord($arr, $keyname, $item)
903    {
904        if (array_search($keyname, $this->arrFormKeyList) !== FALSE //入力対象である
905            && $item[$keyname] != '' // 空ではない
906            && !array_key_exists($item[$keyname], $arr) //配列に存在するか
907        ) {
908            return false;
909        }
910
911        return true;
912    }
913}
Note: See TracBrowser for help on using the repository browser.