Passed
Pull Request — master (#11)
by Jason
01:22
created

Purchasable::updateCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 1
dl 0
loc 32
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Extension;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\CurrencyField;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\NumericField;
9
use SilverStripe\Forms\TextField;
10
use SilverStripe\ORM\DataExtension;
11
use SilverStripe\ORM\ValidationResult;
12
13
class Purchasable extends DataExtension
14
{
15
    /**
16
     * @var array
17
     */
18
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
        'Price' => 'Currency',
20
        'Code' => 'Varchar(100)',
21
        'ReceiptTitle' => 'HTMLVarchar(255)',
22
        'Available' => 'Boolean',
23
    ];
24
25
    /**
26
     * @var array
27
     */
28
    private static $indexes = [
0 ignored issues
show
introduced by
The private property $indexes is not used, and could be removed.
Loading history...
29
        'Code' => [
30
            'type' => 'unique',
31
            'columns' => ['Code'],
32
        ],
33
    ];
34
35
    /**
36
     * @var array
37
     */
38
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
39
        'ShowInMenus' => false,
40
        'Available' => true,
41
        'Weight' => '0.0',
42
    ];
43
44
    /**
45
     * @var array
46
     */
47
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
48
        'Image.CMSThumbnail',
49
        'Title',
50
        'Code',
51
        'Price.Nice',
52
    ];
53
54
    /**
55
     * @var array
56
     */
57
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
58
        'Title',
59
        'Code',
60
        'Available',
61
    ];
62
63
    /**
64
     * @param bool $includerelations
65
     *
66
     * @return array
67
     */
68
    public function updateFieldLabels(&$labels)
69
    {
70
        $labels['Title'] = _t(__CLASS__ . '.TitleLabel', 'Product Name');
71
        $labels['Code'] = _t(__CLASS__ . '.CodeLabel', 'Code');
72
        $labels['Price'] = _t(__CLASS__ . '.PriceLabel', 'Price');
73
        $labels['Price.Nice'] = _t(__CLASS__ . '.PriceLabel', 'Price');
74
        $labels['Available'] = _t(__CLASS__ . '.AvailableLabel', 'Available for purchase');
75
        $labels['Available.Nice'] = _t(__CLASS__ . '.AvailableLabelNice', 'Available');
76
        $labels['Image.CMSThumbnail'] = _t(__CLASS__ . '.ImageLabel', 'Image');
77
        $labels['ReceiptTitle'] = _t(__CLASS__ . '.ReceiptTitleLabel', 'Product title for receipt');
78
    }
79
80
    /**
81
     * @param FieldList $fields
82
     */
83
    public function updateCMSFields(FieldList $fields)
84
    {
85
        $fields->addFieldsToTab(
86
            'Root.Main',
87
            [
88
                TextField::create('Code')
89
                    ->setDescription(_t(
90
                        __CLASS__ . '.CodeDescription',
91
                        'Required, must be unique. Product identifier used by FoxyCart in transactions'
92
                    )),
93
                CurrencyField::create('Price')
94
                    ->setDescription(_t(
95
                        __CLASS__ . '.PriceDescription',
96
                        'Base price for this product. Can be modified using Product Options'
97
                    )),
98
            ],
99
            'Content'
100
        );
101
102
        // Details tab
103
        $fields->addFieldsToTab(
104
            'Root.Details',
105
            [
106
                TextField::create('ReceiptTitle')
107
                    ->setDescription(_t(
108
                        __CLASS__ . '.ReceiptTitleDescription',
109
                        'Optional. Alternate title to display on order receipt'
110
                    )),
111
                CheckboxField::create('Available')
112
                    ->setDescription(_t(
113
                        __CLASS__ . '.AvailableDescription',
114
                        'If unchecked, will remove "Add to Cart" form and instead display "Currently unavailable"'
115
                    )),
116
            ]
117
        );
118
    }
119
120
    /**
121
     * @return \SilverStripe\ORM\ValidationResult
122
     */
123
    public function validate(ValidationResult $validationResult)
124
    {
125
        if(!$this->owner->Code){
126
            $validationResult->addError(
127
                _t(__CLASS__ . '.CodeRequired','You must set a product code')
128
            );
129
        }
130
    }
131
}
132