-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathAddressesController.php
More file actions
92 lines (82 loc) · 2.68 KB
/
AddressesController.php
File metadata and controls
92 lines (82 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\controllers;
use Craft;
use craft\elements\Address;
use craft\helpers\Cp;
use craft\web\Controller;
use yii\web\Response;
/** @noinspection ClassOverridesFieldOfSuperClassInspection */
/**
* The AddressController class is a controller that handles various address-related actions.
* Note that all actions in the controller require an authenticated Craft session as well as the relevant permissions.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.0.0
*/
class AddressesController extends Controller
{
/**
* Returns address fields’ HTML (sans country) for the given country and subdivisions.
*
* @param string $namespace
* @param string $countryCode
* @param string|null $administrativeArea
* @param string|null $locality
* @return Response
*/
public function actionFields(
string $namespace,
string $countryCode,
?string $administrativeArea = null,
?string $locality = null,
): Response {
$address = new Address([
'countryCode' => $countryCode,
'administrativeArea' => $administrativeArea,
'locality' => $locality,
]);
$html = $this->getView()->namespaceInputs(fn() => Cp::addressFieldsHtml($address), $namespace);
return $this->asJson([
'fieldsHtml' => $html,
'headHtml' => $this->getView()->getHeadHtml(),
'bodyHtml' => $this->getView()->getBodyHtml(),
]);
}
/**
* Saves the address field layout.
*
* @return Response|null
*/
public function actionSaveFieldLayout(): ?Response
{
$this->requirePostRequest();
$this->requireAdmin();
// Set the field layout
$fieldLayout = Craft::$app->getFields()->assembleLayoutFromPost();
$fieldLayout->type = Address::class;
$fieldLayout->reservedFieldHandles = [
'address',
'countryCode',
'fullName',
'latLong',
'organization',
'organizationTaxId',
];
if (!Craft::$app->getAddresses()->saveFieldLayout($fieldLayout)) {
Craft::$app->getUrlManager()->setRouteParams([
'variables' => [
'fieldLayout' => $fieldLayout,
],
]);
$this->setFailFlash(Craft::t('app', 'Couldn’t save address fields.'));
return null;
}
$this->setSuccessFlash(Craft::t('app', 'Address fields saved.'));
return $this->redirectToPostedUrl();
}
}