Completed
Pull Request — master (#2)
by Matthew
02:24
created

BaseElementObject::canCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2.032
1
<?php
2
3
namespace Dynamic\BaseObject\Model;
4
5
use Sheadawson\Linkable\Forms\LinkField;
6
use Sheadawson\Linkable\Models\Link;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\CMS\Model\SiteTree;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\ORM\ValidationResult;
13
use SilverStripe\Security\Permission;
14
use SilverStripe\Versioned\Versioned;
15
16
/**
17
 * Class BaseElementObject.
18
 *
19
 * @property string $Name
20
 * @property string $Title
21
 * @property string $Content
22
 *
23
 * @property int $ImageID
24
 * @property int $ElementLinkID
25
 *
26
 * @method Image Image()
27
 * @method Link ElementLink()
28
 *
29
 * @mixin Versioned
30
 */
31
class BaseElementObject extends DataObject
32
{
33
    /**
34
     * @var array
35
     */
36
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
37
        'Name' => 'Varchar(255)',
38
        'Title' => 'Varchar(255)',
39
        'Content' => 'HTMLText',
40
    );
41
42
    /**
43
     * @var array
44
     */
45
    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...
46
        'Image' => Image::class,
47
        'ElementLink' => Link::class,
48
    );
49
50
    /**
51
     * @var array
52
     */
53
    private static $owns = array(
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
54
        'Image',
55
    );
56
57
    /**
58
     * @var string
59
     */
60
    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...
61
62
    /**
63
     * @var array
64
     */
65
    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...
66
        'Image.CMSThumbnail' => 'Image',
67
        'Name' => 'Name',
68
        'Title' => 'Title',
69
    );
70
71
    /**
72
     * @var array
73
     */
74
    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...
75
        'Name' => 'Name',
76
        'Title' => 'Headline',
77
        'Content' => 'Description',
78
    );
79
80
    /**
81
     * @var array
82
     */
83
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
84
        Versioned::class,
85
    ];
86
87
    /**
88
     * Adds Publish button.
89
     *
90
     * @var bool
91
     */
92
    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...
93
94
    /**
95
     * @var string
96
     */
97
    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...
98
99
    /**
100
     * @return FieldList
101
     *
102
     * @throws \Exception
103
     */
104
    public function getCMSFields()
105
    {
106 1
        $this->beforeUpdateCMSFields(function ($fields) {
107 1
            $fields->replaceField(
108 1
                'ElementLinkID',
109 1
                LinkField::create('ElementLinkID')
110 1
                    ->setTitle('Link')
111 1
                    ->setDescription('Optional. Add a call to action link.')
112
            );
113 1
            $fields->insertBefore($fields->dataFieldByName('ElementLinkID'), 'Content');
114
115 1
            $fields->removeByName(array(
116 1
                'ElementFeaturesID',
117
                'Sort',
118
            ));
119
120 1
            $fields->dataFieldByName('Name')->setDescription('Required. For internal reference only');
121
122 1
            $fields->dataFieldByName('Title')->setDescription('Optional. Display a Title with this feature.');
123
124 1
            $image = $fields->dataFieldByName('Image')
125 1
                ->setDescription('Optional. Display an image with this feature.')
126 1
                ->setFolderName('Uploads/Elements/Objects');
127 1
            $fields->insertBefore($image, 'Content');
128
129 1
            $fields->dataFieldByName('Content')
130 1
                ->setTitle('Description')
131 1
                ->setDescription('Optional. Set a description for this feature.')
132 1
                ->setRows(8);
133 1
        });
134
135 1
        return parent::getCMSFields();
136
    }
137
138
    /**
139
     * @return ValidationResult
140
     */
141 1
    public function validate()
142
    {
143 1
        $result = parent::validate();
144
145 1
        if (!$this->Name) {
146 1
            $result->addError('Name is required before you can save');
147
        }
148
149 1
        return $result;
150
    }
151
152
    /**
153
     * @return SiteTree|null
154
     */
155 4
    public function getPage()
156
    {
157 4
        $page = Director::get_current_page();
158
        // because $page can be a SiteTree or Controller
159 4
        return $page instanceof SiteTree ? $page : null;
160
    }
161
162
    /**
163
     * Basic permissions, defaults to page perms where possible.
164
     *
165
     * @param \SilverStripe\Security\Member|null $member
166
     * @return boolean
167
     */
168 1
    public function canView($member = null)
169
    {
170 1
        $extended = $this->extendedCan(__FUNCTION__, $member);
171 1
        if ($extended !== null) {
172
            return $extended;
173
        }
174
175 1
        if ($page = $this->getPage()) {
176
            return $page->canView($member);
177
        }
178
179 1
        return Permission::check('CMS_ACCESS', 'any', $member);
180
    }
181
182
    /**
183
     * Basic permissions, defaults to page perms where possible.
184
     *
185
     * @param \SilverStripe\Security\Member|null $member
186
     *
187
     * @return boolean
188
     */
189 1
    public function canEdit($member = null)
190
    {
191 1
        $extended = $this->extendedCan(__FUNCTION__, $member);
192 1
        if ($extended !== null) {
193
            return $extended;
194
        }
195
196 1
        if ($page = $this->getPage()) {
197
            return $page->canEdit($member);
198
        }
199
200 1
        return Permission::check('CMS_ACCESS', 'any', $member);
201
    }
202
203
    /**
204
     * Basic permissions, defaults to page perms where possible.
205
     *
206
     * Uses archive not delete so that current stage is respected i.e if a
207
     * element is not published, then it can be deleted by someone who doesn't
208
     * have publishing permissions.
209
     *
210
     * @param \SilverStripe\Security\Member|null $member
211
     *
212
     * @return boolean
213
     */
214 1
    public function canDelete($member = null)
215
    {
216 1
        $extended = $this->extendedCan(__FUNCTION__, $member);
217 1
        if ($extended !== null) {
218
            return $extended;
219
        }
220
221 1
        if ($page = $this->getPage()) {
222
            return $page->canArchive($member);
223
        }
224
225 1
        return Permission::check('CMS_ACCESS', 'any', $member);
226
    }
227
228
    /**
229
     * Basic permissions, defaults to page perms where possible.
230
     *
231
     * @param \SilverStripe\Security\Member|null $member
232
     * @param array $context
233
     *
234
     * @return boolean
235
     */
236 1
    public function canCreate($member = null, $context = array())
237
    {
238 1
        $extended = $this->extendedCan(__FUNCTION__, $member);
239 1
        if ($extended !== null) {
240
            return $extended;
241
        }
242
243 1
        return Permission::check('CMS_ACCESS', 'any', $member);
244
    }
245
}
246