Skip to content

Commit

Permalink
#10158 Move categoryids out of of publication_settings (#10265)
Browse files Browse the repository at this point in the history
* #10158 Move categoryids out of of publication_settings

* #10158 Improve publication category handling and optimize query structure

* #10158 Updated copyright and queries
  • Loading branch information
Hafsa-Naeem authored Dec 7, 2024
1 parent 9ca4c44 commit 01210c2
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 40 deletions.
9 changes: 6 additions & 3 deletions classes/category/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Illuminate\Support\LazyCollection;
use PKP\core\interfaces\CollectorInterface;
use PKP\plugins\Hook;
use PKP\publication\PublicationCategory;


/**
* @template T of Category
Expand Down Expand Up @@ -136,9 +138,10 @@ public function getQueryBuilder(): Builder
});

$qb->when($this->publicationIds !== null, function ($query) {
$query->whereIn('c.category_id', function ($query) {
$query->select('category_id')->from('publication_categories')->whereIn('publication_id', $this->publicationIds);
});
$query->whereIn('c.category_id', PublicationCategory::select('category_id')
->whereIn('publication_id', $this->publicationIds)
->toBase()
);
});

$qb->when($this->parentIds !== null, function ($query) {
Expand Down
18 changes: 0 additions & 18 deletions classes/category/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,4 @@ public function resequenceCategories(int $contextId, ?int $parentCategoryId = nu
}
}

/**
* Assign a publication to a category
*/
public function insertPublicationAssignment(int $categoryId, int $publicationId)
{
DB::table('publication_categories')->insert([
'category_id' => $categoryId,
'publication_id' => $publicationId,
]);
}

/**
* Delete the assignment of a category to a publication
*/
public function deletePublicationAssignments(int $publicationId)
{
DB::table('publication_categories')->where('publication_id', '=', $publicationId)->delete();
}
}
25 changes: 8 additions & 17 deletions classes/publication/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,36 +441,27 @@ protected function deleteControlledVocab(int $publicationId)
/**
* Set a publication's category property
*/
protected function setCategories(Publication $publication)
protected function setCategories(Publication $publication): void
{
$publication->setData(
'categoryIds',
Repo::category()->getCollector()
->filterByPublicationIds([$publication->getId()])
->getIds()
->toArray()
);
$categoryIds = PublicationCategory::withPublicationId($publication->getId())->pluck('category_id');
$publication->setData('categoryIds', $categoryIds);
}

/**
* Save the assigned categories
*/
protected function saveCategories(Publication $publication)
protected function saveCategories(Publication $publication): void
{
Repo::category()->dao->deletePublicationAssignments($publication->getId());
if (!empty($publication->getData('categoryIds'))) {
foreach ($publication->getData('categoryIds') as $categoryId) {
Repo::category()->dao->insertPublicationAssignment($categoryId, $publication->getId());
}
}
$categoryIds = (array) $publication->getData('categoryIds');
Repo::publication()->assignCategoriesToPublication($publication->getId(), $categoryIds);
}

/**
* Delete the category assignments
*/
protected function deleteCategories(int $publicationId)
protected function deleteCategories(int $publicationId): void
{
Repo::category()->dao->deletePublicationAssignments($publicationId);
PublicationCategory::where('publication_id', $publicationId)->delete();
}

/**
Expand Down
50 changes: 50 additions & 0 deletions classes/publication/PublicationCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* @file classes/publication/PublicationCategory.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 PublicationCategory
*
* @brief Handles operations related to publication categories
*/

namespace PKP\publication;

use Eloquence\Behaviours\HasCamelCasing;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class PublicationCategory extends Model
{
use HasCamelCasing;

protected $table = 'publication_categories';
protected $primaryKey = 'publication_category_id';
public $timestamps = false;

protected $fillable = [
'publication_id', 'category_id'
];

/**
* Scope a query to only include records with a specific publicationId
*/
public function scopeWithPublicationId(Builder $query, int $publicationId): Builder
{
return $query->where('publication_id', $publicationId);
}

/**
* Scope a query to only include records with specific categoryIds
*
* @param int[] $categoryIds Array of category IDs
*/
public function scopeWithCategoryIds(Builder $query, array $categoryIds): Builder
{
return $query->whereIn('category_id', $categoryIds);
}

}
17 changes: 17 additions & 0 deletions classes/publication/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,23 @@ protected function getErrorMessageOverrides(): array
];
}

/**
* Assign categories to a publication.
*
* @param int[] $categoryIds
*/
public function assignCategoriesToPublication(int $publicationId, array $categoryIds): void
{
$records = array_map(fn ($categoryId) => ['publication_id' => $publicationId, 'category_id' => $categoryId], $categoryIds);

PublicationCategory::upsert($records, ['publication_id', 'category_id']);

// delete categories that are no longer assigned
PublicationCategory::where('publication_id', $publicationId)
->whereNotIn('category_id', $categoryIds)
->delete();
}

/**
* Create all DOIs associated with the publication.
*/
Expand Down
9 changes: 7 additions & 2 deletions classes/submission/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use PKP\search\SubmissionSearch;
use PKP\security\Role;
use PKP\submission\reviewRound\ReviewRound;
use PKP\publication\PublicationCategory;


/**
* @template T of Submission
Expand Down Expand Up @@ -623,8 +625,11 @@ public function getQueryBuilder(): Builder
}

if (isset($this->categoryIds)) {
$q->join('publication_categories as pc', 's.current_publication_id', '=', 'pc.publication_id')
->whereIn('pc.category_id', $this->categoryIds);
$publicationIds = PublicationCategory::withCategoryIds($this->categoryIds)
->select('publication_id')
->toBase();

$q->whereIn('s.current_publication_id', $publicationIds);
}

$q = $this->buildReviewStageQueries($q);
Expand Down
1 change: 1 addition & 0 deletions schemas/publication.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"categoryIds": {
"type": "array",
"apiSummary": true,
"readOnly": true,
"items": {
"type": "integer"
}
Expand Down

0 comments on commit 01210c2

Please sign in to comment.