ACPOptionController::setBreadcrumbs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
/*
4
 * (c) Jim Martens <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace TwoMartens\Bundle\CoreBundle\Controller;
11
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use TwoMartens\Bundle\CoreBundle\Form\Type\OptionConfigurationType;
15
use TwoMartens\Bundle\CoreBundle\Model\Breadcrumb;
16
use TwoMartens\Bundle\CoreBundle\Model\OptionCategory;
17
use TwoMartens\Bundle\CoreBundle\Option\OptionServiceInterface;
18
19
/**
20
 * Manages the routes for the option system.
21
 *
22
 * @author    Jim Martens <[email protected]>
23
 * @copyright 2013-2015 Jim Martens
24
 */
25
class ACPOptionController extends AbstractACPController
26
{
27
    /**
28
     * the global options
29
     * @var OptionCategory
30
     */
31
    private $options;
32
33
    /**
34
     * Shows the options main page.
35
     *
36
     * @param Request $request
37
     *
38
     * @return Response
39
     */
40
    public function indexAction(Request $request)
41
    {
42
        /** @var OptionServiceInterface $optionService */
43
        $optionService = $this->container->get('twomartens.core.option');
44
        $this->options = $optionService->getOptions();
45
46
        $form = $this->createForm(
47
            OptionConfigurationType::class,
48
            $this->options
49
        );
50
51
        $form->handleRequest($request);
52
        $this->assignVariables();
53
54
        if ($form->isValid()) {
55
            // save changed options to file
56
            $submittedData = $request->request->all();
57
            $submittedData = $submittedData['option_configuration'];
58
59
            $categories = $this->options->getCategories();
60
            foreach ($categories as $category) {
61
                $categoryName = $category->getName();
62
                $options = $category->getOptions();
63
64
                foreach ($options as $option) {
65
                    $optionName = $option->getName();
66
                    $optionType = $option->getType();
67
                    $fieldName = str_replace('.', '_', $categoryName) . '_' . $optionName;
68
                    if (!isset($submittedData[$fieldName])) {
69
                        // should be the case only for checkbox
70
                        $fieldValue = false;
71
                    } else {
72
                        $fieldValue = $submittedData[$fieldName];
73
                    }
74
                    settype($fieldValue, $optionType);
75
                    $option->setValue($fieldValue);
76
                }
77
            }
78
79
            $optionService->setOptions($this->options);
80
            $this->templateVariables['success'] = true;
81
        }
82
83
        $this->templateVariables['form'] = $form->createView();
84
85
        return $this->render(
86
            'TwoMartensCoreBundle:ACPOption:index.html.twig',
87
            $this->templateVariables
88
        );
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    protected function setBreadcrumbs()
95
    {
96
        $systemBreadcrumb = new Breadcrumb(
97
            'acp.system',
98
            $this->get('translator')->trans('acp.breadcrumb.system', [], 'TwoMartensCoreBundle')
99
        );
100
        $optionsBreadcrumb = new Breadcrumb(
101
            'acp.system.options',
102
            $this->get('translator')->trans('acp.breadcrumb.options', [], 'TwoMartensCoreBundle')
103
        );
104
        $optionsBreadcrumb->activate();
105
        $this->breadcrumbs = [
106
            $systemBreadcrumb,
107
            $optionsBreadcrumb
108
        ];
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 View Code Duplication
    protected function assignVariables()
115
    {
116
        $this->templateVariables = [
117
            'area' => [
118
                'showBreadcrumbs' => true,
119
                'title' => $this->get('translator')->trans('acp.options', [], 'TwoMartensCoreBundle')
120
            ],
121
            'siteTitle' => $this->get('translator')->trans(
122
                'acp.siteTitle',
123
                ['globalTitle' => 'CoreBundle Test'],
124
                'TwoMartensCoreBundle'
125
            ),
126
            'navigation' => [
127
                'active' => 'system'
128
            ],
129
            'success' => false
130
        ];
131
        parent::assignVariables();
132
    }
133
}
134