CheckoutFieldFactory::getNotesField()   A
last analyzed

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
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverShop\Checkout;
4
5
use SilverShop\Model\Address;
6
use SilverShop\Page\CheckoutPage;
7
use SilverStripe\Forms\CheckboxField;
8
use SilverStripe\Forms\ConfirmedPasswordField;
9
use SilverStripe\Forms\EmailField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\HeaderField;
12
use SilverStripe\Forms\LiteralField;
13
use SilverStripe\Forms\OptionsetField;
14
use SilverStripe\Forms\PasswordField;
15
use SilverStripe\Forms\TextareaField;
16
use SilverStripe\Forms\TextField;
17
use SilverStripe\Omnipay\GatewayInfo;
18
use SilverStripe\Security\Member;
19
use SilverStripe\SiteConfig\SiteConfig;
20
21
/**
22
 * Factory for generating checkout fields
23
 *
24
 * @todo
25
 */
26
class CheckoutFieldFactory
27
{
28
    private static $inst;
29
30
    public static function singleton()
31
    {
32
        if (!self::$inst) {
33
            self::$inst = new CheckoutFieldFactory();
34
        }
35
        return self::$inst;
36
    }
37
38
    //prevent instantiation
39
    private function __construct()
40
    {
41
    }
42
43
    public function getContactFields($subset = [])
44
    {
45
        return $this->getSubset(
46
            FieldList::create(
47
                TextField::create('FirstName', _t('SilverShop\Model\Order.db_FirstName', 'First Name')),
48
                TextField::create('Surname', _t('SilverShop\Model\Order.db_Surname', 'Surname')),
49
                EmailField::create('Email', _t('SilverShop\Model\Order.db_Email', 'Email'))
50
            ),
51
            $subset
52
        );
53
    }
54
55
    public function getAddressFields($type = "shipping", $subset = [])
56
    {
57
        $address = singleton(Address::class);
58
        $fields = $address->getFormFields($type);
59
        return $this->getSubset($fields, $subset);
60
    }
61
62
    public function getMembershipFields()
63
    {
64
        $fields = $this->getContactFields();
65
        $idfield = Member::config()->unique_identifier_field;
66
        if (!$fields->fieldByName($idfield)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $fields->fieldByName($idfield) targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
67
            $fields->push(TextField::create($idfield, $idfield)); //TODO: scaffold the correct id field
68
        }
69
        $fields->push($this->getPasswordField());
70
        return $fields;
71
    }
72
73
    public function getPasswordFields()
74
    {
75
        $loginlink = "Security/login?BackURL=" . CheckoutPage::find_link(true);
76
        $fields = FieldList::create(
77
            HeaderField::create(_t('SilverShop\Checkout\CheckoutField.MembershipDetails', 'Membership Details'), 3),
78
            LiteralField::create(
79
                'MemberInfo',
80
                '<p class="message warning">' .
81
                _t(
82
                    'SilverShop\Checkout\CheckoutField.MemberLoginInfo',
83
                    'If you are already a member please <a href="{LoginUrl}">log in</a>',
84
                    '',
85
                    ['LoginUrl' => $loginlink]
86
                ) .
87
                '</p>'
88
            ),
89
            LiteralField::create(
90
                'AccountInfo',
91
                '<p>' . _t(
92
                    'SilverShop\Checkout\CheckoutField.AccountInfo',
93
                    'Please choose a password, so you can login and check your order history in the future'
94
                ) . '</p>'
95
            ),
96
            $pwf = $this->getPasswordField()
97
        );
98
        if (!Checkout::membership_required()) {
99
            $pwf->setCanBeEmpty(true);
100
        }
101
        return $fields;
102
    }
103
104
    public function getPaymentMethodFields()
105
    {
106
        //TODO: only get one field if there is no option
107
        return OptionsetField::create(
108
            'PaymentMethod',
109
            _t('SilverShop\Checkout\CheckoutField.PaymentType', "Payment Type"),
110
            GatewayInfo::getSupportedGateways(),
111
            array_keys(GatewayInfo::getSupportedGateways())
112
        );
113
    }
114
115
    public function getPasswordField($confirmed = true)
116
    {
117
        if ($confirmed) {
118
            return ConfirmedPasswordField::create('Password', _t('SilverShop\Checkout\CheckoutField.Password', 'Password'));
119
        }
120
        return PasswordField::create('Password', _t('SilverShop\Checkout\CheckoutField.Password', 'Password'));
121
    }
122
123
    public function getNotesField()
124
    {
125
        return TextareaField::create("Notes", _t("SilverShop\Model\Order.db_Notes", "Message"));
126
    }
127
128
    public function getTermsConditionsField()
129
    {
130
        $field = null;
131
132
        if (SiteConfig::current_site_config()->TermsPage()->exists()) {
133
            $termsPage = SiteConfig::current_site_config()->TermsPage();
134
135
            $field = CheckboxField::create(
136
                'ReadTermsAndConditions',
137
                _t(
138
                    'SilverShop\Checkout\Checkout.TermsAndConditionsLink',
139
                    'I agree to the terms and conditions stated on the <a href="{TermsPageLink}" target="new" title="Read the shop terms and conditions for this site">{TermsPageTitle}</a> page',
140
                    '',
141
                    ['TermsPageLink' => $termsPage->Link(), 'TermsPageTitle' => $termsPage->Title]
142
                )
143
            );
144
        }
145
146
        return $field;
147
    }
148
149
    /**
150
     * Helper function for reducing a field set to a given subset,
151
     * in the given order.
152
     *
153
     * @param FieldList $fields form fields to take a subset from.
154
     * @param array     $subset list of field names to return as subset
155
     *
156
     * @return FieldList subset of form fields
157
     */
158
    private function getSubset(FieldList $fields, $subset = [])
159
    {
160
        if (empty($subset)) {
161
            return $fields;
162
        }
163
        $subfieldlist = FieldList::create();
164
        foreach ($subset as $field) {
165
            if ($field = $fields->fieldByName($field)) {
166
                $subfieldlist->push($field);
167
            }
168
        }
169
        return $subfieldlist;
170
    }
171
}
172