Skip to content

Commit

Permalink
commerceguys/addressing v2
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Feb 7, 2024
1 parent 7008e37 commit 7385078
Show file tree
Hide file tree
Showing 17 changed files with 472 additions and 299 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"ext-pdo": "*",
"ext-zip": "*",
"bacon/bacon-qr-code": "^2.0",
"commerceguys/addressing": "^1.2",
"commerceguys/addressing": "^2.1.1",
"composer/semver": "^3.3.2",
"craftcms/plugin-installer": "~1.6.0",
"craftcms/server-check": "~5.0.1",
Expand Down
566 changes: 272 additions & 294 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'id' => 'CraftCMS',
'name' => 'Craft CMS',
'version' => '5.0.0-alpha.13',
'schemaVersion' => '5.0.0.18',
'schemaVersion' => '5.0.0.19',
'minVersionRequired' => '4.4.0',
'basePath' => dirname(__DIR__), // Defines the @app alias
'runtimePath' => '@storage/runtime', // Defines the @runtime alias
Expand Down
16 changes: 16 additions & 0 deletions src/elements/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private static function _addressAttributes(): array
'sortingCode',
'addressLine1',
'addressLine2',
'addressLine3',
'organization',
'organizationTaxId',
'fullName',
Expand Down Expand Up @@ -201,6 +202,12 @@ public static function addressAttributeLabel(string $attribute, string $countryC
*/
public ?string $addressLine2 = null;

/**
* @var string|null Third line of the address
* @since 5.0.0
*/
public ?string $addressLine3 = null;

/**
* @var string|null Organization name
*/
Expand Down Expand Up @@ -409,6 +416,14 @@ public function getAddressLine2(): ?string
return $this->addressLine2;
}

/**
* @inheritdoc
*/
public function getAddressLine3(): ?string
{
return $this->addressLine3;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -583,6 +598,7 @@ public function afterSave(bool $isNew): void
$record->sortingCode = $this->sortingCode;
$record->addressLine1 = $this->addressLine1;
$record->addressLine2 = $this->addressLine2;
$record->addressLine3 = $this->addressLine3;
$record->organization = $this->organization;
$record->organizationTaxId = $this->organizationTaxId;
$record->fullName = $this->fullName;
Expand Down
1 change: 1 addition & 0 deletions src/elements/conditions/addresses/AddressCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected function selectableConditionRules(): array
return array_merge(parent::selectableConditionRules(), [
AddressLine1ConditionRule::class,
AddressLine2ConditionRule::class,
AddressLine3ConditionRule::class,
AdministrativeAreaConditionRule::class,
CountryConditionRule::class,
DependentLocalityConditionRule::class,
Expand Down
54 changes: 54 additions & 0 deletions src/elements/conditions/addresses/AddressLine3ConditionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace craft\elements\conditions\addresses;

use Craft;
use craft\base\conditions\BaseTextConditionRule;
use craft\base\ElementInterface;
use craft\elements\Address;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\AddressQuery;
use craft\elements\db\ElementQueryInterface;

/**
* Address address line 3 condition rule.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 5.0.0
*/
class AddressLine3ConditionRule extends BaseTextConditionRule implements ElementConditionRuleInterface
{
/**
* @inheritdoc
*/
public function getLabel(): string
{
return Craft::t('app', 'Address Line 3');
}

/**
* @inheritdoc
*/
public function getExclusiveQueryParams(): array
{
return ['addressLine3'];
}

/**
* @inheritdoc
*/
public function modifyQuery(ElementQueryInterface $query): void
{
/** @var AddressQuery $query */
$query->addressLine3($this->paramValue());
}

/**
* @inheritdoc
*/
public function matchElement(ElementInterface $element): bool
{
/** @var Address $element */
return $this->matchValue($element->addressLine3);
}
}
64 changes: 64 additions & 0 deletions src/elements/db/AddressQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,26 @@ class AddressQuery extends ElementQuery
*/
public ?string $addressLine2 = null;

/**
* @var string|null Narrows the query results based on the third address line the addresses have.
* ---
* ```php
* // fetch addresses by address line 3
* $addresses = \craft\elements\Address::find()
* ->addressLine3('Suite 212')
* ->all();
* ```
* ```twig
* {# fetch addresses by address line 3 #}
* {% set addresses = craft.addresses()
* .addressLine3('Suite 212')
* .all() %}
* ```
* @used-by addressLine3()
* @since 5.0.0
*/
public ?string $addressLine3 = null;

/**
* @var string|null Narrows the query results based on the full name the addresses have.
* ---
Expand Down Expand Up @@ -733,6 +753,45 @@ public function addressLine2(?string $value): static
return $this;
}

/**
* Narrows the query results based on the third address line the addresses have.
*
* Possible values include:
*
* | Value | Fetches addresses…
* | - | -
* | `'Suite 212'` | with an addressLine3 of `Suite 212`.
* | `'*Suite*'` | with an addressLine3 containing `Suite`.
* | `'Suite*'` | with an addressLine3 beginning with `Suite`.
*
* ---
*
* ```twig
* {# Fetch addresses at Suite 212 #}
* {% set {elements-var} = {twig-method}
* .addressLine3('Suite 212')
* .all() %}
* ```
*
* ```php
* // Fetch addresses at Suite 212
* ${elements-var} = {php-method}
* ->addressLine3('Suite 212')
* ->all();
* ```
*
* @param string|null $value The property value
* @return static self reference
* @uses $addressLine3
* @since 5.0.0
*/
public function addressLine3(?string $value): static
{
$this->addressLine3 = $value;

return $this;
}

/**
* Narrows the query results based on the full name the addresses have.
*
Expand Down Expand Up @@ -1158,6 +1217,7 @@ protected function beforePrepare(): bool
'addresses.sortingCode',
'addresses.addressLine1',
'addresses.addressLine2',
'addresses.addressLine3',
'addresses.organization',
'addresses.organizationTaxId',
'addresses.fullName',
Expand Down Expand Up @@ -1256,6 +1316,10 @@ protected function beforePrepare(): bool
$this->subQuery->andWhere(Db::parseParam('addresses.addressLine2', $this->addressLine2));
}

if ($this->addressLine3) {
$this->subQuery->andWhere(Db::parseParam('addresses.addressLine3', $this->addressLine3));
}

if ($this->lastName) {
$this->subQuery->andWhere(Db::parseParam('addresses.lastName', $this->lastName));
}
Expand Down
1 change: 1 addition & 0 deletions src/fieldlayoutelements/addresses/AddressField.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function formHtml(ElementInterface $element = null, bool $static = false)
'countryCode',
'addressLine1',
'addressLine2',
'addressLine3',
'administrativeArea',
'locality',
'dependentLocality',
Expand Down
2 changes: 2 additions & 0 deletions src/fields/Addresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ private function createAddressesFromSerializedData(array $value, ElementInterfac
'sortingCode',
'addressLine1',
'addressLine2',
'addressLine3',
'organization',
'organizationTaxId',
'latitude',
Expand Down Expand Up @@ -574,6 +575,7 @@ public function serializeValue(mixed $value, ?ElementInterface $element): mixed
'sortingCode' => $address->sortingCode,
'addressLine1' => $address->addressLine1,
'addressLine2' => $address->addressLine2,
'addressLine3' => $address->addressLine3,
'organization' => $address->organization,
'organizationTaxId' => $address->organizationTaxId,
'fullName' => $address->fullName,
Expand Down
5 changes: 5 additions & 0 deletions src/gql/interfaces/elements/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public static function getFieldDefinitions(): array
'type' => Type::string(),
'description' => 'Second line of the address',
],
'addressLine3' => [
'name' => 'addressLine3',
'type' => Type::string(),
'description' => 'Third line of the address',
],
'organization' => [
'name' => 'organization',
'type' => Type::string(),
Expand Down
5 changes: 5 additions & 0 deletions src/gql/types/input/Addresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public static function getType(): mixed
'type' => Type::string(),
'description' => 'Second line of the address',
],
'addressLine3' => [
'name' => 'addressLine3',
'type' => Type::string(),
'description' => 'Third line of the address',
],
'organization' => [
'name' => 'organization',
'type' => Type::string(),
Expand Down
13 changes: 13 additions & 0 deletions src/helpers/Cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,19 @@ public static function addressFieldsHtml(Address $address): string
'error-key' => 'addressLine2',
],
]) .
static::textFieldHtml([
'status' => $address->getAttributeStatus('addressLine3'),
'label' => $address->getAttributeLabel('addressLine3'),
'id' => 'addressLine3',
'name' => 'addressLine3',
'value' => $address->addressLine3,
'autocomplete' => $belongsToCurrentUser ? 'address-line3' : 'off',
'required' => isset($requiredFields['addressLine3']),
'errors' => $address->getErrors('addressLine3'),
'data' => [
'error-key' => 'addressLine3',
],
]) .
self::_subdivisionField(
$address,
'administrativeArea',
Expand Down
1 change: 1 addition & 0 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function createTables(): void
'sortingCode' => $this->string(),
'addressLine1' => $this->string(),
'addressLine2' => $this->string(),
'addressLine3' => $this->string(),
'organization' => $this->string(),
'organizationTaxId' => $this->string(),
'fullName' => $this->string(),
Expand Down
30 changes: 30 additions & 0 deletions src/migrations/m240207_182452_address_line_3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace craft\migrations;

use craft\db\Migration;
use craft\db\Table;

/**
* m240207_182452_address_line_3 migration.
*/
class m240207_182452_address_line_3 extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
$this->addColumn(Table::ADDRESSES, 'addressLine3', $this->string()->after('addressLine2'));
return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m240207_182452_address_line_3 cannot be reverted.\n";
return false;
}
}
1 change: 1 addition & 0 deletions src/records/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @property string|null $sortingCode Sorting code
* @property string|null $addressLine1 First line of the address block
* @property string|null $addressLine2 Second line of the address block
* @property string|null $addressLine3 Third line of the address block
* @property string|null $organization Organization name
* @property string|null $organizationTaxId Organization tax ID
* @property string|null $fullName Full name
Expand Down
3 changes: 2 additions & 1 deletion src/services/Addresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public function getFieldLabel(string $field, string $countryCode): string
AddressField::SORTING_CODE => Craft::t('app', 'Sorting Code'),
AddressField::ADDRESS_LINE1 => Craft::t('app', 'Address Line 1'),
AddressField::ADDRESS_LINE2 => Craft::t('app', 'Address Line 2'),
AddressField::ADDRESS_LINE3 => Craft::t('app', 'Address Line 3'),
AddressField::ORGANIZATION => Craft::t('app', 'Organization'),
AddressField::GIVEN_NAME => Craft::t('app', 'First Name'),
AddressField::ADDITIONAL_NAME => 'Additional Name', // Unused in Craft
Expand Down Expand Up @@ -321,9 +322,9 @@ public function getAdministrativeAreaTypeLabel(?string $type): string
AdministrativeAreaType::DO_SI => Craft::t('app', 'Do Si'),
AdministrativeAreaType::EMIRATE => Craft::t('app', 'Emirate'),
AdministrativeAreaType::ISLAND => Craft::t('app', 'Island'),
AdministrativeAreaType::OBLAST => Craft::t('app', 'Oblast'),
AdministrativeAreaType::PARISH => Craft::t('app', 'Parish'),
AdministrativeAreaType::PREFECTURE => Craft::t('app', 'Prefecture'),
AdministrativeAreaType::REGION => Craft::t('app', 'Region'),
AdministrativeAreaType::STATE => Craft::t('app', 'State'),
default => Craft::t('app', 'Province'),
};
Expand Down
5 changes: 3 additions & 2 deletions src/translations/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
'Access the site when the system is off' => 'Access the site when the system is off',
'Access {plugin}' => 'Access {plugin}',
'Accessibility' => 'Accessibility',
'Account' => 'Account',
'Account Security' => 'Account Security',
'Account Type' => 'Account Type',
'Account has not been activated.' => 'Account has not been activated.',
'Account locked. Try again in {time}.' => 'Account locked. Try again in {time}.',
'Account locked.' => 'Account locked.',
'Account suspended.' => 'Account suspended.',
'Account' => 'Account',
'Accounts with this permission could use it to escalate their own permissions.' => 'Accounts with this permission could use it to escalate their own permissions.',
'Action' => 'Action',
'Actions' => 'Actions',
Expand Down Expand Up @@ -65,6 +65,7 @@
'Address Fields' => 'Address Fields',
'Address Line 1' => 'Address Line 1',
'Address Line 2' => 'Address Line 2',
'Address Line 3' => 'Address Line 3',
'Address fields saved.' => 'Address fields saved.',
'Address' => 'Address',
'Addresses' => 'Addresses',
Expand Down Expand Up @@ -1076,7 +1077,6 @@
'OFF Label' => 'OFF Label',
'OK' => 'OK',
'ON Label' => 'ON Label',
'Oblast' => 'Oblast',
'Of the enabled sites above, which sites should entries in this section be saved to?' => 'Of the enabled sites above, which sites should entries in this section be saved to?',
'Offline' => 'Offline',
'One of {name}’s migrations failed.' => 'One of {name}’s migrations failed.',
Expand Down Expand Up @@ -1236,6 +1236,7 @@
'Refresh' => 'Refresh',
'Regenerate' => 'Regenerate',
'Regenerating project config YAML files from the loaded project config…' => 'Regenerating project config YAML files from the loaded project config…',
'Region' => 'Region',
'Register users' => 'Register users',
'Relate {type} from a specific site?' => 'Relate {type} from a specific site?',
'Related To' => 'Related To',
Expand Down

0 comments on commit 7385078

Please sign in to comment.