Skip to content

Commit

Permalink
Merge pull request #5674 from christianbeeznest/ofaj-21826-4
Browse files Browse the repository at this point in the history
Internal: Refactor file search logic into repository and fix UUID search - refs BT#21826
  • Loading branch information
christianbeeznest authored Jul 18, 2024
2 parents 2f2d8fc + 0d08f7c commit df17d56
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 44 deletions.
48 changes: 5 additions & 43 deletions src/CoreBundle/Controller/Admin/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Chamilo\CoreBundle\Controller\Admin;

use Chamilo\CoreBundle\Controller\BaseController;
use Chamilo\CoreBundle\Entity\ResourceFile;
use Chamilo\CoreBundle\Repository\ResourceFileRepository;
use Chamilo\CoreBundle\Repository\ResourceNodeRepository;
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;
use Chamilo\CoreBundle\Settings\SettingsManager;
Expand Down Expand Up @@ -50,52 +50,14 @@ public function registerCampus(Request $request, SettingsManager $settingsManage

#[IsGranted('ROLE_ADMIN')]
#[Route('/files_info', name: 'admin_files_info', methods: ['GET'])]
public function listFilesInfo(Request $request): Response
public function listFilesInfo(Request $request, ResourceFileRepository $resourceFileRepository): Response
{
$page = $request->query->getInt('page', 1);
$search = $request->query->get('search', '');
$offset = ($page - 1) * self::ITEMS_PER_PAGE;

$queryBuilder = $this->entityManager->getRepository(ResourceFile::class)->createQueryBuilder('rf')
->leftJoin('rf.resourceNode', 'rn')
->leftJoin('rn.resourceLinks', 'rl')
->leftJoin('rl.course', 'c')
->leftJoin('rl.user', 'u')
->addSelect('rn', 'rl', 'c', 'u');

if ($search) {
$queryBuilder->where('rf.title LIKE :search')
->orWhere('rf.originalName LIKE :search')
->orWhere('c.title LIKE :search')
->orWhere('u.username LIKE :search')
->orWhere('rn.uuid LIKE :search')
->setParameter('search', '%' . $search . '%');
}

$queryBuilder->orderBy('rf.id', 'DESC')
->setFirstResult($offset)
->setMaxResults(self::ITEMS_PER_PAGE);

$files = $queryBuilder->getQuery()->getResult();

$totalItemsQuery = $this->entityManager->getRepository(ResourceFile::class)
->createQueryBuilder('rf')
->leftJoin('rf.resourceNode', 'rn')
->leftJoin('rn.resourceLinks', 'rl')
->leftJoin('rl.course', 'c')
->leftJoin('rl.user', 'u')
->select('COUNT(rf.id)');

if ($search) {
$totalItemsQuery->where('rf.title LIKE :search')
->orWhere('rf.originalName LIKE :search')
->orWhere('c.title LIKE :search')
->orWhere('u.username LIKE :search')
->orWhere('rn.uuid LIKE :search')
->setParameter('search', '%' . $search . '%');
}

$totalItems = $totalItemsQuery->getQuery()->getSingleScalarResult();
$files = $resourceFileRepository->searchFiles($search, $offset, self::ITEMS_PER_PAGE);
$totalItems = $resourceFileRepository->countFiles($search);
$totalPages = ceil($totalItems / self::ITEMS_PER_PAGE);

$fileUrls = [];
Expand All @@ -109,7 +71,7 @@ public function listFilesInfo(Request $request): Response
$fileUrls[$file->getId()] = null;
$creator = null;
}
$filePaths[$file->getId()] = $this->resourceNodeRepository->getFilename($file);
$filePaths[$file->getId()] = '/upload/resources'.$this->resourceNodeRepository->getFilename($file);
}

return $this->render('@ChamiloCore/Admin/files_info.html.twig', [
Expand Down
3 changes: 2 additions & 1 deletion src/CoreBundle/Entity/ResourceFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ApiPlatform\Metadata\Post;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use Chamilo\CoreBundle\Controller\CreateResourceFileAction;
use Chamilo\CoreBundle\Repository\ResourceFileRepository;
use DateTime;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
Expand Down Expand Up @@ -74,7 +75,7 @@
]
)]
#[ORM\Table(name: 'resource_file')]
#[ORM\Entity]
#[ORM\Entity(repositoryClass: ResourceFileRepository::class)]
#[ApiFilter(filterClass: PropertyFilter::class)]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['name' => 'partial'])]
#[ApiFilter(filterClass: OrderFilter::class, properties: ['id', 'name', 'size', 'updatedAt'])]
Expand Down
81 changes: 81 additions & 0 deletions src/CoreBundle/Repository/ResourceFileRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

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

namespace Chamilo\CoreBundle\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Chamilo\CoreBundle\Entity\ResourceFile;

class ResourceFileRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ResourceFile::class);
}

public function searchFiles(string $search, int $offset, int $limit): array
{
$uuid = '';
if (preg_match('/[a-f0-9]{32}|[a-f0-9\-]{36}/i', $search, $matches)) {
$uuid = strtoupper(str_replace('-', '', $matches[0]));
}

$uuidBinary = pack('H*', $uuid);

$queryBuilder = $this->createQueryBuilder('rf')
->leftJoin('rf.resourceNode', 'rn')
->leftJoin('rn.resourceLinks', 'rl')
->leftJoin('rl.course', 'c')
->leftJoin('rl.user', 'u')
->addSelect('rn', 'rl', 'c', 'u');

if ($search) {
$queryBuilder->where('rf.title LIKE :search')
->orWhere('rf.originalName LIKE :search')
->orWhere('c.title LIKE :search')
->orWhere('u.username LIKE :search')
->orWhere('rn.uuid = :uuid')
->setParameter('search', '%' . $search . '%')
->setParameter('uuid', $uuidBinary);
}

$queryBuilder->orderBy('rf.id', 'DESC')
->setFirstResult($offset)
->setMaxResults($limit);

return $queryBuilder->getQuery()->getResult();
}

public function countFiles(string $search): int
{
$uuid = '';
if (preg_match('/[a-f0-9]{32}|[a-f0-9\-]{36}/i', $search, $matches)) {
$uuid = strtoupper(str_replace('-', '', $matches[0]));
}

$uuidBinary = pack('H*', $uuid);

$queryBuilder = $this->createQueryBuilder('rf')
->leftJoin('rf.resourceNode', 'rn')
->leftJoin('rn.resourceLinks', 'rl')
->leftJoin('rl.course', 'c')
->leftJoin('rl.user', 'u')
->select('COUNT(rf.id)');

if ($search) {
$queryBuilder->where('rf.title LIKE :search')
->orWhere('rf.originalName LIKE :search')
->orWhere('c.title LIKE :search')
->orWhere('u.username LIKE :search')
->orWhere('rn.uuid = :uuid')
->setParameter('search', '%' . $search . '%')
->setParameter('uuid', $uuidBinary);
}

return $queryBuilder->getQuery()->getSingleScalarResult();
}
}

0 comments on commit df17d56

Please sign in to comment.