/**
  * 商品登録を行う.
  *
  * FIXME: 商品登録の実処理自体は、LC_Page_Admin_Products_Productと共通化して欲しい。
  *
  * @param  SC_Query       $objQuery SC_Queryインスタンス
  * @param  string|integer $line     処理中の行数
  * @return void
  */
 public function lfRegistProduct($objQuery, $line = '', &$objFormParam)
 {
     $objProduct = new SC_Product_Ex();
     // 登録データ対象取得
     $arrList = $objFormParam->getDbArray();
     // 登録時間を生成(DBのCURRENT_TIMESTAMPだとcommitした際、全て同一の時間になってしまう)
     $arrList['update_date'] = $this->lfGetDbFormatTimeWithLine($line);
     // 商品登録情報を生成する。
     // 商品テーブルのカラムに存在しているもののうち、Form投入設定されていないデータは上書きしない。
     $sqlval = SC_Utils_Ex::sfArrayIntersectKeys($arrList, $this->arrProductColumn);
     // 必須入力では無い項目だが、空文字では問題のある特殊なカラム値の初期値設定
     $sqlval = $this->lfSetProductDefaultData($sqlval);
     if ($sqlval['product_id'] != '') {
         // 同じidが存在すればupdate存在しなければinsert
         $where = 'product_id = ?';
         $product_exists = $objQuery->exists('dtb_products', $where, array($sqlval['product_id']));
         if ($product_exists) {
             $objQuery->update('dtb_products', $sqlval, $where, array($sqlval['product_id']));
         } else {
             $sqlval['create_date'] = $arrList['update_date'];
             // INSERTの実行
             $objQuery->insert('dtb_products', $sqlval);
             // シーケンスの調整
             $seq_count = $objQuery->currVal('dtb_products_product_id');
             if ($seq_count < $sqlval['product_id']) {
                 $objQuery->setVal('dtb_products_product_id', $sqlval['product_id'] + 1);
             }
         }
         $product_id = $sqlval['product_id'];
     } else {
         // 新規登録
         $sqlval['product_id'] = $objQuery->nextVal('dtb_products_product_id');
         $product_id = $sqlval['product_id'];
         $sqlval['create_date'] = $arrList['update_date'];
         // INSERTの実行
         $objQuery->insert('dtb_products', $sqlval);
     }
     // カテゴリ登録
     if (isset($arrList['category_ids'])) {
         $arrCategory_id = explode(',', $arrList['category_ids']);
         $this->objDb->updateProductCategories($arrCategory_id, $product_id);
     }
     // 商品ステータス登録
     if (isset($arrList['product_statuses'])) {
         $arrStatus_id = explode(',', $arrList['product_statuses']);
         $objProduct->setProductStatus($product_id, $arrStatus_id);
     }
     // 商品規格情報を登録する
     $this->lfRegistProductClass($objQuery, $arrList, $product_id, $arrList['product_class_id']);
     // 関連商品登録
     $this->lfRegistReccomendProducts($objQuery, $arrList, $product_id);
 }
 /**
  * DBに商品データを登録する
  *
  * @param object $objUpFile SC_UploadFileインスタンス
  * @param object $objDownFile SC_UploadFileインスタンス
  * @param array $arrList フォーム入力パラメーター配列
  * @return integer 登録商品ID
  */
 function lfRegistProduct(&$objUpFile, &$objDownFile, $arrList)
 {
     $objQuery =& SC_Query_Ex::getSingletonInstance();
     $objDb = new SC_Helper_DB_Ex();
     // 配列の添字を定義
     $checkArray = array('name', 'status', 'main_list_comment', 'main_comment', 'deliv_fee', 'comment1', 'comment2', 'comment3', 'comment4', 'comment5', 'comment6', 'sale_limit', 'deliv_date_id', 'maker_id', 'note');
     $arrList = SC_Utils_Ex::arrayDefineIndexes($arrList, $checkArray);
     // INSERTする値を作成する。
     $sqlval['name'] = $arrList['name'];
     $sqlval['status'] = $arrList['status'];
     $sqlval['main_list_comment'] = $arrList['main_list_comment'];
     $sqlval['main_comment'] = $arrList['main_comment'];
     $sqlval['comment1'] = $arrList['comment1'];
     $sqlval['comment2'] = $arrList['comment2'];
     $sqlval['comment3'] = $arrList['comment3'];
     $sqlval['comment4'] = $arrList['comment4'];
     $sqlval['comment5'] = $arrList['comment5'];
     $sqlval['comment6'] = $arrList['comment6'];
     $sqlval['deliv_date_id'] = $arrList['deliv_date_id'];
     $sqlval['maker_id'] = $arrList['maker_id'];
     $sqlval['note'] = $arrList['note'];
     $sqlval['update_date'] = 'CURRENT_TIMESTAMP';
     $sqlval['creator_id'] = $_SESSION['member_id'];
     $arrRet = $objUpFile->getDBFileList();
     $sqlval = array_merge($sqlval, $arrRet);
     for ($cnt = 1; $cnt <= PRODUCTSUB_MAX; $cnt++) {
         $sqlval['sub_title' . $cnt] = $arrList['sub_title' . $cnt];
         $sqlval['sub_comment' . $cnt] = $arrList['sub_comment' . $cnt];
     }
     $objQuery->begin();
     // 新規登録(複製時を含む)
     if ($arrList['product_id'] == '') {
         $product_id = $objQuery->nextVal('dtb_products_product_id');
         $sqlval['product_id'] = $product_id;
         // INSERTの実行
         $sqlval['create_date'] = 'CURRENT_TIMESTAMP';
         $objQuery->insert('dtb_products', $sqlval);
         $arrList['product_id'] = $product_id;
         // カテゴリを更新
         $objDb->updateProductCategories($arrList['category_id'], $product_id);
         // 複製商品の場合には規格も複製する
         if ($arrList['copy_product_id'] != '' && SC_Utils_Ex::sfIsInt($arrList['copy_product_id'])) {
             if (!$arrList['has_product_class']) {
                 //規格なしの場合、複製は価格等の入力が発生しているため、その内容で追加登録を行う
                 $this->lfCopyProductClass($arrList, $objQuery);
             } else {
                 //規格がある場合の複製は複製元の内容で追加登録を行う
                 // dtb_products_class のカラムを取得
                 $dbFactory = SC_DB_DBFactory_Ex::getInstance();
                 $arrColList = $objQuery->listTableFields('dtb_products_class');
                 $arrColList_tmp = array_flip($arrColList);
                 // 複製しない列
                 unset($arrColList[$arrColList_tmp['product_class_id']]);
                 //規格ID
                 unset($arrColList[$arrColList_tmp['product_id']]);
                 //商品ID
                 unset($arrColList[$arrColList_tmp['create_date']]);
                 // 複製元商品の規格データ取得
                 $col = SC_Utils_Ex::sfGetCommaList($arrColList);
                 $table = 'dtb_products_class';
                 $where = 'product_id = ?';
                 $objQuery->setOrder('product_class_id');
                 $arrProductsClass = $objQuery->select($col, $table, $where, array($arrList['copy_product_id']));
                 // 規格データ登録
                 $objQuery =& SC_Query_Ex::getSingletonInstance();
                 foreach ($arrProductsClass as $arrData) {
                     $sqlval = $arrData;
                     $sqlval['product_class_id'] = $objQuery->nextVal('dtb_products_class_product_class_id');
                     $sqlval['deliv_fee'] = $arrList['deliv_fee'];
                     $sqlval['point_rate'] = $arrList['point_rate'];
                     $sqlval['sale_limit'] = $arrList['sale_limit'];
                     $sqlval['product_id'] = $product_id;
                     $sqlval['create_date'] = 'CURRENT_TIMESTAMP';
                     $sqlval['update_date'] = 'CURRENT_TIMESTAMP';
                     $objQuery->insert($table, $sqlval);
                 }
             }
         }
         // 更新
     } else {
         $product_id = $arrList['product_id'];
         // 削除要求のあった既存ファイルの削除
         $arrRet = $this->lfGetProductData_FromDB($arrList['product_id']);
         // TODO: SC_UploadFile::deleteDBFileの画像削除条件見直し要
         $objImage = new SC_Image_Ex($objUpFile->temp_dir);
         $arrKeyName = $objUpFile->keyname;
         $arrSaveFile = $objUpFile->save_file;
         $arrImageKey = array();
         foreach ($arrKeyName as $key => $keyname) {
             if ($arrRet[$keyname] && !$arrSaveFile[$key]) {
                 $arrImageKey[] = $keyname;
                 $has_same_image = $this->lfHasSameProductImage($arrList['product_id'], $arrImageKey, $arrRet[$keyname]);
                 if (!$has_same_image) {
                     $objImage->deleteImage($arrRet[$keyname], $objUpFile->save_dir);
                 }
             }
         }
         $objDownFile->deleteDBDownFile($arrRet);
         // UPDATEの実行
         $where = 'product_id = ?';
         $objQuery->update('dtb_products', $sqlval, $where, array($product_id));
         // カテゴリを更新
         $objDb->updateProductCategories($arrList['category_id'], $product_id);
     }
     // 商品登録の時は規格を生成する。複製の場合は規格も複製されるのでこの処理は不要。
     if ($arrList['copy_product_id'] == '') {
         // 規格登録
         if ($objDb->sfHasProductClass($product_id)) {
             // 規格あり商品(商品規格テーブルのうち、商品登録フォームで設定するパラメーターのみ更新)
             $this->lfUpdateProductClass($arrList);
         } else {
             // 規格なし商品(商品規格テーブルの更新)
             $this->lfInsertDummyProductClass($arrList);
         }
     }
     // 商品ステータス設定
     $objProduct = new SC_Product_Ex();
     $objProduct->setProductStatus($product_id, $arrList['product_status']);
     // 関連商品登録
     $this->lfInsertRecommendProducts($objQuery, $arrList, $product_id);
     $objQuery->commit();
     return $product_id;
 }