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

Revision 22503, 33.7 KB checked in by m_uehara, 11 years ago (diff)

#2084 メッセージIDの振り直し

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