Passed
Pull Request — master (#309)
by Jason
13:47
created

ProductCategory::getDataMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
namespace Dynamic\FoxyStripe\Model;
4
5
use Dynamic\FoxyStripe\Model\FoxyStripeClient;
6
use SilverStripe\Forms\DropdownField;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\HeaderField;
9
use SilverStripe\Forms\OptionsetField;
10
use SilverStripe\Forms\ReadonlyField;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\Security\Permission;
13
14
class ProductCategory extends DataObject
15
{
16
    /**
17
     * @var array
18
     */
19
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
20
        'Title' => 'Varchar(255)',
21
        'Code' => 'Varchar(50)',
22
        'DeliveryType' => 'Varchar(50)',
23
        'MaxDownloads' => 'Int',
24
        'MaxDownloadsTime' => 'Int',
25
        'DefaultWeight' => 'Float',
26
        'DefaultWeightUnit' => 'Enum("LBS, KBS", "LBS")',
27
        'DefaultLengthUnit' => 'Enum("in, cm", "in")',
28
        'ShippingFlatRate' => 'Currency',
29
        'ShippingFlatRateType' => 'Varchar(50)',
30
        'HandlingFeeType' => 'Varchar(50)',
31
        'HandlingFee' => 'Currency',
32
        'HandlingFeePercentage' => 'Decimal',
33
        'HandlingFeeMinimum' => 'Currency',
34
        'DiscountType' => 'Varchar(50)',
35
        'DiscountName' => 'Varchar(50)',
36
        'DiscountDetails' => 'Varchar(200)',
37
        'CustomsValue' => 'Currency',
38
    );
39
40
    /**
41
     * @var string
42
     */
43
    private static $singular_name = 'FoxyCart Category';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
44
45
    /**
46
     * @var string
47
     */
48
    private static $plural_name = 'FoxyCart Categories';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
49
50
    /**
51
     * @var string
52
     */
53
    private static $description = 'Set the FoxyCart Category on a Product';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
54
55
    /**
56
     * @var array
57
     */
58
    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...
59
        'Title' => 'Name',
60
        'Code' => 'Code'
61
    );
62
63
    /**
64
     * @var array
65
     */
66
    private static $indexes = array(
0 ignored issues
show
introduced by
The private property $indexes is not used, and could be removed.
Loading history...
67
        'Code' => true
68
    );
69
70
    /**
71
     * @var string
72
     */
73
    private static $table_name = 'FS_ProductCategory';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
74
75
    /**
76
     * @return FieldList
77
     */
78
    public function getCMSFields()
79
    {
80
        $fields = parent::getCMSFields();
81
82
        if ($this->ID) {
83
            if ($this->Title == 'Default') {
84
                $fields->replaceField(
85
                    'Title',
86
                    ReadonlyField::create('Title')
0 ignored issues
show
Bug introduced by
'Title' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
                    ReadonlyField::create(/** @scrutinizer ignore-type */ 'Title')
Loading history...
87
                );
88
            }
89
90
            $fields->replaceField(
91
                'Code',
92
                ReadonlyField::create('Code')
93
            );
94
        }
95
96
        $fields->insertBefore(HeaderField::create('DeliveryHD', 'Delivery Options', 3), 'DeliveryType');
0 ignored issues
show
Bug introduced by
'DeliveryType' of type string is incompatible with the type SilverStripe\Forms\FormField expected by parameter $item of SilverStripe\Forms\FieldList::insertBefore(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $fields->insertBefore(HeaderField::create('DeliveryHD', 'Delivery Options', 3), /** @scrutinizer ignore-type */ 'DeliveryType');
Loading history...
Bug introduced by
3 of type integer is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $fields->insertBefore(HeaderField::create('DeliveryHD', 'Delivery Options', /** @scrutinizer ignore-type */ 3), 'DeliveryType');
Loading history...
97
98
        $fields->replaceField(
99
            'DeliveryType',
100
            OptionsetField::create('DeliveryType', 'Delivery Type', $this->getShippingOptions())
101
        );
102
103
        $fields->dataFieldByName('MaxDownloads')
104
            ->displayIf('DeliveryType')->isEqualTo('downloaded');
105
106
        $fields->dataFieldByName('MaxDownloadsTime')
107
            ->displayIf('DeliveryType')->isEqualTo('downloaded');
108
109
        $fields->dataFieldByName('DefaultWeight')
110
            ->displayIf('DeliveryType')->isEqualTo('shipped');
111
112
        $fields->dataFieldByName('DefaultWeightUnit')
113
            ->displayIf('DeliveryType')->isEqualTo('shipped');
114
115
        $fields->dataFieldByName('DefaultLengthUnit')
116
            ->displayIf('DeliveryType')->isEqualTo('shipped');
117
118
        $fields->dataFieldByName('ShippingFlatRate')
119
            ->displayIf('DeliveryType')->isEqualTo('flat_rate');
120
121
        $fields->replaceField(
122
            'ShippingFlatRateType',
123
            DropdownField::create('ShippingFlatRateType', 'Flat Rate Type', $this->getShippingFlatRateTypes())
124
                ->setEmptyString('')
125
                ->displayIf('DeliveryType')->isEqualTo('flat_rate')->end()
126
        );
127
128
        $fields->insertBefore(HeaderField::create('HandlingHD', 'Handling Fees and Discounts', 3), 'HandlingFeeType');
129
130
        $fields->replaceField(
131
            'HandlingFeeType',
132
            DropdownField::create('HandlingFeeType', 'Handling Fee Type', $this->getHandlingFeeTypes())
133
                ->setEmptyString('')
134
                ->setDescription('This determines what type of Handling Fee you would like to use.')
135
        );
136
137
        $fields->dataFieldByName('HandlingFee')
138
            ->displayIf('HandlingFeeType')->isNotEqualTo('');
139
140
        $fields->dataFieldByName('HandlingFeeMinimum')
141
            ->displayIf('HandlingFeeType')->isEqualTo('flat_percent_with_minimum');
142
143
        $fields->dataFieldByName('HandlingFeePercentage')
144
            ->displayIf('HandlingFeeType')->isEqualTo('flat_percent_with_minimum')
145
            ->orIf('HandlingFeeType')->isEqualTo('flat_percent');
146
147
        $fields->replaceField(
148
            'DiscountType',
149
            DropdownField::create('DiscountType', 'Discount Type', $this->getDiscountTypes())
150
                ->setEmptyString('')
151
                ->setDescription('This determines what type of per category discount you would like to use, if any.')
152
        );
153
154
        $fields->dataFieldByName('DiscountName')
155
            ->displayIf('DiscountType')->isNotEqualTo('');
156
157
        $fields->dataFieldByName('DiscountDetails')
158
            ->displayIf('DiscountType')->isNotEqualTo('');
159
160
        $fields->dataFieldByName('CustomsValue')
161
            ->setDescription('Enter a dollar amount here for the declared customs value for international shipments. If you leave this blank, the sale price of the item will be used.');
162
163
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
164
        $fields = FieldList::create(
165
            LiteralField::create(
166
                'PCIntro',
167
                _t(
168
                    'ProductCategory.PCIntro',
169
                    '<p>Categories must be created in your
170
                        <a href="https://admin.foxycart.com/admin.php?ThisAction=ManageProductCategories" target="_blank">
171
                            FoxyCart Product Categories
172
                        </a>, and also manually created in FoxyStripe.
173
                    </p>'
174
                )
175
            ),
176
            TextField::create('Code')
177
                ->setTitle(_t('ProductCategory.Code', 'Category Code'))
178
                ->setDescription(_t('ProductCategory.CodeDescription', 'copy/paste from FoxyCart')),
179
            TextField::create('Title')
180
                ->setTitle(_t('ProductCategory.Title', 'Category Title'))
181
                ->setDescription(_t('ProductCategory.TitleDescription', 'copy/paste from FoxyCart')),
182
            DropdownField::create(
183
                'DeliveryType',
184
                'Delivery Type',
185
                singleton('ProductCategory')->dbObject('DeliveryType')->enumValues()
186
            )->setEmptyString('')
187
        );
188
189
        $this->extend('updateCMSFields', $fields);
190
        */
191
192
        return $fields;
193
    }
194
195
    /**
196
     * @throws \SilverStripe\ORM\ValidationException
197
     */
198
    public function requireDefaultRecords()
199
    {
200
        parent::requireDefaultRecords();
201
        $allCats = ProductCategory::get();
202
        if (!$allCats->count()) {
203
            $cat = new ProductCategory();
204
            $cat->Title = 'Default';
0 ignored issues
show
Bug Best Practice introduced by
The property Title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
205
            $cat->Code = 'DEFAULT';
0 ignored issues
show
Bug Best Practice introduced by
The property Code does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
206
            $cat->write();
207
        }
208
    }
209
210
    /**
211
     * @param bool $member
212
     * @return bool
213
     */
214
    public function canView($member = false)
215
    {
216
        return true;
217
    }
218
219
    /**
220
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
221
     * @return bool|int
222
     */
223
    public function canEdit($member = null)
224
    {
225
        return Permission::check('Product_CANCRUD', 'any', $member);
226
    }
227
228
    /**
229
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
230
     * @return bool|int
231
     */
232
    public function canDelete($member = null)
233
    {
234
235
        //don't allow deletion of DEFAULT category
236
        return ($this->Code == 'DEFAULT') ? false : Permission::check('Product_CANCRUD', 'any', $member);
237
    }
238
239
    /**
240
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
241
     * @return bool|int
242
     */
243
    public function canCreate($member = null, $context = [])
244
    {
245
        return Permission::check('Product_CANCRUD', 'any', $member);
246
    }
247
248
    /**
249
     * @return array
250
     */
251
    public function getShippingOptions()
252
    {
253
        return [
254
            'shipped' => 'Shipped using live shipping rates',
255
            'downloaded' => 'Downloaded by the customer',
256
            'flat_rate' => 'Shipped using a flat rate fee',
257
            'pickup' => 'Picked up by the customer',
258
            'notshipped' => 'No Shipping',
259
        ];
260
    }
261
262
    /**
263
     * @return array
264
     */
265
    public function getShippingFlatRateTypes()
266
    {
267
        return [
268
            'per_order' => 'Charge per order',
269
            'per_item' => 'Charge per item',
270
        ];
271
    }
272
273
    /**
274
     * @return array
275
     */
276
    public function getHandlingFeeTypes()
277
    {
278
        return [
279
            'flat_per_order' => 'Flat fee per order with products in this category',
280
            'flat_per_item' => 'Flat fee per product in this category',
281
            'flat_percent' => 'Flat fee per shipment + % of price for products in this category',
282
            'flat_percent_with_minimum' => 'Flat fee per shipment OR % of order total with products in this category. Whichever is greater.',
283
        ];
284
    }
285
286
    /**
287
     * @return array
288
     */
289
    public function getDiscountTypes()
290
    {
291
        return [
292
            'quantity_amount' => 'Discount by an amount based on the quantity',
293
            'quantity_percentage' => 'Discount by a percentage based on the quantity',
294
            'price_amount' => 'Discount by an amount based on the price in this category',
295
            'price_percentage' => 'Discount by a percentage based on the price in this category',
296
        ];
297
    }
298
299
    /**
300
     * @return array
301
     */
302
    public function getDataMap()
303
    {
304
        return [
305
            'name' => $this->Title,
306
            'code' => $this->Code,
307
            'item_delivery_type' => $this->DeliveryType,
0 ignored issues
show
Bug Best Practice introduced by
The property DeliveryType does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
308
            'max_downloads_per_customer' => $this->MaxDownloads,
0 ignored issues
show
Bug Best Practice introduced by
The property MaxDownloads does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
309
            'max_downloads_time_period' => $this->MaxDownloadsTime,
0 ignored issues
show
Bug Best Practice introduced by
The property MaxDownloadsTime does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
310
            'customs_value' => $this->CustomsValue,
0 ignored issues
show
Bug Best Practice introduced by
The property CustomsValue does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
311
            'default_weight' => $this->DefaultWeight,
0 ignored issues
show
Bug Best Practice introduced by
The property DefaultWeight does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
312
            'default_weight_unit' => $this->DefaultWeightUnit,
0 ignored issues
show
Bug Best Practice introduced by
The property DefaultWeightUnit does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
313
            'default_length_unit' => $this->DefautlLengthUnit,
0 ignored issues
show
Bug Best Practice introduced by
The property DefautlLengthUnit does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
314
            'shipping_flat_rate' => $this->ShippingFlatRate,
0 ignored issues
show
Bug Best Practice introduced by
The property ShippingFlatRate does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
315
            'shipping_flat_rate_type' => $this->ShippingFlatRateType,
0 ignored issues
show
Bug Best Practice introduced by
The property ShippingFlatRateType does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
316
            'handling_fee_type' => $this->HandlingFeeType,
0 ignored issues
show
Bug Best Practice introduced by
The property HandlingFeeType does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
317
            'handling_fee' => $this->HandlingFee,
0 ignored issues
show
Bug Best Practice introduced by
The property HandlingFee does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
318
            'handling_fee_minimum' => $this->HandlingFeeMinimum,
0 ignored issues
show
Bug Best Practice introduced by
The property HandlingFeeMinimum does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
319
            'handling_fee_percentage' => $this->HandlingFeePercentage,
0 ignored issues
show
Bug Best Practice introduced by
The property HandlingFeePercentage does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
320
            'discount_type' => $this->DiscountType,
0 ignored issues
show
Bug Best Practice introduced by
The property DiscountType does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
321
            'discount_name' => $this->DiscountName,
0 ignored issues
show
Bug Best Practice introduced by
The property DiscountName does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
322
            'discount_details' => $this->DiscountDetails,
0 ignored issues
show
Bug Best Practice introduced by
The property DiscountDetails does not exist on Dynamic\FoxyStripe\Model\ProductCategory. Since you implemented __get, consider adding a @property annotation.
Loading history...
323
        ];
324
    }
325
326
    /**
327
     * @throws \Psr\Container\NotFoundExceptionInterface
328
     */
329
    public function onAfterWrite()
330
    {
331
        parent::onAfterWrite();
332
333
        if ($this->isChanged()) {
334
            if ($fc = new FoxyStripeClient()) {
335
                $fc->putCategory($this->getDataMap());
336
            }
337
        }
338
    }
339
340
    /**
341
     * @throws \Psr\Container\NotFoundExceptionInterface
342
     */
343
    public function onAfterDelete()
344
    {
345
        parent::onAfterDelete();
346
347
        if ($fc = new FoxyStripeClient()) {
348
            $fc->deleteCategory($this->getDataMap());
349
        }
350
    }
351
}
352