Completed
Pull Request — master (#4)
by Matthew
03:10 queued 53s
created

BaseElementObject::getCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 26
cts 26
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dynamic\BaseObject\Model;
4
5
use DNADesign\Elemental\Forms\TextCheckboxGroupField;
6
use DNADesign\Elemental\Models\BaseElement;
7
use Sheadawson\Linkable\Forms\LinkField;
8
use Sheadawson\Linkable\Models\Link;
9
use SilverStripe\Assets\Image;
10
use SilverStripe\CMS\Model\SiteTree;
11
use SilverStripe\Control\Director;
12
use SilverStripe\Forms\CheckboxField;
13
use SilverStripe\Forms\FieldList;
14
use SilverStripe\Forms\TextField;
15
use SilverStripe\ORM\DataObject;
16
use SilverStripe\ORM\ValidationResult;
17
use SilverStripe\Security\Permission;
18
use SilverStripe\Versioned\Versioned;
19
20
/**
21
 * Class BaseElementObject.
22
 *
23
 * @property string $Title
24
 * @property booelan $ShowTitle
25
 * @property string $Content
26
 *
27
 * @property int $ImageID
28
 * @property int $ElementLinkID
29
 *
30
 * @method Image Image()
31
 * @method Link ElementLink()
32
 *
33
 * @mixin Versioned
34
 */
35
class BaseElementObject extends DataObject
36
{
37
    /**
38
     * @var array
39
     */
40
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
41
        'Title' => 'Varchar(255)',
42
        'ShowTitle' => 'Boolean',
43
        'Content' => 'HTMLText',
44
    );
45
46
    /**
47
     * @var array
48
     */
49
    private static $has_one = array(
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
50
        'Image' => Image::class,
51
        'ElementLink' => Link::class,
52
    );
53
54
    /**
55
     * @var array
56
     */
57
    private static $owns = array(
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
58
        'Image',
59
    );
60
61
    /**
62
     * @var string
63
     */
64
    private static $default_sort = 'Name ASC';
0 ignored issues
show
introduced by
The private property $default_sort is not used, and could be removed.
Loading history...
65
66
    /**
67
     * @var array
68
     */
69
    private static $summary_fields = array(
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
70
        'Image.CMSThumbnail' => 'Image',
71
        'Name' => 'Name',
72
        'Title' => 'Title',
73
    );
74
75
    /**
76
     * @var array
77
     */
78
    private static $searchable_fields = array(
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
79
        'Name' => 'Name',
80
        'Title' => 'Headline',
81
        'Content' => 'Description',
82
    );
83
84
    /**
85
     * @var array
86
     */
87
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
88
        Versioned::class,
89
    ];
90
91
    /**
92
     * Adds Publish button.
93
     *
94
     * @var bool
95
     */
96
    private static $versioned_gridfield_extensions = true;
0 ignored issues
show
introduced by
The private property $versioned_gridfield_extensions is not used, and could be removed.
Loading history...
97
98
    /**
99
     * @var string
100
     */
101
    private static $table_name = 'BaseElementObject';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
102
103
    /**
104
     * @return FieldList
105
     *
106
     * @throws \Exception
107
     */
108
    public function getCMSFields()
109
    {
110 1
        $this->beforeUpdateCMSFields(function ($fields) {
111
            /** @var FieldList $fields */
112 1
            $fields->replaceField(
113 1
                'ElementLinkID',
114 1
                LinkField::create('ElementLinkID')
115 1
                    ->setTitle('Link')
116 1
                    ->setDescription('Optional. Add a call to action link.')
117
            );
118 1
            $fields->insertBefore($fields->dataFieldByName('ElementLinkID'), 'Content');
0 ignored issues
show
Bug introduced by
'Content' of type string is incompatible with the type SilverStripe\Forms\FormField expected by parameter $item of SilverStripe\Forms\FieldList::insertBefore(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
            $fields->insertBefore($fields->dataFieldByName('ElementLinkID'), /** @scrutinizer ignore-type */ 'Content');
Loading history...
119
120 1
            $fields->removeByName(array(
121 1
                'ElementFeaturesID',
122
                'Sort',
123
            ));
124
125
            // Add a combined field for "Title" and "Displayed" checkbox in a Bootstrap input group
126 1
            $fields->removeByName('ShowTitle');
127 1
            $fields->replaceField(
128 1
                'Title',
129 1
                TextCheckboxGroupField::create(
130 1
                    TextField::create('Title', _t(BaseElement::class . '.TitleLabel', 'Title (displayed if checked)')),
131 1
                    CheckboxField::create('ShowTitle', _t(BaseElement::class . '.ShowTitleLabel', 'Displayed'))
132 1
                )->setName('TitleAndDisplayed')
133
            );
134
            // $fields->dataFieldByName('Title')->setDescription('Optional. Display a Title with this feature.');
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
135
136 1
            $image = $fields->dataFieldByName('Image')
137 1
                ->setDescription('Optional. Display an image with this feature.')
138 1
                ->setFolderName('Uploads/Elements/Objects');
139 1
            $fields->insertBefore($image, 'Content');
140
141 1
            $fields->dataFieldByName('Content')
142 1
                ->setTitle('Description')
143 1
                ->setDescription('Optional. Set a description for this feature.')
144 1
                ->setRows(8);
145 1
        });
146
147 1
        return parent::getCMSFields();
148
    }
149
150
    /**
151
     * @return SiteTree|null
152
     */
153 4
    public function getPage()
154
    {
155 4
        $page = Director::get_current_page();
156
        // because $page can be a SiteTree or Controller
157 4
        return $page instanceof SiteTree ? $page : null;
158
    }
159
160
    /**
161
     * Basic permissions, defaults to page perms where possible.
162
     *
163
     * @param \SilverStripe\Security\Member|null $member
164
     * @return boolean
165
     */
166 1
    public function canView($member = null)
167
    {
168 1
        $extended = $this->extendedCan(__FUNCTION__, $member);
169 1
        if ($extended !== null) {
170
            return $extended;
171
        }
172
173 1
        if ($page = $this->getPage()) {
174
            return $page->canView($member);
175
        }
176
177 1
        return Permission::check('CMS_ACCESS', 'any', $member);
178
    }
179
180
    /**
181
     * Basic permissions, defaults to page perms where possible.
182
     *
183
     * @param \SilverStripe\Security\Member|null $member
184
     *
185
     * @return boolean
186
     */
187 1
    public function canEdit($member = null)
188
    {
189 1
        $extended = $this->extendedCan(__FUNCTION__, $member);
190 1
        if ($extended !== null) {
191
            return $extended;
192
        }
193
194 1
        if ($page = $this->getPage()) {
195
            return $page->canEdit($member);
196
        }
197
198 1
        return Permission::check('CMS_ACCESS', 'any', $member);
199
    }
200
201
    /**
202
     * Basic permissions, defaults to page perms where possible.
203
     *
204
     * Uses archive not delete so that current stage is respected i.e if a
205
     * element is not published, then it can be deleted by someone who doesn't
206
     * have publishing permissions.
207
     *
208
     * @param \SilverStripe\Security\Member|null $member
209
     *
210
     * @return boolean
211
     */
212 1
    public function canDelete($member = null)
213
    {
214 1
        $extended = $this->extendedCan(__FUNCTION__, $member);
215 1
        if ($extended !== null) {
216
            return $extended;
217
        }
218
219 1
        if ($page = $this->getPage()) {
220
            return $page->canArchive($member);
221
        }
222
223 1
        return Permission::check('CMS_ACCESS', 'any', $member);
224
    }
225
226
    /**
227
     * Basic permissions, defaults to page perms where possible.
228
     *
229
     * @param \SilverStripe\Security\Member|null $member
230
     * @param array $context
231
     *
232
     * @return boolean
233
     */
234 1
    public function canCreate($member = null, $context = array())
235
    {
236 1
        $extended = $this->extendedCan(__FUNCTION__, $member);
237 1
        if ($extended !== null) {
238
            return $extended;
239
        }
240
241 1
        return Permission::check('CMS_ACCESS', 'any', $member);
242
    }
243
}
244