Skip to content
Closed
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
1 change: 1 addition & 0 deletions config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ doctrine:
string_functions:
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
MATCH: DoctrineExtensions\Query\Mysql\MatchAgainst
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
Expand Down
5 changes: 3 additions & 2 deletions src/CoreBundle/Entity/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use Chamilo\CoreBundle\Entity\Listener\MessageListener;
use Chamilo\CoreBundle\Filter\SearchOrFilter;
use Chamilo\CoreBundle\Filter\MatchSearchFilter;
use Chamilo\CoreBundle\Repository\MessageRepository;
use Chamilo\CoreBundle\State\MessageByGroupStateProvider;
use Chamilo\CoreBundle\State\MessageProcessor;
Expand All @@ -35,6 +35,7 @@
#[ORM\Index(columns: ['user_sender_id'], name: 'idx_message_user_sender')]
#[ORM\Index(columns: ['group_id'], name: 'idx_message_group')]
#[ORM\Index(columns: ['msg_type'], name: 'idx_message_type')]
#[Orm\Index(columns: ['title', 'content'], name: 'idx_message_search', flags: ['fulltext'])]
#[ORM\Entity(repositoryClass: MessageRepository::class)]
#[ORM\EntityListeners([MessageListener::class])]
#[ApiResource(
Expand Down Expand Up @@ -78,7 +79,7 @@
BooleanFilter::class,
properties: ['receivers.read']
)]
#[ApiFilter(SearchOrFilter::class, properties: ['title', 'content'])]
#[ApiFilter(MatchSearchFilter::class, properties: ['title', 'content'])]
#[ApiFilter(ExistsFilter::class, properties: ['receivers.deletedAt'])]
class Message
{
Expand Down
4 changes: 2 additions & 2 deletions src/CoreBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use ApiPlatform\Metadata\Put;
use Chamilo\CoreBundle\Controller\Api\UserSkillsController;
use Chamilo\CoreBundle\Entity\Listener\UserListener;
use Chamilo\CoreBundle\Filter\SearchOrFilter;
use Chamilo\CoreBundle\Filter\PartialSearchOrFilter;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Chamilo\CoreBundle\Traits\UserCreatorTrait;
use Chamilo\CourseBundle\Entity\CGroupRelTutor;
Expand Down Expand Up @@ -78,7 +78,7 @@
'lastname' => 'partial',
]
)]
#[ApiFilter(SearchOrFilter::class, properties: ['username', 'firstname', 'lastname'])]
#[ApiFilter(PartialSearchOrFilter::class, properties: ['username', 'firstname', 'lastname'])]
#[ApiFilter(filterClass: BooleanFilter::class, properties: ['isActive'])]
class User implements UserInterface, EquatableInterface, ResourceInterface, ResourceIllustrationInterface, PasswordAuthenticatedUserInterface, LegacyPasswordAuthenticatedUserInterface, ExtraFieldItemInterface, Stringable
{
Expand Down Expand Up @@ -2412,6 +2412,6 @@

public function isCourseTutor(?Course $course = null, ?Session $session = null): bool
{
return $session?->hasCoachInCourseList($user) || $course?->getSubscriptionByUser($user)?->isTutor();

Check failure on line 2415 in src/CoreBundle/Entity/User.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

UndefinedVariable

src/CoreBundle/Entity/User.php:2415:48: UndefinedVariable: Cannot find referenced variable $user (see https://psalm.dev/024)

Check failure on line 2415 in src/CoreBundle/Entity/User.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

UndefinedVariable

src/CoreBundle/Entity/User.php:2415:90: UndefinedVariable: Cannot find referenced variable $user (see https://psalm.dev/024)
}
}
60 changes: 60 additions & 0 deletions src/CoreBundle/Filter/MatchSearchFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/* For licensing terms, see /license.txt */

declare(strict_types=1);

namespace Chamilo\CoreBundle\Filter;

use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use Doctrine\ORM\QueryBuilder;
use InvalidArgumentException;

class MatchSearchFilter extends AbstractFilter
{
protected function filterProperty(
string $property,
$value,
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
?Operation $operation = null,
array $context = []
): void {
if ('search' !== $property) {
return;
}

if (empty($value)) {
throw new InvalidArgumentException('The property must not be empty.');
}

$alias = $queryBuilder->getRootAliases()[0];
$valueParameter = ':'.$queryNameGenerator->generateParameterName($property);

$matchFields = [];

foreach (array_keys($this->properties) as $field) {
$matchFields[] = "$alias.$field";
}

$queryBuilder
->andWhere('MATCH ('.implode(', ', $matchFields).') AGAINST ('.$valueParameter.' IN BOOLEAN MODE) > 0')
->setParameter($valueParameter, '+"'.$value.'"')
;
}

public function getDescription(string $resourceClass): array
{
return [
'search' => [
'property' => null,
'type' => 'string',
'required' => false,
'description' => 'It does a "Search OR" using `MATCH AGAINST` to search for fields that contain `text`',
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Doctrine\ORM\QueryBuilder;
use InvalidArgumentException;

class SearchOrFilter extends AbstractFilter
class PartialSearchOrFilter extends AbstractFilter
{
protected function filterProperty(
string $property,
Expand All @@ -32,17 +32,17 @@ protected function filterProperty(
}

$alias = $queryBuilder->getRootAliases()[0];
$valueParameter = $queryNameGenerator->generateParameterName($property);
$valueParameter = ':'.$queryNameGenerator->generateParameterName($property);

$ors = [];

foreach (array_keys($this->properties) as $field) {
$ors[] = $queryBuilder->expr()->like(
"$alias.$field",
":$valueParameter"
(string) $queryBuilder->expr()->concat("'%'", $valueParameter, "'%'")
);

$queryBuilder->setParameter($valueParameter, "%$value%");
$queryBuilder->setParameter($valueParameter, $value);
}

$queryBuilder
Expand Down
31 changes: 31 additions & 0 deletions src/CoreBundle/Migrations/Schema/V200/Version20241113110000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/* For licensing terms, see /license.txt */

declare(strict_types=1);

namespace Chamilo\CoreBundle\Migrations\Schema\V200;

use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;

final class Version20241113110000 extends AbstractMigrationChamilo
{
public function getDescription(): string
{
return 'Add fulltext index to messages';
}

/**
* @inheritDoc
*/
public function up(Schema $schema): void
{
$this->addSql('CREATE FULLTEXT INDEX idx_message_search ON message (title, content)');
}

public function down(Schema $schema): void
{
$this->addSql('DROP INDEX idx_message_search ON message');
}
}
Loading