Completed
Pull Request — 1.0 (#28)
by Nic
11:32
created

CatalogProductInquiry::getProductIDForInquiry()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 8
nop 1
crap 20
1
<?php
2
3
/**
4
 * Class CatalogProductInquiry
5
 */
6
class CatalogProductInquiry extends DataObject
7
{
8
9
    /**
10
     * @var string
11
     */
12
    private static $singular_name = 'Product Inquiry';
13
14
    /**
15
     * @var string
16
     */
17
    private static $plural_name = 'Product Inquiries';
18
19
    /**
20
     * @var array
21
     */
22
    private static $db = [
23
        'Name' => 'Varchar(255)',
24
        'Phone' => 'Varchar',
25
        'Email' => 'Varchar(255)',
26
        'Comment' => 'Text',
27
    ];
28
29
    /**
30
     * @var array
31
     */
32
    private static $has_one = [
33
        'Product' => 'CatalogProduct',
34
    ];
35
36
    /**
37
     * @var array
38
     */
39
    private static $summary_fields = [
40
        'Created.Nice' => 'Inquiry Date',
41
        'Name' => 'Name',
42
    ];
43
44
    /**
45
     * @return FieldList
46
     */
47
    public function getCMSFields()
48
    {
49
        $fields = parent::getCMSFields();
50
51
        $fields->transform(new ReadonlyTransformation());
52
53
        return $fields;
54
    }
55
56
    /**
57
     * @param null $params
58
     *
59
     * @return FieldList
60
     */
61
    public function getFrontEndFields($params = null)
62
    {
63
        $fields = parent::getFrontEndFields($params);
64
65
        $fields->replaceField(
66
            'ProductID',
67
            HiddenField::create('ProductID')
68
                ->setValue(Controller::curr()->getProduct()->ID)
69
        );
70
71
        return $fields;
72
    }
73
74
    /**
75
     * @param $params
76
     *
77
     * @return bool|int
78
     */
79
    protected function getProductIDForInquiry($params)
80
    {
81
        if (isset($params['ProductID'])) {
82
            $product = CatalogProduct::get()->byID($params['ProductID']);
83
        } else {
84
            $product = CatalogProduct::get()->byURLSegment(Controller::curr()->getRequest()->param('ID'));
85
        }
86
87
        return (isset($product) && $product->ID) ? $product->ID : false;
88
    }
89
90
    /**
91
     * @return FieldList
92
     */
93
    public function getFrontEndActions()
94
    {
95
        $actions = FieldList::create(
96
            FormAction::create('doProductInquiry')
97
                ->setTitle('Send Inquiry')
98
        );
99
100
        $this->extend('updateFrontEndActions', $actions);
101
102
        return $actions;
103
    }
104
105
    public function getFrontEndRequiredFields()
106
    {
107
        $required = RequiredFields::create(
108
            'Name',
109
            'Email'
110
        );
111
112
        $this->extend('updateFrontEndRequiredFields', $required);
113
114
        return $required;
115
    }
116
117
}