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

Revision 23627, 36.8 KB checked in by dkazuto, 10 years ago (diff)

#2522 商品登録CSVで商品情報編集時にポイント付与率と商品種別ID未入力の場合は、ポイント付与率と商品種別IDの値を更新しないように変更しました

  • 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(IMAGE_TEMP_REALDIR, IMAGE_SAVE_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            var_dump($sqlval);
507            $where = 'product_class_id = ?';
508            $objQuery->update('dtb_products_class', $sqlval, $where, array($product_class_id));
509        }
510    }
511
512    /**
513     * 関連商品登録を行う.
514     *
515     * FIXME: 商品規格登録の実処理自体は、LC_Page_Admin_Products_Productと共通化して欲しい。
516     *        DELETE/INSERT ではなく UPDATEへの変更も・・・
517     *
518     * @param  SC_Query $objQuery   SC_Queryインスタンス
519     * @param  array    $arrList    商品規格情報配列
520     * @param  integer  $product_id 商品ID
521     * @return void
522     */
523    public function lfRegistReccomendProducts($objQuery, $arrList, $product_id)
524    {
525        $objQuery->delete('dtb_recommend_products', 'product_id = ?', array($product_id));
526        for ($i = 1; $i <= RECOMMEND_PRODUCT_MAX; $i++) {
527            $keyname = 'recommend_product_id' . $i;
528            $comment_key = 'recommend_comment' . $i;
529            if ($arrList[$keyname] != '') {
530                $arrProduct = $objQuery->select('product_id', 'dtb_products', 'product_id = ?', array($arrList[$keyname]));
531                if ($arrProduct[0]['product_id'] != '') {
532                    $arrWhereVal = array();
533                    $arrWhereVal['product_id'] = $product_id;
534                    $arrWhereVal['recommend_product_id'] = $arrProduct[0]['product_id'];
535                    $arrWhereVal['comment'] = $arrList[$comment_key];
536                    $arrWhereVal['update_date'] = $arrList['update_date'];
537                    $arrWhereVal['create_date'] = $arrList['update_date'];
538                    $arrWhereVal['creator_id'] = $_SESSION['member_id'];
539                    $arrWhereVal['rank'] = RECOMMEND_PRODUCT_MAX - $i + 1;
540                    $objQuery->insert('dtb_recommend_products', $arrWhereVal);
541                }
542            }
543        }
544    }
545
546    /**
547     * 初期値の設定
548     *
549     * @param  array $arrCSVFrame CSV構造配列
550     * @return array $arrCSVFrame CSV構造配列
551     */
552    public function lfSetParamDefaultValue(&$arrCSVFrame)
553    {
554        foreach ($arrCSVFrame as $key => $val) {
555            switch ($val['col']) {
556                case 'status':
557                    $arrCSVFrame[$key]['default'] = DEFAULT_PRODUCT_DISP;
558                    break;
559                case 'del_flg':
560                    $arrCSVFrame[$key]['default'] = '0';
561                    break;
562                case 'point_rate':
563                    $arrCSVFrame[$key]['default'] = $this->arrInfo['point_rate'];
564                    break;
565                case 'product_type_id':
566                    $arrCSVFrame[$key]['default'] = DEFAULT_PRODUCT_DOWN;
567                    break;
568                case 'stock_unlimited':
569                    $arrCSVFrame[$key]['default'] = UNLIMITED_FLG_LIMITED;
570                default:
571                    break;
572            }
573        }
574
575        return $arrCSVFrame;
576    }
577
578    /**
579     * 商品データ登録前に特殊な値の持ち方をする部分のデータ部分の初期値補正を行う
580     *
581     * @param array $sqlval 商品登録情報配列
582     * @return $sqlval 登録情報配列
583     */
584    public function lfSetProductDefaultData(&$sqlval)
585    {
586        //新規登録時のみ設定する項目
587        if ($sqlval['product_id'] == '') {
588            if ($sqlval['status'] == '') {
589                $sqlval['status'] = DEFAULT_PRODUCT_DISP;
590            }
591        }
592        //共通で空欄時に上書きする項目
593        if ($sqlval['del_flg'] == '') {
594            $sqlval['del_flg'] = '0'; //有効
595        }
596        if ($sqlval['creator_id'] == '') {
597            $sqlval['creator_id'] = $_SESSION['member_id'];
598        }
599
600        return $sqlval;
601    }
602
603    /**
604     * 商品規格データ登録前に特殊な値の持ち方をする部分のデータ部分の初期値補正を行う
605     *
606     * @param array   $sqlval     商品登録情報配列
607     * @param boolean $upload_flg 更新フラグ(更新の場合true)
608     * @return $sqlval 登録情報配列
609     */
610    public function lfSetProductClassDefaultData(&$sqlval, $upload_flg = false)
611    {
612        //新規登録時のみ設定する項目
613        if ($sqlval['product_class_id'] == '') {
614            if ($sqlval['point_rate'] == '') {
615                $sqlval['point_rate'] = $this->arrInfo['point_rate'];
616            }
617            if ($sqlval['product_type_id'] == '') {
618                $sqlval['product_type_id'] = DEFAULT_PRODUCT_DOWN;
619            }
620        } else {
621            // 更新時に値を明示的に指定していない場合にそのままにする
622            if ($sqlval['point_rate'] == '') {
623                unset($sqlval['point_rate']);
624            }
625            if ($sqlval['product_type_id'] == '') {
626                unset($sqlval['product_type_id']);
627            }
628        }
629        //共通で設定する項目
630        if ($sqlval['del_flg'] == '') {
631            $sqlval['del_flg'] = '0'; //有効
632        }
633        if ($sqlval['creator_id'] == '') {
634            $sqlval['creator_id'] = $_SESSION['member_id'];
635        }
636
637        // 在庫無制限フラグ列を利用する場合、
638        if (array_key_exists('stock_unlimited', $sqlval) and $sqlval['stock_unlimited'] != '') {
639            // 在庫無制限フラグ = 無制限の場合、
640            if ($sqlval['stock_unlimited'] == UNLIMITED_FLG_UNLIMITED) {
641                $sqlval['stock'] = null;
642            }
643        } else {
644            // 初期登録の場合は、在庫数設定がされていない場合、在庫無制限フラグ = 無制限。
645            if (strlen($sqlval['stock']) === 0) {
646                //更新の場合は、sqlvalのキーにstockがある場合のみ対象
647                if (!$upload_flg or ($upload_flg and array_key_exists('stock', $sqlval))) {
648                    $sqlval['stock_unlimited'] = UNLIMITED_FLG_UNLIMITED;
649                }
650            // 在庫数を入力している場合、在庫無制限フラグ = 制限有り
651            } elseif (strlen($sqlval['stock']) >= 1) {
652                $sqlval['stock_unlimited'] = UNLIMITED_FLG_LIMITED;
653            // いずれにも該当しない場合、例外エラー
654            } else {
655                trigger_error('', E_USER_ERROR);
656            }
657        }
658
659        return $sqlval;
660    }
661
662    /**
663     * このフォーム特有の複雑な入力チェックを行う.
664     *
665     * @param array 確認対象データ
666     * @param array エラー配列
667     * @return array エラー配列
668     */
669    public function lfCheckErrorDetail($item, $arrErr)
670    {
671        // 規格IDの存在チェック
672        // FIXME 規格分類ID自体のが有効かを主眼においたチェックをすべきと感じる。
673        if (!$this->lfIsDbRecord('dtb_products_class', 'product_class_id', $item)) {
674            $arrErr['product_class_id'] = '※ 指定の商品規格IDは、登録されていません。';
675        }
676        // 商品ID、規格IDの組合せチェック
677        if (array_search('product_class_id', $this->arrFormKeyList) !== FALSE
678            && $item['product_class_id'] != ''
679        ) {
680            if ($item['product_id'] == '') {
681                $arrErr['product_class_id'] = '※ 商品規格ID指定時には商品IDの指定が必須です。';
682            } else {
683                if (!$this->objDb->sfIsRecord('dtb_products_class', 'product_id, product_class_id',
684                        array($item['product_id'], $item['product_class_id']))
685                ) {
686                    $arrErr['product_class_id'] = '※ 指定の商品IDと商品規格IDの組合せは正しくありません。';
687                }
688
689                // product_class_idは(product_id, classcategory_id1, classcategory_id2)に対して一意。既に異なるproduct_class_idが存在した場合はエラー
690                $classcategory_id1 = $item['classcategory_id'] ? $item['classcategory_id'] : 0;
691                $classcategory_id2 = $item['parent_classcategory_id'] ? $item['parent_classcategory_id'] : 0;
692                $product_class_id = SC_Utils_Ex::sfGetProductClassId($item['product_id'], $classcategory_id1, $classcategory_id2);
693                if ($product_class_id && $product_class_id != $item['product_class_id']) {
694                    $arrErr['product_class_id'] = '※ 指定の商品ID/規格分類と、商品規格IDの組合せは正しくありません。';
695                }
696            }
697        }
698        // 表示ステータスの存在チェック
699        if (!$this->lfIsArrayRecord($this->arrDISP, 'status', $item)) {
700            $arrErr['status'] = '※ 指定の表示ステータスは、登録されていません。';
701        }
702        // メーカーIDの存在チェック
703        if (!$this->lfIsArrayRecord($this->arrMaker, 'maker_id', $item)) {
704            $arrErr['maker_id'] = '※ 指定のメーカーIDは、登録されていません。';
705        }
706        // 発送日目安IDの存在チェック
707        if (!$this->lfIsArrayRecord($this->arrDELIVERYDATE, 'deliv_date_id', $item)) {
708            $arrErr['deliv_date_id'] = '※ 指定の発送日目安IDは、登録されていません。';
709        }
710        // 商品種別IDの存在チェック
711        if (!$this->lfIsArrayRecord($this->arrProductType, 'product_type_id', $item)) {
712            $arrErr['product_type_id'] = '※ 指定の商品種別IDは、登録されていません。';
713        }
714        // 関連商品IDのチェック
715        $arrRecommendProductUnique = array();
716        for ($i = 1; $i <= RECOMMEND_PRODUCT_MAX; $i++) {
717            $recommend_product_id_key = 'recommend_product_id' . $i;
718            if ((array_search($recommend_product_id_key, $this->arrFormKeyList) !== FALSE)
719             && ($item[$recommend_product_id_key] != '')) {
720                // 商品IDの存在チェック
721                if (!$this->objDb->sfIsRecord('dtb_products', 'product_id', (array) $item[$recommend_product_id_key])) {
722                    $arrErr[$recommend_product_id_key] = "※ 指定の関連商品ID($i)は、登録されていません。";
723                    continue;
724                }
725                // 商品IDの重複チェック
726                $recommend_product_id = $item[$recommend_product_id_key];
727                if (isset($arrRecommendProductUnique[$recommend_product_id])) {
728                    $arrErr[$recommend_product_id_key] = "※ 指定の関連商品ID($i)は、すでに登録されています。";
729                } else {
730                    $arrRecommendProductUnique[$recommend_product_id] = true;
731                }
732            }
733        }
734        // カテゴリIDの存在チェック
735        if (!$this->lfIsDbRecordMulti('dtb_category', 'category_id', 'category_ids', $item, ',')) {
736            $arrErr['category_ids'] = '※ 指定のカテゴリIDは、登録されていません。';
737        }
738        // 商品ステータスIDの存在チェック
739        if (!$this->lfIsArrayRecordMulti($this->arrSTATUS, 'product_statuses', $item, ',')) {
740            $arrErr['product_statuses'] = '※ 指定の商品ステータスIDは、登録されていません。';
741        }
742        // 削除フラグのチェック
743        if (array_search('del_flg', $this->arrFormKeyList) !== FALSE
744            && $item['del_flg'] != ''
745        ) {
746            if (!($item['del_flg'] == '0' or $item['del_flg'] == '1')) {
747                $arrErr['del_flg'] = '※ 削除フラグは「0」(有効)、「1」(削除)のみが有効な値です。';
748            }
749        }
750/*
751    TODO: 在庫数の扱いが2.4仕様ではぶれているのでどうするか・・
752        // 在庫数/在庫無制限フラグの有効性に関するチェック
753        if ($item['stock'] == '') {
754            if (array_search('stock_unlimited', $this->arrFormKeyList) === FALSE) {
755                $arrErr['stock'] = '※ 在庫数は必須です(無制限フラグ項目がある場合のみ空欄許可)。';
756            } elseif ($item['stock_unlimited'] != UNLIMITED_FLG_UNLIMITED) {
757                $arrErr['stock'] = '※ 在庫数または在庫無制限フラグのいずれかの入力が必須です。';
758            }
759        }
760*/
761        // ダウンロード商品チェック
762        if (array_search('product_type_id', $this->arrFormKeyList) !== FALSE
763            && $item['product_type_id'] != PRODUCT_TYPE_DOWNLOAD
764        ) {
765            //実商品の場合
766            if ($item['down_filename'] != '') {
767                $arrErr['down_filename'] = '※ ダウンロード商品ではない場合、ダウンロードファイル名は入力できません。';
768            }
769            if ($item['down_realfilename'] != '') {
770                $arrErr['down_realfilename'] = '※ ダウンロード商品ではない場合、ダウンロード商品用ファイルアップロードは入力できません。';
771            }
772        } elseif (array_search('product_type_id', $this->arrFormKeyList) !== FALSE
773                  && $item['product_type_id'] == PRODUCT_TYPE_DOWNLOAD
774        ) {
775            //ダウンロード商品の場合
776            if ($item['down_filename'] == '') {
777                $arrErr['down_filename'] = '※ ダウンロード商品の場合はダウンロードファイル名は必須です。';
778            }
779            if ($item['down_realfilename'] == '') {
780                $arrErr['down_realfilename'] = '※ ダウンロード商品の場合はダウンロード商品用ファイルアップロードは必須です。';
781            }
782        }
783
784        return $arrErr;
785    }
786
787    // TODO: ここから下のルーチンは汎用ルーチンとして移動が望ましい
788
789    /**
790     * 指定された行番号をmicrotimeに付与してDB保存用の時間を生成する。
791     * トランザクション内のCURRENT_TIMESTAMPは全てcommit()時の時間に統一されてしまう為。
792     *
793     * @param  string $line_no 行番号
794     * @return string $time DB保存用の時間文字列
795     */
796    public function lfGetDbFormatTimeWithLine($line_no = '')
797    {
798        $time = date('Y-m-d H:i:s');
799        // 秒以下を生成
800        if ($line_no != '') {
801            $microtime = sprintf('%06d', $line_no);
802            $time .= ".$microtime";
803        }
804
805        return $time;
806    }
807
808    /**
809     * 指定されたキーと複数値の有効性の配列内確認
810     *
811     * @param  string  $arr       チェック対象配列
812     * @param  string  $keyname   フォームキー名
813     * @param  array   $item      入力データ配列
814     * @param  string  $delimiter 分割文字
815     * @return boolean true:有効なデータがある false:有効ではない
816     */
817    public function lfIsArrayRecordMulti($arr, $keyname, $item, $delimiter = ',')
818    {
819        if (array_search($keyname, $this->arrFormKeyList) === FALSE) {
820            return true;
821        }
822        if ($item[$keyname] == '') {
823            return true;
824        }
825        $arrItems = explode($delimiter, $item[$keyname]);
826        //空項目のチェック 1つでも空指定があったら不正とする。
827        if (array_search('', $arrItems) !== FALSE) {
828            return false;
829        }
830        //キー項目への存在チェック
831        foreach ($arrItems as $item) {
832            if (!array_key_exists($item, $arr)) {
833                return false;
834            }
835        }
836
837        return true;
838    }
839
840    /**
841     * 指定されたキーと複数値の有効性のDB確認
842     *
843     * @param  string  $table     テーブル名
844     * @param  string  $tblkey    テーブルキー名
845     * @param  string  $keyname   フォームキー名
846     * @param  array   $item      入力データ配列
847     * @param  string  $delimiter 分割文字
848     * @return boolean true:有効なデータがある false:有効ではない
849     */
850    public function lfIsDbRecordMulti($table, $tblkey, $keyname, $item, $delimiter = ',')
851    {
852        if (array_search($keyname, $this->arrFormKeyList) === FALSE) {
853            return true;
854        }
855        if ($item[$keyname] == '') {
856            return true;
857        }
858        $arrItems = explode($delimiter, $item[$keyname]);
859        //空項目のチェック 1つでも空指定があったら不正とする。
860        if (array_search('', $arrItems) !== FALSE) {
861            return false;
862        }
863        $count = count($arrItems);
864        $where = $tblkey .' IN (' . SC_Utils_Ex::repeatStrWithSeparator('?', $count) . ')';
865
866        $objQuery =& SC_Query_Ex::getSingletonInstance();
867        $db_count = $objQuery->count($table, $where, $arrItems);
868        if ($count != $db_count) {
869            return false;
870        }
871
872        return true;
873    }
874
875    /**
876     * 指定されたキーと値の有効性のDB確認
877     *
878     * @param  string  $table   テーブル名
879     * @param  string  $keyname キー名
880     * @param  array   $item    入力データ配列
881     * @return boolean true:有効なデータがある false:有効ではない
882     */
883    public function lfIsDbRecord($table, $keyname, $item)
884    {
885        if (array_search($keyname, $this->arrFormKeyList) !== FALSE  //入力対象である
886            && $item[$keyname] != ''   // 空ではない
887            && !$this->objDb->sfIsRecord($table, $keyname, (array) $item[$keyname]) //DBに存在するか
888        ) {
889            return false;
890        }
891
892        return true;
893    }
894
895    /**
896     * 指定されたキーと値の有効性の配列内確認
897     *
898     * @param  string  $arr     チェック対象配列
899     * @param  string  $keyname キー名
900     * @param  array   $item    入力データ配列
901     * @return boolean true:有効なデータがある false:有効ではない
902     */
903    public function lfIsArrayRecord($arr, $keyname, $item)
904    {
905        if (array_search($keyname, $this->arrFormKeyList) !== FALSE //入力対象である
906            && $item[$keyname] != '' // 空ではない
907            && !array_key_exists($item[$keyname], $arr) //配列に存在するか
908        ) {
909            return false;
910        }
911
912        return true;
913    }
914}
Note: See TracBrowser for help on using the repository browser.