Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CsvImportController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CsvImportController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class CsvImportController |
||
|
|
|||
| 45 | { |
||
| 46 | |||
| 47 | private $errors = array(); |
||
| 48 | |||
| 49 | private $fileName; |
||
| 50 | |||
| 51 | private $em; |
||
| 52 | |||
| 53 | private $productTwig = 'Product/csv_product.twig'; |
||
| 54 | |||
| 55 | private $categoryTwig = 'Product/csv_category.twig'; |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * 商品登録CSVアップロード |
||
| 60 | */ |
||
| 61 | 5 | public function csvProduct(Application $app, Request $request) |
|
| 62 | { |
||
| 63 | 3 | $form = $app['form.factory']->createBuilder('admin_csv_import')->getForm(); |
|
| 64 | |||
| 65 | 3 | $headers = $this->getProductCsvHeader(); |
|
| 66 | |||
| 67 | 3 | if ('POST' === $request->getMethod()) { |
|
| 68 | |||
| 69 | 3 | $form->handleRequest($request); |
|
| 70 | |||
| 71 | 3 | if ($form->isValid()) { |
|
| 72 | |||
| 73 | 3 | $formFile = $form['import_file']->getData(); |
|
| 74 | |||
| 75 | 3 | if (!empty($formFile)) { |
|
| 76 | |||
| 77 | 3 | $data = $this->getImportData($app, $formFile); |
|
| 78 | 3 | View Code Duplication | if ($data === false) { |
| 79 | $this->addErrors('CSVのフォーマットが一致しません。'); |
||
| 80 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 81 | } |
||
| 82 | |||
| 83 | 3 | $keys = array_keys($headers); |
|
| 84 | 3 | $columnHeaders = $data->getColumnHeaders(); |
|
| 85 | 3 | if ($keys !== $columnHeaders) { |
|
| 86 | $this->addErrors('CSVのフォーマットが一致しません。'); |
||
| 87 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 88 | } |
||
| 89 | |||
| 90 | 3 | $size = count($data); |
|
| 91 | 3 | if ($size < 1) { |
|
| 92 | $this->addErrors('CSVデータが存在しません。'); |
||
| 93 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 94 | } |
||
| 95 | |||
| 96 | 3 | $headerSize = count($keys); |
|
| 97 | |||
| 98 | 3 | $this->em = $app['orm.em']; |
|
| 99 | 3 | $this->em->getConfiguration()->setSQLLogger(null); |
|
| 100 | |||
| 101 | 3 | $this->em->getConnection()->beginTransaction(); |
|
| 102 | |||
| 103 | 3 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
|
| 104 | |||
| 105 | // CSVファイルの登録処理 |
||
| 106 | 3 | foreach ($data as $row) { |
|
| 107 | |||
| 108 | 3 | if ($headerSize != count($row)) { |
|
| 109 | $this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。'); |
||
| 110 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 111 | } |
||
| 112 | |||
| 113 | 3 | if ($row['商品ID'] == '') { |
|
| 114 | 2 | $Product = new Product(); |
|
| 115 | 2 | $this->em->persist($Product); |
|
| 116 | 2 | } else { |
|
| 117 | 2 | if (preg_match('/^\d+$/', $row['商品ID'])) { |
|
| 118 | 2 | $Product = $app['eccube.repository.product']->find($row['商品ID']); |
|
| 119 | 2 | if (!$Product) { |
|
| 120 | $this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。'); |
||
| 121 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 122 | } |
||
| 123 | 2 | View Code Duplication | } else { |
| 124 | $this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。'); |
||
| 125 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 126 | } |
||
| 127 | |||
| 128 | } |
||
| 129 | |||
| 130 | 3 | if ($row['公開ステータス(ID)'] == '') { |
|
| 131 | $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が設定されていません。'); |
||
| 132 | } else { |
||
| 133 | 3 | if (preg_match('/^\d+$/', $row['公開ステータス(ID)'])) { |
|
| 134 | 3 | $Disp = $app['eccube.repository.master.disp']->find($row['公開ステータス(ID)']); |
|
| 135 | 3 | if (!$Disp) { |
|
| 136 | $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。'); |
||
| 137 | } else { |
||
| 138 | 3 | $Product->setStatus($Disp); |
|
| 139 | } |
||
| 140 | 3 | } else { |
|
| 141 | $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。'); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | 3 | View Code Duplication | if (Str::isBlank($row['商品名'])) { |
| 146 | $this->addErrors(($data->key() + 1) . '行目の商品名が設定されていません。'); |
||
| 147 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 148 | } else { |
||
| 149 | 3 | $Product->setName(Str::trimAll($row['商品名'])); |
|
| 150 | } |
||
| 151 | |||
| 152 | 3 | View Code Duplication | if (Str::isNotBlank($row['ショップ用メモ欄'])) { |
| 153 | 2 | $Product->setNote(Str::trimAll($row['ショップ用メモ欄'])); |
|
| 154 | 2 | } else { |
|
| 155 | 1 | $Product->setNote(null); |
|
| 156 | } |
||
| 157 | |||
| 158 | 3 | View Code Duplication | if (Str::isNotBlank($row['商品説明(一覧)'])) { |
| 159 | 3 | $Product->setDescriptionList(Str::trimAll($row['商品説明(一覧)'])); |
|
| 160 | 3 | } else { |
|
| 161 | 1 | $Product->setDescriptionList(null); |
|
| 162 | } |
||
| 163 | |||
| 164 | 3 | View Code Duplication | if (Str::isNotBlank($row['商品説明(詳細)'])) { |
| 165 | 3 | $Product->setDescriptionDetail(Str::trimAll($row['商品説明(詳細)'])); |
|
| 166 | 3 | } else { |
|
| 167 | $Product->setDescriptionDetail(null); |
||
| 168 | } |
||
| 169 | |||
| 170 | 3 | View Code Duplication | if (Str::isNotBlank($row['検索ワード'])) { |
| 171 | 2 | $Product->setSearchWord(Str::trimAll($row['検索ワード'])); |
|
| 172 | 2 | } else { |
|
| 173 | 1 | $Product->setSearchWord(null); |
|
| 174 | } |
||
| 175 | |||
| 176 | 3 | View Code Duplication | if (Str::isNotBlank($row['フリーエリア'])) { |
| 177 | 2 | $Product->setFreeArea(Str::trimAll($row['フリーエリア'])); |
|
| 178 | 2 | } else { |
|
| 179 | 1 | $Product->setFreeArea(null); |
|
| 180 | } |
||
| 181 | |||
| 182 | 3 | if ($row['商品削除フラグ'] == '') { |
|
| 183 | $Product->setDelFlg(Constant::DISABLED); |
||
| 184 | } else { |
||
| 185 | 3 | if ($row['商品削除フラグ'] == (string)Constant::DISABLED || $row['商品削除フラグ'] == (string)Constant::ENABLED) { |
|
| 186 | 3 | $Product->setDelFlg($row['商品削除フラグ']); |
|
| 187 | 3 | View Code Duplication | } else { |
| 188 | $this->addErrors(($data->key() + 1) . '行目の商品削除フラグが設定されていません。'); |
||
| 189 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | // 商品画像登録 |
||
| 194 | 3 | $this->createProductImage($row, $Product); |
|
| 195 | |||
| 196 | 3 | $this->em->flush($Product); |
|
| 197 | |||
| 198 | // 商品カテゴリ登録 |
||
| 199 | 3 | $this->createProductCategory($row, $Product, $app, $data); |
|
| 200 | |||
| 201 | //タグ登録 |
||
| 202 | 3 | $this->createProductTag($row, $Product, $app, $data); |
|
| 203 | |||
| 204 | // 商品規格が存在しなければ新規登録 |
||
| 205 | 3 | $ProductClasses = $Product->getProductClasses(); |
|
| 206 | 3 | if ($ProductClasses->count() < 1) { |
|
| 207 | // 規格分類1(ID)がセットされていると規格なし商品、規格あり商品を作成 |
||
| 208 | 2 | $ProductClassOrg = $this->createProductClass($row, $Product, $app, $data); |
|
| 209 | 2 | if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) { |
|
| 210 | if ($row['送料'] != '') { |
||
| 211 | $deliveryFee = str_replace(',', '', $row['送料']); |
||
| 212 | if (preg_match('/^\d+$/', $deliveryFee) && $deliveryFee >= 0) { |
||
| 213 | $ProductClassOrg->setDeliveryFee($deliveryFee); |
||
| 214 | } else { |
||
| 215 | $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。'); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | 2 | if ($row['規格分類1(ID)'] != '') { |
|
| 221 | |||
| 222 | 1 | if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) { |
|
| 223 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。'); |
||
| 224 | } else { |
||
| 225 | // 商品規格あり |
||
| 226 | // 企画分類あり商品を作成 |
||
| 227 | 1 | $ProductClass = clone $ProductClassOrg; |
|
| 228 | 1 | $ProductStock = clone $ProductClassOrg->getProductStock(); |
|
| 229 | |||
| 230 | // 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット |
||
| 231 | 1 | $ProductClassOrg->setDelFlg(Constant::ENABLED); |
|
| 232 | |||
| 233 | // 規格分類1、2をそれぞれセットし作成 |
||
| 234 | 1 | $ClassCategory1 = null; |
|
| 235 | 1 | if (preg_match('/^\d+$/', $row['規格分類1(ID)'])) { |
|
| 236 | 1 | $ClassCategory1 = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']); |
|
| 237 | 1 | if (!$ClassCategory1) { |
|
| 238 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
| 239 | 5 | } else { |
|
| 240 | 1 | $ProductClass->setClassCategory1($ClassCategory1); |
|
| 241 | 5 | } |
|
| 242 | 1 | } else { |
|
| 243 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
| 244 | } |
||
| 245 | |||
| 246 | 1 | if ($row['規格分類2(ID)'] != '') { |
|
| 247 | 1 | if (preg_match('/^\d+$/', $row['規格分類2(ID)'])) { |
|
| 248 | 1 | $ClassCategory2 = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']); |
|
| 249 | 1 | View Code Duplication | if (!$ClassCategory2) { |
| 250 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
| 251 | } else { |
||
| 252 | 1 | if ($ClassCategory1 && |
|
| 253 | 1 | ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId()) |
|
| 254 | 1 | ) { |
|
| 255 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)の規格名が同じです。'); |
||
| 256 | } else { |
||
| 257 | 1 | $ProductClass->setClassCategory2($ClassCategory2); |
|
| 258 | } |
||
| 259 | } |
||
| 260 | 1 | } else { |
|
| 261 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
| 262 | } |
||
| 263 | 1 | } |
|
| 264 | 1 | $ProductClass->setProductStock($ProductStock); |
|
| 265 | 1 | $ProductStock->setProductClass($ProductClass); |
|
| 266 | |||
| 267 | 1 | $this->em->persist($ProductClass); |
|
| 268 | 1 | $this->em->persist($ProductStock); |
|
| 269 | } |
||
| 270 | |||
| 271 | 1 | } else { |
|
| 272 | 1 | if ($row['規格分類2(ID)'] != '') { |
|
| 273 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | 2 | } else { |
|
| 278 | // 商品規格の更新 |
||
| 279 | |||
| 280 | 2 | $flag = false; |
|
| 281 | 2 | $classCategoryId1 = $row['規格分類1(ID)'] == '' ? null : $row['規格分類1(ID)']; |
|
| 282 | 2 | $classCategoryId2 = $row['規格分類2(ID)'] == '' ? null : $row['規格分類2(ID)']; |
|
| 283 | |||
| 284 | 2 | foreach ($ProductClasses as $pc) { |
|
| 285 | |||
| 286 | 2 | $classCategory1 = is_null($pc->getClassCategory1()) ? null : $pc->getClassCategory1()->getId(); |
|
| 287 | 2 | $classCategory2 = is_null($pc->getClassCategory2()) ? null : $pc->getClassCategory2()->getId(); |
|
| 288 | |||
| 289 | // 登録されている商品規格を更新 |
||
| 290 | 2 | if ($classCategory1 == $classCategoryId1 && |
|
| 291 | $classCategory2 == $classCategoryId2 |
||
| 292 | 2 | ) { |
|
| 293 | 1 | $this->updateProductClass($row, $Product, $pc, $app, $data); |
|
| 294 | |||
| 295 | 1 | if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) { |
|
| 296 | if ($row['送料'] != '') { |
||
| 297 | $deliveryFee = str_replace(',', '', $row['送料']); |
||
| 298 | if (preg_match('/^\d+$/', $deliveryFee) && $deliveryFee >= 0) { |
||
| 299 | $pc->setDeliveryFee($deliveryFee); |
||
| 300 | } else { |
||
| 301 | $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。'); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | 1 | $flag = true; |
|
| 307 | 1 | break; |
|
| 308 | } |
||
| 309 | 2 | } |
|
| 310 | |||
| 311 | // 商品規格を登録 |
||
| 312 | 2 | if (!$flag) { |
|
| 313 | 1 | $pc = $ProductClasses[0]; |
|
| 314 | 1 | if ($pc->getClassCategory1() == null && |
|
| 315 | 1 | $pc->getClassCategory2() == null |
|
| 316 | 1 | ) { |
|
| 317 | |||
| 318 | // 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット |
||
| 319 | 1 | $pc->setDelFlg(Constant::ENABLED); |
|
| 320 | 1 | } |
|
| 321 | |||
| 322 | 1 | if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) { |
|
| 323 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。'); |
||
| 324 | } else { |
||
| 325 | |||
| 326 | // 必ず規格分類1がセットされている |
||
| 327 | // 規格分類1、2をそれぞれセットし作成 |
||
| 328 | 1 | $ClassCategory1 = null; |
|
| 329 | 1 | if (preg_match('/^\d+$/', $classCategoryId1)) { |
|
| 330 | 1 | $ClassCategory1 = $app['eccube.repository.class_category']->find($classCategoryId1); |
|
| 331 | 1 | if (!$ClassCategory1) { |
|
| 332 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
| 333 | } |
||
| 334 | 1 | } else { |
|
| 335 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
| 336 | } |
||
| 337 | |||
| 338 | 1 | $ClassCategory2 = null; |
|
| 339 | 1 | if ($row['規格分類2(ID)'] != '') { |
|
| 340 | 1 | if ($pc->getClassCategory1() != null && $pc->getClassCategory2() == null) { |
|
| 341 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)は設定できません。'); |
||
| 342 | } else { |
||
| 343 | 1 | if (preg_match('/^\d+$/', $classCategoryId2)) { |
|
| 344 | 1 | $ClassCategory2 = $app['eccube.repository.class_category']->find($classCategoryId2); |
|
| 345 | 1 | View Code Duplication | if (!$ClassCategory2) { |
| 346 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
| 347 | } else { |
||
| 348 | 1 | if ($ClassCategory1 && |
|
| 349 | 1 | ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId()) |
|
| 350 | 1 | ) { |
|
| 351 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)の規格名が同じです。'); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | 1 | } else { |
|
| 355 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
| 356 | } |
||
| 357 | |||
| 358 | } |
||
| 359 | 1 | } else { |
|
| 360 | if ($pc->getClassCategory1() != null && $pc->getClassCategory2() != null) { |
||
| 361 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)に値を設定してください。'); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | 1 | $ProductClass = $this->createProductClass($row, $Product, $app, $data, $ClassCategory1, $ClassCategory2); |
|
| 365 | |||
| 366 | 1 | if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) { |
|
| 367 | if ($row['送料'] != '') { |
||
| 368 | $deliveryFee = str_replace(',', '', $row['送料']); |
||
| 369 | if (preg_match('/^\d+$/', $deliveryFee) && $deliveryFee >= 0) { |
||
| 370 | $ProductClass->setDeliveryFee($deliveryFee); |
||
| 371 | } else { |
||
| 372 | $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。'); |
||
| 373 | } |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | 1 | $Product->addProductClass($ProductClass); |
|
| 378 | } |
||
| 379 | |||
| 380 | 1 | } |
|
| 381 | |||
| 382 | } |
||
| 383 | |||
| 384 | |||
| 385 | 3 | if ($this->hasErrors()) { |
|
| 386 | return $this->render($app, $form, $headers, $this->productTwig); |
||
| 387 | } |
||
| 388 | |||
| 389 | 3 | $this->em->persist($Product); |
|
| 390 | |||
| 391 | 3 | } |
|
| 392 | |||
| 393 | 3 | $this->em->flush(); |
|
| 394 | 3 | $this->em->getConnection()->commit(); |
|
| 395 | |||
| 396 | 3 | $app->addSuccess('admin.product.csv_import.save.complete', 'admin'); |
|
| 397 | 3 | } |
|
| 398 | |||
| 399 | 3 | } |
|
| 400 | 3 | } |
|
| 401 | |||
| 402 | 3 | return $this->render($app, $form, $headers, $this->productTwig); |
|
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * カテゴリ登録CSVアップロード |
||
| 407 | */ |
||
| 408 | 2 | public function csvCategory(Application $app, Request $request) |
|
| 409 | { |
||
| 410 | |||
| 411 | 2 | $form = $app['form.factory']->createBuilder('admin_csv_import')->getForm(); |
|
| 412 | |||
| 413 | 2 | $headers = $this->getCategoryCsvHeader(); |
|
| 414 | |||
| 415 | 2 | if ('POST' === $request->getMethod()) { |
|
| 416 | |||
| 417 | 2 | $form->handleRequest($request); |
|
| 418 | |||
| 419 | 2 | if ($form->isValid()) { |
|
| 420 | |||
| 421 | 2 | $formFile = $form['import_file']->getData(); |
|
| 422 | |||
| 423 | 2 | if (!empty($formFile)) { |
|
| 424 | |||
| 425 | 2 | $data = $this->getImportData($app, $formFile); |
|
| 426 | 2 | View Code Duplication | if ($data === false) { |
| 427 | $this->addErrors('CSVのフォーマットが一致しません。'); |
||
| 428 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
| 429 | } |
||
| 430 | |||
| 431 | 2 | $keys = array_keys($headers); |
|
| 432 | 2 | $columnHeaders = $data->getColumnHeaders(); |
|
| 433 | 2 | if ($keys !== $columnHeaders) { |
|
| 434 | $this->addErrors('CSVのフォーマットが一致しません。'); |
||
| 435 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
| 436 | } |
||
| 437 | |||
| 438 | 2 | $size = count($data); |
|
| 439 | 2 | if ($size < 1) { |
|
| 440 | $this->addErrors('CSVデータが存在しません。'); |
||
| 441 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
| 442 | } |
||
| 443 | |||
| 444 | 2 | $headerSize = count($keys); |
|
| 445 | |||
| 446 | 2 | $this->em = $app['orm.em']; |
|
| 447 | 2 | $this->em->getConfiguration()->setSQLLogger(null); |
|
| 448 | |||
| 449 | 2 | $this->em->getConnection()->beginTransaction(); |
|
| 450 | |||
| 451 | // CSVファイルの登録処理 |
||
| 452 | 2 | foreach ($data as $row) { |
|
| 453 | |||
| 454 | 2 | if ($headerSize != count($row)) { |
|
| 455 | $this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。'); |
||
| 456 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
| 457 | } |
||
| 458 | |||
| 459 | 2 | if ($row['カテゴリID'] == '') { |
|
| 460 | 1 | $Category = new Category(); |
|
| 461 | 1 | } else { |
|
| 462 | 1 | View Code Duplication | if (!preg_match('/^\d+$/', $row['カテゴリID'])) { |
| 463 | $this->addErrors(($data->key() + 1) . '行目のカテゴリIDが存在しません。'); |
||
| 464 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
| 465 | } |
||
| 466 | 1 | $Category = $app['eccube.repository.category']->find($row['カテゴリID']); |
|
| 467 | 1 | View Code Duplication | if (!$Category) { |
| 468 | $this->addErrors(($data->key() + 1) . '行目のカテゴリIDが存在しません。'); |
||
| 469 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
| 470 | } |
||
| 471 | 1 | View Code Duplication | if ($row['カテゴリID'] == $row['親カテゴリID']) { |
| 472 | $this->addErrors(($data->key() + 1) . '行目のカテゴリIDと親カテゴリIDが同じです。'); |
||
| 473 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
| 474 | } |
||
| 475 | |||
| 476 | } |
||
| 477 | |||
| 478 | 2 | View Code Duplication | if (Str::isBlank($row['カテゴリ名'])) { |
| 479 | $this->addErrors(($data->key() + 1) . '行目のカテゴリ名が設定されていません。'); |
||
| 480 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
| 481 | } else { |
||
| 482 | 2 | $Category->setName(Str::trimAll($row['カテゴリ名'])); |
|
| 483 | } |
||
| 484 | |||
| 485 | 2 | if ($row['親カテゴリID'] != '') { |
|
| 486 | |||
| 487 | 1 | View Code Duplication | if (!preg_match('/^\d+$/', $row['親カテゴリID'])) { |
| 488 | $this->addErrors(($data->key() + 1) . '行目の親カテゴリIDが存在しません。'); |
||
| 489 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
| 490 | } |
||
| 491 | |||
| 492 | 1 | $ParentCategory = $app['eccube.repository.category']->find($row['親カテゴリID']); |
|
| 493 | 1 | View Code Duplication | if (!$ParentCategory) { |
| 494 | $this->addErrors(($data->key() + 1) . '行目の親カテゴリIDが存在しません。'); |
||
| 495 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
| 496 | } |
||
| 497 | |||
| 498 | 1 | } else { |
|
| 499 | 2 | $ParentCategory = null; |
|
| 500 | } |
||
| 501 | |||
| 502 | 2 | $Category->setParent($ParentCategory); |
|
| 503 | 2 | if ($ParentCategory) { |
|
| 504 | 1 | $Category->setLevel($ParentCategory->getLevel() + 1); |
|
| 505 | 1 | } else { |
|
| 506 | 2 | $Category->setLevel(1); |
|
| 507 | } |
||
| 508 | |||
| 509 | 2 | View Code Duplication | if ($app['config']['category_nest_level'] < $Category->getLevel()) { |
| 510 | $this->addErrors(($data->key() + 1) . '行目のカテゴリが最大レベルを超えているため設定できません。'); |
||
| 511 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
| 512 | } |
||
| 513 | |||
| 514 | 2 | $status = $app['eccube.repository.category']->save($Category); |
|
| 515 | |||
| 516 | 2 | if (!$status) { |
|
| 517 | $this->addErrors(($data->key() + 1) . '行目のカテゴリが設定できません。'); |
||
| 518 | } |
||
| 519 | |||
| 520 | 2 | if ($this->hasErrors()) { |
|
| 521 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
| 522 | } |
||
| 523 | |||
| 524 | 2 | $this->em->persist($Category); |
|
| 525 | |||
| 526 | 2 | } |
|
| 527 | |||
| 528 | 2 | $this->em->flush(); |
|
| 529 | 2 | $this->em->getConnection()->commit(); |
|
| 530 | |||
| 531 | 2 | $app->addSuccess('admin.category.csv_import.save.complete', 'admin'); |
|
| 532 | 2 | } |
|
| 533 | |||
| 534 | 2 | } |
|
| 535 | 2 | } |
|
| 536 | |||
| 537 | 2 | return $this->render($app, $form, $headers, $this->categoryTwig); |
|
| 538 | } |
||
| 539 | |||
| 540 | |||
| 541 | /** |
||
| 542 | * アップロード用CSV雛形ファイルダウンロード |
||
| 543 | */ |
||
| 544 | 2 | public function csvTemplate(Application $app, Request $request, $type) |
|
| 580 | |||
| 581 | |||
| 582 | /** |
||
| 583 | * 登録、更新時のエラー画面表示 |
||
| 584 | * |
||
| 585 | */ |
||
| 586 | 5 | protected function render($app, $form, $headers, $twig) |
|
| 610 | |||
| 611 | |||
| 612 | /** |
||
| 613 | * アップロードされたCSVファイルの行ごとの処理 |
||
| 614 | * |
||
| 615 | * @param $formFile |
||
| 616 | * @return CsvImportService |
||
| 617 | */ |
||
| 618 | 5 | protected function getImportData($app, $formFile) |
|
| 657 | |||
| 658 | |||
| 659 | /** |
||
| 660 | * 商品画像の削除、登録 |
||
| 661 | */ |
||
| 662 | 3 | protected function createProductImage($row, Product $Product) |
|
| 689 | |||
| 690 | |||
| 691 | /** |
||
| 692 | * 商品カテゴリの削除、登録 |
||
| 693 | */ |
||
| 694 | 3 | protected function createProductCategory($row, Product $Product, $app, $data) |
|
| 735 | |||
| 736 | |||
| 737 | /** |
||
| 738 | * タグの登録 |
||
| 739 | * |
||
| 740 | * @param array $row |
||
| 741 | * @param Product $Product |
||
| 742 | * @param Application $app |
||
| 743 | * @param CsvImportService $data |
||
| 744 | */ |
||
| 745 | 3 | protected function createProductTag($row, Product $Product, $app, $data) |
|
| 780 | |||
| 781 | |||
| 782 | /** |
||
| 783 | * 商品規格分類1、商品規格分類2がnullとなる商品規格情報を作成 |
||
| 784 | */ |
||
| 785 | 3 | protected function createProductClass($row, Product $Product, $app, $data, $ClassCategory1 = null, $ClassCategory2 = null) |
|
| 921 | |||
| 922 | |||
| 923 | /** |
||
| 924 | * 商品規格情報を更新 |
||
| 925 | */ |
||
| 926 | 5 | protected function updateProductClass($row, Product $Product, ProductClass $ProductClass, $app, $data) |
|
| 1067 | |||
| 1068 | /** |
||
| 1069 | * 登録、更新時のエラー画面表示 |
||
| 1070 | * |
||
| 1071 | */ |
||
| 1072 | protected function addErrors($message) |
||
| 1077 | |||
| 1078 | /** |
||
| 1079 | * @return array |
||
| 1080 | */ |
||
| 1081 | 5 | protected function getErrors() |
|
| 1085 | |||
| 1086 | /** |
||
| 1087 | * |
||
| 1088 | * @return boolean |
||
| 1089 | */ |
||
| 1090 | 5 | protected function hasErrors() |
|
| 1094 | |||
| 1095 | /** |
||
| 1096 | * 商品登録CSVヘッダー定義 |
||
| 1097 | */ |
||
| 1098 | 4 | private function getProductCsvHeader() |
|
| 1127 | |||
| 1128 | |||
| 1129 | /** |
||
| 1130 | * カテゴリCSVヘッダー定義 |
||
| 1131 | */ |
||
| 1132 | 3 | private function getCategoryCsvHeader() |
|
| 1140 | } |
||
| 1141 |