Completed
Pull Request — master (#4)
by Matthew
11:25
created

BaseElementObject::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Dynamic\BaseObject\Model;
4
5
use DNADesign\Elemental\Forms\TextCheckboxGroupField;
0 ignored issues
show
Bug introduced by
The type DNADesign\Elemental\Forms\TextCheckboxGroupField 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 DNADesign\Elemental\Models\BaseElement;
0 ignored issues
show
Bug introduced by
The type DNADesign\Elemental\Models\BaseElement 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 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 booelan $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 = 'Name 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
        'Name' => 'Name',
72
        'Title' => 'Title',
73
    );
74
75
    /**
76
     * @var array
77
     */
78
    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...
79
        'Name' => 'Name',
80
        'Title' => 'Headline',
81
        'Content' => 'Description',
82
    );
83
84
    /**
85
     * @var array
86
     */
87
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
88
        Versioned::class,
89
    ];
90
91
    /**
92
     * Adds Publish button.
93
     *
94
     * @var bool
95
     */
96
    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...
97
98
    /**
99
     * @var string
100
     */
101
    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...
102
103
    /**
104
     * @return FieldList
105
     *
106 1
     * @throws \Exception
107 1
     */
108 1
    public function getCMSFields()
109 1
    {
110 1
        $this->beforeUpdateCMSFields(function ($fields) {
111 1
            /** @var FieldList $fields */
112
            $fields->replaceField(
113 1
                'ElementLinkID',
114
                LinkField::create('ElementLinkID')
115 1
                    ->setTitle('Link')
116 1
                    ->setDescription('Optional. Add a call to action link.')
117
            );
118
            $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

118
            $fields->insertBefore($fields->dataFieldByName('ElementLinkID'), /** @scrutinizer ignore-type */ 'Content');
Loading history...
119
120 1
            $fields->removeByName(array(
121
                'ElementFeaturesID',
122 1
                'Sort',
123
            ));
124 1
125 1
            // Add a combined field for "Title" and "Displayed" checkbox in a Bootstrap input group
126 1
            $fields->removeByName('ShowTitle');
127 1
            $fields->replaceField(
128
                'Title',
129 1
                TextCheckboxGroupField::create(
130 1
                    TextField::create('Title', _t(BaseElement::class . '.TitleLabel', 'Title (displayed if checked)')),
131 1
                    CheckboxField::create('ShowTitle', _t(BaseElement::class . '.ShowTitleLabel', 'Displayed'))
132 1
                )->setName('TitleAndDisplayed')
133 1
            );
134
            // $fields->dataFieldByName('Title')->setDescription('Optional. Display a Title with this feature.');
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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