Passed
Pull Request — master (#17)
by Jason
02:20
created

BaseElementObject::fieldLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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->removeByName(array(
132 1
                'Sort',
133
            ));
134
135
            // Add a combined field for "Title" and "Displayed" checkbox in a Bootstrap input group
136 1
            $fields->removeByName('ShowTitle');
137 1
            $fields->replaceField(
138 1
                'Title',
139 1
                TextCheckboxGroupField::create()
140 1
                    ->setName($this->fieldLabel('Title'))
141
            );
142
143 1
            $fields->replaceField(
144 1
                'ElementLinkID',
145 1
                LinkField::create('ElementLinkID', $this->fieldLabel('ElementLinkID'))
146 1
                    ->setDescription(_t(__CLASS__.'.LinkDescription', 'optional. Add a call to action link.'))
147
            );
148 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

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