Completed
Push — master ( d67f41...79042b )
by Adam
07:27
created

CategoryController::editAction()   B

Complexity

Conditions 4
Paths 4

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 24
Code Lines 14

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\CatalogBundle\Controller\Admin;
14
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use WellCommerce\Bundle\CatalogBundle\Entity\Category;
19
use WellCommerce\Bundle\CatalogBundle\Manager\CategoryManager;
20
use WellCommerce\Bundle\CatalogBundle\Request\Storage\CategoryStorageInterface;
21
use WellCommerce\Bundle\CoreBundle\Controller\Admin\AbstractAdminController;
22
use WellCommerce\Component\Form\Elements\FormInterface;
23
24
/**
25
 * Class CategoryController
26
 *
27
 * @author  Adam Piotrowski <[email protected]>
28
 */
29
class CategoryController extends AbstractAdminController
30
{
31
    public function indexAction(): Response
32
    {
33
        $categories = $this->getManager()->getRepository()->getCollection();
34
        $tree       = $this->createCategoryTreeForm();
35
        
36
        if ($categories->count()) {
37
            $category = $categories->first();
38
            
39
            return $this->redirectToAction('edit', [
40
                'id' => $category->getId(),
41
            ]);
42
        }
43
        
44
        return $this->displayTemplate('index', [
45
            'tree' => $tree,
46
        ]);
47
    }
48
    
49
    public function addAction(Request $request): Response
50
    {
51
        if (!$request->isXmlHttpRequest()) {
52
            return $this->redirectToAction('index');
53
        }
54
        
55
        $categoriesName = (string)$request->request->get('name');
56
        $parentCategory = (int)$request->request->get('parent');
57
        $shop           = $this->getShopStorage()->getCurrentShop();
58
        $category       = $this->getManager()->quickAddCategory($categoriesName, $parentCategory, $shop);
59
        
60
        return $this->jsonResponse([
61
            'id' => $category->getId(),
62
        ]);
63
    }
64
    
65
    public function editAction(int $id): Response
66
    {
67
        $category = $this->getManager()->getRepository()->find($id);
68
        if (!$category instanceof Category) {
69
            return $this->redirectToAction('index');
70
        }
71
        
72
        $this->getCategoryStorage()->setCurrentCategory($category);
73
        $form = $this->formBuilder->createForm($category);
74
        
75
        if ($form->handleRequest()->isSubmitted()) {
76
            if ($form->isValid()) {
77
                $this->getManager()->updateResource($category);
78
            }
79
            
80
            return $this->createFormDefaultJsonResponse($form);
81
        }
82
        
83
        return $this->displayTemplate('edit', [
84
            'tree'     => $this->createCategoryTreeForm(),
85
            'form'     => $form,
86
            'resource' => $category,
87
        ]);
88
    }
89
    
90
    protected function createCategoryTreeForm(): FormInterface
91
    {
92
        return $this->get('category_tree.form_builder.admin')->createForm(null, [
93
            'class' => 'category-select',
94
        ]);
95
    }
96
    
97
    public function ajaxGetChildrenAction(Request $request): JsonResponse
98
    {
99
        $parentId = $request->request->get('parent');
100
        $parent   = $this->manager->getRepository()->find($parentId);
101
        $items    = $this->get('category.datagrid.filter')->getOptions($parent);
102
        
103
        return $this->jsonResponse($items);
104
    }
105
    
106
    protected function getManager(): CategoryManager
107
    {
108
        return parent::getManager();
109
    }
110
    
111
    protected function getCategoryStorage(): CategoryStorageInterface
112
    {
113
        return $this->get('category.storage');
114
    }
115
}
116