Completed
Push — master ( 87a4b0...824833 )
by
unknown
14s
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 Method

Rating   Name   Duplication   Size   Complexity  
A DMSCheckoutController::getConfirmationBcc() 0 9 2
1
<?php
2
3
class DMSCheckoutController extends DMSCartAbstractController
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
        $this->getCart()->setBackUrl(Director::absoluteBaseURL().$this->Link());
38
        $form = $this->DMSDocumentRequestForm();
39
        return $this
40
            ->customise(array(
41
                'Form'  => $form,
42
                'Title' => _t(__CLASS__ . '.CHECKOUT_TITLE', 'Checkout')
43
            ))
44
            ->renderWith(array('Page', 'DMSDocumentRequestForm'));
45
    }
46
47
    /**
48
     * Gets and displays a list of items within the cart, as well as a contact form with entry
49
     * fields for the recipients information.
50
     *
51
     * To extend use the following from within an Extension subclass:
52
     *
53
     * <code>
54
     * public function updateDMSDocumentRequestForm($form)
55
     * {
56
     *     // Do something here
57
     * }
58
     * </code>
59
     *
60
     * @return Form
61
     */
62
    public function DMSDocumentRequestForm()
63
    {
64
        $fields = DMSDocumentCartSubmission::create()->scaffoldFormFields();
65
        $fields->replaceField('DeliveryAddressLine2', TextField::create('DeliveryAddressLine2', ''));
66
        $fields->replaceField('DeliveryAddressCountry', CountryDropdownField::create(
67
            'DeliveryAddressCountry',
68
            _t(__CLASS__ . '.RECEIVER_COUNTRY', 'Country')
69
        )->setValue('NZ'));
70
71
        $requiredFields = array(
72
            'ReceiverName',
73
            'ReceiverPhone',
74
            'ReceiverEmail',
75
            'DeliveryAddressLine1',
76
            'DeliveryAddressCountry',
77
            'DeliveryAddressPostCode',
78
        );
79
        foreach ($fields as $field) {
80
            if (in_array($field->name, $requiredFields)) {
81
                $field->addExtraClass('requiredField');
82
            }
83
        }
84
        $validator = RequiredFields::create($requiredFields);
85
        $actions = FieldList::create(
86
            FormAction::create(
87
                'doRequestSend',
88
                _t(__CLASS__ . '.SEND_ACTION', 'Send your request')
89
            )
90
        );
91
92
        $form = Form::create($this, 'DMSDocumentRequestForm', $fields, $actions, $validator);
93
94
        if ($receiverInfo = $this->getCart()->getReceiverInfo()) {
95
            $form->loadDataFrom($receiverInfo);
96
        }
97
98
        $form->setTemplate('DMSDocumentRequestForm');
99
        $this->extend('updateDMSDocumentRequestForm', $form);
100
101
        return $form;
102
    }
103
104
    /**
105
     * Sends an email to both the configured recipient as well as the requester. The
106
     * configured recipient is bcc'ed to the email in order to fulfill it.
107
     *
108
     * To extend use the following from within an Extension subclass:
109
     *
110
     * <code>
111
     * public function updateSend($email)
112
     * {
113
     *     // Do something here
114
     * }
115
     * </code>
116
     * @return mixed
117
     */
118
    public function send()
119
    {
120
        $cart = $this->getCart();
121
        $from = Config::inst()->get('Email', 'admin_email');
122
        $emailAddress = ($info = $cart->getReceiverInfo()) ? $info['ReceiverEmail'] : $from;
123
        $email = Email::create(
124
            $from,
125
            $emailAddress,
126
            _t(__CLASS__ . '.EMAIL_SUBJECT', 'Request for Printed Publications')
127
        );
128
129
        if ($bcc = $this->getConfirmationBcc()) {
130
            $email->setBcc($bcc);
131
        }
132
133
        $renderedCart = $cart->renderWith('DocumentCart_email');
134
        $body = sprintf(
135
            '<p>%s</p>',
136
            _t(
137
                __CLASS__ . '.EMAIL_BODY',
138
                'A request for printed publications has been submitted with the following details:'
139
            )
140
        );
141
        $body .= $renderedCart->getValue();
142
        $email->setBody($body)->setReplyTo($emailAddress);
143
        $this->extend('updateSend', $email);
144
145
        return $email->send();
146
    }
147
148
    /**
149
     * Handles form submission.
150
     * Totals requested are updated, delivery details added, email sent for fulfillment
151
     * and print request totals updated.
152
     *
153
     * @param array $data
154
     * @param Form $form
155
     * @param SS_HTTPRequest $request
156
     *
157
     * @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...
158
     */
159
    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...
160
    {
161
        $this->updateCartReceiverInfo($data);
162
        $this->send();
163
        $this->getCart()->saveSubmission($form);
164
        $this->getCart()->emptyCart();
165
166
        return $this->redirect($this->Link('complete'));
167
    }
168
169
    /**
170
     * Displays the preconfigured thank you message to the user upon completion
171
     *
172
     * @return ViewableData
173
     */
174
    public function complete()
175
    {
176
        $data = array(
177
            'Title'   => _t(__CLASS__ . '.COMPLETE_THANKS', 'Thanks!'),
178
            'Content' => _t(
179
                __CLASS__ . '.COMPLETE_MESSAGE',
180
                'Thank you. You will receive a confirmation email shortly.'
181
            )
182
        );
183
184
        $this->extend('updateCompleteMessage', $data);
185
186
        return $this->customise($data)->renderWith('Page');
187
    }
188
189
    /**
190
     * Updates the cart receiver info just before the request is sent.
191
     *
192
     * @param array $data
193
     */
194
    public function updateCartReceiverInfo($data)
195
    {
196
        $info = array_merge(static::config()->get('receiver_info'), $data);
197
        $this->getCart()->setReceiverInfo($info);
198
    }
199
200
    /**
201
     * If BCC email addresses are configured, return the addresses to send to in comma delimited format
202
     *
203
     * @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...
204
     */
205
    protected function getConfirmationBcc()
206
    {
207
        $emails = (array) static::config()->get('recipient_emails');
208
        if (empty($emails)) {
209
            return false;
210
        }
211
212
        return implode(',', $emails);
213
    }
214
}
215