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

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

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

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
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        }
[19752]476        // 支払い方法登録
[21514]477        if ($arrList['product_payment_ids'] != '') {
[19752]478            $arrPayment_id = explode(',', $arrList['product_payment_ids']);
479            $objProduct->setPaymentOptions($product_class_id, $arrPayment_id);
480        }
[15472]481    }
482
483    /**
[19752]484     * 関連商品登録を行う.
[15472]485     *
[19752]486     * FIXME: 商品規格登録の実処理自体は、LC_Page_Admin_Products_Productと共通化して欲しい。
487     *        DELETE/INSERT ではなく UPDATEへの変更も・・・
488     *
489     * @param SC_Query $objQuery SC_Queryインスタンス
490     * @param array $arrList 商品規格情報配列
491     * @param integer $product_id 商品ID
[15472]492     * @return void
493     */
[19752]494    function lfRegistReccomendProducts($objQuery, $arrList, $product_id) {
[21514]495        $objQuery->delete('dtb_recommend_products', 'product_id = ?', array($product_id));
[21441]496        for ($i = 1; $i <= RECOMMEND_PRODUCT_MAX; $i++) {
[21481]497            $keyname = 'recommend_product_id' . $i;
498            $comment_key = 'recommend_comment' . $i;
[21514]499            if ($arrList[$keyname] != '') {
[21515]500                $arrProduct = $objQuery->select('product_id', 'dtb_products', 'product_id = ?', array($arrList[$keyname]));
[21514]501                if ($arrProduct[0]['product_id'] != '') {
[21563]502                    $arrWhereVal = array();
503                    $arrWhereVal['product_id'] = $product_id;
504                    $arrWhereVal['recommend_product_id'] = $arrProduct[0]['product_id'];
505                    $arrWhereVal['comment'] = $arrList[$comment_key];
506                    $arrWhereVal['update_date'] = $arrList['update_date'];
507                    $arrWhereVal['create_date'] = $arrList['update_date'];
508                    $arrWhereVal['creator_id'] = $_SESSION['member_id'];
509                    $arrWhereVal['rank'] = RECOMMEND_PRODUCT_MAX - $i + 1;
510                    $objQuery->insert('dtb_recommend_products', $arrWhereVal);
[19752]511                }
512            }
513        }
514    }
[15472]515
[19752]516    /**
517     * 初期値の設定
518     *
519     * @param array $arrCSVFrame CSV構造配列
520     * @return array $arrCSVFrame CSV構造配列
521     */
522    function lfSetParamDefaultValue(&$arrCSVFrame) {
[21441]523        foreach ($arrCSVFrame as $key => $val) {
524            switch ($val['col']) {
[19752]525                case 'status':
526                    $arrCSVFrame[$key]['default'] = DEFAULT_PRODUCT_DISP;
527                    break;
528                case 'del_flg':
529                    $arrCSVFrame[$key]['default'] = '0';
530                    break;
531                case 'point_rate':
532                    $arrCSVFrame[$key]['default'] = $this->arrInfo['point_rate'];
533                    break;
534                case 'product_type_id':
535                    $arrCSVFrame[$key]['default'] = DEFAULT_PRODUCT_DOWN;
536                    break;
537                case 'product_payment_ids':
538                    $arrCSVFrame[$key]['default'] = implode(',',array_keys($this->arrPayments));
539                    break;
[19950]540                case 'stock_unlimited':
541                    $arrCSVFrame[$key]['default'] = UNLIMITED_FLG_LIMITED;
[19752]542                default:
543                    break;
544            }
545        }
546        return $arrCSVFrame;
547    }
[15472]548
[19752]549    /**
550     * 商品データ登録前に特殊な値の持ち方をする部分のデータ部分の初期値補正を行う
551     *
552     * @param array $sqlval 商品登録情報配列
553     * @return $sqlval 登録情報配列
554     */
555    function lfSetProductDefaultData(&$sqlval) {
556        //新規登録時のみ設定する項目
[21514]557        if ($sqlval['product_id'] == '') {
558            if ($sqlval['status'] == '') {
[19752]559                $sqlval['status'] = DEFAULT_PRODUCT_DISP;
[15472]560            }
[19752]561        }
562        //共通で空欄時に上書きする項目
[21514]563        if ($sqlval['del_flg'] == '') {
[19752]564            $sqlval['del_flg'] = '0'; //有効
565        }
[21514]566        if ($sqlval['creator_id'] == '') {
[19752]567            $sqlval['creator_id'] = $_SESSION['member_id'];
568        }
569        return $sqlval;
570    }
[15472]571
[19752]572    /**
573     * 商品規格データ登録前に特殊な値の持ち方をする部分のデータ部分の初期値補正を行う
574     *
575     * @param array $sqlval 商品登録情報配列
576     * @return $sqlval 登録情報配列
577     */
578    function lfSetProductClassDefaultData(&$sqlval) {
579        //新規登録時のみ設定する項目
[21514]580        if ($sqlval['product_class_id'] == '') {
581            if ($sqlval['point_rate'] == '') {
[19752]582                $sqlval['point_rate'] = $this->arrInfo['point_rate'];
[15472]583            }
[21514]584            if ($sqlval['product_type_id'] == '') {
[19752]585                $sqlval['product_type_id'] = DEFAULT_PRODUCT_DOWN;
586            }
587        }
588        //共通で設定する項目
[21514]589        if ($sqlval['del_flg'] == '') {
[19752]590            $sqlval['del_flg'] = '0'; //有効
591        }
[21514]592        if ($sqlval['creator_id'] == '') {
[19752]593            $sqlval['creator_id'] = $_SESSION['member_id'];
594        }
[21538]595        // 在庫無制限フラグ列を利用する場合、
596        if (array_key_exists('stock_unlimited', $sqlval)) {
597            // 在庫無制限フラグ = 無制限の場合、
598            if ($sqlval['stock_unlimited'] == UNLIMITED_FLG_UNLIMITED) {
599                $sqlval['stock'] = null;
600            }
601        } else {
602            // 在庫数設定がされていない場合、在庫無制限フラグ = 無制限
603            if (strlen($sqlval['stock']) === 0) {
604                $sqlval['stock_unlimited'] = UNLIMITED_FLG_UNLIMITED;
605            }
606            // 在庫数を入力している場合、在庫無制限フラグ = 制限有り
607            elseif (strlen($sqlval['stock']) >= 1) {
608                $sqlval['stock_unlimited'] = UNLIMITED_FLG_LIMITED;
609            }
610            // いずれにも該当しない場合、例外エラー
611            else {
[21582]612                trigger_error('', E_USER_ERROR);
[21538]613            }
614        }
[19752]615        return $sqlval;
616    }
[15472]617
[19752]618    /**
619     * このフォーム特有の複雑な入力チェックを行う.
620     *
621     * @param array 確認対象データ
622     * @param array エラー配列
623     * @return array エラー配列
624     */
625    function lfCheckErrorDetail($item, $arrErr) {
626        // 規格IDの存在チェック
[21502]627        // FIXME 規格分類ID自体のが有効かを主眼においたチェックをすべきと感じる。
[21441]628        if (!$this->lfIsDbRecord('dtb_products_class', 'product_class_id', $item)) {
[21514]629            $arrErr['product_class_id'] = '※ 指定の商品規格IDは、登録されていません。';
[19752]630        }
631        // 商品ID、規格IDの組合せチェック
632        if(array_search('product_class_id', $this->arrFormKeyList) !== FALSE
[21514]633                and $item['product_class_id'] != '') {
634            if ($item['product_id'] == '') {
635                $arrErr['product_class_id'] = '※ 商品規格ID指定時には商品IDの指定が必須です。';
[21441]636            } else {
[19752]637                if(!$this->objDb->sfIsRecord('dtb_products_class', 'product_id, product_class_id'
638                        , array($item['product_id'], $item['product_class_id']))) {
[21514]639                    $arrErr['product_class_id'] = '※ 指定の商品IDと商品規格IDの組合せは正しくありません。';
[16559]640                }
[15472]641            }
642        }
[19752]643        // 表示ステータスの存在チェック
[21441]644        if (!$this->lfIsArrayRecord($this->arrDISP, 'status', $item)) {
[21514]645            $arrErr['status'] = '※ 指定の表示ステータスは、登録されていません。';
[19752]646        }
647        // メーカーIDの存在チェック
[21441]648        if (!$this->lfIsArrayRecord($this->arrMaker, 'maker_id', $item)) {
[21514]649            $arrErr['maker_id'] = '※ 指定のメーカーIDは、登録されていません。';
[19752]650        }
651        // 発送日目安IDの存在チェック
[21441]652        if (!$this->lfIsArrayRecord($this->arrDELIVERYDATE, 'deliv_date_id', $item)) {
[21514]653            $arrErr['deliv_date_id'] = '※ 指定の発送日目安IDは、登録されていません。';
[19752]654        }
655        // 発送日目安IDの存在チェック
[21441]656        if (!$this->lfIsArrayRecord($this->arrProductType, 'product_type_id', $item)) {
[21514]657            $arrErr['product_type_id'] = '※ 指定の商品種別IDは、登録されていません。';
[19752]658        }
659        // 関連商品IDの存在チェック
[21441]660        for ($i = 1; $i <= RECOMMEND_PRODUCT_MAX; $i++) {
[19752]661            if(array_search('recommend_product_id' . $i, $this->arrFormKeyList) !== FALSE
[21514]662                    and $item['recommend_product_id' . $i] != ''
[21444]663                    and !$this->objDb->sfIsRecord('dtb_products', 'product_id', (array)$item['recommend_product_id' . $i])) {
[19752]664                $arrErr['recommend_product_id' . $i] = "※ 指定の関連商品ID($i)は、登録されていません。";
665            }
666        }
667        // カテゴリIDの存在チェック
[21441]668        if (!$this->lfIsDbRecordMulti('dtb_category', 'category_id', 'category_ids', $item, ',')) {
[21514]669            $arrErr['category_ids'] = '※ 指定のカテゴリIDは、登録されていません。';
[19752]670        }
[21182]671        // 商品ステータスIDの存在チェック
[21441]672        if (!$this->lfIsArrayRecordMulti($this->arrSTATUS, 'product_statuses', $item, ',')) {
[21514]673            $arrErr['product_statuses'] = '※ 指定の商品ステータスIDは、登録されていません。';
[19752]674        }
675        // 支払い方法IDの存在チェック
[21441]676        if (!$this->lfIsArrayRecordMulti($this->arrPayments, 'product_payment_ids', $item, ',')) {
[21514]677            $arrErr['product_payment_ids'] = '※ 指定の支払い方法IDは、登録されていません。';
[19752]678        }
679        // 削除フラグのチェック
680        if(array_search('del_flg', $this->arrFormKeyList) !== FALSE
[21514]681                and $item['del_flg'] != '') {
[21481]682            if (!($item['del_flg'] == '0' or $item['del_flg'] == '1')) {
[21514]683                $arrErr['del_flg'] = '※ 削除フラグは「0」(有効)、「1」(削除)のみが有効な値です。';
[19752]684            }
685        }
686/*
687    TODO: 在庫数の扱いが2.4仕様ではぶれているのでどうするか・・
688        // 在庫数/在庫無制限フラグの有効性に関するチェック
[21514]689        if ($item['stock'] == '') {
[21441]690            if (array_search('stock_unlimited', $this->arrFormKeyList) === FALSE) {
[21514]691                $arrErr['stock'] = '※ 在庫数は必須です(無制限フラグ項目がある場合のみ空欄許可)。';
[21441]692            }else if ($item['stock_unlimited'] != UNLIMITED_FLG_UNLIMITED) {
[21514]693                $arrErr['stock'] = '※ 在庫数または在庫無制限フラグのいずれかの入力が必須です。';
[19752]694            }
695        }
[20041]696*/
[19752]697        // ダウンロード商品チェック
[21527]698        if (array_search('product_type_id', $this->arrFormKeyList) !== FALSE
699            && $item['product_type_id'] == PRODUCT_TYPE_NORMAL
700        ) {
[19752]701            //実商品の場合
[21514]702            if ($item['down_filename'] != '') {
703                $arrErr['down_filename'] = '※ 実商品の場合はダウンロードファイル名は入力できません。';
[19752]704            }
[21514]705            if ($item['down_realfilename'] != '') {
706                $arrErr['down_realfilename'] = '※ 実商品の場合はダウンロード商品用ファイルアップロードは入力できません。';
[19752]707            }
[21527]708        } elseif (array_search('product_type_id', $this->arrFormKeyList) !== FALSE
709                  && $item['product_type_id'] == PRODUCT_TYPE_DOWNLOAD
710        ) {
[19752]711            //ダウンロード商品の場合
[21514]712            if ($item['down_filename'] == '') {
713                $arrErr['down_filename'] = '※ ダウンロード商品の場合はダウンロードファイル名は必須です。';
[19752]714            }
[21514]715            if ($item['down_realfilename'] == '') {
716                $arrErr['down_realfilename'] = '※ ダウンロード商品の場合はダウンロード商品用ファイルアップロードは必須です。';
[19752]717            }
718        }
719        return $arrErr;
[15472]720    }
721
[19752]722    // TODO: ここから下のルーチンは汎用ルーチンとして移動が望ましい
723
[15472]724    /**
[19752]725     * 指定された行番号をmicrotimeに付与してDB保存用の時間を生成する。
[21185]726     * トランザクション内のCURRENT_TIMESTAMPは全てcommit()時の時間に統一されてしまう為。
[15472]727     *
[19752]728     * @param string $line_no 行番号
729     * @return string $time DB保存用の時間文字列
[15472]730     */
[19752]731    function lfGetDbFormatTimeWithLine($line_no = '') {
[21514]732        $time = date('Y-m-d H:i:s');
[19752]733        // 秒以下を生成
[21441]734        if ($line_no != '') {
[21514]735            $microtime = sprintf('%06d', $line_no);
[19752]736            $time .= ".$microtime";
737        }
738        return $time;
739    }
[15472]740
[19752]741    /**
742     * 指定されたキーと複数値の有効性の配列内確認
743     *
744     * @param string $arr チェック対象配列
745     * @param string $keyname フォームキー名
746     * @param array  $item 入力データ配列
747     * @param string $delimiter 分割文字
748     * @return boolean true:有効なデータがある false:有効ではない
749     */
750    function lfIsArrayRecordMulti($arr, $keyname, $item, $delimiter = ',') {
[21441]751        if (array_search($keyname, $this->arrFormKeyList) === FALSE) {
[19752]752            return true;
[15472]753        }
[21514]754        if ($item[$keyname] == '') {
[19752]755            return true;
[18508]756        }
[19752]757        $arrItems = explode($delimiter, $item[$keyname]);
758        //空項目のチェック 1つでも空指定があったら不正とする。
[21514]759        if (array_search('', $arrItems) !== FALSE) {
[19752]760            return false;
761        }
762        //キー項目への存在チェック
[21441]763        foreach ($arrItems as $item) {
764            if (!array_key_exists($item, $arr)) {
[19752]765                return false;
766            }
767        }
768        return true;
[15472]769    }
770
771    /**
[19752]772     * 指定されたキーと複数値の有効性のDB確認
773     *
774     * @param string $table テーブル名
775     * @param string $tblkey テーブルキー名
776     * @param string $keyname フォームキー名
777     * @param array  $item 入力データ配列
778     * @param string $delimiter 分割文字
779     * @return boolean true:有効なデータがある false:有効ではない
780     */
781    function lfIsDbRecordMulti($table, $tblkey, $keyname, $item, $delimiter = ',') {
[21441]782        if (array_search($keyname, $this->arrFormKeyList) === FALSE) {
[19752]783            return true;
784        }
[21514]785        if ($item[$keyname] == '') {
[19752]786            return true;
787        }
788        $arrItems = explode($delimiter, $item[$keyname]);
789        //空項目のチェック 1つでも空指定があったら不正とする。
[21514]790        if (array_search('', $arrItems) !== FALSE) {
[19752]791            return false;
792        }
793        $count = count($arrItems);
[21564]794        $where = $tblkey .' IN (' . SC_Utils_Ex::repeatStrWithSeparator('?', $count) . ')';
[20041]795
[20507]796        $objQuery =& SC_Query_Ex::getSingletonInstance();
[19752]797        $db_count = $objQuery->count($table, $where, $arrItems);
[21441]798        if ($count != $db_count) {
[19752]799            return false;
800        }
801        return true;
802    }
803
804    /**
805     * 指定されたキーと値の有効性のDB確認
806     *
807     * @param string $table テーブル名
808     * @param string $keyname キー名
809     * @param array  $item 入力データ配列
810     * @return boolean true:有効なデータがある false:有効ではない
811     */
812    function lfIsDbRecord($table, $keyname, $item) {
813        if(array_search($keyname, $this->arrFormKeyList) !== FALSE  //入力対象である
[21514]814                and $item[$keyname] != ''   // 空ではない
[19752]815                and !$this->objDb->sfIsRecord($table, $keyname, (array)$item[$keyname]) //DBに存在するか
816                ) {
817            return false;
818        }
819        return true;
820    }
821
822    /**
823     * 指定されたキーと値の有効性の配列内確認
824     *
825     * @param string $arr チェック対象配列
826     * @param string $keyname キー名
827     * @param array  $item 入力データ配列
828     * @return boolean true:有効なデータがある false:有効ではない
829     */
830    function lfIsArrayRecord($arr, $keyname, $item) {
831        if(array_search($keyname, $this->arrFormKeyList) !== FALSE //入力対象である
[21514]832                and $item[$keyname] != '' // 空ではない
[19752]833                and !array_key_exists($item[$keyname], $arr) //配列に存在するか
834                ) {
835            return false;
836        }
837        return true;
838    }
[15472]839}
Note: See TracBrowser for help on using the repository browser.