Skip to content

Commit

Permalink
pkp#10292 refactored entry dao to model class
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Nov 15, 2024
1 parent c3f5adc commit 8a7f5eb
Show file tree
Hide file tree
Showing 29 changed files with 520 additions and 1,415 deletions.
21 changes: 16 additions & 5 deletions api/v1/vocabs/PKPVocabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Route;
use PKP\controlledVocab\ControlledVocab;
use PKP\controlledVocab\ControlledVocabEntry;
use PKP\core\PKPBaseController;
use PKP\core\PKPRequest;
use PKP\facades\Locale;
Expand Down Expand Up @@ -101,7 +102,12 @@ public function getMany(Request $illuminateRequest): JsonResponse
$vocab = $requestParams['vocab'] ?? '';
$locale = $requestParams['locale'] ?? Locale::getLocale();
$term = $requestParams['term'] ?? null;
$locales = array_merge($context->getSupportedSubmissionMetadataLocales(), isset($requestParams['submissionId']) ? Repo::submission()->get((int) $requestParams['submissionId'])?->getPublicationLanguages() ?? [] : []);
$locales = array_merge(
$context->getSupportedSubmissionMetadataLocales(),
isset($requestParams['submissionId'])
? (Repo::submission()->get((int) $requestParams['submissionId'])?->getPublicationLanguages() ?? [])
: []
);

if (!in_array($locale, $locales)) {
return response()->json([
Expand All @@ -110,17 +116,22 @@ public function getMany(Request $illuminateRequest): JsonResponse
}

if (ControlledVocab::hasDefinedVocabSymbolic($vocab)) {
/** @var \PKP\controlledVocab\ControlledVocabEntryDAO $entryDao */
$entryDao = Repo::controlledVocab()->getEntryDaoBySymbolic($vocab);
$entries = $entryDao->getByContextId($vocab, $context->getId(), $locale, $term)->toArray();
$entries = ControlledVocabEntry::query()
->whereHas(
"controlledVocab",
fn($query) => $query->withSymbolic($vocab)->withContextId($context->getId())
)
->withLocale($locale)
->when($term, fn ($query) => $query->withSetting($vocab, $term))
->get();
} else {
$entries = [];
Hook::call('API::vocabs::getMany', [$vocab, &$entries, $illuminateRequest, response(), $request]);
}

$data = [];
foreach ($entries as $entry) {
$data[] = $entry->getData($vocab, $locale);
$data[] = $entry->getLocalizedData($vocab, $locale);
}

$data = array_values(array_unique($data));
Expand Down
69 changes: 49 additions & 20 deletions classes/controlledVocab/ControlledVocab.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @file classes/controlledVocab/ControlledVocab.php
* @file lib/pkp/classes/controlledVocab/ControlledVocab.php
*
* Copyright (c) 2024 Simon Fraser University
* Copyright (c) 2024 John Willinsky
Expand All @@ -15,11 +15,13 @@
namespace PKP\controlledVocab;

use Eloquence\Behaviours\HasCamelCasing;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Builder;
use PKP\controlledVocab\ControlledVocabEntry;
use PKP\facades\Locale;

class ControlledVocab extends Model
Expand All @@ -36,39 +38,29 @@ class ControlledVocab extends Model
public const CONTROLLED_VOCAB_SUBMISSION_SUBJECT = 'submissionSubject';

/**
* The table associated with the model.
*
* @var string
* @copydoc \Illuminate\Database\Eloquent\Model::$table
*/
protected $table = 'controlled_vocabs';

/**
* The primary key for the model.
*
* @var string
* @copydoc \Illuminate\Database\Eloquent\Model::$primaryKey
*/
protected $primaryKey = 'controlled_vocab_id';

/**
* The attributes that aren't mass assignable.
*
* @var array<string>|bool
* @copydoc \Illuminate\Database\Eloquent\Concerns\GuardsAttributes::$guarded
*/
protected $guarded = [
'controlled_vocab_id',
];

/**
* Indicates if the model should be timestamped.
*
* @var bool
* @copydoc \Illuminate\Database\Eloquent\Concerns\HasTimestamps::$timestamps
*/
public $timestamps = false;

/**
* Get the attributes that should be cast.
*
* @return array<string, string>
* @copydoc \Illuminate\Database\Eloquent\Concerns\HasAttributes::casts
*/
protected function casts(): array
{
Expand All @@ -87,7 +79,7 @@ protected function id(): Attribute
return Attribute::make(
get: fn($value, $attributes) => $attributes[$this->primaryKey] ?? null,
set: fn($value) => [$this->primaryKey => $value],
);
)->shouldCache();
}

/**
Expand All @@ -112,6 +104,7 @@ public static function hasDefinedVocabSymbolic(string $vocab): bool
return in_array($vocab, static::getDefinedVocabSymbolic());
}

// TODO: Investigate if this is necessary anymore
/**
* Get the locale field names for this controlled vocab
*/
Expand All @@ -126,6 +119,7 @@ public function getLocaleFieldNames(): array
: [];
}

// TODO: Investigate if this is necessary anymore
/**
* Compatibility function for including note IDs in grids.
*
Expand All @@ -137,15 +131,23 @@ public function getId(): int
}

/**
* Scope a query to only include notes with a specific user ID.
* Get all controlled vocab entries for this controlled vocab
*/
public function controlledVocabEntries(): HasMany
{
return $this->hasMany(ControlledVocabEntry::class, 'controlled_vocab_id', 'controlled_vocab_id');
}

/**
* Scope a query to only include vocabs with a specific symbolic.
*/
public function scopeWithSymbolic(Builder $query, string $symbolic): Builder
{
return $query->where('symbolic', $symbolic);
}

/**
* Scope a query to only include notes with a specific assoc type and assoc ID.
* Scope a query to only include vocabs with a specific assoc type and assoc ID.
*/
public function scopeWithAssoc(Builder $query, int $assocType, int $assocId): Builder
{
Expand All @@ -154,13 +156,40 @@ public function scopeWithAssoc(Builder $query, int $assocType, int $assocId): Bu
->where('assoc_id', $assocId);
}

/**
* Scope a query to only include vocabs associated with given context id
*/
public function scopeWithContextId(Builder $query, int $contextId): Builder
{
return $query
->where(
fn ($query) => $query
->select('context_id')
->from('submissions')
->whereColumn(
DB::raw(
"(SELECT publications.submission_id
FROM publications
INNER JOIN {$this->table}
ON publications.publication_id = {$this->table}.assoc_id
LIMIT 1)"
),
'=',
'submissions.submission_id'
),
$contextId
);
}

/**
* Get a list of controlled vocabulary options.
*
* @return array $controlledVocabEntryId => name
*/
public function enumerate(string $settingName = 'name'): array
public function enumerate(?string $settingName = null): array
{
$settingName ??= $this->symbolic;

return DB::table('controlled_vocab_entries AS e')
->leftJoin(
'controlled_vocab_entry_settings AS l',
Expand Down
Loading

0 comments on commit 8a7f5eb

Please sign in to comment.