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

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