Completed
Pull Request — 2.x (#535)
by David
02:03
created

ContentAdmin   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 9
dl 0
loc 131
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setManagerRegistry() 0 4 1
A getExportFormats() 0 4 1
A toString() 0 6 3
A configureShowFields() 0 37 1
A configureListFields() 0 6 1
A configureFormFields() 0 54 1
A configureDatagridFilters() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
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 Sonata\DoctrinePHPCRAdminBundle\Tests\Fixtures\App\Admin;
15
16
use Doctrine\Bundle\PHPCRBundle\Form\DataTransformer\DocumentToPathTransformer;
17
use Doctrine\Common\Persistence\ManagerRegistry;
18
use Sonata\AdminBundle\Datagrid\DatagridMapper;
19
use Sonata\AdminBundle\Datagrid\ListMapper;
20
use Sonata\AdminBundle\Form\FormMapper;
21
use Sonata\AdminBundle\Form\Type\ModelType;
22
use Sonata\AdminBundle\Show\ShowMapper;
23
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
24
use Sonata\CoreBundle\Form\Type\CollectionType;
25
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
26
use Sonata\DoctrinePHPCRAdminBundle\Tests\Fixtures\App\Document\Content;
27
use Symfony\Component\Form\Extension\Core\Type\TextType;
28
use Symfony\Cmf\Bundle\TreeBrowserBundle\Form\Type\TreeSelectType;
29
30
/**
31
 * @author Maximilian Berghoff <[email protected]>
32
 */
33
class ContentAdmin extends Admin
34
{
35
    /**
36
     * @var ManagerRegistry
37
     */
38
    private $managerRegistry;
39
40
    public function setManagerRegistry(ManagerRegistry $managerRegistry): void
41
    {
42
        $this->managerRegistry = $managerRegistry;
43
    }
44
45
    public function getExportFormats()
46
    {
47
        return [];
48
    }
49
50
    public function toString($object)
51
    {
52
        return $object instanceof Content && $object->getTitle()
53
            ? $object->getTitle()
54
            : $this->trans('link_add', [], 'SonataAdminBundle');
55
    }
56
57
    public function configureShowFields(ShowMapper $showMapper): void
58
    {
59
        $showMapper
60
            ->tab('General') // the tab call is optional
61
                ->with('Content', [
62
                    'class' => 'col-md-8',
63
                    'box_class' => 'box box-solid box-danger',
64
                    'description' => 'Main Content',
65
                ])
66
                    ->add('title')
67
                    ->add('name')
68
                ->end()
69
                ->with('References')
70
                    ->add('children', null, [
71
                        'route' => ['name' => 'edit', 'parameters' => []],
72
                        'associated_property' => 'id',
73
                        'admin_code' => 'sonata_admin_doctrine_phpcr.test.admin',
74
                        ])
75
                    ->add('child', null, [
76
                        'route' => ['name' => 'edit', 'parameters' => []],
77
                        'associated_property' => 'id',
78
                        'admin_code' => 'sonata_admin_doctrine_phpcr.test.admin',
79
                    ])
80
                    ->add(
81
                        'singleRoute',
82
                        null,
83
                        ['route' => ['name' => 'edit', 'parameters' => []], 'associated_property' => 'id']
84
                    )
85
                    ->add(
86
                        'routes',
87
                        null,
88
                        ['route' => ['name' => 'edit', 'parameters' => []], 'associated_property' => 'id']
89
                    )
90
                ->end()
91
            ->end()
92
        ;
93
    }
94
95
    protected function configureListFields(ListMapper $listMapper): void
96
    {
97
        $listMapper
98
            ->addIdentifier('id')
99
            ->add('title');
100
    }
101
102
    protected function configureFormFields(FormMapper $formMapper): void
103
    {
104
        $formMapper
105
            ->with('form.group_general')
106
            ->add('name', TextType::class)
107
            ->add('title', TextType::class)
108
            ->add(
109
                'children',
110
                CollectionType::class,
111
                ['label' => false, 'type_options' => [
112
                    'delete' => true,
113
                    'delete_options' => [
114
                        'type' => CheckboxType::class,
115
                        'type_options' => ['required' => false, 'mapped' => false],
116
                    ], ],
117
                ],
118
                ['edit' => 'inline', 'inline' => 'table', 'admin_code' => 'sonata_admin_doctrine_phpcr.test.admin']
119
            )
120
            ->add(
121
                'routes',
122
                ModelType::class,
123
                ['property' => 'title', 'multiple' => true, 'expanded' => false]
124
            )
125
            ->add('parentDocument',
126
                  TreeSelectType::class, [
127
                'widget' => 'browser',
128
                'root_node' => $this->getRootPath(),
129
            ])
130
            ->add(
131
                'child',
132
                ModelType::class,
133
                [
134
                    'property' => 'title',
135
                    'class' => Content::class,
136
                    'btn_catalogue' => 'List',
137
                    'required' => false,
138
                ],
139
                ['admin_code' => 'sonata_admin_doctrine_phpcr.test.admin']
140
            )
141
            ->add('singleRoute',
142
                  TreeSelectType::class, [
143
                'widget' => 'browser',
144
                'root_node' => $this->getRootPath(),
145
            ])
146
            ->end();
147
148
        $formMapper->getFormBuilder()->get('parentDocument')->addModelTransformer(new DocumentToPathTransformer(
149
            $this->managerRegistry->getManagerForClass($this->getClass())
150
        ));
151
152
        $formMapper->getFormBuilder()->get('singleRoute')->addModelTransformer(new DocumentToPathTransformer(
153
            $this->managerRegistry->getManagerForClass($this->getClass())
154
        ));
155
    }
156
157
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
158
    {
159
        $datagridMapper
160
            ->add('title', 'doctrine_phpcr_string')
161
            ->add('name', 'doctrine_phpcr_nodename');
162
    }
163
}
164