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

Revision 22298, 34.4 KB checked in by undertree, 11 years ago (diff)

#1713(商品種別を追加した際に支障をきたしている懸念)
・判定式「== PRODUCT_TYPE_NORMAL」を「!= PRODUCT_TYPE_DOWNLOAD」に修正
・エラーメッセージ「通常商品の場合は~」を「ダウンロード商品ではない場合、~」に修正

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