Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

NodeBundle/AdminList/NodeAdminListConfigurator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\NodeBundle\AdminList;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\QueryBuilder;
7
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
8
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
9
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition;
10
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
11
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractDoctrineORMAdminListConfigurator;
12
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\BooleanFilterType;
13
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\DateFilterType;
14
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\StringFilterType;
15
use Kunstmaan\AdminListBundle\AdminList\ListAction\SimpleListAction;
16
use Kunstmaan\NodeBundle\Entity\Node;
17
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
18
19
/**
20
 * NodeAdminListConfigurator
21
 */
22
class NodeAdminListConfigurator extends AbstractDoctrineORMAdminListConfigurator
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $locale;
28
29
    /**
30
     * @var string
31
     */
32
    protected $permission;
33
34
    /**
35
     * @var DomainConfigurationInterface
36
     */
37
    protected $domainConfiguration;
38
39
    /**
40
     * @var bool
41
     */
42
    protected $showAddHomepage;
43
44
    /**
45
     * @var AuthorizationCheckerInterface
46
     */
47
    protected $authorizationChecker;
48
49
    /**
50
     * @param EntityManager $em         The entity
51
     *                                  manager
52
     * @param AclHelper     $aclHelper  The ACL helper
53
     * @param string        $locale     The current
54
     *                                  locale
55
     * @param string        $permission The permission
56
     */
57 View Code Duplication
    public function __construct(EntityManager $em, AclHelper $aclHelper, $locale, $permission, AuthorizationCheckerInterface $authorizationChecker)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        parent::__construct($em, $aclHelper);
60
        $this->locale = $locale;
61
        $this->authorizationChecker = $authorizationChecker;
62
        $this->setPermissionDefinition(
63
            new PermissionDefinition(
64
                [$permission],
65
                'Kunstmaan\NodeBundle\Entity\Node',
66
                'n'
67
            )
68
        );
69
    }
70
71
    public function setDomainConfiguration(DomainConfigurationInterface $domainConfiguration)
72
    {
73
        $this->domainConfiguration = $domainConfiguration;
74
    }
75
76
    /**
77
     * @param bool $showAddHomepage
78
     */
79
    public function setShowAddHomepage($showAddHomepage)
80
    {
81
        $this->showAddHomepage = $showAddHomepage;
82
    }
83
84
    /**
85
     * Build list actions ...
86
     */
87
    public function buildListActions()
88
    {
89
        if (!$this->showAddHomepage) {
90
            return;
91
        }
92
93
        $addHomepageRoute = [
94
            'path' => '',
95
            'attributes' => [
96
                'class' => 'btn btn-default btn--raise-on-hover',
97
                'data-target' => '#add-homepage-modal',
98
                'data-keyboard' => 'true',
99
                'data-toggle' => 'modal',
100
                'type' => 'button',
101
            ],
102
        ];
103
104
        $this->addListAction(
105
            new SimpleListAction(
106
                $addHomepageRoute,
107
                'kuma_node.modal.add_homepage.h',
108
                null,
109
                '@KunstmaanNode/Admin/list_action_button.html.twig'
110
            )
111
        );
112
    }
113
114
    /**
115
     * Configure filters
116
     */
117 View Code Duplication
    public function buildFilters()
118
    {
119
        $this
120
            ->addFilter('title', new StringFilterType('title'), 'kuma_node.admin.list.filter.title')
121
            ->addFilter('created', new DateFilterType('created'), 'kuma_node.admin.list.filter.created_at')
122
            ->addFilter('updated', new DateFilterType('updated'), 'kuma_node.admin.list.filter.updated_at')
123
            ->addFilter('online', new BooleanFilterType('online'), 'kuma_node.admin.list.filter.online');
124
    }
125
126
    /**
127
     * Configure the visible columns
128
     */
129
    public function buildFields()
130
    {
131
        $this
132
            ->addField('title', 'kuma_node.admin.list.header.title', true, '@KunstmaanNode/Admin/title.html.twig')
133
            ->addField('created', 'kuma_node.admin.list.header.created_at', true)
134
            ->addField('updated', 'kuma_node.admin.list.header.updated_at', true)
135
            ->addField('online', 'kuma_node.admin.list.header.online', true, '@KunstmaanNode/Admin/online.html.twig');
136
    }
137
138
    /**
139
     * @param mixed $item
140
     *
141
     * @return array
142
     */
143 View Code Duplication
    public function getEditUrlFor($item)
144
    {
145
        /* @var Node $node */
146
        $node = $item->getNode();
147
148
        return [
149
            'path' => 'KunstmaanNodeBundle_nodes_edit',
150
            'params' => ['id' => $node->getId()],
151
        ];
152
    }
153
154
    /**
155
     * @return bool
156
     */
157
    public function canAdd()
158
    {
159
        return false;
160
    }
161
162
    public function canEdit($item)
163
    {
164
        return $this->authorizationChecker->isGranted(PermissionMap::PERMISSION_EDIT, $item->getNode());
165
    }
166
167
    /**
168
     * Return if current user can delete the specified item
169
     *
170
     * @param array|object $item
171
     *
172
     * @return bool
173
     */
174
    public function canDelete($item)
175
    {
176
        return false;
177
    }
178
179
    /**
180
     * @param object $item
181
     *
182
     * @return array
183
     */
184
    public function getDeleteUrlFor($item)
185
    {
186
        return [];
187
    }
188
189
    /**
190
     * @return string
191
     */
192
    public function getBundleName()
193
    {
194
        return 'KunstmaanNodeBundle';
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function getEntityName()
201
    {
202
        return 'NodeTranslation';
203
    }
204
205
    /**
206
     * Override path convention (because settings is a virtual admin subtree)
207
     *
208
     * @param string $suffix
209
     *
210
     * @return string
211
     */
212
    public function getPathByConvention($suffix = null)
213
    {
214
        if (empty($suffix)) {
215
            return sprintf('%s_nodes', $this->getBundleName());
216
        }
217
218
        return sprintf('%s_nodes_%s', $this->getBundleName(), $suffix);
219
    }
220
221
    /**
222
     * Override controller path (because actions for different entities are
223
     * defined in a single Settings controller).
224
     *
225
     * @return string
226
     */
227
    public function getControllerPath()
228
    {
229
        return 'KunstmaanNodeBundle:NodeAdmin';
230
    }
231
232
    /**
233
     * @param QueryBuilder $queryBuilder The query builder
234
     */
235
    public function adaptQueryBuilder(QueryBuilder $queryBuilder)
236
    {
237
        parent::adaptQueryBuilder($queryBuilder);
238
239
        $queryBuilder
240
            ->select('b,n')
241
            ->innerJoin('b.node', 'n', 'WITH', 'b.node = n.id')
242
            ->andWhere('b.lang = :lang')
243
            ->andWhere('n.deleted = 0')
244
            ->addOrderBy('b.updated', 'DESC')
245
            ->setParameter('lang', $this->locale);
246
247
        if (!$this->domainConfiguration) {
248
            return;
249
        }
250
251
        $rootNode = $this->domainConfiguration->getRootNode();
252
        if (!\is_null($rootNode)) {
253
            $queryBuilder->andWhere('n.lft >= :left')
254
                ->andWhere('n.rgt <= :right')
255
                ->setParameter('left', $rootNode->getLeft())
256
                ->setParameter('right', $rootNode->getRight());
257
        }
258
    }
259
}
260