Passed
Push — master ( 1243b9...007eeb )
by Jason
01:52
created

Discount::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dynamic\Foxy\Discount\Model;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\Security\Permission;
8
9
class Discount extends DataObject
10
{
11
    /**
12
     * @var string
13
     */
14
    private static $singular_name = 'Discount';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
15
16
    /**
17
     * @var string
18
     */
19
    private static $plural_name = 'Discounts';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
20
21
    /**
22
     * @var array
23
     */
24
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
25
        'Title' => 'Varchar(255)',
26
        'Quantity' => 'Int',
27
        'Percentage' => 'Int'
28
    );
29
30
    /**
31
     * @var array
32
     */
33
    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...
34
        'Title',
35
        'Quantity',
36
        'DiscountPercentage' => [
37
            'title' => 'Discount',
38
        ],
39
    );
40
41
    /**
42
     * @var string
43
     */
44
    private static $table_name = 'FoxyDiscount';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
45
46
    /**
47
     * @return FieldList
48
     */
49
    public function getCMSFields()
50
    {
51
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
52
            $quantity = $fields->dataFieldByName('Quantity');
53
            $quantity->setTitle('Quantity to trigger discount');
54
55
            $percentage = $fields->dataFieldByName('Percentage');
56
            $percentage->setTitle('Percent discount');
57
        });
58
59
        return parent::getCMSFields();
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getDiscountPercentage()
66
    {
67
        return "{$this->Percentage}%";
0 ignored issues
show
Bug Best Practice introduced by
The property Percentage does not exist on Dynamic\Foxy\Discount\Model\Discount. Since you implemented __get, consider adding a @property annotation.
Loading history...
68
    }
69
70
    /**
71
     * Basic permissions, defaults to page perms where possible.
72
     *
73
     * @param \SilverStripe\Security\Member|null $member
74
     * @return boolean
75
     */
76
    public function canView($member = null)
77
    {
78
        $extended = $this->extendedCan(__FUNCTION__, $member);
79
        if ($extended !== null) {
80
            return $extended;
81
        }
82
83
        return Permission::check('CMS_ACCESS', 'any', $member);
84
    }
85
86
    /**
87
     * Basic permissions, defaults to page perms where possible.
88
     *
89
     * @param \SilverStripe\Security\Member|null $member
90
     *
91
     * @return boolean
92
     */
93
    public function canEdit($member = null)
94
    {
95
        $extended = $this->extendedCan(__FUNCTION__, $member);
96
        if ($extended !== null) {
97
            return $extended;
98
        }
99
100
        return Permission::check('CMS_ACCESS', 'any', $member);
101
    }
102
103
    /**
104
     * Basic permissions, defaults to page perms where possible.
105
     *
106
     * Uses archive not delete so that current stage is respected i.e if a
107
     * element is not published, then it can be deleted by someone who doesn't
108
     * have publishing permissions.
109
     *
110
     * @param \SilverStripe\Security\Member|null $member
111
     *
112
     * @return boolean
113
     */
114
    public function canDelete($member = null)
115
    {
116
        $extended = $this->extendedCan(__FUNCTION__, $member);
117
        if ($extended !== null) {
118
            return $extended;
119
        }
120
121
        return Permission::check('CMS_ACCESS', 'any', $member);
122
    }
123
124
    /**
125
     * Basic permissions, defaults to page perms where possible.
126
     *
127
     * @param \SilverStripe\Security\Member|null $member
128
     * @param array $context
129
     *
130
     * @return boolean
131
     */
132
    public function canCreate($member = null, $context = array())
133
    {
134
        $extended = $this->extendedCan(__FUNCTION__, $member);
135
        if ($extended !== null) {
136
            return $extended;
137
        }
138
139
        return Permission::check('CMS_ACCESS', 'any', $member);
140
    }
141
}
142