Completed
Pull Request — master (#16)
by Jason
03:54 queued 01:33
created

BaseElementObject::canDelete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.2098
1
<?php
2
3
namespace Dynamic\BaseObject\Model;
4
5
use DNADesign\Elemental\Forms\TextCheckboxGroupField;
6
use DNADesign\Elemental\Models\BaseElement;
7
use gorriecoe\Link\Models\Link;
8
use gorriecoe\LinkField\LinkField;
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 boolean $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('ElementLink', 'Link', $this)
117 1
                    ->setDescription('Optional. Add a call to action link.')
118
            );
119 1
            $fields->insertBefore($fields->dataFieldByName('ElementLink'), '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

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