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