Completed
Pull Request — 4.0 (#4083)
by Kentaro
06:02
created

CssController::index()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 1
dl 0
loc 30
rs 8.8177
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.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\Content;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Form\Type\Admin\MainEditType;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
19
use Symfony\Component\Filesystem\Exception\IOException;
20
use Symfony\Component\Filesystem\Filesystem;
21
use Symfony\Component\Form\Extension\Core\Type\FormType;
22
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\Routing\Annotation\Route;
25
26
class CssController extends AbstractController
27
{
28
    /**
29
     * @Route("/%eccube_admin_route%/content/css", name="admin_content_css")
30
     * @Template("@admin/Content/css.twig")
31
     */
32
    public function index(Request $request)
33
    {
34
        $builder = $this->formFactory
35
            ->createBuilder(FormType::class)
36
            ->add('css', TextareaType::class, [
37
                'required' => false
38
            ]);
39
        $form = $builder->getForm();
40
41
        $cssPath = $this->getParameter('eccube_html_dir').'/user_data/assets/css/customize.css';
42
        if (file_exists($cssPath) && is_writable($cssPath)) {
43
            $form->get('css')->setData(file_get_contents($cssPath));
44
        }
45
46
        $form->handleRequest($request);
47
        if ($form->isSubmitted() && $form->isValid()) {
48
            $fs = new Filesystem();
49
            try {
50
                $fs->dumpFile($cssPath, $form->get('css')->getData());
51
                $this->addSuccess('admin.common.save_complete', 'admin');
52
            } catch (IOException $e) {
53
                $message = trans('admin.common.save_error');
54
                $this->addError($message, 'admin');
55
                log_error($message, [$cssPath, $e]);
56
            }
57
        }
58
        return [
59
            'form' => $form->createView()
60
        ];
61
    }
62
}
63