Skip to content

Commit

Permalink
pkp#10292 fixed vocab setting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Nov 15, 2024
1 parent bd68a3b commit 7cc935d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 43 deletions.
60 changes: 23 additions & 37 deletions classes/controlledVocab/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public function getBySymbolic(
ControlledVocabEntry::query()
->whereHas(
"controlledVocab",
fn($query) => $query->withSymbolic($symbolic)->withAssoc($assocType, $assocId)
fn ($query) => $query->withSymbolic($symbolic)->withAssoc($assocType, $assocId)
)
->withLocale($locales)
->when(!empty($locales), fn ($query) => $query->withLocale($locales))
->get()
->each(function ($entry) use (&$result, $symbolic) {
foreach ($entry->{$symbolic} as $locale => $value) {
Expand All @@ -65,20 +65,6 @@ public function getBySymbolic(
return $result;
}

/**
* Get an array of all of the vocabs for given symbolic
*/
public function getAllUniqueBySymbolic(string $symbolic): array
{
return DB::table('controlled_vocab_entry_settings')
->select('setting_value')
->where('setting_name', $symbolic)
->distinct()
->get()
->pluck('setting_value')
->toArray();
}

/**
* Add an array of vocabs
*/
Expand All @@ -88,7 +74,7 @@ public function insertBySymbolic(
int $assocType,
int $assocId,
bool $deleteFirst = true,
): bool
): void
{
$controlledVocab = $this->build($symbolic, $assocType, $assocId);
$controlledVocab->load('controlledVocabEntries');
Expand All @@ -98,52 +84,52 @@ public function insertBySymbolic(
DB::beginTransaction();

if ($deleteFirst) {
ControlledVocabEntry::whereIn(
'id',
$controlledVocab->controlledVocabEntries->pluck('id')->toArray()
)->delete();
ControlledVocabEntry::query()
->whereIn(
(new ControlledVocabEntry)->getKeyName(),
$controlledVocab->controlledVocabEntries->pluck('id')->toArray()
)
->delete();
}

collect($vocabs)
->each(
fn (array|string $entries, string $locale) => collect(Arr::wrap($entries))
fn (array|string $entries, string $locale) => collect(array_values(Arr::wrap($entries)))
->each(
fn (string $vocab, $seq = 1) =>
fn (string $vocab, int $index) =>
ControlledVocabEntry::create([
'controlledVocabId' => $controlledVocab->id,
'seq' => $seq,
'seq' => $index + 1,
"{$symbolic}" => [
$locale => $vocab
],
])
)
);

// TODO: Should Resequence?
DB::commit();

return true;
DB::commit();

} catch (Throwable $exception) {

DB::rollBack();
}

return false;
throw $exception;
}
}

/**
* Resequence controlled vocab entries for a given controlled vocab id
*/
public function resequence(int $controlledVocabId): void
{
$seq = 1;

ControlledVocabEntry::query()
->withControlledVocabId($controlledVocabId)
->each(
fn ($controlledVocabEntry, $seq = 1) => $controlledVocabEntry->update([
'seq' => $seq,
])
);
->each(function ($controlledVocabEntry) use (&$seq) {
$controlledVocabEntry->update([
'seq' => $seq++,
]);
});
}
}
10 changes: 4 additions & 6 deletions classes/user/interest/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Repository
/**
* Update a user's set of interests
*/
public function setUserInterests(array $interests, int $userId): bool
public function setUserInterests(array $interests, int $userId): void
{
$controlledVocab = Repo::controlledVocab()->build(
UserInterest::CONTROLLED_VOCAB_INTEREST
Expand Down Expand Up @@ -74,17 +74,15 @@ public function setUserInterests(array $interests, int $userId): bool
'controlledVocabEntryId' => $interestId,
]));

// TODO: Should Resequence?
Repo::controlledVocab()->resequence($controlledVocab->id);

DB::commit();

return true;

} catch (Throwable $exception) {

DB::rollBack();
}

return false;
throw $exception;
}
}
}

0 comments on commit 7cc935d

Please sign in to comment.