forked from pkp/pkp-lib
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkp#6903 Convert categories to new collector toolset and set foreign …
…keys
- Loading branch information
Showing
19 changed files
with
721 additions
and
627 deletions.
There are no files selected for viewing
90 changes: 22 additions & 68 deletions
90
classes/context/Category.inc.php → classes/category/Category.inc.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,213 +1,167 @@ | ||
<?php | ||
|
||
/** | ||
* @file classes/context/Category.inc.php | ||
* @file classes/category/Category.inc.php | ||
* | ||
* Copyright (c) 2014-2021 Simon Fraser University | ||
* Copyright (c) 2003-2021 John Willinsky | ||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. | ||
* | ||
* @class Category | ||
* @ingroup context | ||
* | ||
* @see CategoryDAO | ||
* | ||
* @brief Describes basic Category properties. | ||
*/ | ||
|
||
namespace PKP\context; | ||
namespace PKP\category; | ||
|
||
class Category extends \PKP\core\DataObject | ||
{ | ||
/** | ||
* Get ID of context. | ||
* | ||
* @return int | ||
*/ | ||
public function getContextId() | ||
public function getContextId(): int | ||
{ | ||
return $this->getData('contextId'); | ||
} | ||
|
||
/** | ||
* Set ID of context. | ||
* | ||
* @param $contextId int | ||
*/ | ||
public function setContextId($contextId) | ||
public function setContextId(int $contextId) | ||
{ | ||
return $this->setData('contextId', $contextId); | ||
} | ||
|
||
/** | ||
* Get ID of parent category. | ||
* | ||
* @return int | ||
*/ | ||
public function getParentId() | ||
public function getParentId(): ?int | ||
{ | ||
return $this->getData('parentId'); | ||
} | ||
|
||
/** | ||
* Set ID of parent category. | ||
* | ||
* @param $parentId int | ||
*/ | ||
public function setParentId($parentId) | ||
public function setParentId(?int $parentId) | ||
{ | ||
return $this->setData('parentId', $parentId); | ||
} | ||
|
||
/** | ||
* Get sequence of category. | ||
* | ||
* @return int | ||
*/ | ||
public function getSequence() | ||
public function getSequence(): float | ||
{ | ||
return $this->getData('sequence'); | ||
return (float) $this->getData('sequence'); | ||
} | ||
|
||
/** | ||
* Set sequence of category. | ||
* | ||
* @param $sequence int | ||
*/ | ||
public function setSequence($sequence) | ||
public function setSequence(float $sequence) | ||
{ | ||
return $this->setData('sequence', $sequence); | ||
} | ||
|
||
/** | ||
* Get category path. | ||
* | ||
* @return string | ||
*/ | ||
public function getPath() | ||
public function getPath(): string | ||
{ | ||
return $this->getData('path'); | ||
} | ||
|
||
/** | ||
* Set category path. | ||
* | ||
* @param $path string | ||
*/ | ||
public function setPath($path) | ||
public function setPath(string $path) | ||
{ | ||
return $this->setData('path', $path); | ||
} | ||
|
||
/** | ||
* Get localized title of the category. | ||
* | ||
* @return string | ||
*/ | ||
public function getLocalizedTitle() | ||
public function getLocalizedTitle(): string | ||
{ | ||
return $this->getLocalizedData('title'); | ||
} | ||
|
||
/** | ||
* Get title of category. | ||
* | ||
* @param $locale string | ||
* | ||
* @return string | ||
*/ | ||
public function getTitle($locale) | ||
public function getTitle(?string $locale = null) | ||
{ | ||
return $this->getData('title', $locale); | ||
} | ||
|
||
/** | ||
* Set title of category. | ||
* | ||
* @param $title string | ||
* @param $locale string | ||
*/ | ||
public function setTitle($title, $locale) | ||
public function setTitle($title, ?string $locale) | ||
{ | ||
return $this->setData('title', $title, $locale); | ||
} | ||
|
||
/** | ||
* Get localized description of the category. | ||
* | ||
* @return string | ||
*/ | ||
public function getLocalizedDescription() | ||
public function getLocalizedDescription(): string | ||
{ | ||
return $this->getLocalizedData('description'); | ||
} | ||
|
||
/** | ||
* Get description of category. | ||
* | ||
* @param $locale string | ||
* | ||
* @return string | ||
*/ | ||
public function getDescription($locale) | ||
public function getDescription(?string $locale) | ||
{ | ||
return $this->getData('description', $locale); | ||
} | ||
|
||
/** | ||
* Set description of category. | ||
* | ||
* @param $description string | ||
* @param $locale string | ||
*/ | ||
public function setDescription($description, $locale) | ||
public function setDescription($description, ?string $locale) | ||
{ | ||
return $this->setData('description', $description, $locale); | ||
} | ||
|
||
/** | ||
* Get the image. | ||
* | ||
* @return array | ||
*/ | ||
public function getImage() | ||
public function getImage(): ?array | ||
{ | ||
return $this->getData('image'); | ||
} | ||
|
||
/** | ||
* Set the image. | ||
* | ||
* @param $image array | ||
*/ | ||
public function setImage($image) | ||
public function setImage(?array $image) | ||
{ | ||
return $this->setData('image', $image); | ||
} | ||
|
||
/** | ||
* Get the option how the books in this category should be sorted, | ||
* in the form: concat(sortBy, sortDir). | ||
* | ||
* @return string | ||
*/ | ||
public function getSortOption() | ||
public function getSortOption(): ?string | ||
{ | ||
return $this->getData('sortOption'); | ||
} | ||
|
||
/** | ||
* Set the option how the books in this categpry should be sorted, | ||
* in the form: concat(sortBy, sortDir). | ||
* | ||
* @param $sortOption string | ||
*/ | ||
public function setSortOption($sortOption) | ||
public function setSortOption(?string $sortOption) | ||
{ | ||
return $this->setData('sortOption', $sortOption); | ||
} | ||
} | ||
|
||
if (!PKP_STRICT_MODE) { | ||
class_alias('\PKP\context\Category', '\Category'); | ||
class_alias('\PKP\category\Category', '\Category'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
/** | ||
* @file classes/category/Collector.inc.php | ||
* | ||
* Copyright (c) 2014-2021 Simon Fraser University | ||
* Copyright (c) 2000-2021 John Willinsky | ||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. | ||
* | ||
* @class Collector | ||
* | ||
* @brief A helper class to configure a Query Builder to get a collection of categories | ||
*/ | ||
|
||
namespace PKP\category; | ||
|
||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Support\Facades\DB; | ||
use PKP\core\interfaces\CollectorInterface; | ||
use PKP\plugins\HookRegistry; | ||
|
||
class Collector implements CollectorInterface | ||
{ | ||
public DAO $dao; | ||
public ?array $contextIds = null; | ||
public ?array $parentIds = null; | ||
public ?array $paths = null; | ||
public ?array $publicationIds = null; | ||
public ?int $count = null; | ||
public ?int $offset = null; | ||
|
||
public function __construct(DAO $dao) | ||
{ | ||
$this->dao = $dao; | ||
} | ||
|
||
/** | ||
* Filter categories by one or more contexts | ||
*/ | ||
public function filterByContextIds(?array $contextIds): self | ||
{ | ||
$this->contextIds = $contextIds; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Filter categories by one or more parent category IDs | ||
*/ | ||
public function filterByParentIds(?array $parentIds): self | ||
{ | ||
$this->parentIds = $parentIds; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Filter categories by one or more publication IDs | ||
*/ | ||
public function filterByPublicationIds(?array $publicationIds): self | ||
{ | ||
$this->publicationIds = $publicationIds; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Filter categories by one or more paths | ||
*/ | ||
public function filterByPaths(?array $paths): self | ||
{ | ||
$this->paths = $paths; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Limit the number of objects retrieved | ||
*/ | ||
public function limit(?int $count): self | ||
{ | ||
$this->count = $count; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Offset the number of objects retrieved, for example to | ||
* retrieve the second page of contents | ||
*/ | ||
public function offset(?int $offset): self | ||
{ | ||
$this->offset = $offset; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @copydoc CollectorInterface::getQueryBuilder() | ||
*/ | ||
public function getQueryBuilder(): Builder | ||
{ | ||
$qb = DB::table($this->dao->table . ' as c') | ||
->select(['c.*']); | ||
|
||
$qb->when($this->contextIds !== null, function ($query) { | ||
$query->whereIn('c.context_id', $this->contextIds); | ||
}); | ||
|
||
$qb->when($this->paths !== null, function ($query) { | ||
$query->whereIn('c.path', $this->paths); | ||
}); | ||
|
||
$qb->when($this->publicationIds !== null, function ($query) { | ||
$query->whereIn('c.category_id', function ($query) { | ||
$query->select('category_id')->from('publication_categories')->whereIn('category_id', $this->publicationIds); | ||
}); | ||
}); | ||
|
||
$qb->when($this->parentIds !== null, function ($query) { | ||
// parentIds may contain mixed values and nulls; make sure the mix translates into the query accurately | ||
$nonNullParentIds = array_filter($this->parentIds); | ||
if (count($nonNullParentIds)) { | ||
$query->whereIn('c.parent_id', array_filter($this->parentIds)); | ||
} | ||
if (in_array(null, $this->parentIds)) { | ||
if (count($nonNullParentIds)) { | ||
$query->orWhereNull('c.parent_id'); | ||
} else { | ||
$query->whereNull('c.parent_id'); | ||
} | ||
} | ||
}); | ||
|
||
$qb->orderBy('c.seq'); | ||
|
||
if (isset($this->count)) { | ||
$qb->limit($this->count); | ||
} | ||
|
||
if (isset($this->offset)) { | ||
$qb->offset($this->offset); | ||
} | ||
|
||
HookRegistry::call('Category::Collector', [&$qb, $this]); | ||
|
||
return $qb; | ||
} | ||
} |
Oops, something went wrong.