Passed
Pull Request — master (#98)
by Nic
02:51
created

Variation::getOptionModifierActionSymbol()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 14
rs 9.9332
1
<?php
2
3
namespace Dynamic\Foxy\Model;
4
5
use Bummzack\SortableFile\Forms\SortableUploadField;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Forms\CheckboxField;
8
use SilverStripe\Forms\CurrencyField;
9
use SilverStripe\Forms\DropdownField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\NumericField;
12
use SilverStripe\Forms\ReadonlyField;
13
use SilverStripe\Forms\TextField;
14
use SilverStripe\ORM\DataObject;
15
16
/**
17
 * Class Variation
18
 * @package Dynamic\Foxy\Model
19
 */
20
class Variation extends DataObject
21
{
22
    /**
23
     * @var string
24
     */
25
    private static $table_name = 'Variation';
26
27
    /**
28
     * @var string
29
     */
30
    private static $singular_name = 'Variation';
31
32
    /**
33
     * @var string
34
     */
35
    private static $plural_name = 'Variations';
36
37
    /**
38
     * @var string[]
39
     */
40
    private static $db = [
41
        'Title' => 'Varchar(255)',
42
        'Content' => 'HTMLText',
43
        'WeightModifier' => 'Decimal(9,3)',
44
        'CodeModifier' => 'Text',
45
        'PriceModifier' => 'Currency',
46
        'WeightModifierAction' => "Enum('Add,Subtract,Set', null)",
47
        'CodeModifierAction' => "Enum('Add,Subtract,Set', null)",
48
        'PriceModifierAction' => "Enum('Add,Subtract,Set', null)",
49
        'Available' => 'Boolean',
50
        'Type' => 'Int',
51
        'OptionModifierKey' => 'Varchar(255)',
52
        'SortOrder' => 'Int',
53
    ];
54
55
    /**
56
     * @var array
57
     */
58
    private static $many_many = [
59
        'Images' => File::class,
60
    ];
61
62
    /**
63
     * @var \string[][]
64
     */
65
    private static $many_many_extraFields = [
66
        'Images' => [
67
            'SortOrder' => 'Int',
68
        ],
69
    ];
70
71
    /**
72
     * @var string[]
73
     */
74
    private static $owns = [
75
        'Images',
76
    ];
77
78
    /**
79
     * The relation name was established before requests for videos.
80
     * The relation has subsequently been updated from Image::class to File::class
81
     * to allow for additional file types such as mp4
82
     *
83
     * @var array
84
     */
85
    private static $allowed_images_extensions = [
86
        'gif',
87
        'jpeg',
88
        'jpg',
89
        'png',
90
        'bmp',
91
        'ico',
92
        'mp4',
93
    ];
94
95
96
    /**
97
     * @return FieldList
98
     */
99
    public function getCMSFields()
100
    {
101
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
102
            $fields->removeByName([
103
                'Images',
104
                'OptionModifierKey',
105
            ]);
106
107
            // Images tab
108
            $images = SortableUploadField::create('Images')
109
                ->setSortColumn('SortOrder')
110
                ->setIsMultiUpload(true)
111
                ->setAllowedExtensions($this->config()->get('allowed_images_extensions'))
112
                ->setFolderName('Uploads/Products/Images');
113
114
            $fields->addFieldsToTab('Root.Images', [
115
                $images,
116
            ]);
117
118
            if ($this->exists()) {
119
                $fields->addFieldToTab(
120
                    'Root.Main',
121
                    ReadonlyField::create('OptionModifierKey')
122
                        ->setTitle(_t('Variation.ModifierKey', 'Modifier Key'))
123
                );
124
            }
125
126
            $fields->addFieldsToTab(
127
                'Root.Main',
128
                [
129
                    CheckboxField::create('Available', 'Available for purchase'),
130
131
                    // Weight Modifier Fields
132
                    //HeaderField::create('WeightHD', _t('Variation.WeightHD', 'Modify Weight'), 4),
133
                    NumericField::create("WeightModifier")
134
                        ->setTitle(_t('Variation.WeightModifier', 'Weight'))
135
                        ->setScale(3)
136
                        ->setDescription(_t(
137
                            'Variation.WeightDescription',
138
                            'Only supports up to 3 decimal places'
139
                        )),
140
                    DropdownField::create(
141
                        'WeightModifierAction',
142
                        _t('Variation.WeightModifierAction', 'Weight Modification'),
143
                        [
144
                            'Add' => _t(
145
                                'Variation.WeightAdd',
146
                                'Add to Base Weight',
147
                                'Add to weight'
148
                            ),
149
                            'Subtract' => _t(
150
                                'Variation.WeightSubtract',
151
                                'Subtract from Base Weight',
152
                                'Subtract from weight'
153
                            ),
154
                            'Set' => _t('Variation.WeightSet', 'Set as a new Weight'),
155
                        ]
156
                    )
157
                        ->setEmptyString('')
158
                        ->setDescription(_t(
159
                            'Variation.WeightDescription',
160
                            'Does weight modify or replace base weight?'
161
                        )),
162
163
                    // Price Modifier Fields
164
                    //HeaderField::create('PriceHD', _t('Variation.PriceHD', 'Modify Price'), 4),
165
                    CurrencyField::create('PriceModifier')
166
                        ->setTitle(_t('Variation.PriceModifier', 'Price')),
167
                    DropdownField::create(
168
                        'PriceModifierAction',
169
                        _t('Variation.PriceModifierAction', 'Price Modification'),
170
                        [
171
                            'Add' => _t(
172
                                'Variation.PriceAdd',
173
                                'Add to Base Price',
174
                                'Add to price'
175
                            ),
176
                            'Subtract' => _t(
177
                                'Variation.PriceSubtract',
178
                                'Subtract from Base Price',
179
                                'Subtract from price'
180
                            ),
181
                            'Set' => _t('Variation.PriceSet', 'Set as a new Price'),
182
                        ]
183
                    )
184
                        ->setEmptyString('')
185
                        ->setDescription(_t('Variation.PriceDescription', 'Does price modify or replace base price?')),
186
187
                    // Code Modifier Fields
188
                    //HeaderField::create('CodeHD', _t('Variation.CodeHD', 'Modify Code'), 4),
189
                    TextField::create('CodeModifier')
190
                        ->setTitle(_t('Variation.CodeModifier', 'Code')),
191
                    DropdownField::create(
192
                        'CodeModifierAction',
193
                        _t('Variation.CodeModifierAction', 'Code Modification'),
194
                        [
195
                            'Add' => _t(
196
                                'Variation.CodeAdd',
197
                                'Add to Base Code',
198
                                'Add to code'
199
                            ),
200
                            'Subtract' => _t(
201
                                'Variation.CodeSubtract',
202
                                'Subtract from Base Code',
203
                                'Subtract from code'
204
                            ),
205
                            'Set' => _t('Variation.CodeSet', 'Set as a new Code'),
206
                        ]
207
                    )
208
                        ->setEmptyString('')
209
                        ->setDescription(_t('Variation.CodeDescription', 'Does code modify or replace base code?')),
210
                ]
211
            );
212
        });
213
214
        return parent::getCMSFields();
215
    }
216
217
    /**
218
     *
219
     */
220
    public function onBeforeWrite()
221
    {
222
        parent::onBeforeWrite();
223
224
        $modifierKeyField = 'OptionModifierKey';
225
        $this->{$modifierKeyField} = $this->getGeneratedValue();
226
227
        $codeModifierField = 'CodeModifier';
228
        switch ($this->CodeModifierAction) {
0 ignored issues
show
Bug Best Practice introduced by
The property CodeModifierAction does not exist on Dynamic\Foxy\Model\Variation. Since you implemented __get, consider adding a @property annotation.
Loading history...
229
            case 'Subtract':
230
            case 'Add':
231
                if ($this->config()->get('trimAllWhitespace') == false) {
232
                    // trim the right of the code - some companies use spaces to denote options
233
                    $trimmed = rtrim($this->{$codeModifierField});
234
                    // replace duplicate spaces
235
                    $this->{$codeModifierField} = preg_replace('/\s+/', ' ', $trimmed);
236
                    break;
237
                }
238
            /* falls through */
239
            case 'Set':
240
                $trimmed = trim($this->{$codeModifierField});
241
                $this->{$codeModifierField} = preg_replace('/\s+/', ' ', $trimmed);
242
                break;
243
        }
244
    }
245
246
    /**
247
     * @return string
248
     */
249
    public function getGeneratedValue()
250
    {
251
        $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\Variation. Since you implemented __get, consider adding a @property annotation.
Loading history...
252
        $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\Variation. Since you implemented __get, consider adding a @property annotation.
Loading history...
253
        $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\Variation. Since you implemented __get, consider adding a @property annotation.
Loading history...
254
        $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\Variation. Since you implemented __get, consider adding a @property annotation.
Loading history...
255
        $modCode = self::getOptionModifierActionSymbol($this->CodeModifierAction) . $this->CodeModifier;
0 ignored issues
show
Bug Best Practice introduced by
The property CodeModifierAction does not exist on Dynamic\Foxy\Model\Variation. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property CodeModifier does not exist on Dynamic\Foxy\Model\Variation. Since you implemented __get, consider adding a @property annotation.
Loading history...
256
257
        return $this->Title . '{p' . $modPriceWithSymbol . '|w' . $modWeight . '|c' . $modCode . '}';
258
    }
259
260
    /**
261
     * @param $oma
262
     * @param bool $returnWithOnlyPlusMinus
263
     *
264
     * @return string
265
     */
266
    public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus = false)
267
    {
268
        switch ($oma) {
269
            case 'Subtract':
270
                $symbol = '-';
271
                break;
272
            case 'Set':
273
                $symbol = ($returnWithOnlyPlusMinus) ? '' : ':';
274
                break;
275
            default:
276
                $symbol = '+';
277
        }
278
279
        return $symbol;
280
    }
281
282
    /**
283
     * @return string
284
     */
285
    protected function getWeightModifierWithSymbol()
286
    {
287
        return $this->getOptionModifierActionSymbol($this->WeightModifierAction) . $this->WeightModifier;
0 ignored issues
show
Bug Best Practice introduced by
The property WeightModifier does not exist on Dynamic\Foxy\Model\Variation. 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\Variation. Since you implemented __get, consider adding a @property annotation.
Loading history...
288
    }
289
290
    /**
291
     * @return string
292
     */
293
    protected function getPriceModifierWithSymbol()
294
    {
295
        return $this->getOptionModifierActionSymbol($this->PriceModifierAction) . $this->PriceModifier;
0 ignored issues
show
Bug Best Practice introduced by
The property PriceModifier does not exist on Dynamic\Foxy\Model\Variation. 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\Variation. Since you implemented __get, consider adding a @property annotation.
Loading history...
296
    }
297
298
    /**
299
     * @return string
300
     */
301
    protected function getCodeModifierWithSymbol()
302
    {
303
        return $this->getOptionModifierActionSymbol($this->CodeModifierAction) . $this->CodeModifier;
0 ignored issues
show
Bug Best Practice introduced by
The property CodeModifierAction does not exist on Dynamic\Foxy\Model\Variation. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property CodeModifier does not exist on Dynamic\Foxy\Model\Variation. Since you implemented __get, consider adding a @property annotation.
Loading history...
304
    }
305
}
306