Completed
Push — 4.0 ( 87d096...bcc1be )
by Kiyotaka
05:44 queued 11s
created

Admin/Product/ClassCategoryController.php (1 issue)

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
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller\Admin\Product;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Event\EccubeEvents;
18
use Eccube\Event\EventArgs;
19
use Eccube\Form\Type\Admin\ClassCategoryType;
20
use Eccube\Repository\ClassCategoryRepository;
21
use Eccube\Repository\ClassNameRepository;
22
use Eccube\Repository\ProductClassRepository;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
27
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
28
use Symfony\Component\Routing\Annotation\Route;
29
30
class ClassCategoryController extends AbstractController
31
{
32
    /**
33
     * @var ProductClassRepository
34
     */
35
    protected $productClassRepository;
36
37
    /**
38
     * @var ClassCategoryRepository
39
     */
40
    protected $classCategoryRepository;
41
42
    /**
43
     * @var ClassNameRepository
44
     */
45
    protected $classNameRepository;
46
47
    /**
48
     * ClassCategoryController constructor.
49
     *
50
     * @param ProductClassRepository $productClassRepository
51
     * @param ClassCategoryRepository $classCategoryRepository
52
     * @param ClassNameRepository $classNameRepository
53
     */
54 6
    public function __construct(
55
        ProductClassRepository $productClassRepository,
56
        ClassCategoryRepository $classCategoryRepository,
57
        ClassNameRepository $classNameRepository
58
    ) {
59 6
        $this->productClassRepository = $productClassRepository;
60 6
        $this->classCategoryRepository = $classCategoryRepository;
61 6
        $this->classNameRepository = $classNameRepository;
62
    }
63
64
    /**
65
     * @Route("/%eccube_admin_route%/product/class_category/{class_name_id}", requirements={"class_name_id" = "\d+"}, name="admin_product_class_category")
66
     * @Route("/%eccube_admin_route%/product/class_category/{class_name_id}/{id}/edit", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_edit")
67
     * @Template("@admin/Product/class_category.twig")
68
     */
69 4
    public function index(Request $request, $class_name_id, $id = null)
70
    {
71 4
        $ClassName = $this->classNameRepository->find($class_name_id);
72 4
        if (!$ClassName) {
73
            throw new NotFoundHttpException();
74
        }
75 4
        if ($id) {
76 2
            $TargetClassCategory = $this->classCategoryRepository->find($id);
77 2
            if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
78 2
                throw new NotFoundHttpException();
79
            }
80
        } else {
81 3
            $TargetClassCategory = new \Eccube\Entity\ClassCategory();
82 3
            $TargetClassCategory->setClassName($ClassName);
83
        }
84
85 4
        $builder = $this->formFactory
86 4
            ->createBuilder(ClassCategoryType::class, $TargetClassCategory);
87
88 4
        $event = new EventArgs(
89
            [
90 4
                'builder' => $builder,
91 4
                'ClassName' => $ClassName,
92 4
                'TargetClassCategory' => $TargetClassCategory,
93
            ],
94 4
            $request
95
        );
96 4
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_INDEX_INITIALIZE, $event);
97
98 4
        $ClassCategories = $this->classCategoryRepository->getList($ClassName);
99
100 4
        $forms = [];
101 4
        foreach ($ClassCategories as $ClassCategory) {
102 3
            $id = $ClassCategory->getId();
103 3
            $forms[$id] = $this->formFactory->createNamed('class_category_'.$id, ClassCategoryType::class, $ClassCategory);
104
        }
105
106 4
        $form = $builder->getForm();
107
108 4
        if ($request->getMethod() === 'POST') {
109 1
            $form->handleRequest($request);
110 1
            if ($form->isValid()) {
111
                log_info('規格分類登録開始', [$id]);
112
113
                $this->classCategoryRepository->save($TargetClassCategory);
114
115
                log_info('規格分類登録完了', [$id]);
116
117
                $event = new EventArgs(
118
                    [
119
                        'form' => $form,
120
                        'ClassName' => $ClassName,
121
                        'TargetClassCategory' => $TargetClassCategory,
122
                    ],
123
                    $request
124
                );
125
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_INDEX_COMPLETE, $event);
126
127
                $this->addSuccess('admin.common.save_complete', 'admin');
128
129
                return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
130
            }
131
132 1 View Code Duplication
            foreach ($forms as $editForm) {
133 1
                $editForm->handleRequest($request);
134 1
                if ($editForm->isSubmitted() && $editForm->isValid()) {
135 1
                    $this->classCategoryRepository->save($editForm->getData());
136 1
                    $this->addSuccess('admin.common.save_complete', 'admin');
137
138 1
                    return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
139
                }
140
            }
141
        }
142
143 4
        $formViews = [];
144 4
        foreach ($forms as $key => $value) {
145 3
            $formViews[$key] = $value->createView();
146
        }
147
148
        return [
149 4
            'form' => $form->createView(),
150 4
            'ClassName' => $ClassName,
151 4
            'ClassCategories' => $ClassCategories,
152 4
            'TargetClassCategory' => $TargetClassCategory,
153 4
            'forms' => $formViews,
154
        ];
155
    }
156
157
    /**
158
     * @Route("/%eccube_admin_route%/product/class_category/{class_name_id}/{id}/delete", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_delete", methods={"DELETE"})
159
     */
160
    public function delete(Request $request, $class_name_id, $id)
161 1
    {
162
        $this->isTokenValid();
163 1
164
        $ClassName = $this->classNameRepository->find($class_name_id);
165 1
        if (!$ClassName) {
166 1
            throw new NotFoundHttpException();
167
        }
168
169
        log_info('規格分類削除開始', [$id]);
170 1
171
        $TargetClassCategory = $this->classCategoryRepository->find($id);
172 1 View Code Duplication
        if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
173 1
            $this->deleteMessage();
174
175
            return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
176
        }
177
178
        try {
179 1
            $this->classCategoryRepository->delete($TargetClassCategory);
180 1
181 1
            $event = new EventArgs(
182 1
                [
183 1
                    'ClassName' => $ClassName,
184 1
                    'TargetClassCategory' => $TargetClassCategory,
185 1
                ],
186
                $request
187
            );
188
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_DELETE_COMPLETE, $event);
189 1
190
            $this->addSuccess('admin.common.delete_complete', 'admin');
191 1
192
            log_info('規格分類削除完了', [$id]);
193 1
        } catch (\Exception $e) {
194 1
            log_error('規格分類削除エラー', [$id, $e]);
195
196 1
            $message = trans('admin.common.delete_error_foreign_key', ['%name%' => $TargetClassCategory->getName()]);
197
            $this->addError($message, 'admin');
198 1
        }
199
200 1
        return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
201
    }
202 1
203
    /**
204
     * @Route("/%eccube_admin_route%/product/class_category/{class_name_id}/{id}/visibility", requirements={"class_name_id" = "\d+", "id" = "\d+"}, name="admin_product_class_category_visibility", methods={"PUT"})
205
     */
206
    public function visibility(Request $request, $class_name_id, $id)
207
    {
208
        $this->isTokenValid();
209
210
        $ClassName = $this->classNameRepository->find($class_name_id);
211 1
        if (!$ClassName) {
212
            throw new NotFoundHttpException();
213
        }
214
215
        log_info('規格分類表示変更開始', [$id]);
216
217
        $TargetClassCategory = $this->classCategoryRepository->find($id);
218 1 View Code Duplication
        if (!$TargetClassCategory || $TargetClassCategory->getClassName() != $ClassName) {
219
            $this->deleteMessage();
220 1
221
            return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
222 1
        }
223 1
224
        $this->classCategoryRepository->toggleVisibility($TargetClassCategory);
225
226
        log_info('規格分類表示変更完了', [$id]);
227 1
228
        $event = new EventArgs(
229 1
            [
230 1
                'ClassName' => $ClassName,
231
                'TargetClassCategory' => $TargetClassCategory,
232
            ],
233
            $request
234
        );
235
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_PRODUCT_CLASS_CATEGORY_DELETE_COMPLETE, $event);
236 1
237 View Code Duplication
        if ($TargetClassCategory->isVisible()) {
238 1
            $this->addSuccess(trans('admin.common.to_show_complete', ['%name%' => $TargetClassCategory->getName()]), 'admin');
239
        } else {
240 1
            $this->addSuccess(trans('admin.common.to_hide_complete', ['%name%' => $TargetClassCategory->getName()]), 'admin');
241
        }
242 1
243 1
        return $this->redirectToRoute('admin_product_class_category', ['class_name_id' => $ClassName->getId()]);
244
    }
245 1
246
    /**
247 1
     * @Route("/%eccube_admin_route%/product/class_category/sort_no/move", name="admin_product_class_category_sort_no_move", methods={"POST"})
248
     */
249 1 View Code Duplication
    public function moveSortNo(Request $request)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
250
    {
251 1
        if (!$request->isXmlHttpRequest()) {
252
            throw new BadRequestHttpException();
253
        }
254
255
        if ($this->isTokenValid()) {
256
            $sortNos = $request->request->all();
257
            foreach ($sortNos as $categoryId => $sortNo) {
258 1
                $ClassCategory = $this->classCategoryRepository
259
                    ->find($categoryId);
260 1
                $ClassCategory->setSortNo($sortNo);
261 1
                $this->entityManager->persist($ClassCategory);
262 1
            }
263 1
            $this->entityManager->flush();
264 1
265 1
            return new Response('Successful');
266 1
        }
267
    }
268
}
269