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

Revision 22537, 36.1 KB checked in by pineray, 11 years ago (diff)

pageクラスからdtb_paymentテーブルを直接指定している箇所をなくす

以下のファイルが残っている.
LC_Page_Shopping_LoadPaymentModule.php
LC_Page_Admin_Home.php

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