Completed
Push — master ( 4b21f0...aa63ba )
by Jason
8s
created

src/Model/BaseElementObject.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Dynamic\BaseObject\Model;
4
5
use Sheadawson\Linkable\Forms\LinkField;
0 ignored issues
show
The type Sheadawson\Linkable\Forms\LinkField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Sheadawson\Linkable\Models\Link;
0 ignored issues
show
The type Sheadawson\Linkable\Models\Link was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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