Issues (464)

src/Checkout/Step/Membership.php (1 issue)

1
<?php
2
3
namespace SilverShop\Checkout\Step;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverShop\Checkout\CheckoutComponentConfig;
7
use SilverShop\Checkout\Component\CustomerDetails;
8
use SilverShop\Forms\CheckoutForm;
9
use SilverShop\Model\Order;
10
use SilverShop\Page\CheckoutPage;
11
use SilverStripe\Control\Controller;
12
use SilverStripe\Control\HTTPRequest;
13
use SilverStripe\Forms\FieldList;
14
use SilverStripe\Forms\Form;
15
use SilverStripe\Forms\FormAction;
16
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
17
use SilverStripe\Security\MemberAuthenticator\MemberLoginForm;
18
use SilverStripe\Security\Security;
19
20
/**
21
 * Login, sign-up, or proceed as guest
22
 */
23
class Membership extends CheckoutStep
24
{
25
    private static $allowed_actions = [
0 ignored issues
show
The private property $allowed_actions is not used, and could be removed.
Loading history...
26
        'membership',
27
        'MembershipForm',
28
        'LoginForm',
29
        'createaccount',
30
        'docreateaccount',
31
        'CreateAccountForm',
32
    ];
33
34
    private static $url_handlers = [
35
        'login' => 'index',
36
    ];
37
38
    /**
39
     * Whether or not this step should be skipped if user is logged in
40
     *
41
     * @config
42
     * @var    bool
43
     */
44
    private static $skip_if_logged_in = true;
45
46
    public function membership()
47
    {
48
        //if logged in, then redirect to next step
49
        if (ShoppingCart::curr() && self::config()->skip_if_logged_in && Security::getCurrentUser()) {
50
            return Controller::curr()->redirect($this->NextStepLink());
51
        }
52
        return [
53
            'Form' => $this->MembershipForm(),
54
            'LoginForm' => $this->LoginForm(),
55
            'GuestLink' => $this->NextStepLink(),
56
        ];
57
    }
58
59
    public function MembershipForm()
60
    {
61
        $fields = FieldList::create();
62
        $actions = FieldList::create(
63
            FormAction::create(
64
                'createaccount',
65
                _t(
66
                    __CLASS__ . '.CreateAccount',
67
                    'Create an Account',
68
                    'This is an option presented to the user'
69
                )
70
            ),
71
            FormAction::create('guestcontinue', _t(__CLASS__ . '.ContinueAsGuest', 'Continue as Guest'))
72
        );
73
        $form = Form::create($this->owner, 'MembershipForm', $fields, $actions);
74
        $this->owner->extend('updateMembershipForm', $form);
75
        return $form;
76
    }
77
78
    public function guestcontinue()
79
    {
80
        $this->owner->redirect($this->NextStepLink());
81
    }
82
83
    public function LoginForm()
84
    {
85
        $form = MemberLoginForm::create($this->owner, MemberAuthenticator::class, 'LoginForm');
86
        $this->owner->extend('updateLoginForm', $form);
87
        return $form;
88
    }
89
90
    public function createaccount($requestdata)
91
    {
92
        //we shouldn't create an account if already a member
93
        if (Security::getCurrentUser()) {
94
            return Controller::curr()->redirect($this->NextStepLink());
95
        }
96
        //using this function to redirect, and display action
97
        if (!($requestdata instanceof HTTPRequest)) {
98
            return Controller::curr()->redirect($this->NextStepLink('createaccount'));
99
        }
100
        return [
101
            'Form' => $this->CreateAccountForm(),
102
        ];
103
    }
104
105
    public function registerconfig()
106
    {
107
        $order = ShoppingCart::curr();
108
        //hack to make components work when there is no order
109
        if (!$order) {
110
            $order = Order::create();
111
        }
112
        $config = CheckoutComponentConfig::create($order, false);
113
        $config->addComponent(CustomerDetails::create());
114
        $config->addComponent(\SilverShop\Checkout\Component\Membership::create());
115
116
        return $config;
117
    }
118
119
    public function CreateAccountForm()
120
    {
121
        $form = CheckoutForm::create($this->owner, 'CreateAccountForm', $this->registerconfig());
122
        $form->setActions(
123
            FieldList::create(
124
                FormAction::create(
125
                    'docreateaccount',
126
                    _t(
127
                        __CLASS__ . '.CreateNewAccount',
128
                        'Create New Account',
129
                        'This is an action (Button label)'
130
                    )
131
                )
132
            )
133
        );
134
        $form->getValidator()->addRequiredField('Password');
135
136
        $this->owner->extend('updateCreateAccountForm', $form);
137
        return $form;
138
    }
139
140
    public function docreateaccount($data, Form $form)
141
    {
142
        $this->registerconfig()->setData($form->getData());
143
144
        return Controller::curr()->redirect($this->NextStepLink());
145
    }
146
}
147