Skip to content

Commit

Permalink
JSON decode options fields on import
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Apr 28, 2023
1 parent bf14d88 commit bddb134
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Create Release
run-name: Create release for ${{ github.event.client_payload.version }}

on:
repository_dispatch:
types:
- craftcms/new-release

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: ncipollo/release-action@v1
with:
body: ${{ github.event.client_payload.notes }}
makeLatest: ${{ github.event.client_payload.latest }}
name: ${{ github.event.client_payload.version }}
prerelease: ${{ github.event.client_payload.prerelease }}
tag: ${{ github.event.client_payload.tag }}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Campaign

## 2.6.1 - 2023-04-28
### Added
- Added the ability to import options fields (multi-select, checkboxes, etc.) into contact fields ([#380](https://github.com/putyourlightson/craft-campaign/issues/380)).

## 2.6.0 - 2023-04-04
### Added
- Added messaging that explains why charts are not appearing in reports.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "putyourlightson/craft-campaign",
"description": "Send and manage email campaigns, contacts and mailing lists.",
"version": "2.6.0",
"version": "2.6.1",
"type": "craft-plugin",
"homepage": "https://putyourlightson.com/plugins/campaign",
"license": "proprietary",
Expand Down
20 changes: 16 additions & 4 deletions src/services/ImportsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Craft;
use craft\base\Component;
use craft\elements\User;
use craft\fields\BaseOptionsField;
use craft\fields\BaseRelationField;
use craft\helpers\Json;
use craft\helpers\Queue;
Expand All @@ -33,6 +34,14 @@ class ImportsService extends Component
*/
public const EVENT_AFTER_IMPORT = 'afterImport';

/**
* @const string[]
*/
public const JSON_DECODE_FIELDS = [
BaseOptionsField::class,
BaseRelationField::class,
];

/**
* @var array
*/
Expand Down Expand Up @@ -290,10 +299,13 @@ public function importRow(ImportModel $import, array $row, int $lineNumber): Imp
if ($index !== '' && isset($row[$index])) {
$values[$fieldHandle] = $row[$index];

// Attempt to JSON decode the value if this is a relation field.
$field = $contact->getFieldLayout()->getFieldByHandle($fieldHandle);
if ($field instanceof BaseRelationField) {
$values[$fieldHandle] = Json::decodeIfJson($row[$index]);

// JSON decode if the field is an instance of specific base fields.
foreach (self::JSON_DECODE_FIELDS as $class) {
if ($field instanceof $class) {
$values[$fieldHandle] = Json::decodeIfJson($row[$index]);
}
}
}
}
Expand Down Expand Up @@ -357,7 +369,7 @@ public function deleteImportById(int $importId): bool
/**
* Updates the search indexes of imported contacts.
*/
public function updateSearchIndexes()
public function updateSearchIndexes(): void
{
$customFields = Campaign::$plugin->settings->getContactFields();
$fieldHandles = array_map(fn($field) => $field->handle, $customFields);
Expand Down

0 comments on commit bddb134

Please sign in to comment.