Completed
Pull Request — master (#2)
by Matthew
07:05 queued 04:26
created

ProductWishList::canDelete()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 6
nop 1
crap 4
1
<?php
2
3
namespace Dynamic\Wishlist\Model;
4
5
use Dynamic\AdditionalFormFields\Form\CancelFormAction;
6
use Dynamic\ManageableDataObject\Interfaces\ManageableDataObjectInterface;
7
use Dynamic\ViewableDataObject\VDOInterfaces\ViewableDataObjectInterface;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\FormAction;
11
use SilverStripe\Forms\RequiredFields;
12
use SilverStripe\ORM\DataObject;
13
use SilverStripe\Security\Member;
14
use SilverStripe\Security\Permission;
15
use SilverStripe\Security\PermissionProvider;
16
use SilverStripe\Security\Security;
17
18
/**
19
 * Class ProductWishList
20
 *
21
 * @property string $Title
22
 * @property bool $Private
23
 * @property int $MemberID
24
 * @method Member $Member
25
 */
26
class ProductWishList extends DataObject implements PermissionProvider, ViewableDataObjectInterface, ManageableDataObjectInterface
27
{
28
29
    /**
30
     * @var string
31
     */
32
    private static $table_name = 'ProductWishList';
1 ignored issue
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
33
34
    /**
35
     * @var array
36
     */
37
    private static $db = [
1 ignored issue
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
38
        'Title' => 'Varchar(100)',
39
        'Private' => 'Boolean',
40
    ];
41
42
    /**
43
     * @var array
44
     */
45
    private static $has_one = [
1 ignored issue
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
46
        'Member' => Member::class,
47
    ];
48
49
    /**
50
     *
51
     */
52 2
    public function onBeforeWrite()
53
    {
54 2
        parent::onBeforeWrite();
55
56 2
        if (!$this->MemberID > 0) {
57 2
            $this->MemberID = Security::getCurrentUser();
0 ignored issues
show
Documentation Bug introduced by
It seems like SilverStripe\Security\Security::getCurrentUser() of type SilverStripe\Security\Member is incompatible with the declared type integer of property $MemberID.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
        }
59
    }
60
61
    /**
62
     * @param array|null $params
63
     *
64
     * @return FieldList
65
     */
66 1
    public function getFrontEndFields($params = null)
67
    {
68 1
        $fields = parent::getFrontEndFields($params);
69
70 1
        $fields->removeByName([
71 1
            'MemberID',
72
            'MenuTitle',
73
            'URLSegment',
74
            'MetaTitle',
75
            'MetaDescription',
76
        ]);
77
78 1
        return $fields;
79
    }
80
81
    /**
82
     * @param bool $showCancel
83
     *
84
     * @return FieldList
85
     */
86 1
    public function getFrontEndActions($showCancel = false)
87
    {
88 1
        $processTitle = ($this->getIsEditing()) ? "Update {$this->i18n_singular_name()}" : "Create {$this->i18n_singular_name()}";
89 1
        $actions = FieldList::create(
90 1
            FormAction::create('doSaveObject')
91 1
                      ->setTitle($processTitle)
92
        );
93
94 1
        if ($showCancel === true) {
95 1
            $actions->insertBefore('action_doSaveObject', CancelFormAction::create('Cancel'));
96
        }
97
98 1
        $this->extend('updateFrontEndActions', $actions);
99
100 1
        return $actions;
101
    }
102
103
    /**
104
     * @return RequiredFields
105
     */
106 1
    public function getFrontEndRequiredFields()
107
    {
108 1
        $fields = RequiredFields::create([
109 1
            'Title',
110
        ]);
111
112 1
        $this->extend('updateFrontEndRequiredFields', $fields);
113
114 1
        return $fields;
115
    }
116
117
    /**
118
     * @return bool
119
     */
120 1
    public function getIsEditing()
121
    {
122 1
        $params = Controller::curr()->getRequest()->latestParams();
123
124 1
        return isset($params['Action']) && $params['Action'] == 'edit' && isset($params['ID']);
125
    }
126
127
    /**
128
     * set ParentPage for ViewableDataobject
129
     *
130
     * @return \SilverStripe\CMS\Model\SiteTree
131
     */
132
    public function getParentPage()
133
    {
134
        $class = $this->config()->get('listing_page_class');
135
        return $class::get()->first();
136
    }
137
138
    /**
139
     * @return String|bool
140
     */
141
    public function getUpdateLink()
142
    {
143
        return ($this->ID > 0) ? Controller::join_links($this->getParentPage()->Link(), 'edit', $this->ID) : false;
144
    }
145
146
    /**
147
     * set ViewAction for ViewableDataobject
148
     *
149
     * @return string
150
     */
151 1
    public function getViewAction()
152
    {
153 1
        return 'view';
154
    }
155
156
    /**
157
     * @return array
158
     */
159 1
    public function providePermissions()
160
    {
161
        return [
162 1
            'WishList_EDIT' => [
163
                'name' => 'Edit a Wish List',
164
                'category' => 'Wish List Permissions',
165
            ],
166
            'WishList_DELETE' => [
167
                'name' => 'Delete a Wish List',
168
                'category' => 'Wish List Permissions',
169
            ],
170
            'WishList_CREATE' => [
171
                'name' => 'Create a Wish List',
172
                'category' => 'Wish List Permissions',
173
            ],
174
            'WishList_VIEW' => [
175
                'name' => 'View a Wish List',
176
                'category' => 'Wish List Permissions',
177
            ],
178
        ];
179
    }
180
181
    /**
182
     * @param Member|null $member
183
     *
184
     * @return bool|int
185
     */
186 1
    public function canEdit($member = null)
187
    {
188 1
        $member = ($member === null) ? Security::getCurrentUser() : $member;
189
190 1
        return ((Permission::check('WishList_EDIT', 'any',
191 1
                        $member) && $member->ID == $this->MemberID) || Permission::check('ADMIN'));
192
    }
193
194
    /**
195
     * @param Member|null $member
196
     *
197
     * @return bool|int
198
     */
199 1
    public function canDelete($member = null)
200
    {
201 1
        $member = ($member === null) ? Security::getCurrentUser() : $member;
202
203 1
        return ((Permission::check('WishList_DELETE', 'any',
204 1
                        $member) && $member->ID == $this->MemberID) || Permission::check('ADMIN'));
205
    }
206
207
    /**
208
     * @param Member|null $member
209
     * @param array $context
210
     *
211
     * @return bool|int
212
     */
213 1
    public function canCreate($member = null, $context = array())
214
    {
215 1
        return Security::getCurrentUser() && Permission::check('WishList_CREATE', 'any', $member);
216
    }
217
218
    /**
219
     * @param Member|null $member
220
     *
221
     * @return bool
222
     */
223 3
    public function canView($member = null)
224
    {
225 3
        $member = ($member === null) ? Security::getCurrentUser() : $member;
226
227 3
        return (!$this->Private) || ((Permission::check('WishList_VIEW', 'any',
228 3
                        $member) && $member->ID == $this->MemberID) || Permission::check('ADMIN'));
229
    }
230
231
}
232