BaseAssetAdmin::disableCodeMirror()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace NVBooster\PHPCRAssetsBundle\Admin;
3
4
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
5
use Sonata\AdminBundle\Datagrid\DatagridMapper;
6
use Sonata\AdminBundle\Datagrid\ListMapper;
7
use Sonata\AdminBundle\Form\FormMapper;
8
use NVBooster\PHPCRAssetsBundle\Asset\BaseAsset;
9
use NVBooster\PHPCRAssetsBundle\Asset\CssAsset;
10
use NVBooster\PHPCRAssetsBundle\Asset\JsAsset;
11
use PHPCR\Util\UUIDHelper;
12
use PHPCR\Util\PathHelper;
13
14
/**
15
 * AssetAdmin
16
 *
17
 * @author nvb <[email protected]>
18
 */
19
class BaseAssetAdmin extends Admin
20
{
21
    /**
22
     * {@inheritDoc}
23
     *
24
     * @var string
25
     */
26
    protected $baseRouteName = 'admin_cmf_assets_baseasset';
27
28
    /**
29
     * {@inheritDoc}
30
     *
31
     * @var string
32
     */
33
    protected $baseRoutePattern = '/cms/assets/baseasset';
34
35
    /**
36
     * @var boolean
37
     */
38
    protected $codeMirrorEnabled = false;
39
40
    /**
41
     * @var array
42
     */
43
    protected $codeMirrorParams = array();
44
45
    /**
46
     * {@inheritDoc}
47
     *
48
     * @see \Sonata\AdminBundle\Admin\Admin::configureListFields()
49
     */
50
    protected function configureListFields(ListMapper $listMapper)
51
    {
52
        $listMapper
53
            ->addIdentifier('id', 'text')
54
            ->addIdentifier('name', 'text')
55
            ->addIdentifier('extension', 'text');
56
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     *
62
     * @see \Sonata\AdminBundle\Admin\Admin::configureFormFields()
63
     */
64
    protected function configureFormFields(FormMapper $formMapper)
65
    {
66
        $group = $formMapper
67
            ->with('General')
68
                ->add('name', 'text');
69
70
        $asset = $this->getSubject();
71
72
        if ($this->codeMirrorEnabled) {
73
            $codeMirrorParams = $this->getCodeMirrorParams();
74
            if ($asset instanceof JsAsset) {
75
                $codeMirrorParams['lineNumbers'] = true;
76
                $codeMirrorParams['mode'] = 'javascript';
77
            } elseif ($asset instanceof CssAsset) {
78
                $codeMirrorParams['lineNumbers'] = true;
79
                $codeMirrorParams['mode'] = 'css';
80
            }
81
82
            $group->add('content', 'textasset', array(
83
                'required' => false,
84
                'codemirror' => $codeMirrorParams
85
            ));
86
        } else {
87
            $group->add('content', 'textarea');
88
        }
89
        $group->end();
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     *
95
     * @see \Sonata\AdminBundle\Admin\Admin::configureDatagridFilters()
96
     */
97
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
98
    {
99
        $datagridMapper->add('name', 'doctrine_phpcr_string');
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     *
105
     * @see \Sonata\AdminBundle\Admin\Admin::getExportFormats()
106
     */
107
    public function getExportFormats()
108
    {
109
        return array();
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     *
115
     * @see \Sonata\DoctrinePHPCRAdminBundle\Admin\Admin::toString()
116
     */
117
    public function toString($object)
118
    {
119
        return $object instanceof BaseAsset && $object->getName()
120
        ? $object->getName()
121
        : $this->trans('link_add', array(), 'SonataAdminBundle');
122
    }
123
124
    /**
125
     * Replaces textarea with code mirror input
126
     */
127
    public function enableCodeMirror()
128
    {
129
        $this->codeMirrorEnabled = true;
130
    }
131
132
    /**
133
     * Uses plain textarea
134
     */
135
    public function disableCodeMirror()
136
    {
137
        $this->codeMirrorEnabled = false;
138
    }
139
140
    /**
141
     * @return array
142
     */
143
    public function getCodeMirrorParams()
144
    {
145
        return $this->codeMirrorParams;
146
    }
147
148
    /**
149
     * @param $params array
150
     */
151
    public function setCodeMirrorParams(array $params)
152
    {
153
        $this->codeMirrorParams = $params;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     * @see \Sonata\AdminBundle\Admin\Admin::getNewInstance()
159
     */
160
    public function getNewInstance()
161
    {
162
        /* @var $object BaseAsset */
163
        $object = parent::getNewInstance();
164
        $object->setParentDocument($this->getModelManager()->find(null, $this->getRootPath()));
165
166
        return $object;
167
    }
168
169
    /**
170
     * @{inheritdocs}
171
     *
172
     * @see https://github.com/sonata-project/SonataDoctrinePhpcrAdminBundle/issues/354
173
     */
174
    public function getSubject()
175
    {
176
        if ($this->subject === null && $this->request) {
177
            $id = $this->request->get($this->getIdParameter());
178
            if (!preg_match('#^[0-9A-Za-z/\-_]+$#', $id)) {
179
                $this->subject = false;
180
            } else {
181
                if (!UUIDHelper::isUUID($id)) {
182
                    $id = PathHelper::absolutizePath($id, '/');
183
                }
184
                $this->subject = $this->getModelManager()->find(null, $id);
185
            }
186
        }
187
188
        return $this->subject;
189
    }
190
191
}