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

Revision 21668, 32.7 KB checked in by Seasoft, 12 years ago (diff)

#1607 (未使用定義の削除)

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