Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UserEnabled criterion and field mapper for Solr #13

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/API/Values/Content/Query/Criterion/UserEnabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Netgen\IbexaSearchExtra\API\Values\Content\Query\Criterion;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator\Specifications;

class UserEnabled extends Criterion
{
public function __construct(bool $value)
{
parent::__construct(null, null, $value);
}

public function getSpecifications(): array
{
return [
new Specifications(
Operator::EQ,
Specifications::FORMAT_SINGLE,
Specifications::TYPE_BOOLEAN,
),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Netgen\IbexaSearchExtra\Core\Search\Solr\FieldMapper\Content;

use Ibexa\Contracts\Core\Persistence\Content as SPIContent;
use Ibexa\Contracts\Core\Search\Field;
use Ibexa\Contracts\Core\Search\FieldType;
use Ibexa\Contracts\Solr\FieldMapper\ContentFieldMapper;

final class UserEnabledFieldMapper extends ContentFieldMapper
{
public function accept(SPIContent $content): bool
{
return $this->getUserField($content) !== null;
}

public function mapFields(SPIContent $content): array
{
$userField = $this->getUserField($content);
if ($userField === null) {
return [];
}

$fields = [];

if (isset($userField->value->externalData['enabled'])) {
$fields[] = new Field(
'ng_user_enabled',
$userField->value->externalData['enabled'],
new FieldType\BooleanField(),
);
}

return $fields;
}

private function getUserField(SPIContent $content): ?SPIContent\Field
{
foreach ($content->fields as $field) {
if ($field->type === 'ezuser') {
return $field;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Netgen\IbexaSearchExtra\Core\Search\Solr\Query\Content\CriterionVisitor;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;
use Netgen\IbexaSearchExtra\API\Values\Content\Query\Criterion\UserEnabled as UserEnabledCriterion;

class UserEnabled extends CriterionVisitor
{
public function canVisit(Criterion $criterion): bool
{
return $criterion instanceof UserEnabledCriterion;
}

public function visit(Criterion $criterion, ?CriterionVisitor $subVisitor = null): string
{
$isEnabled = $criterion->value[0];

return 'ng_user_enabled_b:' . ($isEnabled ? 'true' : 'false');
}
}
5 changes: 5 additions & 0 deletions lib/Resources/config/search/solr/criterion_visitors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ services:
tags:
- { name: ibexa.search.solr.query.content.criterion.visitor }
- { name: ibexa.search.solr.query.location.criterion.visitor }

netgen.ibexa_search_extra.solr.query.content.criterion_visitor.user_enabled:
class: Netgen\IbexaSearchExtra\Core\Search\Solr\Query\Content\CriterionVisitor\UserEnabled
tags:
- { name: ibexa.search.solr.query.content.criterion.visitor }
5 changes: 5 additions & 0 deletions lib/Resources/config/search/solr/field_mappers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ services:
class: Netgen\IbexaSearchExtra\Core\Search\Solr\FieldMapper\Location\LocationIdFieldMapper
tags:
- { name: ibexa.search.solr.field.mapper.location }

netgen.ibexa_search_extra.solr.field_mapper.content.user_enabled:
class: Netgen\IbexaSearchExtra\Core\Search\Solr\FieldMapper\Content\UserEnabledFieldMapper
tags:
- { name: ibexa.search.solr.field.mapper.content }
Loading