Completed
Push — dev ( 5c06f5...dcd39b )
by Arnaud
09:19
created

AdminExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 2
1
<?php
2
3
namespace LAG\AdminBundle\Twig;
4
5
use LAG\AdminBundle\Admin\Configuration\ApplicationConfiguration;
6
use LAG\AdminBundle\Field\Field;
7
use LAG\AdminBundle\Field\EntityFieldInterface;
8
use LAG\AdminBundle\Field\FieldInterface;
9
use Symfony\Component\DependencyInjection\Container;
10
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
11
use Symfony\Component\PropertyAccess\PropertyAccess;
12
use Symfony\Component\Translation\TranslatorInterface;
13
use Twig_Extension;
14
use Twig_SimpleFilter;
15
use Twig_SimpleFunction;
16
use Symfony\Component\Routing\RouterInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * Class AdminExtension.
21
 *
22
 * Admin utils functions for twig
23
 */
24
class AdminExtension extends Twig_Extension
25
{
26
    /**
27
     * @var ApplicationConfiguration
28
     */
29
    protected $configuration;
30
31
    /**
32
     * @var RouterInterface
33
     */
34
    protected $router;
35
36
    /**
37
     * @var TranslatorInterface
38
     */
39
    protected $translator;
40
41
    /**
42
     * AdminExtension constructor.
43
     *
44
     * @param RouterInterface $router
45
     * @param TranslatorInterface $translator
46
     * @param ApplicationConfiguration $configuration
47
     */
48
    public function __construct(RouterInterface $router, TranslatorInterface $translator, ApplicationConfiguration $configuration)
49
    {
50
        $this->router = $router;
51
        $this->translator = $translator;
52
        $this->configuration = $configuration;
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function getFunctions()
59
    {
60
        return [
61
            new Twig_SimpleFunction('getSortColumnUrl', [$this, 'getSortColumnUrl']),
62
            new Twig_SimpleFunction('getSortColumnIconClass', [$this, 'getSortColumnIconClass']),
63
            new Twig_SimpleFunction('field', [$this, 'field']),
64
            new Twig_SimpleFunction('field_title', [$this, 'fieldTitle']),
65
            new Twig_SimpleFunction('route_parameters', [$this, 'routeParameters']),
66
        ];
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function getFilters()
73
    {
74
        return [
75
            new Twig_SimpleFilter('camelize', [$this, 'camelize']),
76
        ];
77
    }
78
79
    /**
80
     * Return sort column url, with existing request parameters, according to field name.
81
     *
82
     * @param Request $request
83
     * @param $fieldName
84
     *
85
     * @return string
86
     *                TODO rename function
87
     */
88
    public function getSortColumnUrl(Request $request, $fieldName)
89
    {
90
        // get query string to not delete existing parameters
91
        $queryString = $request->query->all();
92
        $queryString['sort'] = $fieldName;
93
94
        if (array_key_exists('order', $queryString)) {
95
            // sort by opposite sorting than current
96
            $sort = $queryString['order'] == 'ASC' ? 'DESC' : 'ASC';
97
            $queryString['order'] = $sort;
98
        } else {
99
            // if no order was provided, it means that list is sorted by default order (ASC), so we must sort by DESC
100
            $queryString['order'] = 'DESC';
101
        }
102
103
        return $this
104
            ->router
105
            ->generate($request->get('_route'), $queryString);
106
    }
107
108
    /**
109
     * Return an array of query string parameters, updated with sort field name.
110
     *
111
     * @param ParameterBagInterface $parameters
112
     * @param $fieldName
113
     * @return array
114
     */
115
    public function getOrderQueryString(ParameterBagInterface $parameters, $fieldName)
116
    {
117
        $parameters->set('sort', $fieldName);
118
119
        if ($parameters->has('order')) {
120
            // sort by opposite order
121
            $order = $parameters->get('order') == 'ASC' ? 'DESC' : 'ASC';
122
            $parameters->set('order', $order);
123
        } else {
124
            // if no order was provided, it means that list is sorted by default order (ASC), so we must sort by DESC
125
            $parameters->set('order', 'DESC');
126
        }
127
        return $parameters->all();
128
    }
129
130
    /**
131
     * @param null $order
132
     * @param $fieldName
133
     * @param $sort
134
     * @return string
135
     */
136
    public function getSortColumnIconClass($order = null, $fieldName, $sort)
137
    {
138
        // when no order is provided, no icon should be displayed
139
        $class = '';
140
141
        if ($fieldName == $sort) {
142
            if ($order == 'ASC') {
143
                $class = 'fa fa-sort-asc';
144
            } elseif ($order == 'DESC') {
145
                $class = 'fa fa-sort-desc';
146
            }
147
        }
148
149
        return $class;
150
    }
151
152
    /**
153
     * Render a field of an entity.
154
     *
155
     * @param FieldInterface $field
156
     * @param $entity
157
     *
158
     * @return mixed
159
     */
160
    public function field(FieldInterface $field, $entity)
161
    {
162
        $accessor = PropertyAccess::createPropertyAccessorBuilder()
163
            ->enableMagicCall()
164
            ->getPropertyAccessor();
165
        $value = null;
166
        // if name starts with a underscore, it is a custom field, not mapped to the entity
167 View Code Duplication
        if (substr($field->getName(), 0, 1) != '_') {
168
            // get raw value from object
169
            $value = $accessor->getValue($entity, $field->getName());
170
        }
171
        if ($field instanceof EntityFieldInterface) {
172
            $field->setEntity($entity);
173
        }
174
        $render = $field->render($value);
175
176
        return $render;
177
    }
178
179
    /**
180
     * @param $fieldName
181
     * @param null $adminName
182
     * @return string
183
     */
184
    public function fieldTitle($fieldName, $adminName = null)
185
    {
186
        if ($this->configuration->useTranslation()) {
187
            $title = $this
188
                ->translator
189
                ->trans($this->configuration->getTranslationKey($fieldName, $adminName));
190
        } else {
191
            $title = $this->camelize($fieldName);
192
        }
193
194
        return $title;
195
    }
196
197
    /**
198
     * @param array $parameters
199
     * @param $entity
200
     * @return array
201
     */
202
    public function routeParameters(array $parameters, $entity)
203
    {
204
        $accessor = PropertyAccess::createPropertyAccessor();
205
        $routeParameters = [];
206
207
        foreach ($parameters as $parameterName => $fieldName) {
208
            if (is_array($fieldName) && !count($fieldName)) {
209
                $fieldName = $parameterName;
210
            }
211
            $routeParameters[$parameterName] = $accessor->getValue($entity, $fieldName);
212
        }
213
214
        return $routeParameters;
215
    }
216
217
    /**
218
     * Camelize a string (using Container camelize method)
219
     *
220
     * @param $string
221
     * @return string
222
     */
223
    public function camelize($string)
224
    {
225
        return Container::camelize($string);
226
    }
227
228
    /**
229
     * Returns the name of the extension.
230
     *
231
     * @return string The extension name
232
     */
233
    public function getName()
234
    {
235
        return 'lag.admin';
236
    }
237
}
238