Passed
Pull Request — master (#17)
by Jason
01:59
created

BaseElementObject::canView()   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 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 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
     * @param bool $includerelations
107
     * @return array
108
     */
109 1
    public function fieldLabels($includerelations = true)
110
    {
111 1
        $labels = parent::fieldLabels($includerelations);
112
113 1
        $labels['Title'] = _t(__CLASS__ . '.TitleLabel', 'Title');
114 1
        $labels['ElementLinkID'] = _t(__CLASS__ . '.LinkLabel', 'Link');
115 1
        $labels['Image'] = _t(__CLASS__ . '.ImageLabel', 'Image');
116 1
        $labels['Image.CMSThumbnail'] = _t(__CLASS__ . '.ImageLabel', 'Image');
117 1
        $labels['Content'] = _t(__CLASS__. '.ContentLabel', 'Content');
118
119 1
        return $labels;
120
    }
121
122
    /**
123
     * @return FieldList
124
     *
125
     * @throws \Exception
126
     */
127
    public function getCMSFields()
128
    {
129 1
        $this->beforeUpdateCMSFields(function ($fields) {
130
            /** @var FieldList $fields */
131 1
            $fields->replaceField(
132 1
                'ElementLinkID',
133 1
                LinkField::create('ElementLinkID', $this->fieldLabel('ElementLinkID'))
134 1
                    ->setDescription(_t(__CLASS__.'.LinkDescription', 'optional. Add a call to action link.'))
135
            );
136 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

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