From 02334cda294a5123e21cf0bfe30aed3f07731974 Mon Sep 17 00:00:00 2001 From: Touhidur Rahman Date: Fri, 20 Dec 2024 18:58:53 +0600 Subject: [PATCH] pkp/pkp-lib#10292 migrated controlled vocabs setting name from symbolics to name --- api/v1/vocabs/PKPVocabController.php | 4 +- classes/controlledVocab/ControlledVocab.php | 8 ---- .../controlledVocab/ControlledVocabEntry.php | 12 +----- classes/controlledVocab/Repository.php | 8 ++-- ..._UpdateControlledVocabEntrySettingName.php | 39 +++++++++++++++++++ classes/user/interest/Repository.php | 19 +++------ .../FormValidatorControlledVocabTest.php | 4 +- .../classes/metadata/MetadataPropertyTest.php | 2 +- .../ValidatorControlledVocabTest.php | 4 +- 9 files changed, 58 insertions(+), 42 deletions(-) create mode 100644 classes/migration/upgrade/v3_5_0/I10292_UpdateControlledVocabEntrySettingName.php diff --git a/api/v1/vocabs/PKPVocabController.php b/api/v1/vocabs/PKPVocabController.php index 8a5cf38652f..465a5087282 100644 --- a/api/v1/vocabs/PKPVocabController.php +++ b/api/v1/vocabs/PKPVocabController.php @@ -116,7 +116,7 @@ public function getMany(Request $illuminateRequest): JsonResponse ], Response::HTTP_BAD_REQUEST); } - if (ControlledVocab::hasDefinedVocabSymbolic($vocab)) { + if (in_array($vocab, ControlledVocab::getDefinedVocabSymbolic())) { $entries = ControlledVocabEntry::query() ->whereHas( 'controlledVocab', @@ -135,7 +135,7 @@ public function getMany(Request $illuminateRequest): JsonResponse $data = []; foreach ($entries as $entry) { - $data[] = $entry->getLocalizedData($vocab, $locale); + $data[] = $entry->getLocalizedData('name', $locale); } $data = array_values(array_unique($data)); diff --git a/classes/controlledVocab/ControlledVocab.php b/classes/controlledVocab/ControlledVocab.php index 6af1ea1e8e7..380e92705a1 100644 --- a/classes/controlledVocab/ControlledVocab.php +++ b/classes/controlledVocab/ControlledVocab.php @@ -96,14 +96,6 @@ public static function getDefinedVocabSymbolic(): array ]; } - /** - * Check if a provided vocab symbolic defined in pre defined symbolic list - */ - public static function hasDefinedVocabSymbolic(string $vocab): bool - { - return in_array($vocab, static::getDefinedVocabSymbolic()); - } - /** * Get all controlled vocab entries for this controlled vocab */ diff --git a/classes/controlledVocab/ControlledVocabEntry.php b/classes/controlledVocab/ControlledVocabEntry.php index e8086a159b4..052f98d8e91 100644 --- a/classes/controlledVocab/ControlledVocabEntry.php +++ b/classes/controlledVocab/ControlledVocabEntry.php @@ -83,11 +83,7 @@ public static function getSchemaName(): ?string */ public function getMultilingualProps(): array { - return array_merge( - $this->multilingualProps, - ControlledVocab::getDefinedVocabSymbolic(), - Arr::wrap(UserInterest::CONTROLLED_VOCAB_INTEREST) - ); + return array_merge($this->multilingualProps, ['name']); } /** @@ -95,11 +91,7 @@ public function getMultilingualProps(): array */ public function getSettings(): array { - return array_merge( - $this->settings, - ControlledVocab::getDefinedVocabSymbolic(), - Arr::wrap(UserInterest::CONTROLLED_VOCAB_INTEREST) - ); + return array_merge($this->settings, ['name']); } /** diff --git a/classes/controlledVocab/Repository.php b/classes/controlledVocab/Repository.php index 5ebdd2c4095..04ae0758b5c 100644 --- a/classes/controlledVocab/Repository.php +++ b/classes/controlledVocab/Repository.php @@ -53,13 +53,13 @@ public function getBySymbolic( ControlledVocabEntry::query() ->whereHas( - "controlledVocab", + 'controlledVocab', fn ($query) => $query->withSymbolics([$symbolic])->withAssoc($assocType, $assocId) ) ->when(!empty($locales), fn ($query) => $query->withLocales($locales)) ->get() - ->each(function ($entry) use (&$result, $symbolic) { - foreach ($entry->{$symbolic} as $locale => $value) { + ->each(function ($entry) use (&$result) { + foreach ($entry->name as $locale => $value) { $result[$locale][] = $value; } }); @@ -98,7 +98,7 @@ public function insertBySymbolic( ControlledVocabEntry::create([ 'controlledVocabId' => $controlledVocab->id, 'seq' => $index + 1, - "{$symbolic}" => [ + 'name' => [ $locale => $vocab ], ]) diff --git a/classes/migration/upgrade/v3_5_0/I10292_UpdateControlledVocabEntrySettingName.php b/classes/migration/upgrade/v3_5_0/I10292_UpdateControlledVocabEntrySettingName.php new file mode 100644 index 00000000000..636275f3a06 --- /dev/null +++ b/classes/migration/upgrade/v3_5_0/I10292_UpdateControlledVocabEntrySettingName.php @@ -0,0 +1,39 @@ +update(['setting_name' => 'name']); + } + + /** + * Reverse the migration. + */ + public function down(): void + { + throw new DowngradeNotSupportedException(); + } +} diff --git a/classes/user/interest/Repository.php b/classes/user/interest/Repository.php index 3923391efc3..c4b917d720a 100644 --- a/classes/user/interest/Repository.php +++ b/classes/user/interest/Repository.php @@ -37,18 +37,13 @@ public function getAllInterests(?string $filter = null): array ->withControlledVocabId($controlledVocab->id) ->when( $filter, - fn ($query) => $query->withSetting( - UserInterest::CONTROLLED_VOCAB_INTEREST, - $filter - ) + fn ($query) => $query->withSetting('name', $filter) ) ->get() ->sortBy(UserInterest::CONTROLLED_VOCAB_INTEREST) ->mapWithKeys( fn(ControlledVocabEntry $controlledVocabEntry, int $id) => [ - $id => collect( - $controlledVocabEntry->{UserInterest::CONTROLLED_VOCAB_INTEREST} - )->first() + $id => collect($controlledVocabEntry->name)->first() ] ) ->toArray(); @@ -73,9 +68,7 @@ public function getInterestsForUser(User $user): array ->get() ->mapWithKeys( fn(ControlledVocabEntry $controlledVocabEntry, int $id) => [ - $id => collect( - $controlledVocabEntry->{UserInterest::CONTROLLED_VOCAB_INTEREST} - )->first() + $id => collect($controlledVocabEntry->name)->first() ] ) ->toArray(); @@ -117,7 +110,7 @@ public function setInterestsForUser(User $user, string|array|null $interests = n ) ) ->withLocales(['']) - ->withSettings(UserInterest::CONTROLLED_VOCAB_INTEREST, $interests) + ->withSettings('name', $interests) ->get(); // Delete user's existing interests association. @@ -126,7 +119,7 @@ public function setInterestsForUser(User $user, string|array|null $interests = n $newInterestIds = collect( array_diff( $interests, - $currentInterests->pluck(UserInterest::CONTROLLED_VOCAB_INTEREST)->flatten()->toArray() + $currentInterests->pluck('name')->flatten()->toArray() ) ) ->map(fn (string $interest): string => trim($interest)) @@ -134,7 +127,7 @@ public function setInterestsForUser(User $user, string|array|null $interests = n ->map( fn (string $interest) => ControlledVocabEntry::create([ 'controlledVocabId' => $controlledVocab->id, - UserInterest::CONTROLLED_VOCAB_INTEREST => [ + 'name' => [ '' => $interest ], ])->id diff --git a/tests/classes/form/validation/FormValidatorControlledVocabTest.php b/tests/classes/form/validation/FormValidatorControlledVocabTest.php index c837efe9bb9..dcb60b01d99 100644 --- a/tests/classes/form/validation/FormValidatorControlledVocabTest.php +++ b/tests/classes/form/validation/FormValidatorControlledVocabTest.php @@ -47,14 +47,14 @@ public function testIsValid() $controlledVocabEntryId1 = ControlledVocabEntry::create([ 'controlledVocabId' => $testControlledVocab->id, - ControlledVocab::CONTROLLED_VOCAB_SUBMISSION_KEYWORD => [ + 'name' => [ 'en' => 'testEntry', ], ])->id; $controlledVocabEntryId2 = ControlledVocabEntry::create([ 'controlledVocabId' => $testControlledVocab->id, - ControlledVocab::CONTROLLED_VOCAB_SUBMISSION_KEYWORD => [ + 'name' => [ 'en' => 'testEntry', ], ])->id; diff --git a/tests/classes/metadata/MetadataPropertyTest.php b/tests/classes/metadata/MetadataPropertyTest.php index 7e71fd19f8a..4af2ee540d6 100644 --- a/tests/classes/metadata/MetadataPropertyTest.php +++ b/tests/classes/metadata/MetadataPropertyTest.php @@ -153,7 +153,7 @@ public function testValidateControlledVocabulary() $controlledVocabEntry = ControlledVocabEntry::create([ 'controlledVocabId' => $vocab->id, - ControlledVocab::CONTROLLED_VOCAB_SUBMISSION_KEYWORD => [ + 'name' => [ 'en' => 'testEntry', ], ]); diff --git a/tests/classes/validation/ValidatorControlledVocabTest.php b/tests/classes/validation/ValidatorControlledVocabTest.php index 3b760a8ccd7..b1d3afd05d8 100644 --- a/tests/classes/validation/ValidatorControlledVocabTest.php +++ b/tests/classes/validation/ValidatorControlledVocabTest.php @@ -42,14 +42,14 @@ public function testValidatorControlledVocab() $controlledVocabEntryId1 = ControlledVocabEntry::create([ 'controlledVocabId' => $testControlledVocab->id, - ControlledVocab::CONTROLLED_VOCAB_SUBMISSION_KEYWORD => [ + 'name' => [ 'en' => 'testEntry', ], ])->id; $controlledVocabEntryId2 = ControlledVocabEntry::create([ 'controlledVocabId' => $testControlledVocab->id, - ControlledVocab::CONTROLLED_VOCAB_SUBMISSION_KEYWORD => [ + 'name' => [ 'en' => 'testEntry', ], ])->id;