[Svn-src-all:1387] [version-2_5-dev 20277] #1010 カテゴリ登録CSVリファクタリング
adachi
admin @ mail.ec-cube.net
2011年 2月 21日 (月) 01:33:29 JST
Subversion committed to /home/svn/open 20277
http://svn.ec-cube.net/open_trac/changeset/20277
┌────────────────────────────┐
│更新者 : adachi │
│更新日時: 2011-02-21 01:33:29 +0900 (月, 21 2月 2011)│
└────────────────────────────┘
Log:
--------------------------------------------------------
#1010 カテゴリ登録CSVリファクタリング
Changed: [U:修正,A:追加,D:削除]
--------------------------------------------------------
U branches/version-2_5-dev/data/Smarty/templates/admin/products/upload_csv_category.tpl
U branches/version-2_5-dev/data/class/pages/admin/products/LC_Page_Admin_Products_UploadCSVCategory.php
変更: branches/version-2_5-dev/data/Smarty/templates/admin/products/upload_csv_category.tpl
===================================================================
--- branches/version-2_5-dev/data/Smarty/templates/admin/products/upload_csv_category.tpl 2011-02-20 16:11:35 UTC (rev 20276)
+++ branches/version-2_5-dev/data/Smarty/templates/admin/products/upload_csv_category.tpl 2011-02-20 16:33:29 UTC (rev 20277)
@@ -63,5 +63,27 @@
<li><a class="btn-action" href="javascript:;" onclick="fnFormModeSubmit('form1', 'csv_upload', '', ''); return false;"><span class="btn-next">この内容で登録する</span></a></li>
</ul>
</div>
+ <!--{if $arrRowErr}-->
+ <table class="form">
+ <tr>
+ <td>
+ <!--{foreach item=err from=$arrRowErr}-->
+ <span class="attention"><!--{$err}--></span>
+ <!--{/foreach}-->
+ </td>
+ </tr>
+ </table>
+ <!--{/if}-->
+ <!--{if $arrRowResult}-->
+ <table class="form">
+ <tr>
+ <td>
+ <!--{foreach item=result from=$arrRowResult}-->
+ <span><!--{$result}--><br/></span>
+ <!--{/foreach}-->
+ </td>
+ </tr>
+ </table>
+ <!--{/if}-->
</div>
</form>
変更: branches/version-2_5-dev/data/class/pages/admin/products/LC_Page_Admin_Products_UploadCSVCategory.php
===================================================================
--- branches/version-2_5-dev/data/class/pages/admin/products/LC_Page_Admin_Products_UploadCSVCategory.php 2011-02-20 16:11:35 UTC (rev 20276)
+++ branches/version-2_5-dev/data/class/pages/admin/products/LC_Page_Admin_Products_UploadCSVCategory.php 2011-02-20 16:33:29 UTC (rev 20277)
@@ -38,6 +38,14 @@
// }}}
// {{{ functions
+ var $arrErr;
+
+ var $arrTitle;
+
+ var $arrRowResult;
+
+ var $arrRowErr;
+
/**
* Page を初期化する.
*
@@ -46,9 +54,9 @@
function init() {
parent::init();
$this->tpl_mainpage = 'products/upload_csv_category.tpl';
- $this->tpl_subnavi = 'products/subnavi.tpl';
- $this->tpl_mainno = 'products';
- $this->tpl_subno = 'upload_csv_category';
+ $this->tpl_subnavi = 'products/subnavi.tpl';
+ $this->tpl_mainno = 'products';
+ $this->tpl_subno = 'upload_csv_category';
$this->tpl_subtitle = 'カテゴリ登録CSV';
}
@@ -68,336 +76,292 @@
* @return void
*/
function action() {
- $objSess = new SC_Session();
- $objDb = new SC_Helper_DB_Ex();
+ $objSess = new SC_Session();
+ $objDb = new SC_Helper_DB_Ex();
+ $objUpFile = new SC_UploadFile(IMAGE_TEMP_REALDIR, IMAGE_SAVE_REALDIR);
+ $objFormParam = new SC_FormParam();
// 認証可否の判定
SC_Utils_Ex::sfIsSuccess($objSess);
- // ファイル管理クラス
- $this->objUpFile = new SC_UploadFile(IMAGE_TEMP_REALDIR, IMAGE_SAVE_REALDIR);
- // ファイル情報の初期化
- $this->lfInitFile();
- // パラメータ管理クラス
- $this->objFormParam = new SC_FormParam();
- // パラメータ情報の初期化
- $this->lfInitParam();
- $colmax = $this->objFormParam->getCount();
- $this->objFormParam->setHtmlDispNameArray();
- $this->arrTitle = $this->objFormParam->getHtmlDispNameArray();
+ // ファイルオブジェクト初期化
+ $this->initFile($objUpFile);
+ // 入力パラメータ初期化
+ $this->initParam($objFormParam);
+ $objFormParam->setHtmlDispNameArray();
+ $this->arrTitle = $objFormParam->getHtmlDispNameArray();
+
switch ($this->getMode()) {
- case 'csv_upload':
- $err = false;
- // エラーチェック
- $arrErr['csv_file'] = $this->objUpFile->makeTempFile('csv_file');
+ case 'csv_upload':
+ $this->doUploadCsv($objFormParam, $objUpFile, $objDb);
+ break;
+ default:
+ }
+ }
- if($arrErr['csv_file'] == "") {
- $arrErr = $this->objUpFile->checkEXISTS();
- }
+ /**
+ * CSVアップロードを実行する
+ *
+ * @param SC_FormParam $objFormParam
+ * @param SC_UploadFile $objUpFile
+ * @param SC_Helper_DB $objDb
+ * @return void
+ */
+ function doUploadCsv(&$objFormParam, &$objUpFile, &$objDb) {
+ // ファイルアップロードのチェック
+ $objUpFile->makeTempFile('csv_file');
+ $arrErr = $objUpFile->checkExists();
+ if (count($arrErr) > 0) {
+ $this->arrErr = $arrErr;
+ return;
+ }
- // 実行時間を制限しない
- set_time_limit(0);
+ // 一時ファイル名の取得
+ $filepath = $objUpFile->getTempFilePath('csv_file');
+ // CSVファイルの文字コード変換
+ $enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath, CHAR_CODE, CSV_TEMP_REALDIR);
+ // CSVファイルのオープン
+ $fp = fopen($enc_filepath, "r");
+ // 失敗した場合はエラー表示
+ if (!$fp) {
+ SC_Utils_Ex::sfDispError("");
+ }
- // 出力をバッファリングしない(==日本語自動変換もしない)
- ob_end_clean();
+ // 登録対象の列数
+ $col_max_count = $objFormParam->getCount();
+ // 行数
+ $line_count = 0;
- // IEのために256バイト空文字出力
- echo str_pad('',256);
+ $objQuery = SC_Query::getSingletonInstance();
+ $objQuery->begin();
- if (empty($arrErr['csv_file'])) {
- // 一時ファイル名の取得
- $filepath = $this->objUpFile->getTempFilePath('csv_file');
- // エンコード
- $enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath,
- CHAR_CODE, CSV_TEMP_REALDIR);
+ $errFlg = false;
+
+ while (!feof($fp)) {
+ $arrRow = fgetcsv($fp, CSV_LINE_MAX);
+ $line_count++;
- $fp = fopen($enc_filepath, "r");
+ // ヘッダ行はスキップ
+ if ($line_count == 1) {
+ continue;
+ }
+ // 空行はスキップ
+ if (empty($arrRow)) {
+ continue;
+ }
+ // 列数が異なる場合はエラー
+ if ($col_max_count != count($arrRow)) {
+ $errFlg = true;
+ break;
+ }
+ // 数値インデックスから, カラム名 => 値の連想配列へ変換
+ $objFormParam->setParam($arrRow, true);
+ $arrRow = $objFormParam->getHashArray();
+ $objFormParam->setParam($arrRow);
+ $objFormParam->convParam();
+ // 入力項目チェック
+ $arrErr = $objFormParam->checkError();
+ if (count($arrErr) > 0) {
+ foreach ($arrErr as $err) {
+ $this->addRowErr($line_count, $err);
+ }
+ $errFlg = true;
+ break;
+ }
- // 無効なファイルポインタが渡された場合はエラー表示
- if ($fp === false) {
- SC_Utils_Ex::sfDispError("");
- }
+ // 親カテゴリIDがない場合はルートのカテゴリIDをセット
+ if ($arrRow['parent_category_id'] == '') {
+ $arrRow['parent_category_id'] = 0;
+ }
- // レコード数を得る
- $rec_count = $this->lfCSVRecordCount($fp);
+ // 親カテゴリIDの存在チェック
+ $count = $objQuery->count("dtb_category", "category_id = ?", array($arrRow['parent_category_id']));
+ if ($arrRow['parent_category_id'] != 0 && $count == 0) {
+ $errFlg = true;
+ $this->addRowErr($line_count, "指定の親カテゴリID(" . $arrRow['parent_category_id'] . ")は、存在しません。");
+ break;
+ }
+
+ $count = $objQuery->count("dtb_category", "category_id = ?", array($arrRow['category_id']));
- $line = 0; // 行数
- $regist = 0; // 登録数
+ // 編集
+ if ($count > 0) {
+ // 重複チェック
+ $where = "parent_category_id = ? AND category_id <> ? AND category_name = ?";
+ $count = $objQuery->count("dtb_category",
+ $where,
+ array($arrRow['parent_category_id'],
+ $arrRow['category_id'],
+ $arrRow['category_name']));
+ if ($count > 0) {
+ $errFlg = true;
+ $this->addRowErr($line_count, "既に同じ内容の登録が存在します。");
+ break;
+ }
- $objQuery = new SC_Query();
- $objQuery->begin();
+ // カテゴリ更新
+ $arrCategory = array();
+ $arrCategory['category_name'] = $arrRow['category_name'];
+ $arrCategory['update_date'] = 'NOW()';
+ $where = "category_id = ?";
+ $objQuery->update("dtb_category", $arrCategory, $where, array($arrRow['category_id']));
+
+ $message = "[更新] カテゴリID: " . $arrRow['category_id'] . " カテゴリ名 : " . $arrRow['category_name'];
+ $this->addRowResult($line_count, $message);
+ // 登録
+ } else {
+ // 登録数上限チェック
+ $where = "del_flg = 0";
+ $count = $objQuery->count("dtb_category", $where);
+ if ($count >= CATEGORY_MAX) {
+ $errFlg = true;
+ $this->addRowErr($line_count, "カテゴリの登録最大数を超えました。");
+ break;
+ }
+ // 階層上限チェック
+ if ($this->isOverLevel($arrRow['parent_category_id'])) {
+ $errFlg = true;
+ $this->addRowErr($line_count, LEVEL_MAX . "階層以上の登録はできません。");
+ break;
+ }
+ // 重複チェック
+ $where = "parent_category_id = ? AND category_name = ?";
+ $count = $objQuery->count("dtb_category",
+ $where,
+ array($arrRow['parent_category_id'],
+ $arrRow['category_name']));
+ if ($count > 0) {
+ $errFlg = true;
+ $this->addRowErr($line_count, "既に同じ内容の登録が存在します。");
+ break;
+ }
+ // カテゴリ登録
+ $this->registerCategory($arrRow['parent_category_id'],
+ $arrRow['category_name'],
+ $_SESSION['member_id']);
- echo "■ CSV登録進捗状況 <br/><br/>\n";
- while (!feof($fp) && !$err) {
- $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
+ $message = "[登録] カテゴリ名 : " . $arrRow['category_name'];
+ $this->addRowResult($line_count, $message);
+ }
+ }
+
+ fclose($fp);
- // 行カウント
- $line++;
+ if ($errFlg) {
+ $objQuery->rollback();
+ return;
+ }
- if ($line <= 1) {
- continue;
- }
+ $objQuery->commit();
- // 項目数カウント
- $max = count($arrCSV);
-
- // 項目数が1以下の場合は無視する
- if ($max <= 1) {
- continue;
- }
-
- // 項目数チェック
- if ($max != $colmax) {
- echo "※ 項目数が" . $max . "個検出されました。項目数は" . $colmax . "個になります。</br>\n";
- $err = true;
- } else {
- // シーケンス配列を格納する。
- $this->objFormParam->setParam($arrCSV, true);
- $arrRet = $this->objFormParam->getHashArray();
- $this->objFormParam->setParam($arrRet);
- // 入力値の変換
- $this->objFormParam->convParam();
- // <br>なしでエラー取得する。
- $arrCSVErr = $this->lfCheckError($arrCSV);
- }
-
- // 入力エラーチェック
- if (count($arrCSVErr) > 0) {
- echo "<font color=\"red\">■" . $line . "行目でエラーが発生しました。</font></br>\n";
- foreach($arrCSVErr as $val) {
- $this->printError($val);
- }
- $err = true;
- }
-
- if (!$err) {
- $this->lfRegistProduct($objQuery, $line,$arrCSV);
- $regist++;
- }
- $arrParam = $this->objFormParam->getHashArray();
-
- if (!$err) echo $line." / ".$rec_count. "行目 (カテゴリID:".$arrParam['category_id']." / カテゴリ名:".$arrParam['category_name'].")\n<br />";
- SC_Utils_Ex::sfFlush();
- }
- fclose($fp);
-
- if (!$err) {
- $objQuery->commit();
- echo "■" . $regist . "件のレコードを登録しました。";
- // カテゴリ件数カウント関数の実行
- $objDb->sfCountCategory($objQuery);
- $objDb->sfCountMaker($objQuery);
- } else {
- $objQuery->rollback();
- }
- } else {
- foreach($arrErr as $val) {
- $this->printError($val);
- }
- }
- echo "<br/><a href=\"javascript:window.close()\">→閉じる</a>";
- exit;
- default:
- break;
- }
+ // カテゴリ件数を更新
+ $objDb->sfCountCategory($objQuery);
+ $objDb->sfCountMaker($objQuery);
}
/**
- * デストラクタ.
+ * 登録/編集結果のメッセージをプロパティへ追加する
*
+ * @param integer $line_count 行数
+ * @param stirng $message メッセージ
* @return void
*/
- function destroy() {
- parent::destroy();
+ function addRowResult($line_count, $message) {
+ $this->arrRowResult[] = $line_count . "行目:" . $message;
}
-
/**
- * ファイル情報の初期化を行う.
+ * 登録/編集結果のエラーメッセージをプロパティへ追加する
*
+ * @param integer $line_count 行数
+ * @param stirng $message メッセージ
* @return void
*/
- function lfInitFile() {
- $this->objUpFile->addFile("CSVファイル", 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false);
+ function addRowErr($line_count, $message) {
+ $this->arrRowErr[] = $line_count . "行目:" . $message;
}
/**
- * 入力情報の初期化を行う.
+ * カテゴリの階層が上限を超えているかを判定する
*
- * @return void
+ * @param integer 親カテゴリID
+ * @param 超えている場合 true
*/
- function lfInitParam() {
- $this->objFormParam->addParam("カテゴリID","category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
- $this->objFormParam->addParam("カテゴリ名","category_name",STEXT_LEN,"KVa",array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK"));
- $this->objFormParam->addParam("親カテゴリID","parent_category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
+ function isOverLevel($parent_category_id) {
+ $objQuery =& SC_Query::getSingletonInstance();
+ $level = $objQuery->get("level", "dtb_category", "category_id = ?", array($parent_category_id));
+ return $level >= LEVEL_MAX;
}
/**
- * カテゴリ登録を行う.
+ * デストラクタ.
*
- * @param SC_Query $objQuery SC_Queryインスタンス
- * @param string|integer $line 処理中の行数
* @return void
*/
- function lfRegistProduct($objQuery, $line = "",$arrCSV) {
-
- $objDb = new SC_Helper_DB_Ex();
- $sqlval['category_id'] = $arrCSV[0];
- $sqlval['category_name'] = $arrCSV[1];
- $sqlval['parent_category_id'] = strlen($arrCSV[2]) ? $arrCSV[2] : 0;
-
- //存在確認
- $count = $objQuery->count("dtb_category","category_id = ?",array($sqlval['category_id']));
- $update = $count != 0;
-
- // 親カテゴリID、レベル
- if ($sqlval['parent_category_id'] == 0) {
- $sqlval['level'] = 1;
- } else {
- $parent_level = $objQuery->get("level", "dtb_category", "category_id = ?", array($sqlval['parent_category_id']));
- $sqlval['level'] = $parent_level + 1;
- }
-
- // その他
- $time = date("Y-m-d H:i:s");
- if ($line != "") {
- $microtime = sprintf("%06d", $line);
- $time .= ".$microtime";
- }
- $sqlval['update_date'] = $time;
- $sqlval['creator_id'] = $_SESSION['member_id'];
-
- // 更新
- if ($update) {
- echo "更新 ";
- $where = "category_id = ?";
- $objQuery->update("dtb_category", $sqlval, $where, array($sqlval['category_id']));
-
- // 新規登録
- } else {
- echo "登録 ";
- $sqlval['create_date'] = $time;
- // ランク
- if ($sqlval['parent_category_id'] == 0) {
- // ROOT階層で最大のランクを取得する。
- $where = "parent_category_id = ?";
- $sqlval['rank'] = $objQuery->max("rank", "dtb_category", $where, array($sqlval['parent_category_id'])) + 1;
- } else {
- // 親のランクを自分のランクとする。
- $where = "category_id = ?";
- $sqlval['rank'] = $objQuery->get("rank", "dtb_category", $where, array($sqlval['parent_category_id']));
- // 追加レコードのランク以上のレコードを一つあげる。
- $sqlup = "UPDATE dtb_category SET rank = rank + 1 WHERE rank >= ?";
- $objQuery->exec($sqlup, array($sqlval['rank']));
-
- }
- $sqlval['category_id'] = $objQuery->nextVal('dtb_category_category_id');
- $objQuery->insert("dtb_category", $sqlval);
- }
+ function destroy() {
+ parent::destroy();
}
/**
- * 入力チェックを行う.
+ * ファイル情報の初期化を行う.
*
* @return void
*/
- function lfCheckError($arrCSV) {
-// $arrRet = $this->objFormParam->getHashArray();
- $arrRet['category_id'] = $arrCSV[0];
- $arrRet['category_name'] = $arrCSV[1];
- $arrRet['parent_category_id'] = $arrCSV[2];
-
- $objQuery = new SC_Query();
-
- $objErr = new SC_CheckError($arrRet);
- $objErr->arrErr = $this->objFormParam->checkError(false);
-
- // 親カテゴリID設定
- if ($arrRet['parent_category_id'] == 0) {
- $parent_category_id = "0";
- } else {
- $parent_category_id = $arrRet['parent_category_id'];
- }
-
- // 存在する親カテゴリIDかチェック
- if (count($objErr->arrErr) == 0) {
- if ($parent_category_id != 0){
- $count = $objQuery->count("dtb_category", "category_id = ?", array($parent_category_id));
- if ($count == 0) {
- $objErr->arrErr['parent_category_id'] = "※ 指定の親カテゴリID(".$parent_category_id.")は、存在しません。";
- }
- }
- }
-
- // 階層チェック
- if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
- $level = $objQuery->get("level", "dtb_category", "category_id = ?", array($parent_category_id));
- if ($level >= LEVEL_MAX) {
- $objErr->arrErr['category_name'] = "※ ".LEVEL_MAX."階層以上の登録はできません。<br>";
- }
- }
-
- // 重複チェック
- if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
- $where = "parent_category_id = ? AND category_name = ?";
- $arrCat = $objQuery->select("category_id, category_name", "dtb_category", $where, array($parent_category_id, $arrRet['category_name']));
- if (empty($arrCat)) {
- $arrCat = array(array("category_id" => "", "category_name" => ""));
- }
- // 編集中のレコード以外に同じ名称が存在する場合
- if ($arrCat[0]['category_id'] != $arrRet['category_id'] && $arrCat[0]['category_name'] == $arrRet['category_name']) {
- echo $arrCat[0]['category_id'];
- echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
-
- echo $arrRet['category_id'];
- echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
-
- echo $arrCat[0]['category_name'] ;
- echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
- echo $arrRet['category_name'];
- echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
-
-
- $objErr->arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。</br>";
-
- }
- }
- return $objErr->arrErr;
+ function initFile(&$objUpFile) {
+ $objUpFile->addFile("CSVファイル", 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false);
}
/**
- * CSVのカウント数を得る.
+ * 入力情報の初期化を行う.
*
- * @param resource $fp fopenを使用して作成したファイルポインタ
- * @return integer CSV のカウント数
+ * @param SC_FormParam $objFormParam
+ * @return void
*/
- function lfCSVRecordCount($fp) {
- $count = 0;
- while(!feof($fp)) {
- $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
- $count++;
- }
- // ファイルポインタを戻す
- if (rewind($fp)) {
- return $count-1;
- } else {
- SC_Utils_Ex::sfDispError("");
- }
+ function initParam(&$objFormParam) {
+ $objFormParam->addParam("カテゴリID","category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
+ $objFormParam->addParam("カテゴリ名","category_name",STEXT_LEN,"KVa",array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK"));
+ $objFormParam->addParam("親カテゴリID","parent_category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
}
/**
- * 引数の文字列をエラー出力する.
+ * カテゴリを登録する
*
- * 引数 $val の内容は, htmlspecialchars() によってサニタイズされ
- *
- * @param string $val 出力する文字列
+ * @param integer 親カテゴリID
+ * @param string カテゴリ名
+ * @param integer 作成者のID
* @return void
*/
- function printError($val) {
- echo "<font color=\"red\">"
- . htmlspecialchars($val, ENT_QUOTES)
- . "</font></br>\n";
+ function registerCategory($parent_category_id, $category_name, $creator_id) {
+ $objQuery =& SC_Query::getSingletonInstance();
+
+ $rank = null;
+ if ($parent_category_id == 0) {
+ // ROOT階層で最大のランクを取得する。
+ $where = "parent_category_id = ?";
+ $rank = $objQuery->max("rank", "dtb_category", $where, array($parent_category_id)) + 1;
+ } else {
+ // 親のランクを自分のランクとする。
+ $where = "category_id = ?";
+ $rank = $objQuery->get("rank", "dtb_category", $where, array($parent_category_id));
+ // 追加レコードのランク以上のレコードを一つあげる。
+ $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?";
+ $objQuery->exec($sqlup, array($rank));
+ }
+
+ $where = "category_id = ?";
+ // 自分のレベルを取得する(親のレベル + 1)
+ $level = $objQuery->get("level", "dtb_category", $where, array($parent_category_id)) + 1;
+
+ $arrCategory = array();
+ $arrCategory['category_name'] = $category_name;
+ $arrCategory['parent_category_id'] = $parent_category_id;
+ $arrCategory['create_date'] = "Now()";
+ $arrCategory['update_date'] = "Now()";
+ $arrCategory['creator_id'] = $creator_id;
+ $arrCategory['rank'] = $rank;
+ $arrCategory['level'] = $level;
+ $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id');
+ $objQuery->insert("dtb_category", $arrCategory);
}
}
-?>
Svn-src-all メーリングリストの案内