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

Revision 21743, 32.3 KB checked in by AMUAMU, 12 years ago (diff)

#1754 (exit;を個別の処理でしない) #1692 (プラグイン機能) 各ファイルでフックポイントの呼出を書かないで、自動的にフックポイントを呼び出すように修正。

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