Completed
Pull Request — 2.x (#521)
by Andrey F.
05:59
created

ContentAdmin::configureShowFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

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