From bddb1340f95cbecbe54213dbc63d19570c1ef105 Mon Sep 17 00:00:00 2001 From: bencroker Date: Fri, 28 Apr 2023 10:34:37 -0500 Subject: [PATCH] JSON decode options fields on import --- .github/workflows/create-release.yml | 21 +++++++++++++++++++++ CHANGELOG.md | 4 ++++ composer.json | 2 +- src/services/ImportsService.php | 20 ++++++++++++++++---- 4 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/create-release.yml diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml new file mode 100644 index 00000000..8e33492e --- /dev/null +++ b/.github/workflows/create-release.yml @@ -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 }} diff --git a/CHANGELOG.md b/CHANGELOG.md index c228c38e..86da6c23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/composer.json b/composer.json index 669ed873..d432404d 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/services/ImportsService.php b/src/services/ImportsService.php index 362bf832..bb19e338 100755 --- a/src/services/ImportsService.php +++ b/src/services/ImportsService.php @@ -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; @@ -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 */ @@ -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]); + } } } } @@ -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);