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

Revision 21591, 33.4 KB checked in by h_yoshimoto, 12 years ago (diff)

#1686 管理画面にフックポイントを配置

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