Completed
Push — master ( bda24f...066aa2 )
by Matthew
03:02
created

BaseElementObject::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
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
        'Title' => array(
80
            'title' => 'Headline',
81
        ),
82
        'Name' => array(
83
            'title' => 'Name',
84
        ),
85
        'Content' => array(
86
            'title' => 'Description',
87
        ),
88
    );
89
90
    /**
91
     * @var array
92
     */
93
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
94
        Versioned::class,
95
    ];
96
97
    /**
98
     * Adds Publish button.
99
     *
100
     * @var bool
101
     */
102
    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...
103
104
    /**
105
     * @var string
106
     */
107
    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...
108
109
    /**
110
     * @return FieldList
111
     *
112
     * @throws \Exception
113
     */
114
    public function getCMSFields()
115
    {
116 1
        $this->beforeUpdateCMSFields(function ($fields) {
117
            /** @var FieldList $fields */
118 1
            $fields->replaceField(
119 1
                'ElementLinkID',
120 1
                LinkField::create('ElementLinkID')
121 1
                    ->setTitle('Link')
122 1
                    ->setDescription('Optional. Add a call to action link.')
123
            );
124 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

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