FancyGridExtension::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 2
1
<?php
2
/*
3
 * This file is part of cwdFancyGridBundle
4
 *
5
 * (c)2017 cwd.at GmbH <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Cwd\FancyGridBundle\Twig;
11
use Cwd\FancyGridBundle\Grid\GridInterface;
12
use Symfony\Bundle\FrameworkBundle\Routing\Router;
13
14
/**
15
 * Class FancyGridExtension
16
 * @package Cwd\FancyGridBundle\Twig
17
 * @author Ludwig Ruderstaler <[email protected]>
18
 */
19
class FancyGridExtension extends \Twig_Extension
20
{
21
    protected $jsOptions = [];
22
    protected $router;
23
    protected $license;
24
25
    public function __construct(Router $router, $options = [])
26
    {
27
        if (!isset($options['js_options'])) {
28
            $options['js_options'] = [];
29
        }
30
        if (isset($options['license'])) {
31
            $this->license = $options['license'];
32
        }
33
        $this->jsOptions = $options['js_options'];
34
        $this->router = $router;
35
    }
36
37
    /**
38
     * @return \Twig_SimpleFunction[]
39
     */
40
    public function getFunctions()
41
    {
42
        return array(
43
            new \Twig_SimpleFunction('fancygrid', [$this, 'fancygrid'], [
44
                'needs_environment' => true,
45
                'is_safe' => ['html'],
46
            ]),
47
        );
48
    }
49
50
    /**
51
     * @param \Twig_Environment $twig
52
     * @param GridInterface     $grid
53
     * @param array             $options
54
     *
55
     * @return string
56
     */
57
    public function fancygrid($twig, $grid, array $options = [])
58
    {
59
        $options = array_merge($options, $this->jsOptions);
60
        $options['renderTo'] = $grid->getId();
61
        $options['paging'] = [
62
            'pageSize' => $grid->getOption('limit'),
63
            'pageSizeData' => [5, 10, 20, 50, 100],
64
            'refreshButton' => true,
65
        ];
66
        $options['columns'] = $grid->getColumnDefinition();
67
        $options['data'] = [
68
            'remoteSort' => true,
69
            'remoteFilter' => true,
70
            'proxy' => [
71
                'url' => $this->router->generate($grid->getOption('data_route'), $grid->getOption('data_route_options')),
72
            ],
73
        ];
74
75
76
        return $twig->render('CwdFancyGridBundle::grid.html.twig', [
77
            'grid' => $grid,
78
            'jsOptions' => $options,
79
            'license' => $this->license
80
        ]);
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getName()
87
    {
88
        return 'cwd_fancygrid.twig_extension.fancygrid';
89
    }
90
}
91
92