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 | 2 | public function index(Application $app, Request $request, $page_no = null) |
|
| 185 | |||
| 186 | public function addImage(Application $app, Request $request) |
||
| 187 | { |
||
| 188 | if (!$request->isXmlHttpRequest()) { |
||
| 189 | throw new BadRequestHttpException('リクエストが不正です'); |
||
| 190 | } |
||
| 191 | |||
| 192 | $images = $request->files->get('admin_product'); |
||
| 193 | |||
| 194 | $files = array(); |
||
| 195 | if (count($images) > 0) { |
||
| 196 | foreach ($images as $img) { |
||
| 197 | foreach ($img as $image) { |
||
| 198 | //ファイルフォーマット検証 |
||
| 199 | $mimeType = $image->getMimeType(); |
||
| 200 | if (0 !== strpos($mimeType, 'image')) { |
||
| 201 | throw new UnsupportedMediaTypeHttpException('ファイル形式が不正です'); |
||
| 202 | } |
||
| 203 | |||
| 204 | $extension = $image->getClientOriginalExtension(); |
||
| 205 | $filename = date('mdHis') . uniqid('_') . '.' . $extension; |
||
| 206 | $image->move($app['config']['image_temp_realdir'], $filename); |
||
| 207 | $files[] = $filename; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | $event = new EventArgs( |
||
| 213 | array( |
||
| 214 | 'images' => $images, |
||
| 215 | 'files' => $files, |
||
| 216 | ), |
||
| 217 | $request |
||
| 218 | ); |
||
| 219 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_ADD_IMAGE_COMPLETE, $event); |
||
| 220 | $files = $event->getArgument('files'); |
||
| 221 | |||
| 222 | return $app->json(array('files' => $files), 200); |
||
| 223 | } |
||
| 224 | |||
| 225 | 19 | public function edit(Application $app, Request $request, $id = null) |
|
| 226 | { |
||
| 227 | 19 | $has_class = false; |
|
| 228 | 19 | if (is_null($id)) { |
|
| 229 | 6 | $Product = new \Eccube\Entity\Product(); |
|
| 230 | 6 | $ProductClass = new \Eccube\Entity\ProductClass(); |
|
| 231 | 6 | $Disp = $app['eccube.repository.master.disp']->find(\Eccube\Entity\Master\Disp::DISPLAY_HIDE); |
|
| 232 | $Product |
||
| 233 | 6 | ->setDelFlg(Constant::DISABLED) |
|
| 234 | 6 | ->addProductClass($ProductClass) |
|
| 235 | 6 | ->setStatus($Disp); |
|
| 236 | $ProductClass |
||
| 237 | 6 | ->setDelFlg(Constant::DISABLED) |
|
| 238 | 6 | ->setStockUnlimited(true) |
|
| 239 | 6 | ->setProduct($Product); |
|
| 240 | 6 | $ProductStock = new \Eccube\Entity\ProductStock(); |
|
| 241 | 6 | $ProductClass->setProductStock($ProductStock); |
|
| 242 | 6 | $ProductStock->setProductClass($ProductClass); |
|
| 243 | } else { |
||
| 244 | 13 | $Product = $app['eccube.repository.product']->find($id); |
|
| 245 | 13 | if (!$Product) { |
|
| 246 | throw new NotFoundHttpException(); |
||
| 247 | } |
||
| 248 | // 規格あり商品か |
||
| 249 | 13 | $has_class = $Product->hasProductClass(); |
|
| 250 | 13 | if (!$has_class) { |
|
| 251 | 11 | $ProductClasses = $Product->getProductClasses(); |
|
| 252 | 11 | $ProductClass = $ProductClasses[0]; |
|
| 253 | 11 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
|
| 254 | 11 | View Code Duplication | if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED && $ProductClass->getTaxRule() && !$ProductClass->getTaxRule()->getDelFlg()) { |
| 255 | 6 | $ProductClass->setTaxRate($ProductClass->getTaxRule()->getTaxRate()); |
|
| 256 | } |
||
| 257 | 11 | $ProductStock = $ProductClasses[0]->getProductStock(); |
|
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | 19 | $builder = $app['form.factory'] |
|
| 262 | 19 | ->createBuilder('admin_product', $Product); |
|
| 263 | |||
| 264 | // 規格あり商品の場合、規格関連情報をFormから除外 |
||
| 265 | 19 | if ($has_class) { |
|
| 266 | 2 | $builder->remove('class'); |
|
| 267 | } |
||
| 268 | |||
| 269 | 19 | $event = new EventArgs( |
|
| 270 | array( |
||
| 271 | 19 | 'builder' => $builder, |
|
| 272 | 19 | 'Product' => $Product, |
|
| 273 | ), |
||
| 274 | $request |
||
| 275 | ); |
||
| 276 | 19 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_INITIALIZE, $event); |
|
| 277 | |||
| 278 | 19 | $form = $builder->getForm(); |
|
| 279 | |||
| 280 | 19 | if (!$has_class) { |
|
| 281 | 17 | $ProductClass->setStockUnlimited((boolean)$ProductClass->getStockUnlimited()); |
|
| 282 | 17 | $form['class']->setData($ProductClass); |
|
| 283 | } |
||
| 284 | |||
| 285 | // ファイルの登録 |
||
| 286 | 19 | $images = array(); |
|
| 287 | 19 | $ProductImages = $Product->getProductImage(); |
|
| 288 | 19 | foreach ($ProductImages as $ProductImage) { |
|
| 289 | 19 | $images[] = $ProductImage->getFileName(); |
|
| 290 | } |
||
| 291 | 19 | $form['images']->setData($images); |
|
| 292 | |||
| 293 | 19 | $categories = array(); |
|
| 294 | 19 | $ProductCategories = $Product->getProductCategories(); |
|
| 295 | 19 | foreach ($ProductCategories as $ProductCategory) { |
|
| 296 | /* @var $ProductCategory \Eccube\Entity\ProductCategory */ |
||
| 297 | 19 | $categories[] = $ProductCategory->getCategory(); |
|
| 298 | } |
||
| 299 | 19 | $form['Category']->setData($categories); |
|
| 300 | |||
| 301 | 19 | $Tags = array(); |
|
| 302 | 19 | $ProductTags = $Product->getProductTag(); |
|
| 303 | 19 | foreach ($ProductTags as $ProductTag) { |
|
| 304 | 19 | $Tags[] = $ProductTag->getTag(); |
|
| 305 | } |
||
| 306 | 19 | $form['Tag']->setData($Tags); |
|
| 307 | |||
| 308 | 19 | if ('POST' === $request->getMethod()) { |
|
| 309 | 14 | $form->handleRequest($request); |
|
| 310 | 14 | if ($form->isValid()) { |
|
| 311 | 14 | log_info('商品登録開始', array($id)); |
|
| 312 | 14 | $Product = $form->getData(); |
|
| 313 | |||
| 314 | 14 | if (!$has_class) { |
|
| 315 | 14 | $ProductClass = $form['class']->getData(); |
|
| 316 | |||
| 317 | // 個別消費税 |
||
| 318 | 14 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
|
| 319 | 14 | if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) { |
|
| 320 | 12 | if ($ProductClass->getTaxRate() !== null) { |
|
| 321 | 8 | View Code Duplication | if ($ProductClass->getTaxRule()) { |
| 322 | 4 | if ($ProductClass->getTaxRule()->getDelFlg() == Constant::ENABLED) { |
|
| 323 | $ProductClass->getTaxRule()->setDelFlg(Constant::DISABLED); |
||
| 324 | } |
||
| 325 | |||
| 326 | 4 | $ProductClass->getTaxRule()->setTaxRate($ProductClass->getTaxRate()); |
|
| 327 | } else { |
||
| 328 | 4 | $taxrule = $app['eccube.repository.tax_rule']->newTaxRule(); |
|
| 329 | 4 | $taxrule->setTaxRate($ProductClass->getTaxRate()); |
|
| 330 | 4 | $taxrule->setApplyDate(new \DateTime()); |
|
| 331 | 4 | $taxrule->setProduct($Product); |
|
| 332 | 4 | $taxrule->setProductClass($ProductClass); |
|
| 333 | 8 | $ProductClass->setTaxRule($taxrule); |
|
| 334 | } |
||
| 335 | } else { |
||
| 336 | 4 | if ($ProductClass->getTaxRule()) { |
|
| 337 | 2 | $ProductClass->getTaxRule()->setDelFlg(Constant::ENABLED); |
|
| 338 | } |
||
| 339 | } |
||
| 340 | } |
||
| 341 | 14 | $app['orm.em']->persist($ProductClass); |
|
| 342 | |||
| 343 | // 在庫情報を作成 |
||
| 344 | 14 | if (!$ProductClass->getStockUnlimited()) { |
|
| 345 | $ProductStock->setStock($ProductClass->getStock()); |
||
| 346 | } else { |
||
| 347 | // 在庫無制限時はnullを設定 |
||
| 348 | 14 | $ProductStock->setStock(null); |
|
| 349 | } |
||
| 350 | 14 | $app['orm.em']->persist($ProductStock); |
|
| 351 | } |
||
| 352 | |||
| 353 | // カテゴリの登録 |
||
| 354 | // 一度クリア |
||
| 355 | /* @var $Product \Eccube\Entity\Product */ |
||
| 356 | 14 | foreach ($Product->getProductCategories() as $ProductCategory) { |
|
| 357 | 11 | $Product->removeProductCategory($ProductCategory); |
|
| 358 | 14 | $app['orm.em']->remove($ProductCategory); |
|
| 359 | } |
||
| 360 | 14 | $app['orm.em']->persist($Product); |
|
| 361 | 14 | $app['orm.em']->flush(); |
|
| 362 | |||
| 363 | 14 | $count = 1; |
|
| 364 | 14 | $Categories = $form->get('Category')->getData(); |
|
| 365 | 14 | $categoriesIdList = array(); |
|
| 366 | 14 | foreach ($Categories as $Category) { |
|
| 367 | View Code Duplication | foreach($Category->getPath() as $ParentCategory){ |
|
| 368 | if (!isset($categoriesIdList[$ParentCategory->getId()])){ |
||
| 369 | $ProductCategory = $this->createProductCategory($Product, $ParentCategory, $count); |
||
| 370 | $app['orm.em']->persist($ProductCategory); |
||
| 371 | $count++; |
||
| 372 | /* @var $Product \Eccube\Entity\Product */ |
||
| 373 | $Product->addProductCategory($ProductCategory); |
||
| 374 | $categoriesIdList[$ParentCategory->getId()] = true; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | if (!isset($categoriesIdList[$Category->getId()])){ |
||
| 378 | $ProductCategory = $this->createProductCategory($Product, $Category, $count); |
||
| 379 | $app['orm.em']->persist($ProductCategory); |
||
| 380 | $count++; |
||
| 381 | /* @var $Product \Eccube\Entity\Product */ |
||
| 382 | $Product->addProductCategory($ProductCategory); |
||
| 383 | 14 | $categoriesIdList[$Category->getId()] = true; |
|
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | // 画像の登録 |
||
| 388 | 14 | $add_images = $form->get('add_images')->getData(); |
|
| 389 | 14 | foreach ($add_images as $add_image) { |
|
| 390 | $ProductImage = new \Eccube\Entity\ProductImage(); |
||
| 391 | $ProductImage |
||
| 392 | ->setFileName($add_image) |
||
| 393 | ->setProduct($Product) |
||
| 394 | ->setRank(1); |
||
| 395 | $Product->addProductImage($ProductImage); |
||
| 396 | $app['orm.em']->persist($ProductImage); |
||
| 397 | |||
| 398 | // 移動 |
||
| 399 | $file = new File($app['config']['image_temp_realdir'] . '/' . $add_image); |
||
| 400 | 14 | $file->move($app['config']['image_save_realdir']); |
|
| 401 | } |
||
| 402 | |||
| 403 | // 画像の削除 |
||
| 404 | 14 | $delete_images = $form->get('delete_images')->getData(); |
|
| 405 | 14 | foreach ($delete_images as $delete_image) { |
|
| 406 | $ProductImage = $app['eccube.repository.product_image'] |
||
| 407 | ->findOneBy(array('file_name' => $delete_image)); |
||
| 408 | |||
| 409 | // 追加してすぐに削除した画像は、Entityに追加されない |
||
| 410 | if ($ProductImage instanceof \Eccube\Entity\ProductImage) { |
||
| 411 | $Product->removeProductImage($ProductImage); |
||
| 412 | $app['orm.em']->remove($ProductImage); |
||
| 413 | |||
| 414 | } |
||
| 415 | $app['orm.em']->persist($Product); |
||
| 416 | |||
| 417 | // 削除 |
||
| 418 | $fs = new Filesystem(); |
||
| 419 | 14 | $fs->remove($app['config']['image_save_realdir'] . '/' . $delete_image); |
|
| 420 | } |
||
| 421 | 14 | $app['orm.em']->persist($Product); |
|
| 422 | 14 | $app['orm.em']->flush(); |
|
| 423 | |||
| 424 | |||
| 425 | 14 | $ranks = $request->get('rank_images'); |
|
| 426 | 14 | if ($ranks) { |
|
| 427 | foreach ($ranks as $rank) { |
||
| 428 | list($filename, $rank_val) = explode('//', $rank); |
||
| 429 | $ProductImage = $app['eccube.repository.product_image'] |
||
| 430 | ->findOneBy(array( |
||
| 431 | 'file_name' => $filename, |
||
| 432 | 'Product' => $Product, |
||
| 433 | )); |
||
| 434 | $ProductImage->setRank($rank_val); |
||
| 435 | $app['orm.em']->persist($ProductImage); |
||
| 436 | } |
||
| 437 | } |
||
| 438 | 14 | $app['orm.em']->flush(); |
|
| 439 | |||
| 440 | // 商品タグの登録 |
||
| 441 | // 商品タグを一度クリア |
||
| 442 | 14 | $ProductTags = $Product->getProductTag(); |
|
| 443 | 14 | foreach ($ProductTags as $ProductTag) { |
|
| 444 | $Product->removeProductTag($ProductTag); |
||
| 445 | 14 | $app['orm.em']->remove($ProductTag); |
|
| 446 | } |
||
| 447 | |||
| 448 | // 商品タグの登録 |
||
| 449 | 14 | $Tags = $form->get('Tag')->getData(); |
|
| 450 | 14 | foreach ($Tags as $Tag) { |
|
| 451 | 14 | $ProductTag = new ProductTag(); |
|
| 452 | $ProductTag |
||
| 453 | 14 | ->setProduct($Product) |
|
| 454 | 14 | ->setTag($Tag); |
|
| 455 | 14 | $Product->addProductTag($ProductTag); |
|
| 456 | 14 | $app['orm.em']->persist($ProductTag); |
|
| 457 | } |
||
| 458 | 14 | $app['orm.em']->flush(); |
|
| 459 | |||
| 460 | 14 | log_info('商品登録完了', array($id)); |
|
| 461 | |||
| 462 | 14 | $event = new EventArgs( |
|
| 463 | array( |
||
| 464 | 14 | 'form' => $form, |
|
| 465 | 14 | 'Product' => $Product, |
|
| 466 | ), |
||
| 467 | $request |
||
| 468 | ); |
||
| 469 | 14 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE, $event); |
|
| 470 | |||
| 471 | 14 | $app->addSuccess('admin.register.complete', 'admin'); |
|
| 472 | |||
| 473 | 14 | return $app->redirect($app->url('admin_product_product_edit', array('id' => $Product->getId()))); |
|
| 474 | } else { |
||
| 475 | log_info('商品登録チェックエラー', array($id)); |
||
| 476 | $app->addError('admin.register.failed', 'admin'); |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | // 検索結果の保持 |
||
| 481 | 5 | $builder = $app['form.factory'] |
|
| 482 | 5 | ->createBuilder('admin_search_product'); |
|
| 483 | |||
| 484 | 5 | $event = new EventArgs( |
|
| 485 | array( |
||
| 486 | 5 | 'builder' => $builder, |
|
| 487 | 5 | 'Product' => $Product, |
|
| 488 | ), |
||
| 489 | $request |
||
| 490 | ); |
||
| 491 | 5 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_SEARCH, $event); |
|
| 492 | |||
| 493 | 5 | $searchForm = $builder->getForm(); |
|
| 494 | |||
| 495 | 5 | if ('POST' === $request->getMethod()) { |
|
| 496 | $searchForm->handleRequest($request); |
||
| 497 | } |
||
| 498 | |||
| 499 | 5 | return $app->render('Product/product.twig', array( |
|
| 500 | 5 | 'Product' => $Product, |
|
| 501 | 5 | 'form' => $form->createView(), |
|
| 502 | 5 | 'searchForm' => $searchForm->createView(), |
|
| 503 | 5 | 'has_class' => $has_class, |
|
| 504 | 5 | 'id' => $id, |
|
| 505 | )); |
||
| 506 | } |
||
| 507 | |||
| 508 | 2 | public function delete(Application $app, Request $request, $id = null) |
|
| 509 | { |
||
| 510 | 2 | $this->isTokenValid($app); |
|
| 511 | 2 | $session = $request->getSession(); |
|
| 512 | 2 | $page_no = intval($session->get('eccube.admin.product.search.page_no')); |
|
| 513 | 2 | $page_no = $page_no ? $page_no : Constant::ENABLED; |
|
| 514 | |||
| 515 | 2 | if (!is_null($id)) { |
|
| 516 | /* @var $Product \Eccube\Entity\Product */ |
||
| 517 | 2 | $Product = $app['eccube.repository.product']->find($id); |
|
| 518 | 2 | if (!$Product) { |
|
| 519 | $app->deleteMessage(); |
||
| 520 | return $app->redirect($app->url('admin_product_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED); |
||
| 521 | } |
||
| 522 | |||
| 523 | 2 | if ($Product instanceof \Eccube\Entity\Product) { |
|
| 524 | 2 | log_info('商品削除開始', array($id)); |
|
| 525 | |||
| 526 | 2 | $Product->setDelFlg(Constant::ENABLED); |
|
| 527 | |||
| 528 | 2 | $ProductClasses = $Product->getProductClasses(); |
|
| 529 | 2 | $deleteImages = array(); |
|
| 530 | 2 | foreach ($ProductClasses as $ProductClass) { |
|
| 531 | 2 | $ProductClass->setDelFlg(Constant::ENABLED); |
|
| 532 | 2 | $Product->removeProductClass($ProductClass); |
|
| 533 | |||
| 534 | 2 | $ProductClasses = $Product->getProductClasses(); |
|
| 535 | 2 | foreach ($ProductClasses as $ProductClass) { |
|
| 536 | 2 | $ProductClass->setDelFlg(Constant::ENABLED); |
|
| 537 | 2 | $Product->removeProductClass($ProductClass); |
|
| 538 | |||
| 539 | 2 | $ProductStock = $ProductClass->getProductStock(); |
|
| 540 | 2 | $app['orm.em']->remove($ProductStock); |
|
| 541 | } |
||
| 542 | |||
| 543 | 2 | $ProductImages = $Product->getProductImage(); |
|
| 544 | 2 | foreach ($ProductImages as $ProductImage) { |
|
| 545 | 2 | $Product->removeProductImage($ProductImage); |
|
| 546 | 2 | $deleteImages[] = $ProductImage->getFileName(); |
|
| 547 | 2 | $app['orm.em']->remove($ProductImage); |
|
| 548 | } |
||
| 549 | |||
| 550 | 2 | $ProductCategories = $Product->getProductCategories(); |
|
| 551 | 2 | foreach ($ProductCategories as $ProductCategory) { |
|
| 552 | 2 | $Product->removeProductCategory($ProductCategory); |
|
| 553 | 2 | $app['orm.em']->remove($ProductCategory); |
|
| 554 | } |
||
| 555 | |||
| 556 | } |
||
| 557 | |||
| 558 | 2 | $app['orm.em']->persist($Product); |
|
| 559 | |||
| 560 | 2 | $app['orm.em']->flush(); |
|
| 561 | |||
| 562 | 2 | $event = new EventArgs( |
|
| 563 | array( |
||
| 564 | 2 | 'Product' => $Product, |
|
| 565 | 2 | 'ProductClass' => $ProductClasses, |
|
| 566 | 2 | 'deleteImages' => $deleteImages, |
|
| 567 | ), |
||
| 568 | $request |
||
| 569 | ); |
||
| 570 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_DELETE_COMPLETE, $event); |
|
| 571 | 2 | $deleteImages = $event->getArgument('deleteImages'); |
|
| 572 | |||
| 573 | // 画像ファイルの削除(commit後に削除させる) |
||
| 574 | 2 | foreach ($deleteImages as $deleteImage) { |
|
| 575 | try { |
||
| 576 | 2 | $fs = new Filesystem(); |
|
| 577 | 2 | $fs->remove($app['config']['image_save_realdir'] . '/' . $deleteImage); |
|
| 578 | 2 | } catch (\Exception $e) { |
|
| 579 | // エラーが発生しても無視する |
||
| 580 | } |
||
| 581 | } |
||
| 582 | |||
| 583 | 2 | log_info('商品削除完了', array($id)); |
|
| 584 | |||
| 585 | 2 | $app->addSuccess('admin.delete.complete', 'admin'); |
|
| 586 | } else { |
||
| 587 | log_info('商品削除エラー', array($id)); |
||
| 588 | 2 | $app->addError('admin.delete.failed', 'admin'); |
|
| 589 | } |
||
| 590 | } else { |
||
| 591 | log_info('商品削除エラー', array($id)); |
||
| 592 | $app->addError('admin.delete.failed', 'admin'); |
||
| 593 | } |
||
| 594 | |||
| 595 | 2 | return $app->redirect($app->url('admin_product_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED); |
|
| 596 | } |
||
| 597 | |||
| 598 | 2 | public function copy(Application $app, Request $request, $id = null) |
|
| 692 | |||
| 693 | 1 | public function display(Application $app, Request $request, $id = null) |
|
| 707 | |||
| 708 | /** |
||
| 709 | * 商品CSVの出力. |
||
| 710 | * |
||
| 711 | * @param Application $app |
||
| 712 | * @param Request $request |
||
| 713 | * @return StreamedResponse |
||
| 714 | */ |
||
| 715 | public function export(Application $app, Request $request) |
||
| 716 | { |
||
| 717 | // タイムアウトを無効にする. |
||
| 718 | set_time_limit(0); |
||
| 719 | |||
| 720 | // sql loggerを無効にする. |
||
| 721 | $em = $app['orm.em']; |
||
| 722 | $em->getConfiguration()->setSQLLogger(null); |
||
| 723 | |||
| 724 | $response = new StreamedResponse(); |
||
| 725 | $response->setCallback(function () use ($app, $request) { |
||
| 726 | |||
| 727 | // CSV種別を元に初期化. |
||
| 728 | $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_PRODUCT); |
||
| 729 | |||
| 730 | // ヘッダ行の出力. |
||
| 731 | $app['eccube.service.csv.export']->exportHeader(); |
||
| 732 | |||
| 733 | // 商品データ検索用のクエリビルダを取得. |
||
| 734 | $qb = $app['eccube.service.csv.export'] |
||
| 735 | ->getProductQueryBuilder($request); |
||
| 736 | |||
| 737 | // joinする場合はiterateが使えないため, select句をdistinctする. |
||
| 738 | // http://qiita.com/suin/items/2b1e98105fa3ef89beb7 |
||
| 739 | // distinctのmysqlとpgsqlの挙動をあわせる. |
||
| 740 | // http://uedatakeshi.blogspot.jp/2010/04/distinct-oeder-by-postgresmysql.html |
||
| 741 | $qb->resetDQLPart('select') |
||
| 742 | ->resetDQLPart('orderBy') |
||
| 743 | ->select('p') |
||
| 744 | ->orderBy('p.update_date', 'DESC') |
||
| 745 | ->distinct(); |
||
| 746 | |||
| 747 | // データ行の出力. |
||
| 748 | $app['eccube.service.csv.export']->setExportQueryBuilder($qb); |
||
| 749 | View Code Duplication | $app['eccube.service.csv.export']->exportData(function ($entity, $csvService) { |
|
| 750 | |||
| 751 | $Csvs = $csvService->getCsvs(); |
||
| 752 | |||
| 753 | /** @var $Product \Eccube\Entity\Product */ |
||
| 754 | $Product = $entity; |
||
| 755 | |||
| 756 | /** @var $Product \Eccube\Entity\ProductClass[] */ |
||
| 757 | $ProductClassess = $Product->getProductClasses(); |
||
| 758 | |||
| 759 | foreach ($ProductClassess as $ProductClass) { |
||
| 760 | $row = array(); |
||
| 761 | |||
| 762 | // CSV出力項目と合致するデータを取得. |
||
| 763 | foreach ($Csvs as $Csv) { |
||
| 764 | // 商品データを検索. |
||
| 765 | $data = $csvService->getData($Csv, $Product); |
||
| 766 | if (is_null($data)) { |
||
| 767 | // 商品規格情報を検索. |
||
| 768 | $data = $csvService->getData($Csv, $ProductClass); |
||
| 769 | } |
||
| 770 | $row[] = $data; |
||
| 771 | } |
||
| 772 | |||
| 773 | //$row[] = number_format(memory_get_usage(true)); |
||
| 774 | // 出力. |
||
| 775 | $csvService->fputcsv($row); |
||
| 776 | } |
||
| 777 | }); |
||
| 778 | }); |
||
| 779 | |||
| 780 | $now = new \DateTime(); |
||
| 781 | $filename = 'product_' . $now->format('YmdHis') . '.csv'; |
||
| 782 | $response->headers->set('Content-Type', 'application/octet-stream'); |
||
| 783 | $response->headers->set('Content-Disposition', 'attachment; filename=' . $filename); |
||
| 784 | $response->send(); |
||
| 785 | |||
| 786 | log_info('商品CSV出力ファイル名', array($filename)); |
||
| 787 | |||
| 788 | return $response; |
||
| 789 | } |
||
| 790 | |||
| 791 | /** |
||
| 792 | * ProductCategory作成 |
||
| 793 | * @param \Eccube\Entity\Product $Product |
||
| 794 | * @param \Eccube\Entity\Category $Category |
||
| 795 | * @return \Eccube\Entity\ProductCategory |
||
| 796 | */ |
||
| 797 | View Code Duplication | private function createProductCategory($Product, $Category, $count) |
|
| 808 | } |
||
| 809 |