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

Revision 22578, 36.2 KB checked in by pineray, 11 years ago (diff)

#2156 pageクラスからdtb_makerテーブルを直接指定している箇所をなくす
メイカー管理のページクラス内の処理を他と統一する

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