Skip to content

Commit

Permalink
Made compatible with Omeka 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-KM committed Nov 3, 2019
1 parent bbdd71f commit 6a69cf7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
10 changes: 6 additions & 4 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,14 +622,16 @@ public function searchQuery(Event $event)
{
// TODO Add option for tagging status in admin search view.

$adapter = $event->getTarget();
$isOldOmeka = \Omeka\Module::VERSION < 2;
$resourceAlias = $isOldOmeka ? $adapter->getEntityClass() : 'omeka_root';

$qb = $event->getParam('queryBuilder');
$expr = $qb->expr();
$query = $event->getParam('request')->getContent();

if (!empty($query['has_tags'])) {
$adapter = $event->getTarget();
$taggingAlias = $adapter->createAlias();
$resourceAlias = $adapter->getEntityClass();
$resourceName = $adapter->getResourceName() === 'users'
? 'owner'
: 'resource';
Expand All @@ -650,11 +652,11 @@ public function searchQuery(Event $event)
if (empty($tags)) {
return;
}
$adapter = $event->getTarget();
$resourceAlias = $adapter->getEntityClass();
// All resources with any tag ("OR").
// TODO The resquest is working, but it needs a format for querying.
/*
$tagAlias = $adapter->createAlias();
$taggingAlias = $adapter->createAlias();
$qb
->innerJoin(
Tagging::class,
Expand Down
4 changes: 2 additions & 2 deletions config/module.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ author_link = "https://github.com/Daniel-KM"
module_link = "https://github.com/Daniel-KM/Omeka-S-module-Folksonomy"
support_link = "https://github.com/Daniel-KM/Omeka-S-module-Folksonomy/issues"
configurable = true
version = "3.3.8"
omeka_version_constraint = "^1.1.0"
version = "3.3.9"
omeka_version_constraint = "^1.1.0 || ^2.0.0"
20 changes: 13 additions & 7 deletions src/Api/Adapter/QueryBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ protected function buildQueryValues(QueryBuilder $qb, $values, $column, $target)
*/
protected function buildQueryOneValue(QueryBuilder $qb, $value, $column)
{
$isOldOmeka = \Omeka\Module::VERSION < 2;
$alias = $isOldOmeka ? $this->getEntityClass() : 'omeka_root';
$expr = $qb->expr();

if (is_null($value)) {
$qb->andWhere($expr->isNull(
$this->getEntityClass() . '.' . $column
$alias . '.' . $column
));
} else {
$qb->andWhere($expr->eq(
$this->getEntityClass() . '.' . $column,
$alias . '.' . $column,
$this->createNamedParameter($qb, $value)
));
}
Expand All @@ -62,6 +64,8 @@ protected function buildQueryOneValue(QueryBuilder $qb, $value, $column)
*/
protected function buildQueryMultipleValues(QueryBuilder $qb, array $values, $column, $target)
{
$isOldOmeka = \Omeka\Module::VERSION < 2;
$alias = $isOldOmeka ? $this->getEntityClass() : 'omeka_root';
$expr = $qb->expr();

$hasNull = in_array(null, $values, true);
Expand All @@ -71,7 +75,7 @@ protected function buildQueryMultipleValues(QueryBuilder $qb, array $values, $co
if ($values) {
$valueAlias = $this->createAlias();
$qb->innerJoin(
$this->getEntityClass() . '.' . $column,
$alias . '.' . $column,
$valueAlias,
'WITH',
$hasNull
Expand All @@ -93,7 +97,7 @@ protected function buildQueryMultipleValues(QueryBuilder $qb, array $values, $co
// Check no value only.
elseif ($hasNull) {
$qb->andWhere($expr->isNull(
$this->getEntityClass() . '.' . $column
$alias . '.' . $column
));
}
}
Expand Down Expand Up @@ -185,6 +189,8 @@ protected function buildQueryValuesItself(QueryBuilder $qb, $values, $target)
*/
protected function buildQueryMultipleValuesItself(QueryBuilder $qb, array $values, $target)
{
$isOldOmeka = \Omeka\Module::VERSION < 2;
$alias = $isOldOmeka ? $this->getEntityClass() : 'omeka_root';
$expr = $qb->expr();

$hasNull = in_array(null, $values, true);
Expand All @@ -195,11 +201,11 @@ protected function buildQueryMultipleValuesItself(QueryBuilder $qb, array $value
$valueAlias = $this->createAlias();
$qb
->innerJoin(
$this->getEntityClass(),
$alias,
$valueAlias,
'WITH',
$expr->eq(
$this->getEntityClass() . '.id',
$alias . '.id',
$valueAlias . '.id'
)
)
Expand All @@ -223,7 +229,7 @@ protected function buildQueryMultipleValuesItself(QueryBuilder $qb, array $value
// Check no value only.
elseif ($hasNull) {
$qb->andWhere($expr->isNull(
$this->getEntityClass() . '.' . $target
$alias . '.' . $target
));
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Api/Adapter/TagAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ protected function validateTagName($name, ErrorStore $errorStore)

public function buildQuery(QueryBuilder $qb, array $query)
{
$isOldOmeka = \Omeka\Module::VERSION < 2;
$alias = $isOldOmeka ? $this->getEntityClass() : 'omeka_root';
$expr = $qb->expr();

if (isset($query['internal_id'])) {
Expand Down Expand Up @@ -148,7 +150,7 @@ public function buildQuery(QueryBuilder $qb, array $query)
$taggingAlias,
'WITH',
$expr->andX(
$expr->eq($taggingAlias . '.tag', $this->getEntityClass() . '.id'),
$expr->eq($taggingAlias . '.tag', $alias . '.id'),
$expr->in(
$taggingAlias . '.' . $column,
$this->createNamedParameter($qb, $entities)
Expand Down Expand Up @@ -212,7 +214,6 @@ public function sortQuery(QueryBuilder $qb, array $query)
break;
case 'recent':
$taggingAlias = $this->createAlias();
$orderAlias = $this->createAlias();
$orderBy = $taggingAlias . '.created';
$qb
->leftJoin(
Expand Down
14 changes: 8 additions & 6 deletions src/Api/Adapter/TaggingAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ public function buildQuery(QueryBuilder $qb, array $query)
// TODO Check status according to admin/public.
// TODO Check resource and owner visibility for public view.

$isOldOmeka = \Omeka\Module::VERSION < 2;
$alias = $isOldOmeka ? $this->getEntityClass() : 'omeka_root';
$expr = $qb->expr();

if (array_key_exists('id', $query)) {
Expand All @@ -192,7 +194,7 @@ public function buildQuery(QueryBuilder $qb, array $query)
$tagAlias,
'WITH',
$expr->andX(
$expr->eq($tagAlias . '.id', $this->getEntityClass(). '.tag'),
$expr->eq($tagAlias . '.id', $alias. '.tag'),
$expr->in(
$tagAlias . '.name',
$this->createNamedParameter($qb, $tags)
Expand All @@ -219,10 +221,10 @@ public function buildQuery(QueryBuilder $qb, array $query)
// An empty string means true in order to manage get/post query.
if (in_array($query['has_resource'], [false, 'false', 0, '0'], true)) {
$qb
->andWhere($expr->isNull($this->getEntityClass() . '.resource'));
->andWhere($expr->isNull($alias . '.resource'));
} else {
$qb
->andWhere($expr->isNotNull($this->getEntityClass() . '.resource'));
->andWhere($expr->isNotNull($alias . '.resource'));
}
}

Expand All @@ -236,13 +238,13 @@ public function buildQuery(QueryBuilder $qb, array $query)
];
if ($query['resource_type'] === 'resources') {
$qb
->andWhere($expr->isNotNull($this->getEntityClass() . '.resource'));
->andWhere($expr->isNotNull($alias . '.resource'));
// TODO Distinct users, else there may be x times the same tagger.
// The issue doesn't occur for resource, since there is a check
// before.
// } elseif ($query['resource_type'] === 'users') {
// $qb
// ->andWhere($expr->isNotNull($this->getEntityClass() . '.owner'));
// ->andWhere($expr->isNotNull($alias . '.owner'));
} elseif (isset($mapResourceTypes[$query['resource_type']])) {
$entityAlias = $this->createAlias();
$qb
Expand All @@ -251,7 +253,7 @@ public function buildQuery(QueryBuilder $qb, array $query)
$entityAlias,
'WITH',
$expr->eq(
$this->getEntityClass() . '.resource',
$alias . '.resource',
$entityAlias . '.id'
)
);
Expand Down

0 comments on commit 6a69cf7

Please sign in to comment.