Skip to content

Commit

Permalink
pkp#6903 Convert categories to new collector toolset and set foreign …
Browse files Browse the repository at this point in the history
…keys
  • Loading branch information
asmecher committed Nov 4, 2021
1 parent c846949 commit b85857d
Show file tree
Hide file tree
Showing 19 changed files with 721 additions and 627 deletions.
@@ -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');
}
142 changes: 142 additions & 0 deletions classes/category/Collector.inc.php
@@ -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;
}
}

0 comments on commit b85857d

Please sign in to comment.