Issues (88)

src/extensions/AccountControllerExtension.php (5 issues)

1
<?php
2
3
namespace SilverCommerce\ContactAdmin\Extensions;
4
5
use SilverStripe\i18n\i18n;
6
use SilverStripe\Forms\Form;
7
use SilverStripe\Core\Extension;
8
use SilverStripe\View\ArrayData;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\TextField;
11
use SilverStripe\Forms\FormAction;
12
use SilverStripe\Forms\HeaderField;
13
use SilverStripe\Forms\HiddenField;
14
use SilverStripe\ORM\PaginatedList;
15
use SilverStripe\Security\Security;
16
use SilverStripe\Control\Controller;
17
use SilverStripe\Forms\LiteralField;
18
use SilverStripe\Forms\CheckboxField;
19
use SilverStripe\Forms\DropdownField;
20
use SilverStripe\Forms\CompositeField;
21
use SilverStripe\Forms\RequiredFields;
22
use SilverStripe\ORM\ValidationResult;
23
use SilverStripe\Core\Injector\Injector;
24
use SilverCommerce\ContactAdmin\Model\ContactLocation;
25
use SilverCommerce\GeoZones\Forms\RegionSelectionField;
0 ignored issues
show
The type SilverCommerce\GeoZones\Forms\RegionSelectionField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use ilateral\SilverStripe\Users\Control\AccountController;
0 ignored issues
show
The type ilateral\SilverStripe\Us...ntrol\AccountController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
/**
29
 * Add extra fields to a user account (if the users module is
30
 * installed) to allow logged in users to see their invoices.
31
 *
32
 * @package orders
33
 */
34
class AccountControllerExtension extends Extension
35
{
36
    /**
37
     * Add extra URL endpoints
38
     *
39
     * @var array
40
     */
41
    private static $allowed_actions = [
0 ignored issues
show
The private property $allowed_actions is not used, and could be removed.
Loading history...
42
        "addresses",
43
        "addaddress",
44
        "editaddress",
45
        "removeaddress",
46
        "AddressForm"
47
    ];
48
49
    /**
50
     * Display all addresses associated with the current user
51
     *
52
     * @return HTMLText
0 ignored issues
show
The type SilverCommerce\ContactAdmin\Extensions\HTMLText was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
     */
54
    public function addresses()
55
    {
56
        $member = Security::getCurrentUser();
57
58
        $this
59
            ->getOwner()
60
            ->customise(
61
                [
62
                "Title" => _t(
63
                    "SilverCommerce\ContactAdmin.YourAddresses",
64
                    "Your Addresses"
65
                ),
66
                "MetaTitle" => _t(
67
                    "SilverCommerce\ContactAdmin.YourAddresses",
68
                    "Your Addresses"
69
                ),
70
                "Content" => $this->getOwner()->renderWith(
71
                    "SilverCommerce\\ContactAdmin\\Includes\\Addresses",
72
                    ["Contact" => $member->Contact()]
73
                )
74
                ]
75
            );
76
77
        $this->getOwner()->extend("updateAddresses");
78
79
        return $this
80
            ->getOwner()
81
            ->renderWith(
82
                [
83
                'AccountController_addresses',
84
                AccountController::class . '_addresses',
85
                'AccountController',
86
                AccountController::class,
87
                'Page'
88
                ]
89
            );
90
    }
91
92
    /**
93
     * Display all addresses associated with the current user
94
     *
95
     * @return HTMLText
96
     */
97
    public function addaddress()
98
    {
99
        $form = $this->getOwner()->AddressForm();
100
        $member = Security::getCurrentUser();
101
102
        $form
103
            ->Fields()
104
            ->dataFieldByName("ContactID")
105
            ->setValue($member->Contact()->ID);
106
107
        $this
108
            ->getOwner()
109
            ->customise(
110
                [
111
                "Title" => _t(
112
                    "SilverCommerce\ContactAdmin.AddAddress",
113
                    "Add Address"
114
                ),
115
                "MetaTitle" => _t(
116
                    "SilverCommerce\ContactAdmin.AddAddress",
117
                    "Add Address"
118
                ),
119
                "Form" => $form
120
                ]
121
            );
122
123
        $this->getOwner()->extend("updateAddAddress");
124
125
        return $this
126
            ->getOwner()
127
            ->renderWith(
128
                [
129
                'AccountController_addaddress',
130
                AccountController::class . '_addaddress',
131
                'AccountController',
132
                AccountController::class,
133
                'Page'
134
                ]
135
            );
136
    }
137
138
    /**
139
     * Display all addresses associated with the current user
140
     *
141
     * @return HTMLText
142
     */
143
    public function editaddress()
144
    {
145
        $member = Security::getCurrentUser();
146
        $id = $this->getOwner()->request->param("ID");
147
        $address = ContactLocation::get()->byID($id);
148
149
        if ($address && $address->canEdit($member)) {
150
            $form = $this->AddressForm();
151
            $form->loadDataFrom($address);
152
            $form
153
                ->Actions()
154
                ->dataFieldByName("action_doSaveAddress")
155
                ->setTitle(_t("SilverCommerce\ContactAdmin.Save", "Save"));
156
157
            $this
158
                ->getOwner()
159
                ->customise(
160
                    [
161
                    "Title" => _t(
162
                        "SilverCommerce\ContactAdmin.EditAddress",
163
                        "Edit Address"
164
                    ),
165
                    "MenuTitle" => _t(
166
                        "SilverCommerce\ContactAdmin.EditAddress",
167
                        "Edit Address"
168
                    ),
169
                    "Form" => $form
170
                    ]
171
                );
172
173
            $this->getOwner()->extend("updateEditAddress");
174
            
175
            return $this
176
                ->getOwner()
177
                ->renderWith(
178
                    [
179
                    'AccountController_editaddress',
180
                    AccountController::class . '_editaddress',
181
                    'AccountController',
182
                    AccountController::class,
183
                    'Page'
184
                    ]
185
                );
186
        } else {
187
            return $this->getOwner()->httpError(404);
188
        }
189
    }
190
191
    /**
192
     * Remove an addresses by the given ID (if allowed)
193
     *
194
     * @return HTMLText
195
     */
196
    public function removeaddress()
197
    {
198
        $member = Security::getCurrentUser();
199
        $id = $this->getOwner()->request->param("ID");
200
        $address = ContactLocation::get()->byID($id);
201
202
        if ($address && $address->canDelete($member)) {
203
            $address->delete();
204
            $this
205
                ->owner
206
                ->customise(
207
                    [
208
                    "Title" => _t(
209
                        "SilverCommerce\ContactAdmin.AddressRemoved",
210
                        "Address Removed"
211
                    ),
212
                    "MenuTitle" => _t(
213
                        "SilverCommerce\ContactAdmin.AddressRemoved",
214
                        "Address Removed"
215
                    )
216
                    ]
217
                );
218
219
            $this->getOwner()->extend("updateEditAddress");
220
221
            return $this
222
                ->getOwner()
223
                ->renderWith(
224
                    [
225
                    'AccountController_removeaddress',
226
                    AccountController::class . '_removeaddress',
227
                    'AccountController',
228
                    AccountController::class,
229
                    'Page'
230
                    ]
231
                );
232
        } else {
233
            return $this->getOwner()->httpError(404);
234
        }
235
    }
236
237
    /**
238
     * Form used for adding or editing addresses
239
     *
240
     * @return Form
241
     */
242
    public function AddressForm()
243
    {
244
        $location = Injector::inst()->get(ContactLocation::class);
245
246
        $fields = FieldList::create(
247
            HiddenField::create("ID")
248
        );
249
250
        $fields->merge($location->getFrontEndFields());
251
252
        // Remove the version field
253
        $fields->removeByName("Version");
254
255
        $fields->replaceField(
256
            "Country",
257
            DropdownField::create(
258
                'Country',
259
                $location->fieldLabel("Country"),
260
                i18n::getData()->getCountries()
261
            )->setEmptyString("")
262
        );
263
264
        if (class_exists(RegionSelectionField::class)) {
265
            $fields->replaceField(
266
                "County",
267
                RegionSelectionField::create(
268
                    "County",
269
                    $location->fieldLabel("County"),
270
                    "Country"
271
                )
272
            );
273
        }
274
275
        $fields->replaceField(
276
            "ContactID",
277
            HiddenField::create('ContactID')
278
        );
279
280
        $back_btn = '<a href="';
281
        $back_btn .= $this->getOwner()->Link('addresses');
282
        $back_btn .= '" class="btn btn-link">';
283
        $back_btn .= _t('SilverCommerce\ContactAdmin.Cancel', 'Cancel');
284
        $back_btn .= '</a>';
285
286
        $form = Form::create(
287
            $this->getOwner(),
288
            "AddressForm",
289
            $fields,
290
            FieldList::create(
291
                LiteralField::create(
292
                    'BackButton',
293
                    $back_btn
294
                ),
295
                FormAction::create(
296
                    'doSaveAddress',
297
                    _t('SilverCommerce\ContactAdmin.Add', 'Add')
298
                )->addExtraClass('btn btn-success')
299
            ),
300
            RequiredFields::create(
301
                [
302
                    'FirstName',
303
                    'Surname',
304
                    'Address1',
305
                    'City',
306
                    'PostCode',
307
                    'Country',
308
                ]
309
            )
310
        );
311
312
        $this->getOwner()->extend("updateAddressForm", $form);
313
314
        return $form;
315
    }
316
317
    /**
318
     * Method responsible for saving (or adding) an address.
319
     * If the ID field is set, the method assums we are saving
320
     * an address.
321
     *
322
     * If the ID field is not set, we assume a new address is being
323
     * created.
324
     */
325
    public function doSaveAddress($data, $form)
326
    {
327
        if (!$data["ID"]) {
328
            $new = true;
329
            $address = ContactLocation::create();
330
        } else {
331
            $new = false;
332
            $address = ContactLocation::get()->byID($data["ID"]);
333
        }
334
335
        if ($address) {
336
            $form->saveInto($address);
337
            $address->write();
338
            $form->sessionMessage(
339
                _t("SilverCommerce\ContactAdmin.AddressSaved", "Address Saved"),
340
                ValidationResult::TYPE_GOOD
341
            );
342
        } else {
343
            $form->sessionMessage(
344
                _t("SilverCommerce\ContactAdmin.Error", "There was an error"),
345
                ValidationResult::TYPE_ERROR
346
            );
347
        }
348
349
        // If a new record, redirect to base, else redirect back to the edit form
350
        if ($new && !empty($address)) {
351
            return $this
352
                ->getOwner()
353
                ->redirect(
354
                    Controller::join_links(
355
                        $this->getOwner()->Link("editaddress"),
356
                        $address->ID
357
                    )
358
                );
359
        } else {
360
            return $this
361
                ->getOwner()
362
                ->redirectBack();
363
        }
364
    }
365
366
    /**
367
     * Add commerce specific links to account menu
368
     *
369
     * @param ArrayList $menu
0 ignored issues
show
The type SilverCommerce\ContactAdmin\Extensions\ArrayList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
370
     */
371
    public function updateAccountMenu($menu)
372
    {
373
        $curr_action = $this
374
            ->owner
375
            ->getRequest()
376
            ->param("Action");
377
378
        $menu->add(
379
            ArrayData::create(
380
                [
381
                "ID"    => 11,
382
                "Title" => _t('SilverCommerce\ContactAdmin.Addresses', 'Addresses'),
383
                "Link"  => $this->getOwner()->Link("addresses"),
384
                "LinkingMode" => ($curr_action == "addresses") ? "current" : "link"
385
                ]
386
            )
387
        );
388
    }
389
}
390