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

Revision 21563, 33.0 KB checked in by Seasoft, 12 years ago (diff)

#1669 (変数の初期化漏れ)
#1613 (typo修正・ソース整形・ソースコメントの改善)

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