Passed
Pull Request — master (#26)
by Jason
02:06
created

ProductOption::getProductOptionGroupTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Model;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\CurrencyField;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\HeaderField;
10
use SilverStripe\Forms\TextField;
11
use SilverStripe\ORM\DataObject;
12
13
class ProductOption extends DataObject
14
{
15
    /**
16
     * @var array
17
     */
18
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
        'Title' => 'Varchar(255)',
20
    );
21
22
    /**
23
     * @var array
24
     */
25
    private static $belongs_many_many = [
0 ignored issues
show
introduced by
The private property $belongs_many_many is not used, and could be removed.
Loading history...
26
        'Types' => OptionType::class,
27
    ];
28
29
    /**
30
     * @var string
31
     */
32
    private static $table_name = 'ProductOption';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
33
34
    /**
35
     * @var string
36
     */
37
    private static $singular_name = 'Option';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
38
39
    /**
40
     * @var string
41
     */
42
    private static $plural_name = 'Options';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
43
44
    /**
45
     * @var array
46
     */
47
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
48
        'ManyMany[Available]' => true,
49
    ];
50
51
    /**
52
     * @return FieldList
53
     */
54
    public function getCMSFields()
55
    {
56
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
57
58
            $fields->removeByName([
59
                'Types',
60
            ]);
61
62
            $fields->addFieldsToTab('Root.Main', array(
63
                CheckboxField::create('ManyMany[Available]', 'Available for purchase'),
64
65
                // Weight Modifier Fields
66
                HeaderField::create('WeightHD', _t('OptionItem.WeightHD', 'Modify Weight'), 4),
67
                TextField::create("ManyMany[WeightModifier]")
68
                    ->setTitle(_t('OptionItem.WeightModifier', 'Weight')),
69
                DropdownField::create(
70
                    'ManyMany[WeightModifierAction]',
71
                    _t('OptionItem.WeightModifierAction', 'Weight Modification'),
72
                    array(
73
                        'Add' => _t(
74
                            'OptionItem.WeightAdd',
75
                            'Add to Base Weight',
76
                            'Add to weight'
77
                        ),
78
                        'Subtract' => _t(
79
                            'OptionItem.WeightSubtract',
80
                            'Subtract from Base Weight',
81
                            'Subtract from weight'
82
                        ),
83
                        'Set' => _t('OptionItem.WeightSet', 'Set as a new Weight'),
84
                    )
85
                )
86
                    ->setEmptyString('')
87
                    ->setDescription(_t(
88
                        'OptionItem.WeightDescription',
89
                        'Does weight modify or replace base weight?'
90
                    )),
91
92
                // Price Modifier FIelds
93
                HeaderField::create('PriceHD', _t('OptionItem.PriceHD', 'Modify Price'), 4),
94
                CurrencyField::create('ManyMany[PriceModifier]')
95
                    ->setTitle(_t('OptionItem.PriceModifier', 'Price')),
96
                DropdownField::create(
97
                    'ManyMany[PriceModifierAction]',
98
                    _t('OptionItem.PriceModifierAction', 'Price Modification'),
99
                    array(
100
                        'Add' => _t(
101
                            'OptionItem.PriceAdd',
102
                            'Add to Base Price',
103
                            'Add to price'
104
                        ),
105
                        'Subtract' => _t(
106
                            'OptionItem.PriceSubtract',
107
                            'Subtract from Base Price',
108
                            'Subtract from price'
109
                        ),
110
                        'Set' => _t('OptionItem.PriceSet', 'Set as a new Price'),
111
                    )
112
                )
113
                    ->setEmptyString('')
114
                    ->setDescription(_t('OptionItem.PriceDescription', 'Does price modify or replace base price?')),
115
116
                // Code Modifier Fields
117
                HeaderField::create('CodeHD', _t('OptionItem.CodeHD', 'Modify Code'), 4),
118
                TextField::create('ManyMany[CodeModifier]')
119
                    ->setTitle(_t('OptionItem.CodeModifier', 'Code')),
120
                DropdownField::create(
121
                    'ManyMany[CodeModifierAction]',
122
                    _t('OptionItem.CodeModifierAction', 'Code Modification'),
123
                    array(
124
                        'Add' => _t(
125
                            'OptionItem.CodeAdd',
126
                            'Add to Base Code',
127
                            'Add to code'
128
                        ),
129
                        'Subtract' => _t(
130
                            'OptionItem.CodeSubtract',
131
                            'Subtract from Base Code',
132
                            'Subtract from code'
133
                        ),
134
                        'Set' => _t('OptionItem.CodeSet', 'Set as a new Code'),
135
                    )
136
                )
137
                    ->setEmptyString('')
138
                    ->setDescription(_t('OptionItem.CodeDescription', 'Does code modify or replace base code?')),
139
            ));
140
        });
141
142
        return parent::getCMSFields();
143
    }
144
145
    /**
146
     * @param $oma
147
     * @param bool $returnWithOnlyPlusMinus
148
     *
149
     * @return string
150
     */
151
    public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus = false)
152
    {
153
        switch ($oma) {
154
            case 'Subtract':
155
                $symbol = '-';
156
                break;
157
            case 'Set':
158
                $symbol = ($returnWithOnlyPlusMinus) ? '' : ':';
159
                break;
160
            default:
161
                $symbol = '+';
162
        }
163
        return $symbol;
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getWeightModifierWithSymbol()
170
    {
171
        return self::getOptionModifierActionSymbol($this->WeightModifierAction) . $this->WeightModifier;
0 ignored issues
show
Bug Best Practice introduced by
The property WeightModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property WeightModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
172
    }
173
174
    /**
175
     * @return string
176
     */
177
    public function getPriceModifierWithSymbol()
178
    {
179
        return self::getOptionModifierActionSymbol($this->PriceModifierAction) . $this->PriceModifier;
0 ignored issues
show
Bug Best Practice introduced by
The property PriceModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property PriceModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    public function getCodeModifierWithSymbol()
186
    {
187
        return self::getOptionModifierActionSymbol($this->CodeModifierAction) . $this->CodeModifier;
0 ignored issues
show
Bug Best Practice introduced by
The property CodeModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property CodeModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
188
    }
189
190
    /**
191
     * @return string
192
     */
193
    public function getGeneratedValue()
194
    {
195
        $modPrice = ($this->PriceModifier) ? (string) $this->PriceModifier : '0';
0 ignored issues
show
Bug Best Practice introduced by
The property PriceModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
196
        $modPriceWithSymbol = self::getOptionModifierActionSymbol($this->PriceModifierAction) . $modPrice;
0 ignored issues
show
Bug Best Practice introduced by
The property PriceModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
197
        $modWeight = ($this->WeightModifier) ? (string) $this->WeightModifier : '0';
0 ignored issues
show
Bug Best Practice introduced by
The property WeightModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
198
        $modWeight = self::getOptionModifierActionSymbol($this->WeightModifierAction) . $modWeight;
0 ignored issues
show
Bug Best Practice introduced by
The property WeightModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
199
        $modCode = self::getOptionModifierActionSymbol($this->CodeModifierAction) . $this->CodeModifier;
0 ignored issues
show
Bug Best Practice introduced by
The property CodeModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property CodeModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
200
        return $this->Title . '{p' . $modPriceWithSymbol . '|w' . $modWeight . '|c' . $modCode . '}';
201
    }
202
203
    /**
204
     * @return mixed|string
205
     */
206
    public function getGeneratedTitle()
207
    {
208
        $modPrice = ($this->PriceModifier) ? (string) $this->PriceModifier : '0';
0 ignored issues
show
Bug Best Practice introduced by
The property PriceModifier does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
209
        $title = $this->Title;
210
        $title .= ($this->PriceModifier != 0) ?
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->PriceModifier of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
211
            ': (' . self::getOptionModifierActionSymbol(
212
                $this->PriceModifierAction,
0 ignored issues
show
Bug Best Practice introduced by
The property PriceModifierAction does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
213
                $returnWithOnlyPlusMinus = true
214
            ) . '$' . $modPrice . ')' :
215
            '';
216
        return $title;
217
    }
218
219
    /**
220
     * @return bool
221
     */
222
    public function getAvailability()
223
    {
224
        $available = ($this->Available == 1) ? true : false;
0 ignored issues
show
Bug Best Practice introduced by
The property Available does not exist on Dynamic\Foxy\Model\ProductOption. Since you implemented __get, consider adding a @property annotation.
Loading history...
225
        $this->extend('updateOptionAvailability', $available);
226
        return $available;
227
    }
228
229
    /**
230
     * @return string
231
     */
232
    public function getIsAvailable()
233
    {
234
        if ($this->getAvailability()) {
235
            return 'yes';
236
        }
237
        return 'no';
238
    }
239
}
240