Issues (10)

src/Model/BaseElementObject.php (10 issues)

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
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
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
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
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
The private property $summary_fields is not used, and could be removed.
Loading history...
70
        'Image.CMSThumbnail',
71
        'Title',
72
    );
73
74
    /**
75
     * @var array
76
     */
77
    private static $searchable_fields = array(
0 ignored issues
show
The private property $searchable_fields is not used, and could be removed.
Loading history...
78
        'Title',
79
        'Content',
80
    );
81
82
    /**
83
     * @var array
84
     */
85
    private static $extensions = [
0 ignored issues
show
The private property $extensions is not used, and could be removed.
Loading history...
86
        Versioned::class,
87
    ];
88
89
    /**
90
     * Adds Publish button.
91
     *
92
     * @var bool
93
     */
94
    private static $versioned_gridfield_extensions = true;
0 ignored issues
show
The private property $versioned_gridfield_extensions is not used, and could be removed.
Loading history...
95
96
    /**
97
     * @var string
98
     */
99
    private static $table_name = 'BaseElementObject';
0 ignored issues
show
The private property $table_name is not used, and could be removed.
Loading history...
100
101
    /**
102
     * @param bool $includerelations
103
     * @return array
104
     */
105 1
    public function fieldLabels($includerelations = true)
106
    {
107 1
        $labels = parent::fieldLabels($includerelations);
108
109 1
        $labels['Title'] = _t(__CLASS__ . '.TitleLabel', 'Title');
110 1
        $labels['ElementLinkID'] = _t(__CLASS__ . '.LinkLabel', 'Link');
111 1
        $labels['Image'] = _t(__CLASS__ . '.ImageLabel', 'Image');
112 1
        $labels['Image.CMSThumbnail'] = _t(__CLASS__ . '.ImageLabel', 'Image');
113 1
        $labels['Content'] = _t(__CLASS__. '.ContentLabel', 'Content');
114
115 1
        return $labels;
116
    }
117
118
    /**
119
     * @return FieldList
120
     *
121
     * @throws \Exception
122
     */
123
    public function getCMSFields()
124
    {
125 1
        $this->beforeUpdateCMSFields(function ($fields) {
126
            /** @var FieldList $fields */
127 1
            $fields->removeByName(array(
128 1
                'Sort',
129
            ));
130
131
            // Add a combined field for "Title" and "Displayed" checkbox in a Bootstrap input group
132 1
            $fields->removeByName('ShowTitle');
133 1
            $fields->replaceField(
134 1
                'Title',
135 1
                TextCheckboxGroupField::create()
136 1
                    ->setName($this->fieldLabel('Title'))
137
            );
138
139 1
            $fields->replaceField(
140 1
                'ElementLinkID',
141 1
                LinkField::create('ElementLinkID', $this->fieldLabel('ElementLinkID'))
142 1
                    ->setDescription(_t(__CLASS__.'.LinkDescription', 'optional. Add a call to action link.'))
143
            );
144 1
            $fields->insertBefore($fields->dataFieldByName('ElementLinkID'), 'Content');
0 ignored issues
show
'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

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