Skip to content

Commit

Permalink
pkp#10292 WIP: Controlled Vocab DAO to Eloquent Model
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Aug 19, 2024
1 parent d1e3e38 commit a581dc9
Show file tree
Hide file tree
Showing 23 changed files with 606 additions and 720 deletions.
12 changes: 6 additions & 6 deletions api/v1/vocabs/PKPVocabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
use PKP\security\authorization\UserRolesRequiredPolicy;
use PKP\security\Role;
use PKP\submission\SubmissionAgencyVocab;
use PKP\submission\SubmissionDisciplineDAO;
use PKP\submission\SubmissionKeywordDAO;
use PKP\submission\SubmissionSubjectDAO;
use PKP\submission\SubmissionDisciplineVocab;
use PKP\submission\SubmissionKeywordVocab;
use PKP\submission\SubmissionSubjectVocab;

class PKPVocabController extends PKPBaseController
{
Expand Down Expand Up @@ -114,15 +114,15 @@ public function getMany(Request $illuminateRequest): JsonResponse
}

switch ($vocab) {
case SubmissionKeywordDAO::CONTROLLED_VOCAB_SUBMISSION_KEYWORD:
case SubmissionKeywordVocab::CONTROLLED_VOCAB_SUBMISSION_KEYWORD:
$submissionKeywordEntryDao = DAORegistry::getDAO('SubmissionKeywordEntryDAO'); /** @var \PKP\submission\SubmissionKeywordEntryDAO $submissionKeywordEntryDao */
$entries = $submissionKeywordEntryDao->getByContextId($vocab, $context->getId(), $locale, $term)->toArray();
break;
case SubmissionSubjectDAO::CONTROLLED_VOCAB_SUBMISSION_SUBJECT:
case SubmissionSubjectVocab::CONTROLLED_VOCAB_SUBMISSION_SUBJECT:
$submissionSubjectEntryDao = DAORegistry::getDAO('SubmissionSubjectEntryDAO'); /** @var \PKP\submission\SubmissionSubjectEntryDAO $submissionSubjectEntryDao */
$entries = $submissionSubjectEntryDao->getByContextId($vocab, $context->getId(), $locale, $term)->toArray();
break;
case SubmissionDisciplineDAO::CONTROLLED_VOCAB_SUBMISSION_DISCIPLINE:
case SubmissionDisciplineVocab::CONTROLLED_VOCAB_SUBMISSION_DISCIPLINE:
$submissionDisciplineEntryDao = DAORegistry::getDAO('SubmissionDisciplineEntryDAO'); /** @var \PKP\submission\SubmissionDisciplineEntryDAO $submissionDisciplineEntryDao */
$entries = $submissionDisciplineEntryDao->getByContextId($vocab, $context->getId(), $locale, $term)->toArray();
break;
Expand Down
4 changes: 2 additions & 2 deletions classes/components/forms/publication/Details.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use APP\publication\Publication;
use PKP\components\forms\FieldControlledVocab;
use PKP\context\Context;
use PKP\submission\SubmissionKeywordDAO;
use PKP\submission\SubmissionKeywordVocab;

class Details extends TitleAbstractForm
{
Expand Down Expand Up @@ -46,7 +46,7 @@ public function __construct(
'label' => __('common.keywords'),
'description' => __('manager.setup.metadata.keywords.description'),
'isMultilingual' => true,
'apiUrl' => str_replace('__vocab__', SubmissionKeywordDAO::CONTROLLED_VOCAB_SUBMISSION_KEYWORD, $suggestionUrlBase),
'apiUrl' => str_replace('__vocab__', SubmissionKeywordVocab::CONTROLLED_VOCAB_SUBMISSION_KEYWORD, $suggestionUrlBase),
'locales' => $this->locales,
'value' => (array) $publication->getData('keywords'),
'isRequired' => $context->getData('keywords') === Context::METADATA_REQUIRE ? true : false,
Expand Down
12 changes: 6 additions & 6 deletions classes/components/forms/publication/PKPMetadataForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
use PKP\components\forms\FormComponent;
use PKP\context\Context;
use PKP\submission\SubmissionAgencyVocab;
use PKP\submission\SubmissionDisciplineDAO;
use PKP\submission\SubmissionKeywordDAO;
use PKP\submission\SubmissionSubjectDAO;
use PKP\submission\SubmissionDisciplineVocab;
use PKP\submission\SubmissionKeywordVocab;
use PKP\submission\SubmissionSubjectVocab;

class PKPMetadataForm extends FormComponent
{
Expand Down Expand Up @@ -55,7 +55,7 @@ public function __construct(string $action, array $locales, Publication $publica
'label' => __('common.keywords'),
'tooltip' => __('manager.setup.metadata.keywords.description'),
'isMultilingual' => true,
'apiUrl' => str_replace('__vocab__', SubmissionKeywordDAO::CONTROLLED_VOCAB_SUBMISSION_KEYWORD, $suggestionUrlBase),
'apiUrl' => str_replace('__vocab__', SubmissionKeywordVocab::CONTROLLED_VOCAB_SUBMISSION_KEYWORD, $suggestionUrlBase),
'locales' => $this->locales,
'value' => (array) $publication->getData('keywords'),
]));
Expand All @@ -66,7 +66,7 @@ public function __construct(string $action, array $locales, Publication $publica
'label' => __('common.subjects'),
'tooltip' => __('manager.setup.metadata.subjects.description'),
'isMultilingual' => true,
'apiUrl' => str_replace('__vocab__', SubmissionSubjectDAO::CONTROLLED_VOCAB_SUBMISSION_SUBJECT, $suggestionUrlBase),
'apiUrl' => str_replace('__vocab__', SubmissionSubjectVocab::CONTROLLED_VOCAB_SUBMISSION_SUBJECT, $suggestionUrlBase),
'locales' => $this->locales,
'value' => (array) $publication->getData('subjects'),
]));
Expand All @@ -77,7 +77,7 @@ public function __construct(string $action, array $locales, Publication $publica
'label' => __('search.discipline'),
'tooltip' => __('manager.setup.metadata.disciplines.description'),
'isMultilingual' => true,
'apiUrl' => str_replace('__vocab__', SubmissionDisciplineDAO::CONTROLLED_VOCAB_SUBMISSION_DISCIPLINE, $suggestionUrlBase),
'apiUrl' => str_replace('__vocab__', SubmissionDisciplineVocab::CONTROLLED_VOCAB_SUBMISSION_DISCIPLINE, $suggestionUrlBase),
'locales' => $this->locales,
'value' => (array) $publication->getData('disciplines'),
]));
Expand Down
42 changes: 28 additions & 14 deletions classes/controlledVocab/ControlledVocab.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class ControlledVocab extends Model
'controlled_vocab_id',
];

/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;

protected function casts(): array
{
return [
Expand Down Expand Up @@ -81,7 +88,7 @@ public function getData(?string $field): mixed
*/
public function scopeWithSymbolic(Builder $query, string $symbolic): Builder
{
return $query->where(DB::raw('lower(symbolic)'), strtolower($symbolic));
return $query->where(DB::raw('LOWER(symbolic)'), strtolower($symbolic));
}

/**
Expand All @@ -103,31 +110,38 @@ public function scopeWithAssoc(Builder $query, int $assocType, int $assocId): Bu
public function enumerate(string $settingName = 'name'): array
{
return DB::table('controlled_vocab_entries AS e')
->leftJoin('controlled_vocab_entry_settings AS l', fn (JoinClause $join) => $join
->on('l.controlled_vocab_entry_id', '=', 'e.controlled_vocab_entry_id')
->where('l.setting_name', '=', $settingName)
->where('l.locale', '=', Locale::getLocale())
->leftJoin(
'controlled_vocab_entry_settings AS l',
fn (JoinClause $join) => $join
->on('l.controlled_vocab_entry_id', '=', 'e.controlled_vocab_entry_id')
->where('l.setting_name', $settingName)
->where('l.locale', Locale::getLocale())
)
->leftJoin('controlled_vocab_entry_settings AS p', fn (JoinClause $join) => $join
->leftJoin(
'controlled_vocab_entry_settings AS p',
fn (JoinClause $join) => $join
->on('p.controlled_vocab_entry_id', '=', 'e.controlled_vocab_entry_id')
->where('p.setting_name', '=', $settingName)
->where('p.locale', '=', Locale::getPrimaryLocale())
->where('p.setting_name', $settingName)
->where('p.locale', Locale::getPrimaryLocale())
)
->leftJoin('controlled_vocab_entry_settings AS n', fn (JoinClause $join) => $join
->leftJoin(
'controlled_vocab_entry_settings AS n',
fn (JoinClause $join) => $join
->on('n.controlled_vocab_entry_id', '=', 'e.controlled_vocab_entry_id')
->where('n.setting_name', '=', $settingName)
->where('n.locale', '=', '')
->where('n.setting_name', $settingName)
->where('n.locale', '')
)
->select([
'e.controlled_vocab_entry_id',
DB::raw(
'coalesce (l.setting_value, p.setting_value, n.setting_value) as setting_value'
'COALESCE (l.setting_value, p.setting_value, n.setting_value) as setting_value'
),
DB::raw(
'coalesce (l.setting_type, p.setting_type, n.setting_type) as setting_type'
'COALESCE (l.setting_type, p.setting_type, n.setting_type) as setting_type'
),
])
->where('e.controlled_vocab_id', '=', $this->id)
->where('e.controlled_vocab_id', $this->id)
->orderBy('e.seq')
->get()
->pluck('setting_value', 'controlled_vocab_entry_id')
->toArray();
Expand Down
57 changes: 54 additions & 3 deletions classes/controlledVocab/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace PKP\controlledVocab;

use PKP\db\DAORegistry;
use APP\facades\Repo;
use Illuminate\Support\Facades\DB;
use PKP\db\DAORegistry;
use PKP\user\UserInterest;
use PKP\user\InterestEntry;
use PKP\user\InterestEntryDAO;
use PKP\controlledVocab\ControlledVocab;
use PKP\controlledVocab\ControlledVocabEntryDAO;

Expand Down Expand Up @@ -44,7 +48,8 @@ public function getBySymbolic(
int $assocType,
int $assocId,
array $locales = [],
?string $entryDaoClassName = null): array
?string $entryDaoClassName = null
): array
{
$result = [];

Expand Down Expand Up @@ -92,7 +97,8 @@ public function insertBySymbolic(
int $assocType,
int $assocId,
bool $deleteFirst = true,
?string $entryDaoClassName = null): void
?string $entryDaoClassName = null
): void
{
/** @var ControlledVocabEntryDAO $entryDao */
$entryDao = $entryDaoClassName
Expand Down Expand Up @@ -124,4 +130,49 @@ public function insertBySymbolic(
}
}
}

/**
* Update a user's set of interests
*/
public function setUserInterests(array $interests, int $userId): void
{
$controlledVocab = Repo::controlledVocab()->build(
UserInterest::CONTROLLED_VOCAB_INTEREST
);

/** @var InterestEntryDAO $interestEntryDao */
$interestEntryDao = DAORegistry::getDAO('InterestEntryDAO');

DB::beginTransaction();

// Delete the existing interests association.
UserInterest::withUserId($userId)->delete();

collect($interests)
->map(fn (string $interest): string => trim($interest))
->unique()
->each(function (string $interest) use ($controlledVocab, $interestEntryDao, $userId): void {
$interestEntry = $interestEntryDao->getBySetting(
$interest,
$controlledVocab->symbolic,
$controlledVocab->assocId,
$controlledVocab->assocType,
$controlledVocab->symbolic
);

if (!$interestEntry) {
$interestEntry = $interestEntryDao->newDataObject(); /** @var InterestEntry $interestEntry */
$interestEntry->setInterest($interest);
$interestEntry->setControlledVocabId($controlledVocab->id);
$interestEntry->setId($interestEntryDao->insertObject($interestEntry));
}

UserInterest::create([
'userId' => $userId,
'controlledVocabEntryId' => $interestEntry->getId(),
]);
});

DB::commit();
}
}
4 changes: 0 additions & 4 deletions classes/core/PKPApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ public function getDAOMap()
'FilterDAO' => 'PKP\filter\FilterDAO',
'FilterGroupDAO' => 'PKP\filter\FilterGroupDAO',
'GenreDAO' => 'PKP\submission\GenreDAO',
'InterestDAO' => 'PKP\user\InterestDAO',
'InterestEntryDAO' => 'PKP\user\InterestEntryDAO',
'LibraryFileDAO' => 'PKP\context\LibraryFileDAO',
'NavigationMenuDAO' => 'PKP\navigationMenu\NavigationMenuDAO',
Expand All @@ -500,12 +499,9 @@ public function getDAOMap()
'SubEditorsDAO' => 'PKP\context\SubEditorsDAO',
'SubmissionAgencyEntryDAO' => 'PKP\submission\SubmissionAgencyEntryDAO',
'SubmissionCommentDAO' => 'PKP\submission\SubmissionCommentDAO',
'SubmissionDisciplineDAO' => 'PKP\submission\SubmissionDisciplineDAO',
'SubmissionDisciplineEntryDAO' => 'PKP\submission\SubmissionDisciplineEntryDAO',
'QueryDAO' => 'PKP\query\QueryDAO',
'SubmissionKeywordDAO' => 'PKP\submission\SubmissionKeywordDAO',
'SubmissionKeywordEntryDAO' => 'PKP\submission\SubmissionKeywordEntryDAO',
'SubmissionSubjectDAO' => 'PKP\submission\SubmissionSubjectDAO',
'SubmissionSubjectEntryDAO' => 'PKP\submission\SubmissionSubjectEntryDAO',
'TemporaryFileDAO' => 'PKP\file\TemporaryFileDAO',
'TemporaryInstitutionsDAO' => 'PKP\statistics\TemporaryInstitutionsDAO',
Expand Down
59 changes: 17 additions & 42 deletions classes/publication/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
use PKP\core\traits\EntityWithParent;
use PKP\services\PKPSchemaService;
use PKP\submission\SubmissionAgencyVocab;
use PKP\submission\SubmissionDisciplineDAO;
use PKP\submission\SubmissionKeywordDAO;
use PKP\submission\SubmissionSubjectDAO;
use PKP\submission\SubmissionDisciplineVocab;
use PKP\submission\SubmissionKeywordVocab;
use PKP\submission\SubmissionSubjectVocab;

/**
* @template T of Publication
Expand All @@ -49,33 +49,16 @@ class DAO extends EntityDAO
/** @copydoc EntityDAO::$primaryKeyColumn */
public $primaryKeyColumn = 'publication_id';

/** @var SubmissionKeywordDAO */
public $submissionKeywordDao;

/** @var SubmissionSubjectDAO */
public $submissionSubjectDao;

/** @var SubmissionDisciplineDAO */
public $submissionDisciplineDao;

/** @var CitationDAO */
public $citationDao;

/**
* Constructor
*/
public function __construct(
SubmissionKeywordDAO $submissionKeywordDao,
SubmissionSubjectDAO $submissionSubjectDao,
SubmissionDisciplineDAO $submissionDisciplineDao,
CitationDAO $citationDao,
PKPSchemaService $schemaService
) {
public function __construct(CitationDAO $citationDao, PKPSchemaService $schemaService)
{
parent::__construct($schemaService);

$this->submissionKeywordDao = $submissionKeywordDao;
$this->submissionSubjectDao = $submissionSubjectDao;
$this->submissionDisciplineDao = $submissionDisciplineDao;
$this->citationDao = $citationDao;
}

Expand Down Expand Up @@ -365,9 +348,9 @@ protected function deleteAuthors(int $publicationId)
*/
protected function setControlledVocab(Publication $publication)
{
$publication->setData('keywords', $this->submissionKeywordDao->getKeywords($publication->getId()));
$publication->setData('subjects', $this->submissionSubjectDao->getSubjects($publication->getId()));
$publication->setData('disciplines', $this->submissionDisciplineDao->getDisciplines($publication->getId()));
$publication->setData('keywords', SubmissionKeywordVocab::getKeywords($publication->getId()));
$publication->setData('subjects', SubmissionSubjectVocab::getSubjects($publication->getId()));
$publication->setData('disciplines', SubmissionDisciplineVocab::getDisciplines($publication->getId()));
$publication->setData('supportingAgencies', SubmissionAgencyVocab::getAgencies($publication->getId()));
}

Expand Down Expand Up @@ -405,20 +388,12 @@ protected function saveControlledVocab(array $values, int $publicationId)
{
// Update controlled vocabularly for which we have props
foreach ($values as $prop => $value) {
switch ($prop) {
case 'keywords':
$this->submissionKeywordDao->insertKeywords($value, $publicationId);
break;
case 'subjects':
$this->submissionSubjectDao->insertSubjects($value, $publicationId);
break;
case 'disciplines':
$this->submissionDisciplineDao->insertDisciplines($value, $publicationId);
break;
case 'supportingAgencies':
SubmissionAgencyVocab::insertAgencies($value, $publicationId);
break;
}
match ($prop) {
'keywords' => SubmissionKeywordVocab::insertKeywords($value, $publicationId),
'subjects' => SubmissionSubjectVocab::insertSubjects($value, $publicationId),
'disciplines' => SubmissionDisciplineVocab::insertDisciplines($value, $publicationId),
'supportingAgencies' => SubmissionAgencyVocab::insertAgencies($value, $publicationId),
};
}
}

Expand All @@ -427,9 +402,9 @@ protected function saveControlledVocab(array $values, int $publicationId)
*/
protected function deleteControlledVocab(int $publicationId)
{
$this->submissionKeywordDao->insertKeywords([], $publicationId);
$this->submissionSubjectDao->insertSubjects([], $publicationId);
$this->submissionDisciplineDao->insertDisciplines([], $publicationId);
SubmissionKeywordVocab::insertKeywords([], $publicationId);
SubmissionSubjectVocab::insertSubjects([], $publicationId);
SubmissionDisciplineVocab::insertDisciplines([], $publicationId);
SubmissionAgencyVocab::insertAgencies([], $publicationId);
}

Expand Down
Loading

0 comments on commit a581dc9

Please sign in to comment.