Completed
Pull Request — master (#33)
by Franco
02:03
created

DMSCheckoutController::updateCartItems()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
3
class DMSCheckoutController extends ContentController
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
6
        'DMSDocumentRequestForm',
7
        'index',
8
        'complete',
9
        'send',
10
    );
11
12
    /**
13
     * An array containing the recipients basic information
14
     *
15
     * @config
16
     * @var array
17
     */
18
    private static $receiver_info = array(
0 ignored issues
show
Unused Code introduced by
The property $receiver_info is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
        'ReceiverName'            => '',
20
        'ReceiverPhone'           => '',
21
        'ReceiverEmail'           => '',
22
        'DeliveryAddressLine1'    => '',
23
        'DeliveryAddressLine2'    => '',
24
        'DeliveryAddressCountry'  => '',
25
        'DeliveryAddressPostCode' => '',
26
    );
27
28
    public function init()
29
    {
30
        parent::init();
31
32
        Requirements::css(DMS_CART_DIR . '/css/dms-cart.css');
33
    }
34
35
    public function index()
36
    {
37
        $form = $this->DMSDocumentRequestForm();
38
        return $this
39
            ->customise(array(
40
                'Form'  => $form,
41
                'Title' => _t(__CLASS__ . '.CHECKOUT_TITLE', 'Checkout')
42
            ))
43
            ->renderWith(array('Page', 'DMSDocumentRequestForm'));
44
    }
45
46
    /**
47
     * Gets and displays a list of items within the cart, as well as a contact form with entry
48
     * fields for the recipients information.
49
     *
50
     * To extend use the following from within an Extension subclass:
51
     *
52
     * <code>
53
     * public function updateDMSDocumentRequestForm($form)
54
     * {
55
     *     // Do something here
56
     * }
57
     * </code>
58
     *
59
     * @return Form
60
     */
61
    public function DMSDocumentRequestForm()
62
    {
63
        $fields = DMSDocumentCartSubmission::create()->scaffoldFormFields();
64
        $fields->replaceField('DeliveryAddressLine2', TextField::create('DeliveryAddressLine2', ''));
65
        $fields->replaceField('DeliveryAddressCountry', CountryDropdownField::create(
66
            'DeliveryAddressCountry',
67
            _t(__CLASS__ . '.RECEIVER_COUNTRY', 'Country')
68
        )->setValue('NZ'));
69
70
        $requiredFields = array(
71
            'ReceiverName',
72
            'ReceiverPhone',
73
            'ReceiverEmail',
74
            'DeliveryAddressLine1',
75
            'DeliveryAddressCountry',
76
            'DeliveryAddressPostCode',
77
        );
78
        foreach ($fields as $field) {
79
            if (in_array($field->name, $requiredFields)) {
80
                $field->addExtraClass('requiredField');
81
            }
82
        }
83
        $validator = RequiredFields::create($requiredFields);
84
        $actions = FieldList::create(
85
            FormAction::create(
86
                'doRequestSend',
87
                _t(__CLASS__ . '.SEND_ACTION', 'Send your request')
88
            )
89
        );
90
91
        $form = Form::create($this, 'DMSDocumentRequestForm', $fields, $actions, $validator);
92
93
        if ($receiverInfo = $this->getCart()->getReceiverInfo()) {
94
            $form->loadDataFrom($receiverInfo);
95
        }
96
97
        $form->setTemplate('DMSDocumentRequestForm');
98
        $this->extend('updateDMSDocumentRequestForm', $form);
99
100
        return $form;
101
    }
102
103
    /**
104
     * Sends an email to both the configured recipient as well as the requester. The
105
     * configured recipient is bcc'ed to the email in order to fulfill it.
106
     *
107
     * To extend use the following from within an Extension subclass:
108
     *
109
     * <code>
110
     * public function updateSend($email)
111
     * {
112
     *     // Do something here
113
     * }
114
     * </code>
115
     * @return mixed
116
     */
117
    public function send()
118
    {
119
        $cart = $this->getCart();
120
        $from = Config::inst()->get('Email', 'admin_email');
121
        $emailAddress = ($info = $cart->getReceiverInfo()) ? $info['ReceiverEmail'] : $from;
122
        $email = Email::create(
123
            $from,
124
            $emailAddress,
125
            _t(__CLASS__ . '.EMAIL_SUBJECT', 'Request for Printed Publications')
126
        );
127
128
        if ($bcc = $this->getConfirmationBcc()) {
129
            $email->setBcc($bcc);
130
        }
131
132
        $renderedCart = $cart->renderWith('DocumentCart_email');
133
        $body = sprintf(
134
            '<p>%s</p>',
135
            _t(
136
                __CLASS__ . '.EMAIL_BODY',
137
                'A request for printed publications has been submitted with the following details:'
138
            )
139
        );
140
        $body .= $renderedCart->getValue();
141
        $email->setBody($body)->setReplyTo($emailAddress);
142
        $this->extend('updateSend', $email);
143
144
        return $email->send();
145
    }
146
147
    /**
148
     * Handles form submission.
149
     * Totals requested are updated, delivery details added, email sent for fulfillment
150
     * and print request totals updated.
151
     *
152
     * @param array $data
153
     * @param Form $form
154
     * @param SS_HTTPRequest $request
155
     *
156
     * @return SS_HTTPResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be SS_HTTPResponse|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
157
     */
158
    public function doRequestSend($data, Form $form, SS_HTTPRequest $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
159
    {
160
        $this->updateCartReceiverInfo($data);
161
        $this->send();
162
        $this->getCart()->saveSubmission($form);
163
        $this->getCart()->emptyCart();
164
165
        return $this->redirect($this->Link('complete'));
166
    }
167
168
    /**
169
     * Displays the preconfigured thank you message to the user upon completion
170
     *
171
     * @return ViewableData
172
     */
173
    public function complete()
174
    {
175
        $data = array(
176
            'Title'   => _t(__CLASS__ . '.COMPLETE_THANKS', 'Thanks!'),
177
            'Content' => _t(
178
                __CLASS__ . '.COMPLETE_MESSAGE',
179
                'Thank you. You will receive a confirmation email shortly.'
180
            )
181
        );
182
183
        $this->extend('updateCompleteMessage', $data);
184
185
        return $this->customise($data)->renderWith('Page');
186
    }
187
188
    /**
189
     * Retrieves a {@link DMSDocumentCart} instance
190
     *
191
     * @return DMSDocumentCart
192
     */
193
    public function getCart()
194
    {
195
        return singleton('DMSDocumentCart');
196
    }
197
198
    /**
199
     * Updates the cart receiver info just before the request is sent.
200
     *
201
     * @param array $data
202
     */
203
    public function updateCartReceiverInfo($data)
204
    {
205
        $info = array_merge(static::config()->get('receiver_info'), $data);
206
        $this->getCart()->setReceiverInfo($info);
207
    }
208
209
    /**
210
     * Ensure that links for this controller use the customised route
211
     *
212
     * @param  string $action
0 ignored issues
show
Documentation introduced by
Should the type for parameter $action not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
213
     * @return string
214
     */
215
    public function Link($action = null)
216
    {
217
        return $this->join_links('checkout', $action);
218
    }
219
220
    /**
221
     * If BCC email addresses are configured, return the addresses to send to in comma delimited format
222
     *
223
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be false|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
224
     */
225
    protected function getConfirmationBcc()
226
    {
227
        $emails = (array) static::config()->get('recipient_emails');
228
        if (empty($emails)) {
229
            return false;
230
        }
231
232
        return implode(',', $emails);
233
    }
234
}
235