[17047] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | * This file is part of EC-CUBE |
---|
| 4 | * |
---|
[23546] | 5 | * Copyright(c) 2000-2014 LOCKON CO.,LTD. All Rights Reserved. |
---|
[17047] | 6 | * |
---|
| 7 | * http://www.lockon.co.jp/ |
---|
| 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. |
---|
| 22 | */ |
---|
| 23 | |
---|
[20534] | 24 | require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php'; |
---|
[17047] | 25 | |
---|
| 26 | /** |
---|
[21317] | 27 | * カテゴリ登録CSVのページクラス |
---|
[17047] | 28 | * |
---|
| 29 | * LC_Page_Admin_Products_UploadCSV をカスタマイズする場合はこのクラスを編集する. |
---|
| 30 | * |
---|
| 31 | * @package Page |
---|
| 32 | * @author LOCKON CO.,LTD. |
---|
| 33 | * @version $$Id$$ |
---|
| 34 | */ |
---|
[22856] | 35 | class LC_Page_Admin_Products_UploadCSVCategory extends LC_Page_Admin_Ex |
---|
[22567] | 36 | { |
---|
[20733] | 37 | /** エラー情報 **/ |
---|
[23020] | 38 | public $arrErr; |
---|
[20277] | 39 | |
---|
[20733] | 40 | /** 表示用項目 **/ |
---|
[23020] | 41 | public $arrTitle; |
---|
[20277] | 42 | |
---|
[20733] | 43 | /** 結果行情報 **/ |
---|
[23020] | 44 | public $arrRowResult; |
---|
[20277] | 45 | |
---|
[20733] | 46 | /** エラー行情報 **/ |
---|
[23020] | 47 | public $arrRowErr; |
---|
[20277] | 48 | |
---|
[20733] | 49 | /** TAGエラーチェックフィールド情報 */ |
---|
[23020] | 50 | public $arrTagCheckItem; |
---|
[20733] | 51 | |
---|
| 52 | /** テーブルカラム情報 (登録処理用) **/ |
---|
[23020] | 53 | public $arrRegistColumn; |
---|
[20733] | 54 | |
---|
| 55 | /** 登録フォームカラム情報 **/ |
---|
[23020] | 56 | public $arrFormKeyList; |
---|
[20733] | 57 | |
---|
[17047] | 58 | /** |
---|
| 59 | * Page を初期化する. |
---|
| 60 | * |
---|
| 61 | * @return void |
---|
| 62 | */ |
---|
[23020] | 63 | public function init() |
---|
[22567] | 64 | { |
---|
[17047] | 65 | parent::init(); |
---|
| 66 | $this->tpl_mainpage = 'products/upload_csv_category.tpl'; |
---|
[20277] | 67 | $this->tpl_mainno = 'products'; |
---|
| 68 | $this->tpl_subno = 'upload_csv_category'; |
---|
[20911] | 69 | $this->tpl_maintitle = '商品管理'; |
---|
[21317] | 70 | $this->tpl_subtitle = 'カテゴリ登録CSV'; |
---|
[20733] | 71 | $this->csv_id = '5'; |
---|
| 72 | |
---|
| 73 | $masterData = new SC_DB_MasterData_Ex(); |
---|
[21481] | 74 | $this->arrAllowedTag = $masterData->getMasterData('mtb_allowed_tag'); |
---|
[20733] | 75 | $this->arrTagCheckItem = array(); |
---|
[17047] | 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * Page のプロセス. |
---|
| 80 | * |
---|
| 81 | * @return void |
---|
| 82 | */ |
---|
[23020] | 83 | public function process() |
---|
[22567] | 84 | { |
---|
[19661] | 85 | $this->action(); |
---|
| 86 | $this->sendResponse(); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * Page のアクション. |
---|
| 91 | * |
---|
| 92 | * @return void |
---|
| 93 | */ |
---|
[23020] | 94 | public function action() |
---|
[22567] | 95 | { |
---|
[20733] | 96 | // CSV管理ヘルパー |
---|
| 97 | $objCSV = new SC_Helper_CSV_Ex(); |
---|
| 98 | // CSV構造読み込み |
---|
| 99 | $arrCSVFrame = $objCSV->sfGetCsvOutput($this->csv_id); |
---|
| 100 | |
---|
| 101 | // CSV構造がインポート可能かのチェック |
---|
[21442] | 102 | if (!$objCSV->sfIsImportCSVFrame($arrCSVFrame)) { |
---|
[20733] | 103 | // 無効なフォーマットなので初期状態に強制変更 |
---|
| 104 | $arrCSVFrame = $objCSV->sfGetCsvOutput($this->csv_id, '', array(), 'no'); |
---|
| 105 | $this->tpl_is_format_default = true; |
---|
| 106 | } |
---|
| 107 | // CSV構造は更新可能なフォーマットかのフラグ取得 |
---|
| 108 | $this->tpl_is_update = $objCSV->sfIsUpdateCSVFrame($arrCSVFrame); |
---|
| 109 | |
---|
| 110 | // CSVファイルアップロード情報の初期化 |
---|
[23659] | 111 | $objUpFile = new SC_UploadFile_Ex(CSV_TEMP_REALDIR, CSV_TEMP_REALDIR); |
---|
[20733] | 112 | $this->lfInitFile($objUpFile); |
---|
| 113 | |
---|
[20970] | 114 | // パラメーター情報の初期化 |
---|
[20501] | 115 | $objFormParam = new SC_FormParam_Ex(); |
---|
[20733] | 116 | $this->lfInitParam($objFormParam, $arrCSVFrame); |
---|
[17047] | 117 | |
---|
[22251] | 118 | $this->max_upload_csv_size = SC_Utils_Ex::getUnitDataSize(CSV_SIZE); |
---|
| 119 | |
---|
[20277] | 120 | $objFormParam->setHtmlDispNameArray(); |
---|
| 121 | $this->arrTitle = $objFormParam->getHtmlDispNameArray(); |
---|
| 122 | |
---|
[21441] | 123 | switch ($this->getMode()) { |
---|
[21526] | 124 | case 'csv_upload': |
---|
| 125 | $this->doUploadCsv($objFormParam, $objUpFile); |
---|
| 126 | break; |
---|
| 127 | default: |
---|
| 128 | break; |
---|
[20277] | 129 | } |
---|
[21743] | 130 | |
---|
[20277] | 131 | } |
---|
[17047] | 132 | |
---|
[20277] | 133 | /** |
---|
[20733] | 134 | * 登録/編集結果のメッセージをプロパティへ追加する |
---|
| 135 | * |
---|
[23020] | 136 | * @param integer $line_count 行数 |
---|
[23439] | 137 | * @param string $message メッセージ |
---|
[20733] | 138 | * @return void |
---|
| 139 | */ |
---|
[23020] | 140 | public function addRowResult($line_count, $message) |
---|
[22567] | 141 | { |
---|
[21514] | 142 | $this->arrRowResult[] = $line_count . '行目:' . $message; |
---|
[20733] | 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * 登録/編集結果のエラーメッセージをプロパティへ追加する |
---|
| 147 | * |
---|
[23020] | 148 | * @param integer $line_count 行数 |
---|
[23439] | 149 | * @param string $message メッセージ |
---|
[20733] | 150 | * @return void |
---|
| 151 | */ |
---|
[23020] | 152 | public function addRowErr($line_count, $message) |
---|
[22567] | 153 | { |
---|
[21514] | 154 | $this->arrRowErr[] = $line_count . '行目:' . $message; |
---|
[20733] | 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
[20277] | 158 | * CSVアップロードを実行する |
---|
| 159 | * |
---|
[23020] | 160 | * @param SC_FormParam $objFormParam |
---|
| 161 | * @param SC_UploadFile $objUpFile |
---|
[20277] | 162 | * @return void |
---|
| 163 | */ |
---|
[23020] | 164 | public function doUploadCsv(&$objFormParam, &$objUpFile) |
---|
[22567] | 165 | { |
---|
[20277] | 166 | // ファイルアップロードのチェック |
---|
| 167 | $objUpFile->makeTempFile('csv_file'); |
---|
| 168 | $arrErr = $objUpFile->checkExists(); |
---|
| 169 | if (count($arrErr) > 0) { |
---|
| 170 | $this->arrErr = $arrErr; |
---|
[23020] | 171 | |
---|
[20277] | 172 | return; |
---|
| 173 | } |
---|
| 174 | // 一時ファイル名の取得 |
---|
| 175 | $filepath = $objUpFile->getTempFilePath('csv_file'); |
---|
| 176 | // CSVファイルの文字コード変換 |
---|
| 177 | $enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath, CHAR_CODE, CSV_TEMP_REALDIR); |
---|
| 178 | // CSVファイルのオープン |
---|
[20538] | 179 | $fp = fopen($enc_filepath, 'r'); |
---|
[20277] | 180 | // 失敗した場合はエラー表示 |
---|
| 181 | if (!$fp) { |
---|
[21527] | 182 | SC_Utils_Ex::sfDispError(''); |
---|
[20277] | 183 | } |
---|
[17047] | 184 | |
---|
[20733] | 185 | // 登録先テーブル カラム情報の初期化 |
---|
| 186 | $this->lfInitTableInfo(); |
---|
| 187 | |
---|
| 188 | // 登録フォーム カラム情報 |
---|
| 189 | $this->arrFormKeyList = $objFormParam->getKeyList(); |
---|
| 190 | |
---|
[20277] | 191 | // 登録対象の列数 |
---|
| 192 | $col_max_count = $objFormParam->getCount(); |
---|
| 193 | // 行数 |
---|
| 194 | $line_count = 0; |
---|
[17047] | 195 | |
---|
[20733] | 196 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[20277] | 197 | $objQuery->begin(); |
---|
[17047] | 198 | |
---|
[20733] | 199 | $errFlag = false; |
---|
[20540] | 200 | |
---|
[20277] | 201 | while (!feof($fp)) { |
---|
[20733] | 202 | $arrCSV = fgetcsv($fp, CSV_LINE_MAX); |
---|
| 203 | // 行カウント |
---|
[20277] | 204 | $line_count++; |
---|
| 205 | // ヘッダ行はスキップ |
---|
| 206 | if ($line_count == 1) { |
---|
| 207 | continue; |
---|
| 208 | } |
---|
| 209 | // 空行はスキップ |
---|
[20733] | 210 | if (empty($arrCSV)) { |
---|
[20277] | 211 | continue; |
---|
| 212 | } |
---|
| 213 | // 列数が異なる場合はエラー |
---|
[20733] | 214 | $col_count = count($arrCSV); |
---|
[20371] | 215 | if ($col_max_count != $col_count) { |
---|
[21515] | 216 | $this->addRowErr($line_count, '※ 項目数が' . $col_count . '個検出されました。項目数は' . $col_max_count . '個になります。'); |
---|
[20733] | 217 | $errFlag = true; |
---|
[20277] | 218 | break; |
---|
| 219 | } |
---|
[20733] | 220 | // シーケンス配列を格納する。 |
---|
| 221 | $objFormParam->setParam($arrCSV, true); |
---|
| 222 | // 入力値の変換 |
---|
[20277] | 223 | $objFormParam->convParam(); |
---|
[20733] | 224 | // <br>なしでエラー取得する。 |
---|
| 225 | $arrCSVErr = $this->lfCheckError($objFormParam); |
---|
| 226 | |
---|
| 227 | // 入力エラーチェック |
---|
| 228 | if (count($arrCSVErr) > 0) { |
---|
| 229 | foreach ($arrCSVErr as $err) { |
---|
[20277] | 230 | $this->addRowErr($line_count, $err); |
---|
| 231 | } |
---|
[20733] | 232 | $errFlag = true; |
---|
[20277] | 233 | break; |
---|
| 234 | } |
---|
[18508] | 235 | |
---|
[23649] | 236 | $category_id = $this->lfRegistCategory($objQuery, $line_count, $objFormParam); |
---|
[21514] | 237 | $this->addRowResult($line_count, 'カテゴリID:'.$category_id . ' / カテゴリ名:' . $objFormParam->getValue('category_name')); |
---|
[20277] | 238 | } |
---|
[20371] | 239 | |
---|
| 240 | // 実行結果画面を表示 |
---|
| 241 | $this->tpl_mainpage = 'products/upload_csv_category_complete.tpl'; |
---|
| 242 | |
---|
[20733] | 243 | fclose($fp); |
---|
| 244 | |
---|
[20869] | 245 | if ($errFlag) { |
---|
[20277] | 246 | $objQuery->rollback(); |
---|
[23020] | 247 | |
---|
[20277] | 248 | return; |
---|
| 249 | } |
---|
[17047] | 250 | |
---|
[20277] | 251 | $objQuery->commit(); |
---|
[17047] | 252 | |
---|
[20277] | 253 | // カテゴリ件数を更新 |
---|
[23020] | 254 | $objDb = new SC_Helper_DB_Ex(); |
---|
| 255 | $objDb->sfCountCategory($objQuery); |
---|
[22856] | 256 | |
---|
[20733] | 257 | return; |
---|
[17047] | 258 | } |
---|
| 259 | |
---|
| 260 | /** |
---|
[20733] | 261 | * ファイル情報の初期化を行う. |
---|
[17047] | 262 | * |
---|
[23439] | 263 | * @param SC_UploadFile $objUpFile |
---|
[17047] | 264 | * @return void |
---|
| 265 | */ |
---|
[23439] | 266 | public function lfInitFile(SC_UploadFile &$objUpFile) |
---|
[22567] | 267 | { |
---|
[21514] | 268 | $objUpFile->addFile('CSVファイル', 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false); |
---|
[17047] | 269 | } |
---|
| 270 | |
---|
| 271 | /** |
---|
[20733] | 272 | * 入力情報の初期化を行う. |
---|
[17047] | 273 | * |
---|
[23439] | 274 | * @param SC_FormParam $objFormParam |
---|
| 275 | * @param array $arrCSVFrame CSV構造設定配列 |
---|
[20733] | 276 | * @return void |
---|
[17047] | 277 | */ |
---|
[23439] | 278 | public function lfInitParam(SC_FormParam &$objFormParam, &$arrCSVFrame) |
---|
[22567] | 279 | { |
---|
[20733] | 280 | // 固有の初期値調整 |
---|
| 281 | $arrCSVFrame = $this->lfSetParamDefaultValue($arrCSVFrame); |
---|
| 282 | // CSV項目毎の処理 |
---|
[21441] | 283 | foreach ($arrCSVFrame as $item) { |
---|
[21684] | 284 | if ($item['status'] == CSV_COLUMN_STATUS_FLG_DISABLE) continue; |
---|
[20733] | 285 | //サブクエリ構造の場合は AS名 を使用 |
---|
[21441] | 286 | if (preg_match_all('/\(.+\) as (.+)$/i', $item['col'], $match, PREG_SET_ORDER)) { |
---|
[20733] | 287 | $col = $match[0][1]; |
---|
[21441] | 288 | } else { |
---|
[20733] | 289 | $col = $item['col']; |
---|
| 290 | } |
---|
| 291 | // HTML_TAG_CHECKは別途実行なので除去し、別保存しておく |
---|
[21441] | 292 | if (strpos(strtoupper($item['error_check_types']), 'HTML_TAG_CHECK') !== FALSE) { |
---|
[20733] | 293 | $this->arrTagCheckItem[] = $item; |
---|
| 294 | $error_check_types = str_replace('HTML_TAG_CHECK', '', $item['error_check_types']); |
---|
[21441] | 295 | } else { |
---|
[20733] | 296 | $error_check_types = $item['error_check_types']; |
---|
| 297 | } |
---|
| 298 | $arrErrorCheckTypes = explode(',', $error_check_types); |
---|
[21441] | 299 | foreach ($arrErrorCheckTypes as $key => $val) { |
---|
[21514] | 300 | if (trim($val) == '') { |
---|
[20733] | 301 | unset($arrErrorCheckTypes[$key]); |
---|
[21441] | 302 | } else { |
---|
[20733] | 303 | $arrErrorCheckTypes[$key] = trim($val); |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | // パラメーター登録 |
---|
| 307 | $objFormParam->addParam( |
---|
[23506] | 308 | $item['disp_name'], |
---|
| 309 | $col, |
---|
| 310 | constant($item['size_const_type']), |
---|
| 311 | $item['mb_convert_kana_option'], |
---|
| 312 | $arrErrorCheckTypes, |
---|
| 313 | $item['default'], |
---|
| 314 | $item['rw_flg'] != CSV_COLUMN_RW_FLG_READ_ONLY |
---|
[20733] | 315 | ); |
---|
| 316 | } |
---|
[21743] | 317 | } |
---|
[20041] | 318 | |
---|
[17047] | 319 | /** |
---|
[20733] | 320 | * 入力チェックを行う. |
---|
[17047] | 321 | * |
---|
[23439] | 322 | * @param SC_FormParam $objFormParam |
---|
| 323 | * @return array |
---|
[17047] | 324 | */ |
---|
[23439] | 325 | public function lfCheckError(SC_FormParam &$objFormParam) |
---|
[22567] | 326 | { |
---|
[20733] | 327 | // 入力データを渡す。 |
---|
| 328 | $arrRet = $objFormParam->getHashArray(); |
---|
| 329 | $objErr = new SC_CheckError_Ex($arrRet); |
---|
| 330 | $objErr->arrErr = $objFormParam->checkError(false); |
---|
| 331 | // HTMLタグチェックの実行 |
---|
[21441] | 332 | foreach ($this->arrTagCheckItem as $item) { |
---|
[21480] | 333 | $objErr->doFunc(array($item['disp_name'], $item['col'], $this->arrAllowedTag), array('HTML_TAG_CHECK')); |
---|
[20733] | 334 | } |
---|
| 335 | // このフォーム特有の複雑系のエラーチェックを行う |
---|
[21441] | 336 | if (count($objErr->arrErr) == 0) { |
---|
[20733] | 337 | $objErr->arrErr = $this->lfCheckErrorDetail($arrRet, $objErr->arrErr); |
---|
| 338 | } |
---|
[22856] | 339 | |
---|
[20733] | 340 | return $objErr->arrErr; |
---|
[17047] | 341 | } |
---|
| 342 | |
---|
| 343 | /** |
---|
[20733] | 344 | * 保存先テーブル情報の初期化を行う. |
---|
[17047] | 345 | * |
---|
| 346 | * @return void |
---|
| 347 | */ |
---|
[23020] | 348 | public function lfInitTableInfo() |
---|
[22567] | 349 | { |
---|
[20733] | 350 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
| 351 | $this->arrRegistColumn = $objQuery->listTableFields('dtb_category'); |
---|
[17047] | 352 | } |
---|
| 353 | |
---|
| 354 | /** |
---|
[21317] | 355 | * カテゴリ登録を行う. |
---|
[17047] | 356 | * |
---|
[20733] | 357 | * FIXME: 登録の実処理自体は、LC_Page_Admin_Products_Categoryと共通化して欲しい。 |
---|
| 358 | * |
---|
[23649] | 359 | * @param SC_Query $objQuery SC_Queryインスタンス |
---|
| 360 | * @param string|integer $line 処理中の行数 |
---|
[23020] | 361 | * @return integer カテゴリID |
---|
[17047] | 362 | */ |
---|
[23649] | 363 | public function lfRegistCategory($objQuery, $line, &$objFormParam) |
---|
[22567] | 364 | { |
---|
[20733] | 365 | // 登録データ対象取得 |
---|
[23366] | 366 | $arrList = $objFormParam->getDbArray(); |
---|
[23649] | 367 | // 登録時間を生成(DBのCURRENT_TIMESTAMPだとcommitした際、全て同一の時間になってしまう) |
---|
| 368 | $arrList['update_date'] = $this->lfGetDbFormatTimeWithLine($line); |
---|
[20733] | 369 | |
---|
| 370 | // 登録情報を生成する。 |
---|
| 371 | // テーブルのカラムに存在しているもののうち、Form投入設定されていないデータは上書きしない。 |
---|
| 372 | $sqlval = SC_Utils_Ex::sfArrayIntersectKeys($arrList, $this->arrRegistColumn); |
---|
| 373 | |
---|
| 374 | // 必須入力では無い項目だが、空文字では問題のある特殊なカラム値の初期値設定 |
---|
| 375 | $sqlval = $this->lfSetCategoryDefaultData($sqlval); |
---|
| 376 | |
---|
[23649] | 377 | if ($sqlval['category_id'] != '') { |
---|
| 378 | // 同じidが存在すればupdate存在しなければinsert |
---|
| 379 | $where = 'category_id = ?'; |
---|
| 380 | $category_exists = $objQuery->exists('dtb_category', $where, array($sqlval['category_id'])); |
---|
| 381 | if ($category_exists) { |
---|
| 382 | // UPDATEの実行 |
---|
| 383 | $where = 'category_id = ?'; |
---|
| 384 | $objQuery->update('dtb_category', $sqlval, $where, array($sqlval['category_id'])); |
---|
| 385 | } else { |
---|
| 386 | $sqlval['create_date'] = $arrList['update_date']; |
---|
| 387 | // 新規登録 |
---|
| 388 | $category_id = $this->registerCategory($sqlval['parent_category_id'], |
---|
| 389 | $sqlval['category_name'], |
---|
| 390 | $_SESSION['member_id'], |
---|
| 391 | $sqlval['category_id']); |
---|
| 392 | } |
---|
| 393 | $category_id = $sqlval['category_id']; |
---|
| 394 | // TODO: 削除時処理 |
---|
| 395 | } else { |
---|
| 396 | // 新規登録 |
---|
| 397 | $category_id = $this->registerCategory($sqlval['parent_category_id'], |
---|
| 398 | $sqlval['category_name'], |
---|
| 399 | $_SESSION['member_id']); |
---|
| 400 | } |
---|
[22856] | 401 | |
---|
[20733] | 402 | return $category_id; |
---|
[17047] | 403 | } |
---|
| 404 | |
---|
| 405 | /** |
---|
[20733] | 406 | * 初期値の設定 |
---|
| 407 | * |
---|
[23020] | 408 | * @param array $arrCSVFrame CSV構造配列 |
---|
[20733] | 409 | * @return array $arrCSVFrame CSV構造配列 |
---|
| 410 | */ |
---|
[23020] | 411 | public function lfSetParamDefaultValue(&$arrCSVFrame) |
---|
[22567] | 412 | { |
---|
[21441] | 413 | foreach ($arrCSVFrame as $key => $val) { |
---|
| 414 | switch ($val['col']) { |
---|
[20733] | 415 | case 'parent_category_id': |
---|
| 416 | $arrCSVFrame[$key]['default'] = '0'; |
---|
| 417 | break; |
---|
| 418 | case 'del_flg': |
---|
| 419 | $arrCSVFrame[$key]['default'] = '0'; |
---|
| 420 | break; |
---|
| 421 | default: |
---|
| 422 | break; |
---|
| 423 | } |
---|
| 424 | } |
---|
[22856] | 425 | |
---|
[20733] | 426 | return $arrCSVFrame; |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | /** |
---|
| 430 | * データ登録前に特殊な値の持ち方をする部分のデータ部分の初期値補正を行う |
---|
| 431 | * |
---|
| 432 | * @param array $sqlval 商品登録情報配列 |
---|
[23439] | 433 | * @return array $sqlval 登録情報配列 |
---|
[20733] | 434 | */ |
---|
[23020] | 435 | public function lfSetCategoryDefaultData(&$sqlval) |
---|
[22567] | 436 | { |
---|
[21514] | 437 | if ($sqlval['del_flg'] == '') { |
---|
[20733] | 438 | $sqlval['del_flg'] = '0'; //有効 |
---|
| 439 | } |
---|
[21514] | 440 | if ($sqlval['creator_id'] == '') { |
---|
[20733] | 441 | $sqlval['creator_id'] = $_SESSION['member_id']; |
---|
| 442 | } |
---|
[21514] | 443 | if ($sqlval['parent_category_id'] == '') { |
---|
[23020] | 444 | $sqlval['parent_category_id'] = (string) '0'; |
---|
[20733] | 445 | } |
---|
[22856] | 446 | |
---|
[20733] | 447 | return $sqlval; |
---|
| 448 | } |
---|
| 449 | |
---|
| 450 | /** |
---|
| 451 | * このフォーム特有の複雑な入力チェックを行う. |
---|
| 452 | * |
---|
[23439] | 453 | * @param array $item 確認対象データ |
---|
| 454 | * @param array $arrErr エラー配列 |
---|
[20733] | 455 | * @return array エラー配列 |
---|
| 456 | */ |
---|
[23020] | 457 | public function lfCheckErrorDetail($item, $arrErr) |
---|
[22567] | 458 | { |
---|
[23649] | 459 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[20851] | 460 | /* |
---|
[20733] | 461 | // カテゴリIDの存在チェック |
---|
[21441] | 462 | if (!$this->lfIsDbRecord('dtb_category', 'category_id', $item)) { |
---|
[21514] | 463 | $arrErr['category_id'] = '※ 指定のカテゴリIDは、登録されていません。'; |
---|
[20733] | 464 | } |
---|
[20851] | 465 | */ |
---|
[20733] | 466 | // 親カテゴリIDの存在チェック |
---|
[21684] | 467 | if (array_search('parent_category_id', $this->arrFormKeyList) !== FALSE |
---|
| 468 | && $item['parent_category_id'] != '' |
---|
| 469 | && $item['parent_category_id'] != '0' |
---|
[23649] | 470 | && !SC_Helper_DB_Ex::sfIsRecord('dtb_category', 'category_id', array($item['parent_category_id'])) |
---|
[21684] | 471 | ) { |
---|
[21515] | 472 | $arrErr['parent_category_id'] = '※ 指定の親カテゴリID(' . $item['parent_category_id'] . ')は、存在しません。'; |
---|
[20733] | 473 | } |
---|
| 474 | // 削除フラグのチェック |
---|
[21684] | 475 | if (array_search('del_flg', $this->arrFormKeyList) !== FALSE |
---|
| 476 | && $item['del_flg'] != '' |
---|
| 477 | ) { |
---|
[21481] | 478 | if (!($item['del_flg'] == '0' or $item['del_flg'] == '1')) { |
---|
[21514] | 479 | $arrErr['del_flg'] = '※ 削除フラグは「0」(有効)、「1」(削除)のみが有効な値です。'; |
---|
[20733] | 480 | } |
---|
| 481 | } |
---|
| 482 | // 重複チェック 同じカテゴリ内に同名の存在は許可されない |
---|
[21684] | 483 | if (array_search('category_name', $this->arrFormKeyList) !== FALSE |
---|
| 484 | && $item['category_name'] != '' |
---|
| 485 | ) { |
---|
[23649] | 486 | $parent_category_id = $item['parent_category_id']; |
---|
| 487 | if ($parent_category_id == '') { |
---|
| 488 | $parent_category_id = (string) '0'; |
---|
[20733] | 489 | } |
---|
[23649] | 490 | $where = 'parent_category_id = ? AND category_id <> ? AND category_name = ?'; |
---|
| 491 | $exists = $objQuery->exists('dtb_category', |
---|
| 492 | $where, |
---|
| 493 | array($parent_category_id, |
---|
| 494 | $item['category_id'], |
---|
| 495 | $item['category_name'])); |
---|
[21376] | 496 | if ($exists) { |
---|
[21514] | 497 | $arrErr['category_name'] = '※ 既に同名のカテゴリが存在します。'; |
---|
[20733] | 498 | } |
---|
| 499 | } |
---|
| 500 | // 登録数上限チェック |
---|
[23649] | 501 | $where = 'del_flg = 0'; |
---|
| 502 | $count = $objQuery->count('dtb_category', $where); |
---|
[20733] | 503 | if ($count >= CATEGORY_MAX) { |
---|
[21514] | 504 | $item['category_name'] = '※ カテゴリの登録最大数を超えました。'; |
---|
[20733] | 505 | } |
---|
[23496] | 506 | |
---|
[20733] | 507 | if (array_search('parent_category_id', $this->arrFormKeyList) !== FALSE |
---|
[21514] | 508 | and $item['parent_category_id'] != '') { |
---|
[23649] | 509 | $level = $objQuery->get('level', 'dtb_category', 'category_id = ?', array($parent_category_id)); |
---|
| 510 | if ($level >= LEVEL_MAX) { |
---|
[21515] | 511 | $arrErr['parent_category_id'] = '※ ' . LEVEL_MAX . '階層以上の登録はできません。'; |
---|
[20733] | 512 | } |
---|
| 513 | } |
---|
[22856] | 514 | |
---|
[23496] | 515 | |
---|
[20733] | 516 | return $arrErr; |
---|
| 517 | } |
---|
[23649] | 518 | |
---|
| 519 | /** |
---|
| 520 | * カテゴリを登録する |
---|
| 521 | * |
---|
| 522 | * @param integer 親カテゴリID |
---|
| 523 | * @param string カテゴリ名 |
---|
| 524 | * @param integer 作成者のID |
---|
| 525 | * @param integer 指定カテゴリID |
---|
| 526 | * @return integer カテゴリID |
---|
| 527 | */ |
---|
| 528 | public function registerCategory($parent_category_id, $category_name, $creator_id, $category_id = null) |
---|
| 529 | { |
---|
| 530 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
| 531 | |
---|
| 532 | $rank = null; |
---|
| 533 | if ($parent_category_id == 0) { |
---|
| 534 | // ROOT階層で最大のランクを取得する。 |
---|
| 535 | $where = 'parent_category_id = ?'; |
---|
| 536 | $rank = $objQuery->max('rank', 'dtb_category', $where, array($parent_category_id)) + 1; |
---|
| 537 | } else { |
---|
| 538 | // 親のランクを自分のランクとする。 |
---|
| 539 | $where = 'category_id = ?'; |
---|
| 540 | $rank = $objQuery->get('rank', 'dtb_category', $where, array($parent_category_id)); |
---|
| 541 | // 追加レコードのランク以上のレコードを一つあげる。 |
---|
| 542 | $where = 'rank >= ?'; |
---|
| 543 | $arrRawSql = array( |
---|
| 544 | 'rank' => '(rank + 1)', |
---|
| 545 | ); |
---|
| 546 | $objQuery->update('dtb_category', array(), $where, array($rank), $arrRawSql); |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | $where = 'category_id = ?'; |
---|
| 550 | // 自分のレベルを取得する(親のレベル + 1) |
---|
| 551 | $level = $objQuery->get('level', 'dtb_category', $where, array($parent_category_id)) + 1; |
---|
| 552 | |
---|
| 553 | $arrCategory = array(); |
---|
| 554 | $arrCategory['category_name'] = $category_name; |
---|
| 555 | $arrCategory['parent_category_id'] = $parent_category_id; |
---|
| 556 | $arrCategory['create_date'] = 'CURRENT_TIMESTAMP'; |
---|
| 557 | $arrCategory['update_date'] = 'CURRENT_TIMESTAMP'; |
---|
| 558 | $arrCategory['creator_id'] = $creator_id; |
---|
| 559 | $arrCategory['rank'] = $rank; |
---|
| 560 | $arrCategory['level'] = $level; |
---|
| 561 | //カテゴリIDが指定されていればそれを利用する |
---|
| 562 | if (isset($category_id)) { |
---|
| 563 | $arrCategory['category_id'] = $category_id; |
---|
| 564 | // シーケンスの調整 |
---|
| 565 | $seq_count = $objQuery->currVal('dtb_category_category_id'); |
---|
| 566 | if ($seq_count < $arrCategory['category_id']) { |
---|
| 567 | $objQuery->setVal('dtb_category_category_id', $arrCategory['category_id'] + 1); |
---|
| 568 | } |
---|
| 569 | } else { |
---|
| 570 | $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id'); |
---|
| 571 | } |
---|
| 572 | $objQuery->insert('dtb_category', $arrCategory); |
---|
| 573 | |
---|
| 574 | return $arrCategory['category_id']; |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | /** |
---|
| 578 | * 指定された行番号をmicrotimeに付与してDB保存用の時間を生成する。 |
---|
| 579 | * トランザクション内のCURRENT_TIMESTAMPは全てcommit()時の時間に統一されてしまう為。 |
---|
| 580 | * |
---|
| 581 | * @param string $line_no 行番号 |
---|
| 582 | * @return string $time DB保存用の時間文字列 |
---|
| 583 | */ |
---|
| 584 | public function lfGetDbFormatTimeWithLine($line_no = '') |
---|
| 585 | { |
---|
| 586 | $time = date('Y-m-d H:i:s'); |
---|
| 587 | // 秒以下を生成 |
---|
| 588 | if ($line_no != '') { |
---|
| 589 | $microtime = sprintf('%06d', $line_no); |
---|
| 590 | $time .= ".$microtime"; |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | return $time; |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | /** |
---|
| 597 | * 指定されたキーと値の有効性のDB確認 |
---|
| 598 | * |
---|
| 599 | * @param string $table テーブル名 |
---|
| 600 | * @param string $keyname キー名 |
---|
| 601 | * @param array $item 入力データ配列 |
---|
| 602 | * @return boolean true:有効なデータがある false:有効ではない |
---|
| 603 | */ |
---|
| 604 | public function lfIsDbRecord($table, $keyname, $item) |
---|
| 605 | { |
---|
| 606 | if (array_search($keyname, $this->arrFormKeyList) !== FALSE //入力対象である |
---|
| 607 | && $item[$keyname] != '' // 空ではない |
---|
| 608 | && !SC_Helper_DB_Ex::sfIsRecord($table, $keyname, (array) $item[$keyname]) //DBに存在するか |
---|
| 609 | ) { |
---|
| 610 | return false; |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | return true; |
---|
| 614 | } |
---|
[17047] | 615 | } |
---|