Passed
Push — master ( 9b6947...49a0cc )
by Jason
02:46
created

BaseElementObject   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Test Coverage

Coverage 86.79%

Importance

Changes 0
Metric Value
wmc 14
eloc 71
dl 0
loc 206
ccs 46
cts 53
cp 0.8679
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreate() 0 8 2
A getPage() 0 5 2
A canEdit() 0 12 3
A getCMSFields() 0 37 1
A canView() 0 12 3
A canDelete() 0 12 3
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 = 'Title 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
        'Title' => 'Title',
72
    );
73
74
    /**
75
     * @var array
76
     */
77
    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...
78
        'Title' => array(
79
            'title' => 'Headline',
80
        ),
81
        'Content' => array(
82
            'title' => 'Description',
83
        ),
84
    );
85
86
    /**
87
     * @var array
88
     */
89
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
90
        Versioned::class,
91
    ];
92
93
    /**
94
     * Adds Publish button.
95
     *
96
     * @var bool
97
     */
98
    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...
99
100
    /**
101
     * @var string
102
     */
103
    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...
104
105
    /**
106
     * @return FieldList
107
     *
108
     * @throws \Exception
109
     */
110
    public function getCMSFields()
111
    {
112 1
        $this->beforeUpdateCMSFields(function ($fields) {
113
            /** @var FieldList $fields */
114 1
            $fields->replaceField(
115 1
                'ElementLinkID',
116 1
                LinkField::create('ElementLinkID')
117 1
                    ->setTitle('Link')
118 1
                    ->setDescription('Optional. Add a call to action link.')
119
            );
120 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

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