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 ProductController 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 ProductController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class ProductController extends AbstractController |
||
|
|
|||
| 43 | { |
||
| 44 | 6 | public function index(Application $app, Request $request, $page_no = null) |
|
| 45 | { |
||
| 46 | |||
| 47 | 2 | $session = $app['session']; |
|
| 48 | |||
| 49 | 2 | $builder = $app['form.factory'] |
|
| 50 | 2 | ->createBuilder('admin_search_product'); |
|
| 51 | |||
| 52 | 2 | $event = new EventArgs( |
|
| 53 | array( |
||
| 54 | 2 | 'builder' => $builder, |
|
| 55 | 2 | ), |
|
| 56 | $request |
||
| 57 | 2 | ); |
|
| 58 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_INDEX_INITIALIZE, $event); |
|
| 59 | |||
| 60 | 2 | $searchForm = $builder->getForm(); |
|
| 61 | |||
| 62 | 6 | $pagination = array(); |
|
| 63 | |||
| 64 | 2 | $disps = $app['eccube.repository.master.disp']->findAll(); |
|
| 65 | 6 | $pageMaxis = $app['eccube.repository.master.page_max']->findAll(); |
|
| 66 | 2 | $page_count = $app['config']['default_page_count']; |
|
| 67 | 2 | $page_status = null; |
|
| 68 | 2 | $active = false; |
|
| 69 | |||
| 70 | 2 | if ('POST' === $request->getMethod()) { |
|
| 71 | |||
| 72 | $searchForm->handleRequest($request); |
||
| 73 | |||
| 74 | if ($searchForm->isValid()) { |
||
| 75 | $searchData = $searchForm->getData(); |
||
| 76 | |||
| 77 | // paginator |
||
| 78 | $qb = $app['eccube.repository.product']->getQueryBuilderBySearchDataForAdmin($searchData); |
||
| 79 | $page_no = 1; |
||
| 80 | |||
| 81 | $event = new EventArgs( |
||
| 82 | array( |
||
| 83 | 'qb' => $qb, |
||
| 84 | 'searchData' => $searchData, |
||
| 85 | ), |
||
| 86 | $request |
||
| 87 | ); |
||
| 88 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_INDEX_SEARCH, $event); |
||
| 89 | $searchData = $event->getArgument('searchData'); |
||
| 90 | |||
| 91 | $pagination = $app['paginator']()->paginate( |
||
| 92 | $qb, |
||
| 93 | $page_no, |
||
| 94 | $page_count, |
||
| 95 | array('wrap-queries' => true) |
||
| 96 | ); |
||
| 97 | |||
| 98 | // sessionのデータ保持 |
||
| 99 | $session->set('eccube.admin.product.search', $searchData); |
||
| 100 | $session->set('eccube.admin.product.search.page_no', $page_no); |
||
| 101 | } |
||
| 102 | } else { |
||
| 103 | 2 | if (is_null($page_no) && $request->get('resume') != Constant::ENABLED) { |
|
| 104 | // sessionを削除 |
||
| 105 | 2 | $session->remove('eccube.admin.product.search'); |
|
| 106 | 2 | $session->remove('eccube.admin.product.search.page_no'); |
|
| 107 | 2 | } else { |
|
| 108 | // pagingなどの処理 |
||
| 109 | $searchData = $session->get('eccube.admin.product.search'); |
||
| 110 | if (is_null($page_no)) { |
||
| 111 | $page_no = intval($session->get('eccube.admin.product.search.page_no')); |
||
| 112 | } else { |
||
| 113 | $session->set('eccube.admin.product.search.page_no', $page_no); |
||
| 114 | } |
||
| 115 | if (!is_null($searchData)) { |
||
| 116 | |||
| 117 | // 公開ステータス |
||
| 118 | $status = $request->get('status'); |
||
| 119 | if (!empty($status)) { |
||
| 120 | if ($status != $app['config']['admin_product_stock_status']) { |
||
| 121 | $searchData['link_status'] = $app['eccube.repository.master.disp']->find($status); |
||
| 122 | $searchData['status'] = null; |
||
| 123 | $session->set('eccube.admin.product.search', $searchData); |
||
| 124 | } else { |
||
| 125 | $searchData['stock_status'] = Constant::DISABLED; |
||
| 126 | } |
||
| 127 | $page_status = $status; |
||
| 128 | } else { |
||
| 129 | $searchData['link_status'] = null; |
||
| 130 | $searchData['stock_status'] = null; |
||
| 131 | } |
||
| 132 | // 表示件数 |
||
| 133 | $pcount = $request->get('page_count'); |
||
| 134 | |||
| 135 | $page_count = empty($pcount) ? $page_count : $pcount; |
||
| 136 | |||
| 137 | $qb = $app['eccube.repository.product']->getQueryBuilderBySearchDataForAdmin($searchData); |
||
| 138 | |||
| 139 | $event = new EventArgs( |
||
| 140 | array( |
||
| 141 | 'qb' => $qb, |
||
| 142 | 'searchData' => $searchData, |
||
| 143 | ), |
||
| 144 | $request |
||
| 145 | ); |
||
| 146 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_INDEX_SEARCH, $event); |
||
| 147 | $searchData = $event->getArgument('searchData'); |
||
| 148 | |||
| 149 | $pagination = $app['paginator']()->paginate( |
||
| 150 | $qb, |
||
| 151 | $page_no, |
||
| 152 | $page_count, |
||
| 153 | array('wrap-queries' => true) |
||
| 154 | ); |
||
| 155 | |||
| 156 | // セッションから検索条件を復元 |
||
| 157 | if (!empty($searchData['category_id'])) { |
||
| 158 | $searchData['category_id'] = $app['eccube.repository.category']->find($searchData['category_id']); |
||
| 159 | } |
||
| 160 | if (empty($status)) { |
||
| 161 | View Code Duplication | if (count($searchData['status']) > 0) { |
|
| 162 | $status_ids = array(); |
||
| 163 | foreach ($searchData['status'] as $Status) { |
||
| 164 | $status_ids[] = $Status->getId(); |
||
| 165 | } |
||
| 166 | $searchData['status'] = $app['eccube.repository.master.disp']->findBy(array('id' => $status_ids)); |
||
| 167 | } |
||
| 168 | $searchData['link_status'] = null; |
||
| 169 | $searchData['stock_status'] = null; |
||
| 170 | } |
||
| 171 | $searchForm->setData($searchData); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | 2 | return $app->render('Product/index.twig', array( |
|
| 177 | 2 | 'searchForm' => $searchForm->createView(), |
|
| 178 | 2 | 'pagination' => $pagination, |
|
| 179 | 2 | 'disps' => $disps, |
|
| 180 | 2 | 'pageMaxis' => $pageMaxis, |
|
| 181 | 2 | 'page_no' => $page_no, |
|
| 182 | 2 | 'page_status' => $page_status, |
|
| 183 | 2 | 'page_count' => $page_count, |
|
| 184 | 2 | 'active' => $active, |
|
| 185 | 2 | )); |
|
| 186 | } |
||
| 187 | |||
| 188 | 6 | public function addImage(Application $app, Request $request) |
|
| 226 | |||
| 227 | 23 | public function edit(Application $app, Request $request, $id = null) |
|
| 228 | { |
||
| 229 | 18 | $has_class = false; |
|
| 230 | 18 | if (is_null($id)) { |
|
| 231 | 5 | $Product = new \Eccube\Entity\Product(); |
|
| 232 | 5 | $ProductClass = new \Eccube\Entity\ProductClass(); |
|
| 233 | 5 | $Disp = $app['eccube.repository.master.disp']->find(\Eccube\Entity\Master\Disp::DISPLAY_HIDE); |
|
| 234 | $Product |
||
| 235 | 5 | ->setDelFlg(Constant::DISABLED) |
|
| 236 | 5 | ->addProductClass($ProductClass) |
|
| 237 | 5 | ->setStatus($Disp); |
|
| 238 | $ProductClass |
||
| 239 | 9 | ->setDelFlg(Constant::DISABLED) |
|
| 240 | 5 | ->setStockUnlimited(true) |
|
| 241 | 9 | ->setProduct($Product); |
|
| 242 | 5 | $ProductStock = new \Eccube\Entity\ProductStock(); |
|
| 243 | 5 | $ProductClass->setProductStock($ProductStock); |
|
| 244 | 5 | $ProductStock->setProductClass($ProductClass); |
|
| 245 | 5 | } else { |
|
| 246 | 13 | $Product = $app['eccube.repository.product']->find($id); |
|
| 247 | 13 | if (!$Product) { |
|
| 248 | throw new NotFoundHttpException(); |
||
| 249 | } |
||
| 250 | // 規格あり商品か |
||
| 251 | 13 | $has_class = $Product->hasProductClass(); |
|
| 252 | 13 | if (!$has_class) { |
|
| 253 | 11 | $ProductClasses = $Product->getProductClasses(); |
|
| 254 | 11 | $ProductClass = $ProductClasses[0]; |
|
| 255 | 11 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
|
| 256 | 11 | View Code Duplication | if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED && $ProductClass->getTaxRule() && !$ProductClass->getTaxRule()->getDelFlg()) { |
| 257 | 6 | $ProductClass->setTaxRate($ProductClass->getTaxRule()->getTaxRate()); |
|
| 258 | 6 | } |
|
| 259 | 11 | $ProductStock = $ProductClasses[0]->getProductStock(); |
|
| 260 | 11 | } |
|
| 261 | } |
||
| 262 | |||
| 263 | 18 | $builder = $app['form.factory'] |
|
| 264 | 18 | ->createBuilder('admin_product', $Product); |
|
| 265 | |||
| 266 | // 規格あり商品の場合、規格関連情報をFormから除外 |
||
| 267 | 18 | if ($has_class) { |
|
| 268 | 2 | $builder->remove('class'); |
|
| 269 | 2 | } |
|
| 270 | |||
| 271 | 18 | $event = new EventArgs( |
|
| 272 | array( |
||
| 273 | 18 | 'builder' => $builder, |
|
| 274 | 18 | 'Product' => $Product, |
|
| 275 | 18 | ), |
|
| 276 | $request |
||
| 277 | 18 | ); |
|
| 278 | 18 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_INITIALIZE, $event); |
|
| 279 | |||
| 280 | 18 | $form = $builder->getForm(); |
|
| 281 | |||
| 282 | 18 | if (!$has_class) { |
|
| 283 | 16 | $ProductClass->setStockUnlimited((boolean)$ProductClass->getStockUnlimited()); |
|
| 284 | 16 | $form['class']->setData($ProductClass); |
|
| 285 | 16 | } |
|
| 286 | |||
| 287 | // ファイルの登録 |
||
| 288 | 18 | $images = array(); |
|
| 289 | 18 | $ProductImages = $Product->getProductImage(); |
|
| 290 | 18 | foreach ($ProductImages as $ProductImage) { |
|
| 291 | 13 | $images[] = $ProductImage->getFileName(); |
|
| 292 | 18 | } |
|
| 293 | 18 | $form['images']->setData($images); |
|
| 294 | |||
| 295 | 18 | $categories = array(); |
|
| 296 | 18 | $ProductCategories = $Product->getProductCategories(); |
|
| 297 | 18 | foreach ($ProductCategories as $ProductCategory) { |
|
| 298 | /* @var $ProductCategory \Eccube\Entity\ProductCategory */ |
||
| 299 | 13 | $categories[] = $ProductCategory->getCategory(); |
|
| 300 | 23 | } |
|
| 301 | 18 | $form['Category']->setData($categories); |
|
| 302 | |||
| 303 | 18 | $Tags = array(); |
|
| 304 | 18 | $ProductTags = $Product->getProductTag(); |
|
| 305 | 18 | foreach ($ProductTags as $ProductTag) { |
|
| 306 | $Tags[] = $ProductTag->getTag(); |
||
| 307 | 18 | } |
|
| 308 | 18 | $form['Tag']->setData($Tags); |
|
| 309 | |||
| 310 | 18 | if ('POST' === $request->getMethod()) { |
|
| 311 | 14 | $form->handleRequest($request); |
|
| 312 | 14 | if ($form->isValid()) { |
|
| 313 | 14 | $Product = $form->getData(); |
|
| 314 | |||
| 315 | 14 | if (!$has_class) { |
|
| 316 | 14 | $ProductClass = $form['class']->getData(); |
|
| 317 | |||
| 318 | // 個別消費税 |
||
| 319 | 14 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
|
| 320 | 14 | if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) { |
|
| 321 | 12 | if ($ProductClass->getTaxRate() !== null) { |
|
| 322 | 8 | View Code Duplication | if ($ProductClass->getTaxRule()) { |
| 323 | 4 | if ($ProductClass->getTaxRule()->getDelFlg() == Constant::ENABLED) { |
|
| 324 | $ProductClass->getTaxRule()->setDelFlg(Constant::DISABLED); |
||
| 325 | } |
||
| 326 | |||
| 327 | 4 | $ProductClass->getTaxRule()->setTaxRate($ProductClass->getTaxRate()); |
|
| 328 | 4 | } else { |
|
| 329 | 4 | $taxrule = $app['eccube.repository.tax_rule']->newTaxRule(); |
|
| 330 | 4 | $taxrule->setTaxRate($ProductClass->getTaxRate()); |
|
| 331 | 4 | $taxrule->setApplyDate(new \DateTime()); |
|
| 332 | 4 | $taxrule->setProduct($Product); |
|
| 333 | 4 | $taxrule->setProductClass($ProductClass); |
|
| 334 | 4 | $ProductClass->setTaxRule($taxrule); |
|
| 335 | } |
||
| 336 | 8 | } else { |
|
| 337 | 4 | if ($ProductClass->getTaxRule()) { |
|
| 338 | 2 | $ProductClass->getTaxRule()->setDelFlg(Constant::ENABLED); |
|
| 339 | 2 | } |
|
| 340 | } |
||
| 341 | 12 | } |
|
| 342 | 14 | $app['orm.em']->persist($ProductClass); |
|
| 343 | |||
| 344 | // 在庫情報を作成 |
||
| 345 | 14 | if (!$ProductClass->getStockUnlimited()) { |
|
| 346 | $ProductStock->setStock($ProductClass->getStock()); |
||
| 347 | } else { |
||
| 348 | // 在庫無制限時はnullを設定 |
||
| 349 | 14 | $ProductStock->setStock(null); |
|
| 350 | } |
||
| 351 | 14 | $app['orm.em']->persist($ProductStock); |
|
| 352 | 14 | } |
|
| 353 | |||
| 354 | // カテゴリの登録 |
||
| 355 | // 一度クリア |
||
| 356 | /* @var $Product \Eccube\Entity\Product */ |
||
| 357 | 14 | foreach ($Product->getProductCategories() as $ProductCategory) { |
|
| 358 | 11 | $Product->removeProductCategory($ProductCategory); |
|
| 359 | 11 | $app['orm.em']->remove($ProductCategory); |
|
| 360 | 14 | } |
|
| 361 | 14 | $app['orm.em']->persist($Product); |
|
| 362 | 14 | $app['orm.em']->flush(); |
|
| 363 | |||
| 364 | 14 | $count = 1; |
|
| 365 | 14 | $Categories = $form->get('Category')->getData(); |
|
| 366 | 14 | foreach ($Categories as $Category) { |
|
| 367 | $ProductCategory = new \Eccube\Entity\ProductCategory(); |
||
| 368 | $ProductCategory |
||
| 369 | ->setProduct($Product) |
||
| 370 | ->setProductId($Product->getId()) |
||
| 371 | ->setCategory($Category) |
||
| 372 | ->setCategoryId($Category->getId()) |
||
| 373 | ->setRank($count); |
||
| 374 | $app['orm.em']->persist($ProductCategory); |
||
| 375 | $count++; |
||
| 376 | /* @var $Product \Eccube\Entity\Product */ |
||
| 377 | $Product->addProductCategory($ProductCategory); |
||
| 378 | 14 | } |
|
| 379 | |||
| 380 | // 画像の登録 |
||
| 381 | 14 | $add_images = $form->get('add_images')->getData(); |
|
| 382 | 14 | foreach ($add_images as $add_image) { |
|
| 383 | $ProductImage = new \Eccube\Entity\ProductImage(); |
||
| 384 | $ProductImage |
||
| 385 | ->setFileName($add_image) |
||
| 386 | ->setProduct($Product) |
||
| 387 | ->setRank(1); |
||
| 388 | $Product->addProductImage($ProductImage); |
||
| 389 | $app['orm.em']->persist($ProductImage); |
||
| 390 | |||
| 391 | // 移動 |
||
| 392 | $file = new File($app['config']['image_temp_realdir'] . '/' . $add_image); |
||
| 393 | $file->move($app['config']['image_save_realdir']); |
||
| 394 | 14 | } |
|
| 395 | |||
| 396 | // 画像の削除 |
||
| 397 | 14 | $delete_images = $form->get('delete_images')->getData(); |
|
| 398 | 14 | foreach ($delete_images as $delete_image) { |
|
| 399 | $ProductImage = $app['eccube.repository.product_image'] |
||
| 400 | ->findOneBy(array('file_name' => $delete_image)); |
||
| 401 | |||
| 402 | // 追加してすぐに削除した画像は、Entityに追加されない |
||
| 403 | if ($ProductImage instanceof \Eccube\Entity\ProductImage) { |
||
| 404 | $Product->removeProductImage($ProductImage); |
||
| 405 | $app['orm.em']->remove($ProductImage); |
||
| 406 | |||
| 407 | } |
||
| 408 | $app['orm.em']->persist($Product); |
||
| 409 | |||
| 410 | // 削除 |
||
| 411 | $fs = new Filesystem(); |
||
| 412 | $fs->remove($app['config']['image_save_realdir'] . '/' . $delete_image); |
||
| 413 | 14 | } |
|
| 414 | 14 | $app['orm.em']->persist($Product); |
|
| 415 | 14 | $app['orm.em']->flush(); |
|
| 416 | |||
| 417 | |||
| 418 | 14 | $ranks = $request->get('rank_images'); |
|
| 419 | 14 | if ($ranks) { |
|
| 420 | foreach ($ranks as $rank) { |
||
| 421 | list($filename, $rank_val) = explode('//', $rank); |
||
| 422 | $ProductImage = $app['eccube.repository.product_image'] |
||
| 423 | ->findOneBy(array( |
||
| 424 | 'file_name' => $filename, |
||
| 425 | 'Product' => $Product, |
||
| 426 | )); |
||
| 427 | $ProductImage->setRank($rank_val); |
||
| 428 | $app['orm.em']->persist($ProductImage); |
||
| 429 | } |
||
| 430 | } |
||
| 431 | 14 | $app['orm.em']->flush(); |
|
| 432 | |||
| 433 | // 商品タグの登録 |
||
| 434 | // 商品タグを一度クリア |
||
| 435 | 14 | $ProductTags = $Product->getProductTag(); |
|
| 436 | 14 | foreach ($ProductTags as $ProductTag) { |
|
| 437 | $Product->removeProductTag($ProductTag); |
||
| 438 | $app['orm.em']->remove($ProductTag); |
||
| 439 | 14 | } |
|
| 440 | |||
| 441 | // 商品タグの登録 |
||
| 442 | 14 | $Tags = $form->get('Tag')->getData(); |
|
| 443 | 14 | foreach ($Tags as $Tag) { |
|
| 444 | 14 | $ProductTag = new ProductTag(); |
|
| 445 | $ProductTag |
||
| 446 | 14 | ->setProduct($Product) |
|
| 447 | 14 | ->setTag($Tag); |
|
| 448 | 14 | $Product->addProductTag($ProductTag); |
|
| 449 | 14 | $app['orm.em']->persist($ProductTag); |
|
| 450 | 14 | } |
|
| 451 | 14 | $app['orm.em']->flush(); |
|
| 452 | |||
| 453 | 14 | $event = new EventArgs( |
|
| 454 | array( |
||
| 455 | 14 | 'form' => $form, |
|
| 456 | 14 | 'Product' => $Product, |
|
| 457 | 14 | ), |
|
| 458 | $request |
||
| 459 | 14 | ); |
|
| 460 | 14 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE, $event); |
|
| 461 | |||
| 462 | 14 | $app->addSuccess('admin.register.complete', 'admin'); |
|
| 463 | |||
| 464 | 14 | return $app->redirect($app->url('admin_product_product_edit', array('id' => $Product->getId()))); |
|
| 465 | } else { |
||
| 466 | $app->addError('admin.register.failed', 'admin'); |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | // 検索結果の保持 |
||
| 471 | 4 | $builder = $app['form.factory'] |
|
| 472 | 4 | ->createBuilder('admin_search_product'); |
|
| 473 | |||
| 474 | 4 | $event = new EventArgs( |
|
| 475 | array( |
||
| 476 | 4 | 'builder' => $builder, |
|
| 477 | 4 | 'Product' => $Product, |
|
| 478 | 4 | ), |
|
| 479 | $request |
||
| 480 | 4 | ); |
|
| 481 | 4 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_SEARCH, $event); |
|
| 482 | |||
| 483 | 4 | $searchForm = $builder->getForm(); |
|
| 484 | |||
| 485 | 4 | if ('POST' === $request->getMethod()) { |
|
| 486 | $searchForm->handleRequest($request); |
||
| 487 | } |
||
| 488 | |||
| 489 | 4 | return $app->render('Product/product.twig', array( |
|
| 490 | 4 | 'Product' => $Product, |
|
| 491 | 4 | 'form' => $form->createView(), |
|
| 492 | 4 | 'searchForm' => $searchForm->createView(), |
|
| 493 | 4 | 'has_class' => $has_class, |
|
| 494 | 4 | 'id' => $id, |
|
| 495 | 4 | )); |
|
| 496 | } |
||
| 497 | |||
| 498 | 4 | public function delete(Application $app, Request $request, $id = null) |
|
| 499 | { |
||
| 500 | 2 | $this->isTokenValid($app); |
|
| 501 | 2 | $session = $request->getSession(); |
|
| 502 | 2 | $page_no = intval($session->get('eccube.admin.product.search.page_no')); |
|
| 503 | 2 | $page_no = $page_no ? $page_no : Constant::ENABLED; |
|
| 504 | |||
| 505 | 2 | if (!is_null($id)) { |
|
| 506 | /* @var $Product \Eccube\Entity\Product */ |
||
| 507 | 2 | $Product = $app['eccube.repository.product']->find($id); |
|
| 508 | 2 | if (!$Product) { |
|
| 509 | $app->deleteMessage(); |
||
| 510 | return $app->redirect($app->url('admin_product_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED); |
||
| 511 | } |
||
| 512 | |||
| 513 | 2 | if ($Product instanceof \Eccube\Entity\Product) { |
|
| 514 | 2 | $Product->setDelFlg(Constant::ENABLED); |
|
| 515 | |||
| 516 | 2 | $ProductClasses = $Product->getProductClasses(); |
|
| 517 | 2 | $deleteImages = array(); |
|
| 518 | 2 | foreach ($ProductClasses as $ProductClass) { |
|
| 519 | 2 | $ProductClass->setDelFlg(Constant::ENABLED); |
|
| 520 | 2 | $Product->removeProductClass($ProductClass); |
|
| 521 | |||
| 522 | 2 | $ProductClasses = $Product->getProductClasses(); |
|
| 523 | 2 | foreach ($ProductClasses as $ProductClass) { |
|
| 524 | 2 | $ProductClass->setDelFlg(Constant::ENABLED); |
|
| 525 | 4 | $Product->removeProductClass($ProductClass); |
|
| 526 | |||
| 527 | 2 | $ProductStock = $ProductClass->getProductStock(); |
|
| 528 | 2 | $app['orm.em']->remove($ProductStock); |
|
| 529 | 2 | } |
|
| 530 | |||
| 531 | 2 | $ProductImages = $Product->getProductImage(); |
|
| 532 | 2 | foreach ($ProductImages as $ProductImage) { |
|
| 533 | 4 | $Product->removeProductImage($ProductImage); |
|
| 534 | 4 | $deleteImages[] = $ProductImage->getFileName(); |
|
| 535 | 2 | $app['orm.em']->remove($ProductImage); |
|
| 536 | 2 | } |
|
| 537 | |||
| 538 | 2 | $ProductCategories = $Product->getProductCategories(); |
|
| 539 | 2 | foreach ($ProductCategories as $ProductCategory) { |
|
| 540 | 2 | $Product->removeProductCategory($ProductCategory); |
|
| 541 | 4 | $app['orm.em']->remove($ProductCategory); |
|
| 542 | 2 | } |
|
| 543 | |||
| 544 | 2 | } |
|
| 545 | |||
| 546 | 2 | $app['orm.em']->persist($Product); |
|
| 547 | |||
| 548 | 2 | $app['orm.em']->flush(); |
|
| 549 | |||
| 550 | 2 | $event = new EventArgs( |
|
| 551 | array( |
||
| 552 | 2 | 'Product' => $Product, |
|
| 553 | 2 | 'ProductClass' => $ProductClasses, |
|
| 554 | 2 | 'deleteImages' => $deleteImages, |
|
| 555 | 2 | ), |
|
| 556 | $request |
||
| 557 | 2 | ); |
|
| 558 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_DELETE_COMPLETE, $event); |
|
| 559 | 2 | $deleteImages = $event->getArgument('deleteImages'); |
|
| 560 | |||
| 561 | // 画像ファイルの削除(commit後に削除させる) |
||
| 562 | 2 | foreach ($deleteImages as $deleteImage) { |
|
| 563 | try { |
||
| 564 | 2 | $fs = new Filesystem(); |
|
| 565 | 2 | $fs->remove($app['config']['image_save_realdir'] . '/' . $deleteImage); |
|
| 566 | 2 | } catch (\Exception $e) { |
|
| 567 | // エラーが発生しても無視する |
||
| 568 | } |
||
| 569 | 2 | } |
|
| 570 | |||
| 571 | 2 | $app->addSuccess('admin.delete.complete', 'admin'); |
|
| 572 | 2 | } else { |
|
| 573 | $app->addError('admin.delete.failed', 'admin'); |
||
| 574 | } |
||
| 575 | 2 | } else { |
|
| 576 | $app->addError('admin.delete.failed', 'admin'); |
||
| 577 | } |
||
| 578 | |||
| 579 | 2 | return $app->redirect($app->url('admin_product_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED); |
|
| 580 | } |
||
| 581 | |||
| 582 | 2 | public function copy(Application $app, Request $request, $id = null) |
|
| 676 | |||
| 677 | 1 | public function display(Application $app, Request $request, $id = null) |
|
| 691 | |||
| 692 | /** |
||
| 693 | * 商品CSVの出力. |
||
| 694 | * |
||
| 695 | * @param Application $app |
||
| 696 | * @param Request $request |
||
| 697 | * @return StreamedResponse |
||
| 698 | */ |
||
| 699 | public function export(Application $app, Request $request) |
||
| 772 | } |
||
| 773 |