Changeset 20277
- Timestamp:
- 2011/02/21 01:33:29 (13 years ago)
- Location:
- branches/version-2_5-dev/data
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_5-dev/data/Smarty/templates/admin/products/upload_csv_category.tpl
r20116 r20277 64 64 </ul> 65 65 </div> 66 <!--{if $arrRowErr}--> 67 <table class="form"> 68 <tr> 69 <td> 70 <!--{foreach item=err from=$arrRowErr}--> 71 <span class="attention"><!--{$err}--></span> 72 <!--{/foreach}--> 73 </td> 74 </tr> 75 </table> 76 <!--{/if}--> 77 <!--{if $arrRowResult}--> 78 <table class="form"> 79 <tr> 80 <td> 81 <!--{foreach item=result from=$arrRowResult}--> 82 <span><!--{$result}--><br/></span> 83 <!--{/foreach}--> 84 </td> 85 </tr> 86 </table> 87 <!--{/if}--> 66 88 </div> 67 89 </form> -
branches/version-2_5-dev/data/class/pages/admin/products/LC_Page_Admin_Products_UploadCSVCategory.php
r20041 r20277 39 39 // {{{ functions 40 40 41 var $arrErr; 42 43 var $arrTitle; 44 45 var $arrRowResult; 46 47 var $arrRowErr; 48 41 49 /** 42 50 * Page を初期化する. … … 47 55 parent::init(); 48 56 $this->tpl_mainpage = 'products/upload_csv_category.tpl'; 49 $this->tpl_subnavi = 'products/subnavi.tpl';50 $this->tpl_mainno = 'products';51 $this->tpl_subno = 'upload_csv_category';57 $this->tpl_subnavi = 'products/subnavi.tpl'; 58 $this->tpl_mainno = 'products'; 59 $this->tpl_subno = 'upload_csv_category'; 52 60 $this->tpl_subtitle = 'カテゴリ登録CSV'; 53 61 } … … 69 77 */ 70 78 function action() { 71 $objSess = new SC_Session(); 72 $objDb = new SC_Helper_DB_Ex(); 79 $objSess = new SC_Session(); 80 $objDb = new SC_Helper_DB_Ex(); 81 $objUpFile = new SC_UploadFile(IMAGE_TEMP_REALDIR, IMAGE_SAVE_REALDIR); 82 $objFormParam = new SC_FormParam(); 73 83 74 84 // 認証可否の判定 75 85 SC_Utils_Ex::sfIsSuccess($objSess); 76 86 77 // ファイル管理クラス 78 $this->objUpFile = new SC_UploadFile(IMAGE_TEMP_REALDIR, IMAGE_SAVE_REALDIR); 79 // ファイル情報の初期化 80 $this->lfInitFile(); 81 // パラメータ管理クラス 82 $this->objFormParam = new SC_FormParam(); 83 // パラメータ情報の初期化 84 $this->lfInitParam(); 85 $colmax = $this->objFormParam->getCount(); 86 $this->objFormParam->setHtmlDispNameArray(); 87 $this->arrTitle = $this->objFormParam->getHtmlDispNameArray(); 87 // ファイルオブジェクト初期化 88 $this->initFile($objUpFile); 89 90 // 入力パラメータ初期化 91 $this->initParam($objFormParam); 92 $objFormParam->setHtmlDispNameArray(); 93 $this->arrTitle = $objFormParam->getHtmlDispNameArray(); 88 94 89 95 switch ($this->getMode()) { 90 case 'csv_upload': 91 $err = false; 92 // エラーチェック 93 $arrErr['csv_file'] = $this->objUpFile->makeTempFile('csv_file'); 94 95 if($arrErr['csv_file'] == "") { 96 $arrErr = $this->objUpFile->checkEXISTS(); 97 } 98 99 // 実行時間を制限しない 100 set_time_limit(0); 101 102 // 出力をバッファリングしない(==日本語自動変換もしない) 103 ob_end_clean(); 104 105 // IEのために256バイト空文字出力 106 echo str_pad('',256); 107 108 if (empty($arrErr['csv_file'])) { 109 // 一時ファイル名の取得 110 $filepath = $this->objUpFile->getTempFilePath('csv_file'); 111 // エンコード 112 $enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath, 113 CHAR_CODE, CSV_TEMP_REALDIR); 114 115 $fp = fopen($enc_filepath, "r"); 116 117 // 無効なファイルポインタが渡された場合はエラー表示 118 if ($fp === false) { 119 SC_Utils_Ex::sfDispError(""); 120 } 121 122 // レコード数を得る 123 $rec_count = $this->lfCSVRecordCount($fp); 124 125 $line = 0; // 行数 126 $regist = 0; // 登録数 127 128 $objQuery = new SC_Query(); 129 $objQuery->begin(); 130 131 echo "■ CSV登録進捗状況 <br/><br/>\n"; 132 while (!feof($fp) && !$err) { 133 $arrCSV = fgetcsv($fp, CSV_LINE_MAX); 134 135 // 行カウント 136 $line++; 137 138 if ($line <= 1) { 139 continue; 140 } 141 142 // 項目数カウント 143 $max = count($arrCSV); 144 145 // 項目数が1以下の場合は無視する 146 if ($max <= 1) { 147 continue; 148 } 149 150 // 項目数チェック 151 if ($max != $colmax) { 152 echo "※ 項目数が" . $max . "個検出されました。項目数は" . $colmax . "個になります。</br>\n"; 153 $err = true; 154 } else { 155 // シーケンス配列を格納する。 156 $this->objFormParam->setParam($arrCSV, true); 157 $arrRet = $this->objFormParam->getHashArray(); 158 $this->objFormParam->setParam($arrRet); 159 // 入力値の変換 160 $this->objFormParam->convParam(); 161 // <br>なしでエラー取得する。 162 $arrCSVErr = $this->lfCheckError($arrCSV); 163 } 164 165 // 入力エラーチェック 166 if (count($arrCSVErr) > 0) { 167 echo "<font color=\"red\">■" . $line . "行目でエラーが発生しました。</font></br>\n"; 168 foreach($arrCSVErr as $val) { 169 $this->printError($val); 170 } 171 $err = true; 172 } 173 174 if (!$err) { 175 $this->lfRegistProduct($objQuery, $line,$arrCSV); 176 $regist++; 177 } 178 $arrParam = $this->objFormParam->getHashArray(); 179 180 if (!$err) echo $line." / ".$rec_count. "行目 (カテゴリID:".$arrParam['category_id']." / カテゴリ名:".$arrParam['category_name'].")\n<br />"; 181 SC_Utils_Ex::sfFlush(); 182 } 183 fclose($fp); 184 185 if (!$err) { 186 $objQuery->commit(); 187 echo "■" . $regist . "件のレコードを登録しました。"; 188 // カテゴリ件数カウント関数の実行 189 $objDb->sfCountCategory($objQuery); 190 $objDb->sfCountMaker($objQuery); 191 } else { 192 $objQuery->rollback(); 193 } 194 } else { 195 foreach($arrErr as $val) { 196 $this->printError($val); 197 } 198 } 199 echo "<br/><a href=\"javascript:window.close()\">→閉じる</a>"; 200 exit; 201 default: 96 case 'csv_upload': 97 $this->doUploadCsv($objFormParam, $objUpFile, $objDb); 98 break; 99 default: 100 } 101 } 102 103 /** 104 * CSVアップロードを実行する 105 * 106 * @param SC_FormParam $objFormParam 107 * @param SC_UploadFile $objUpFile 108 * @param SC_Helper_DB $objDb 109 * @return void 110 */ 111 function doUploadCsv(&$objFormParam, &$objUpFile, &$objDb) { 112 // ファイルアップロードのチェック 113 $objUpFile->makeTempFile('csv_file'); 114 $arrErr = $objUpFile->checkExists(); 115 if (count($arrErr) > 0) { 116 $this->arrErr = $arrErr; 117 return; 118 } 119 120 // 一時ファイル名の取得 121 $filepath = $objUpFile->getTempFilePath('csv_file'); 122 // CSVファイルの文字コード変換 123 $enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath, CHAR_CODE, CSV_TEMP_REALDIR); 124 // CSVファイルのオープン 125 $fp = fopen($enc_filepath, "r"); 126 // 失敗した場合はエラー表示 127 if (!$fp) { 128 SC_Utils_Ex::sfDispError(""); 129 } 130 131 // 登録対象の列数 132 $col_max_count = $objFormParam->getCount(); 133 // 行数 134 $line_count = 0; 135 136 $objQuery = SC_Query::getSingletonInstance(); 137 $objQuery->begin(); 138 139 $errFlg = false; 140 141 while (!feof($fp)) { 142 $arrRow = fgetcsv($fp, CSV_LINE_MAX); 143 $line_count++; 144 145 // ヘッダ行はスキップ 146 if ($line_count == 1) { 147 continue; 148 } 149 // 空行はスキップ 150 if (empty($arrRow)) { 151 continue; 152 } 153 // 列数が異なる場合はエラー 154 if ($col_max_count != count($arrRow)) { 155 $errFlg = true; 202 156 break; 203 } 157 } 158 // 数値インデックスから, カラム名 => 値の連想配列へ変換 159 $objFormParam->setParam($arrRow, true); 160 $arrRow = $objFormParam->getHashArray(); 161 $objFormParam->setParam($arrRow); 162 $objFormParam->convParam(); 163 // 入力項目チェック 164 $arrErr = $objFormParam->checkError(); 165 if (count($arrErr) > 0) { 166 foreach ($arrErr as $err) { 167 $this->addRowErr($line_count, $err); 168 } 169 $errFlg = true; 170 break; 171 } 172 173 // 親カテゴリIDがない場合はルートのカテゴリIDをセット 174 if ($arrRow['parent_category_id'] == '') { 175 $arrRow['parent_category_id'] = 0; 176 } 177 178 // 親カテゴリIDの存在チェック 179 $count = $objQuery->count("dtb_category", "category_id = ?", array($arrRow['parent_category_id'])); 180 if ($arrRow['parent_category_id'] != 0 && $count == 0) { 181 $errFlg = true; 182 $this->addRowErr($line_count, "指定の親カテゴリID(" . $arrRow['parent_category_id'] . ")は、存在しません。"); 183 break; 184 } 185 186 $count = $objQuery->count("dtb_category", "category_id = ?", array($arrRow['category_id'])); 187 188 // 編集 189 if ($count > 0) { 190 // 重複チェック 191 $where = "parent_category_id = ? AND category_id <> ? AND category_name = ?"; 192 $count = $objQuery->count("dtb_category", 193 $where, 194 array($arrRow['parent_category_id'], 195 $arrRow['category_id'], 196 $arrRow['category_name'])); 197 if ($count > 0) { 198 $errFlg = true; 199 $this->addRowErr($line_count, "既に同じ内容の登録が存在します。"); 200 break; 201 } 202 203 // カテゴリ更新 204 $arrCategory = array(); 205 $arrCategory['category_name'] = $arrRow['category_name']; 206 $arrCategory['update_date'] = 'NOW()'; 207 $where = "category_id = ?"; 208 $objQuery->update("dtb_category", $arrCategory, $where, array($arrRow['category_id'])); 209 210 $message = "[更新] カテゴリID: " . $arrRow['category_id'] . " カテゴリ名 : " . $arrRow['category_name']; 211 $this->addRowResult($line_count, $message); 212 // 登録 213 } else { 214 // 登録数上限チェック 215 $where = "del_flg = 0"; 216 $count = $objQuery->count("dtb_category", $where); 217 if ($count >= CATEGORY_MAX) { 218 $errFlg = true; 219 $this->addRowErr($line_count, "カテゴリの登録最大数を超えました。"); 220 break; 221 } 222 // 階層上限チェック 223 if ($this->isOverLevel($arrRow['parent_category_id'])) { 224 $errFlg = true; 225 $this->addRowErr($line_count, LEVEL_MAX . "階層以上の登録はできません。"); 226 break; 227 } 228 // 重複チェック 229 $where = "parent_category_id = ? AND category_name = ?"; 230 $count = $objQuery->count("dtb_category", 231 $where, 232 array($arrRow['parent_category_id'], 233 $arrRow['category_name'])); 234 if ($count > 0) { 235 $errFlg = true; 236 $this->addRowErr($line_count, "既に同じ内容の登録が存在します。"); 237 break; 238 } 239 // カテゴリ登録 240 $this->registerCategory($arrRow['parent_category_id'], 241 $arrRow['category_name'], 242 $_SESSION['member_id']); 243 244 $message = "[登録] カテゴリ名 : " . $arrRow['category_name']; 245 $this->addRowResult($line_count, $message); 246 } 247 } 248 249 fclose($fp); 250 251 if ($errFlg) { 252 $objQuery->rollback(); 253 return; 254 } 255 256 $objQuery->commit(); 257 258 // カテゴリ件数を更新 259 $objDb->sfCountCategory($objQuery); 260 $objDb->sfCountMaker($objQuery); 261 } 262 263 /** 264 * 登録/編集結果のメッセージをプロパティへ追加する 265 * 266 * @param integer $line_count 行数 267 * @param stirng $message メッセージ 268 * @return void 269 */ 270 function addRowResult($line_count, $message) { 271 $this->arrRowResult[] = $line_count . "行目:" . $message; 272 } 273 274 /** 275 * 登録/編集結果のエラーメッセージをプロパティへ追加する 276 * 277 * @param integer $line_count 行数 278 * @param stirng $message メッセージ 279 * @return void 280 */ 281 function addRowErr($line_count, $message) { 282 $this->arrRowErr[] = $line_count . "行目:" . $message; 283 } 284 285 /** 286 * カテゴリの階層が上限を超えているかを判定する 287 * 288 * @param integer 親カテゴリID 289 * @param 超えている場合 true 290 */ 291 function isOverLevel($parent_category_id) { 292 $objQuery =& SC_Query::getSingletonInstance(); 293 $level = $objQuery->get("level", "dtb_category", "category_id = ?", array($parent_category_id)); 294 return $level >= LEVEL_MAX; 204 295 } 205 296 … … 213 304 } 214 305 215 216 306 /** 217 307 * ファイル情報の初期化を行う. … … 219 309 * @return void 220 310 */ 221 function lfInitFile() {222 $ this->objUpFile->addFile("CSVファイル", 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false);311 function initFile(&$objUpFile) { 312 $objUpFile->addFile("CSVファイル", 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false); 223 313 } 224 314 … … 226 316 * 入力情報の初期化を行う. 227 317 * 228 * @return void 229 */ 230 function lfInitParam() { 231 $this->objFormParam->addParam("カテゴリID","category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK")); 232 $this->objFormParam->addParam("カテゴリ名","category_name",STEXT_LEN,"KVa",array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK")); 233 $this->objFormParam->addParam("親カテゴリID","parent_category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK")); 234 } 235 236 /** 237 * カテゴリ登録を行う. 238 * 239 * @param SC_Query $objQuery SC_Queryインスタンス 240 * @param string|integer $line 処理中の行数 241 * @return void 242 */ 243 function lfRegistProduct($objQuery, $line = "",$arrCSV) { 244 245 $objDb = new SC_Helper_DB_Ex(); 246 $sqlval['category_id'] = $arrCSV[0]; 247 $sqlval['category_name'] = $arrCSV[1]; 248 $sqlval['parent_category_id'] = strlen($arrCSV[2]) ? $arrCSV[2] : 0; 249 250 //存在確認 251 $count = $objQuery->count("dtb_category","category_id = ?",array($sqlval['category_id'])); 252 $update = $count != 0; 253 254 // 親カテゴリID、レベル 255 if ($sqlval['parent_category_id'] == 0) { 256 $sqlval['level'] = 1; 318 * @param SC_FormParam $objFormParam 319 * @return void 320 */ 321 function initParam(&$objFormParam) { 322 $objFormParam->addParam("カテゴリID","category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK")); 323 $objFormParam->addParam("カテゴリ名","category_name",STEXT_LEN,"KVa",array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK")); 324 $objFormParam->addParam("親カテゴリID","parent_category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK")); 325 } 326 327 /** 328 * カテゴリを登録する 329 * 330 * @param integer 親カテゴリID 331 * @param string カテゴリ名 332 * @param integer 作成者のID 333 * @return void 334 */ 335 function registerCategory($parent_category_id, $category_name, $creator_id) { 336 $objQuery =& SC_Query::getSingletonInstance(); 337 338 $rank = null; 339 if ($parent_category_id == 0) { 340 // ROOT階層で最大のランクを取得する。 341 $where = "parent_category_id = ?"; 342 $rank = $objQuery->max("rank", "dtb_category", $where, array($parent_category_id)) + 1; 257 343 } else { 258 $parent_level = $objQuery->get("level", "dtb_category", "category_id = ?", array($sqlval['parent_category_id'])); 259 $sqlval['level'] = $parent_level + 1; 260 } 261 262 // その他 263 $time = date("Y-m-d H:i:s"); 264 if ($line != "") { 265 $microtime = sprintf("%06d", $line); 266 $time .= ".$microtime"; 267 } 268 $sqlval['update_date'] = $time; 269 $sqlval['creator_id'] = $_SESSION['member_id']; 270 271 // 更新 272 if ($update) { 273 echo "更新 "; 344 // 親のランクを自分のランクとする。 274 345 $where = "category_id = ?"; 275 $objQuery->update("dtb_category", $sqlval, $where, array($sqlval['category_id'])); 276 277 // 新規登録 278 } else { 279 echo "登録 "; 280 $sqlval['create_date'] = $time; 281 // ランク 282 if ($sqlval['parent_category_id'] == 0) { 283 // ROOT階層で最大のランクを取得する。 284 $where = "parent_category_id = ?"; 285 $sqlval['rank'] = $objQuery->max("rank", "dtb_category", $where, array($sqlval['parent_category_id'])) + 1; 286 } else { 287 // 親のランクを自分のランクとする。 288 $where = "category_id = ?"; 289 $sqlval['rank'] = $objQuery->get("rank", "dtb_category", $where, array($sqlval['parent_category_id'])); 290 // 追加レコードのランク以上のレコードを一つあげる。 291 $sqlup = "UPDATE dtb_category SET rank = rank + 1 WHERE rank >= ?"; 292 $objQuery->exec($sqlup, array($sqlval['rank'])); 293 294 } 295 $sqlval['category_id'] = $objQuery->nextVal('dtb_category_category_id'); 296 $objQuery->insert("dtb_category", $sqlval); 297 } 298 } 299 300 /** 301 * 入力チェックを行う. 302 * 303 * @return void 304 */ 305 function lfCheckError($arrCSV) { 306 // $arrRet = $this->objFormParam->getHashArray(); 307 $arrRet['category_id'] = $arrCSV[0]; 308 $arrRet['category_name'] = $arrCSV[1]; 309 $arrRet['parent_category_id'] = $arrCSV[2]; 310 311 $objQuery = new SC_Query(); 312 313 $objErr = new SC_CheckError($arrRet); 314 $objErr->arrErr = $this->objFormParam->checkError(false); 315 316 // 親カテゴリID設定 317 if ($arrRet['parent_category_id'] == 0) { 318 $parent_category_id = "0"; 319 } else { 320 $parent_category_id = $arrRet['parent_category_id']; 321 } 322 323 // 存在する親カテゴリIDかチェック 324 if (count($objErr->arrErr) == 0) { 325 if ($parent_category_id != 0){ 326 $count = $objQuery->count("dtb_category", "category_id = ?", array($parent_category_id)); 327 if ($count == 0) { 328 $objErr->arrErr['parent_category_id'] = "※ 指定の親カテゴリID(".$parent_category_id.")は、存在しません。"; 329 } 330 } 331 } 332 333 // 階層チェック 334 if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) { 335 $level = $objQuery->get("level", "dtb_category", "category_id = ?", array($parent_category_id)); 336 if ($level >= LEVEL_MAX) { 337 $objErr->arrErr['category_name'] = "※ ".LEVEL_MAX."階層以上の登録はできません。<br>"; 338 } 339 } 340 341 // 重複チェック 342 if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) { 343 $where = "parent_category_id = ? AND category_name = ?"; 344 $arrCat = $objQuery->select("category_id, category_name", "dtb_category", $where, array($parent_category_id, $arrRet['category_name'])); 345 if (empty($arrCat)) { 346 $arrCat = array(array("category_id" => "", "category_name" => "")); 347 } 348 // 編集中のレコード以外に同じ名称が存在する場合 349 if ($arrCat[0]['category_id'] != $arrRet['category_id'] && $arrCat[0]['category_name'] == $arrRet['category_name']) { 350 echo $arrCat[0]['category_id']; 351 echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>"; 352 353 echo $arrRet['category_id']; 354 echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>"; 355 356 echo $arrCat[0]['category_name'] ; 357 echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>"; 358 echo $arrRet['category_name']; 359 echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>"; 360 361 362 $objErr->arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。</br>"; 363 364 } 365 } 366 return $objErr->arrErr; 367 } 368 369 /** 370 * CSVのカウント数を得る. 371 * 372 * @param resource $fp fopenを使用して作成したファイルポインタ 373 * @return integer CSV のカウント数 374 */ 375 function lfCSVRecordCount($fp) { 376 $count = 0; 377 while(!feof($fp)) { 378 $arrCSV = fgetcsv($fp, CSV_LINE_MAX); 379 $count++; 380 } 381 // ファイルポインタを戻す 382 if (rewind($fp)) { 383 return $count-1; 384 } else { 385 SC_Utils_Ex::sfDispError(""); 386 } 387 } 388 389 /** 390 * 引数の文字列をエラー出力する. 391 * 392 * 引数 $val の内容は, htmlspecialchars() によってサニタイズされ 393 * 394 * @param string $val 出力する文字列 395 * @return void 396 */ 397 function printError($val) { 398 echo "<font color=\"red\">" 399 . htmlspecialchars($val, ENT_QUOTES) 400 . "</font></br>\n"; 346 $rank = $objQuery->get("rank", "dtb_category", $where, array($parent_category_id)); 347 // 追加レコードのランク以上のレコードを一つあげる。 348 $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?"; 349 $objQuery->exec($sqlup, array($rank)); 350 } 351 352 $where = "category_id = ?"; 353 // 自分のレベルを取得する(親のレベル + 1) 354 $level = $objQuery->get("level", "dtb_category", $where, array($parent_category_id)) + 1; 355 356 $arrCategory = array(); 357 $arrCategory['category_name'] = $category_name; 358 $arrCategory['parent_category_id'] = $parent_category_id; 359 $arrCategory['create_date'] = "Now()"; 360 $arrCategory['update_date'] = "Now()"; 361 $arrCategory['creator_id'] = $creator_id; 362 $arrCategory['rank'] = $rank; 363 $arrCategory['level'] = $level; 364 $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id'); 365 $objQuery->insert("dtb_category", $arrCategory); 401 366 } 402 367 } 403 ?>
Note: See TracChangeset
for help on using the changeset viewer.