Skip to content

Commit

Permalink
#10292 migrated controlled vocabs setting name from symbolics to name
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Dec 21, 2024
1 parent fb469a3 commit 9414859
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 42 deletions.
4 changes: 2 additions & 2 deletions api/v1/vocabs/PKPVocabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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));
Expand Down
8 changes: 0 additions & 8 deletions classes/controlledVocab/ControlledVocab.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
12 changes: 2 additions & 10 deletions classes/controlledVocab/ControlledVocabEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,15 @@ 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']);
}

/**
* @copydoc \PKP\core\traits\ModelWithSettings::getSettings
*/
public function getSettings(): array
{
return array_merge(
$this->settings,
ControlledVocab::getDefinedVocabSymbolic(),
Arr::wrap(UserInterest::CONTROLLED_VOCAB_INTEREST)
);
return array_merge($this->settings, ['name']);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions classes/controlledVocab/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
Expand Down Expand Up @@ -98,7 +98,7 @@ public function insertBySymbolic(
ControlledVocabEntry::create([
'controlledVocabId' => $controlledVocab->id,
'seq' => $index + 1,
"{$symbolic}" => [
'name' => [
$locale => $vocab
],
])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* @file classes/migration/upgrade/v3_5_0/I10292_UpdateControlledVocabEntrySettingName.php
*
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class I10292_UpdateControlledVocabEntrySettingName
*
* @brief update the `setting_name` from sybmolics to `name` for `controlled_vocab_entry_settings` table
*/

namespace PKP\migration\upgrade\v3_5_0;

use Illuminate\Support\Facades\DB;
use PKP\install\DowngradeNotSupportedException;
use PKP\migration\Migration;

class I10292_UpdateControlledVocabEntrySettingName extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::table('controlled_vocab_entry_settings')
->update(['setting_name' => 'name']);
}

/**
* Reverse the migration.
*/
public function down(): void
{
throw new DowngradeNotSupportedException();
}
}
19 changes: 6 additions & 13 deletions classes/user/interest/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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.
Expand All @@ -126,15 +119,15 @@ 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))
->unique()
->map(
fn (string $interest) => ControlledVocabEntry::create([
'controlledVocabId' => $controlledVocab->id,
UserInterest::CONTROLLED_VOCAB_INTEREST => [
'name' => [
'' => $interest
],
])->id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/classes/metadata/MetadataPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function testValidateControlledVocabulary()

$controlledVocabEntry = ControlledVocabEntry::create([
'controlledVocabId' => $vocab->id,
ControlledVocab::CONTROLLED_VOCAB_SUBMISSION_KEYWORD => [
'name' => [
'en' => 'testEntry',
],
]);
Expand Down
4 changes: 2 additions & 2 deletions tests/classes/validation/ValidatorControlledVocabTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9414859

Please sign in to comment.