Completed
Push — master ( e39dc3...2ecca8 )
by Ryo
367:08 queued 359:36
created

Controller/Admin/Product/ProductController.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
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
24
25
namespace Eccube\Controller\Admin\Product;
26
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Controller\AbstractController;
30
use Eccube\Entity\Master\CsvType;
31
use Eccube\Entity\ProductTag;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Symfony\Component\Filesystem\Filesystem;
35
use Symfony\Component\HttpFoundation\File\File;
36
use Symfony\Component\HttpFoundation\Request;
37
use Symfony\Component\HttpFoundation\StreamedResponse;
38
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
39
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
40
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
41
42
class ProductController extends AbstractController
43
{
44 7
    public function index(Application $app, Request $request, $page_no = null)
45
    {
46
47 7
        $session = $app['session'];
48
49 7
        $builder = $app['form.factory']
50 7
            ->createBuilder('admin_search_product');
51
52 7
        $event = new EventArgs(
53
            array(
54 7
                'builder' => $builder,
55
            ),
56
            $request
57
        );
58 7
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_INDEX_INITIALIZE, $event);
59
60 7
        $searchForm = $builder->getForm();
61
62 7
        $pagination = array();
63
64 7
        $disps = $app['eccube.repository.master.disp']->findAll();
65 7
        $pageMaxis = $app['eccube.repository.master.page_max']->findAll();
66 7
        $page_count = $app['config']['default_page_count'];
67 7
        $page_status = null;
68 7
        $active = false;
69
70 7
        if ('POST' === $request->getMethod()) {
71
72 5
            $searchForm->handleRequest($request);
73
74 5
            if ($searchForm->isValid()) {
75 5
                $searchData = $searchForm->getData();
76
77
                // paginator
78 5
                $qb = $app['eccube.repository.product']->getQueryBuilderBySearchDataForAdmin($searchData);
79 5
                $page_no = 1;
80
81 5
                $event = new EventArgs(
82
                    array(
83 5
                        'qb' => $qb,
84 5
                        'searchData' => $searchData,
85
                    ),
86
                    $request
87
                );
88 5
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_INDEX_SEARCH, $event);
89 5
                $searchData = $event->getArgument('searchData');
90
91 5
                $pagination = $app['paginator']()->paginate(
92
                    $qb,
93
                    $page_no,
94
                    $page_count,
95 5
                    array('wrap-queries' => true)
96
                );
97
98
                // sessionのデータ保持
99 5
                $session->set('eccube.admin.product.search', $searchData);
100 5
                $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
            } 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
                    // セッションから検索条件を復元(スーテタス)
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
                    
169
                    $searchForm->setData($searchData);
170
                }
171
            }
172
        }
173
174 7
        return $app->render('Product/index.twig', array(
175 7
            'searchForm' => $searchForm->createView(),
176 7
            'pagination' => $pagination,
177 7
            'disps' => $disps,
178 7
            'pageMaxis' => $pageMaxis,
179 7
            'page_no' => $page_no,
180 7
            'page_status' => $page_status,
181 7
            'page_count' => $page_count,
182 7
            'active' => $active,
183
        ));
184
    }
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
459 14
                $Product->setUpdateDate(new \DateTime());
460 14
                $app['orm.em']->flush();
461
462 14
                log_info('商品登録完了', array($id));
463
464 14
                $event = new EventArgs(
465
                    array(
466 14
                        'form' => $form,
467 14
                        'Product' => $Product,
468
                    ),
469
                    $request
470
                );
471 14
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE, $event);
472
473 14
                $app->addSuccess('admin.register.complete', 'admin');
474
475 14
                return $app->redirect($app->url('admin_product_product_edit', array('id' => $Product->getId())));
476
            } else {
477
                log_info('商品登録チェックエラー', array($id));
478
                $app->addError('admin.register.failed', 'admin');
479
            }
480
        }
481
482
        // 検索結果の保持
483 5
        $builder = $app['form.factory']
484 5
            ->createBuilder('admin_search_product');
485
486 5
        $event = new EventArgs(
487
            array(
488 5
                'builder' => $builder,
489 5
                'Product' => $Product,
490
            ),
491
            $request
492
        );
493 5
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_SEARCH, $event);
494
495 5
        $searchForm = $builder->getForm();
496
497 5
        if ('POST' === $request->getMethod()) {
498
            $searchForm->handleRequest($request);
499
        }
500
501 5
        return $app->render('Product/product.twig', array(
502 5
            'Product' => $Product,
503 5
            'form' => $form->createView(),
504 5
            'searchForm' => $searchForm->createView(),
505 5
            'has_class' => $has_class,
506 5
            'id' => $id,
507
        ));
508
    }
509
510 2
    public function delete(Application $app, Request $request, $id = null)
511
    {
512 2
        $this->isTokenValid($app);
513 2
        $session = $request->getSession();
514 2
        $page_no = intval($session->get('eccube.admin.product.search.page_no'));
515 2
        $page_no = $page_no ? $page_no : Constant::ENABLED;
516
517 2
        if (!is_null($id)) {
518
            /* @var $Product \Eccube\Entity\Product */
519 2
            $Product = $app['eccube.repository.product']->find($id);
520 2
            if (!$Product) {
521
                $app->deleteMessage();
522
                return $app->redirect($app->url('admin_product_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED);
523
            }
524
525 2
            if ($Product instanceof \Eccube\Entity\Product) {
526 2
                log_info('商品削除開始', array($id));
527
528 2
                $Product->setDelFlg(Constant::ENABLED);
529
530 2
                $ProductClasses = $Product->getProductClasses();
531 2
                $deleteImages = array();
532 2
                foreach ($ProductClasses as $ProductClass) {
533 2
                    $ProductClass->setDelFlg(Constant::ENABLED);
534 2
                    $Product->removeProductClass($ProductClass);
535
536 2
                    $ProductClasses = $Product->getProductClasses();
537 2
                    foreach ($ProductClasses as $ProductClass) {
538 2
                        $ProductClass->setDelFlg(Constant::ENABLED);
539 2
                        $Product->removeProductClass($ProductClass);
540
541 2
                        $ProductStock = $ProductClass->getProductStock();
542 2
                        $app['orm.em']->remove($ProductStock);
543
                    }
544
545 2
                    $ProductImages = $Product->getProductImage();
546 2
                    foreach ($ProductImages as $ProductImage) {
547 2
                        $Product->removeProductImage($ProductImage);
548 2
                        $deleteImages[] = $ProductImage->getFileName();
549 2
                        $app['orm.em']->remove($ProductImage);
550
                    }
551
552 2
                    $ProductCategories = $Product->getProductCategories();
553 2
                    foreach ($ProductCategories as $ProductCategory) {
554 2
                        $Product->removeProductCategory($ProductCategory);
555 2
                        $app['orm.em']->remove($ProductCategory);
556
                    }
557
558
                }
559
560 2
                $app['orm.em']->persist($Product);
561
562 2
                $app['orm.em']->flush();
563
564 2
                $event = new EventArgs(
565
                    array(
566 2
                        'Product' => $Product,
567 2
                        'ProductClass' => $ProductClasses,
568 2
                        'deleteImages' => $deleteImages,
569
                    ),
570
                    $request
571
                );
572 2
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_DELETE_COMPLETE, $event);
573 2
                $deleteImages = $event->getArgument('deleteImages');
574
575
                // 画像ファイルの削除(commit後に削除させる)
576 2
                foreach ($deleteImages as $deleteImage) {
577
                    try {
578 2
                        $fs = new Filesystem();
579 2
                        $fs->remove($app['config']['image_save_realdir'] . '/' . $deleteImage);
580 2
                    } catch (\Exception $e) {
581
                        // エラーが発生しても無視する
582
                    }
583
                }
584
585 2
                log_info('商品削除完了', array($id));
586
587 2
                $app->addSuccess('admin.delete.complete', 'admin');
588
            } else {
589
                log_info('商品削除エラー', array($id));
590 2
                $app->addError('admin.delete.failed', 'admin');
591
            }
592
        } else {
593
            log_info('商品削除エラー', array($id));
594
            $app->addError('admin.delete.failed', 'admin');
595
        }
596
597 2
        return $app->redirect($app->url('admin_product_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED);
598
    }
599
600 2
    public function copy(Application $app, Request $request, $id = null)
601
    {
602 2
        $this->isTokenValid($app);
603
604 2
        if (!is_null($id)) {
605 2
            $Product = $app['eccube.repository.product']->find($id);
606 2
            if ($Product instanceof \Eccube\Entity\Product) {
607 2
                $CopyProduct = clone $Product;
608 2
                $CopyProduct->copy();
609 2
                $Disp = $app['eccube.repository.master.disp']->find(\Eccube\Entity\Master\Disp::DISPLAY_HIDE);
610 2
                $CopyProduct->setStatus($Disp);
611
612 2
                $CopyProductCategories = $CopyProduct->getProductCategories();
613 2
                foreach ($CopyProductCategories as $Category) {
614 2
                    $app['orm.em']->persist($Category);
615
                }
616
617
                // 規格あり商品の場合は, デフォルトの商品規格を取得し登録する.
618 2
                if ($CopyProduct->hasProductClass()) {
619 2
                    $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete');
620 2
                    $softDeleteFilter->setExcludes(array(
621
                        'Eccube\Entity\ProductClass'
622 2
                    ));
623 2
                    $dummyClass = $app['eccube.repository.product_class']->findOneBy(array(
624 2
                        'del_flg' => \Eccube\Common\Constant::ENABLED,
625
                        'ClassCategory1' => null,
626
                        'ClassCategory2' => null,
627 2
                        'Product' => $Product,
628
                    ));
629 2
                    $dummyClass = clone $dummyClass;
630 2
                    $dummyClass->setProduct($CopyProduct);
631 2
                    $CopyProduct->addProductClass($dummyClass);
632 2
                    $softDeleteFilter->setExcludes(array());
633
                }
634
635 2
                $CopyProductClasses = $CopyProduct->getProductClasses();
636 2
                foreach ($CopyProductClasses as $Class) {
637 2
                    $Stock = $Class->getProductStock();
638 2
                    $CopyStock = clone $Stock;
639 2
                    $CopyStock->setProductClass($Class);
640 2
                    $app['orm.em']->persist($CopyStock);
641
642 2
                    $app['orm.em']->persist($Class);
643
                }
644 2
                $Images = $CopyProduct->getProductImage();
645 2
                foreach ($Images as $Image) {
646
647
                    // 画像ファイルを新規作成
648 2
                    $extension = pathinfo($Image->getFileName(), PATHINFO_EXTENSION);
649 2
                    $filename = date('mdHis') . uniqid('_') . '.' . $extension;
650
                    try {
651 2
                        $fs = new Filesystem();
652 2
                        $fs->copy($app['config']['image_save_realdir'] . '/' . $Image->getFileName(), $app['config']['image_save_realdir'] . '/' . $filename);
653 2
                    } catch (\Exception $e) {
654
                        // エラーが発生しても無視する
655
                    }
656 2
                    $Image->setFileName($filename);
657
658 2
                    $app['orm.em']->persist($Image);
659
                }
660 2
                $Tags = $CopyProduct->getProductTag();
661 2
                foreach ($Tags as $Tag) {
662 2
                    $app['orm.em']->persist($Tag);
663
                }
664
665 2
                $app['orm.em']->persist($CopyProduct);
666
667 2
                $app['orm.em']->flush();
668
669 2
                $event = new EventArgs(
670
                    array(
671 2
                        'Product' => $Product,
672 2
                        'CopyProduct' => $CopyProduct,
673 2
                        'CopyProductCategories' => $CopyProductCategories,
674 2
                        'CopyProductClasses' => $CopyProductClasses,
675 2
                        'images' => $Images,
676 2
                        'Tags' => $Tags,
677
                    ),
678
                    $request
679
                );
680 2
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE, $event);
681
682 2
                $app->addSuccess('admin.product.copy.complete', 'admin');
683
684 2
                return $app->redirect($app->url('admin_product_product_edit', array('id' => $CopyProduct->getId())));
685
            } else {
686
                $app->addError('admin.product.copy.failed', 'admin');
687
            }
688
        } else {
689
            $app->addError('admin.product.copy.failed', 'admin');
690
        }
691
692
        return $app->redirect($app->url('admin_product'));
693
    }
694
695 1
    public function display(Application $app, Request $request, $id = null)
696
    {
697 1
        $event = new EventArgs(
698 1
            array(),
699
            $request
700
        );
701 1
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_DISPLAY_COMPLETE, $event);
702
703 1
        if (!is_null($id)) {
704 1
            return $app->redirect($app->url('product_detail', array('id' => $id, 'admin' => '1')));
705
        }
706
707
        return $app->redirect($app->url('admin_product'));
708
    }
709
710
    /**
711
     * 商品CSVの出力.
712
     *
713
     * @param Application $app
714
     * @param Request $request
715
     * @return StreamedResponse
716
     */
717
    public function export(Application $app, Request $request)
718
    {
719
        // タイムアウトを無効にする.
720
        set_time_limit(0);
721
722
        // sql loggerを無効にする.
723
        $em = $app['orm.em'];
724
        $em->getConfiguration()->setSQLLogger(null);
725
726
        $response = new StreamedResponse();
727
        $response->setCallback(function () use ($app, $request) {
728
729
            // CSV種別を元に初期化.
730
            $app['eccube.service.csv.export']->initCsvType(CsvType::CSV_TYPE_PRODUCT);
731
732
            // ヘッダ行の出力.
733
            $app['eccube.service.csv.export']->exportHeader();
734
735
            // 商品データ検索用のクエリビルダを取得.
736
            $qb = $app['eccube.service.csv.export']
737
                ->getProductQueryBuilder($request);
738
739
            // joinする場合はiterateが使えないため, select句をdistinctする.
740
            // http://qiita.com/suin/items/2b1e98105fa3ef89beb7
741
            // distinctのmysqlとpgsqlの挙動をあわせる.
742
            // http://uedatakeshi.blogspot.jp/2010/04/distinct-oeder-by-postgresmysql.html
743
            $qb->resetDQLPart('select')
744
                ->resetDQLPart('orderBy')
745
                ->select('p')
746
                ->orderBy('p.update_date', 'DESC')
747
                ->distinct();
748
749
            // データ行の出力.
750
            $app['eccube.service.csv.export']->setExportQueryBuilder($qb);
751 View Code Duplication
            $app['eccube.service.csv.export']->exportData(function ($entity, $csvService) {
752
753
                $Csvs = $csvService->getCsvs();
754
755
                /** @var $Product \Eccube\Entity\Product */
756
                $Product = $entity;
757
758
                /** @var $Product \Eccube\Entity\ProductClass[] */
759
                $ProductClassess = $Product->getProductClasses();
760
761
                foreach ($ProductClassess as $ProductClass) {
762
                    $row = array();
763
764
                    // CSV出力項目と合致するデータを取得.
765
                    foreach ($Csvs as $Csv) {
766
                        // 商品データを検索.
767
                        $data = $csvService->getData($Csv, $Product);
768
                        if (is_null($data)) {
769
                            // 商品規格情報を検索.
770
                            $data = $csvService->getData($Csv, $ProductClass);
771
                        }
772
                        $row[] = $data;
773
                    }
774
775
                    //$row[] = number_format(memory_get_usage(true));
776
                    // 出力.
777
                    $csvService->fputcsv($row);
778
                }
779
            });
780
        });
781
782
        $now = new \DateTime();
783
        $filename = 'product_' . $now->format('YmdHis') . '.csv';
784
        $response->headers->set('Content-Type', 'application/octet-stream');
785
        $response->headers->set('Content-Disposition', 'attachment; filename=' . $filename);
786
        $response->send();
787
788
        log_info('商品CSV出力ファイル名', array($filename));
789
790
        return $response;
791
    }
792
    
0 ignored issues
show
Please trim any trailing whitespace
Loading history...
793
    /**
794
     * ProductCategory作成
795
     * @param \Eccube\Entity\Product $Product
796
     * @param \Eccube\Entity\Category $Category
797
     * @return \Eccube\Entity\ProductCategory
798
     */
799 View Code Duplication
    private function createProductCategory($Product, $Category, $count)
800
    {
801
        $ProductCategory = new \Eccube\Entity\ProductCategory();
802
        $ProductCategory->setProduct($Product);
803
        $ProductCategory->setProductId($Product->getId());
804
        $ProductCategory->setCategory($Category);
805
        $ProductCategory->setCategoryId($Category->getId());
806
        $ProductCategory->setRank($count);
807
        
0 ignored issues
show
Please trim any trailing whitespace
Loading history...
808
        return $ProductCategory;
809
    }
810
}
811