diff --git a/src/CoreBundle/Controller/Admin/AdminController.php b/src/CoreBundle/Controller/Admin/AdminController.php index d5360063a73..1650a312c0e 100644 --- a/src/CoreBundle/Controller/Admin/AdminController.php +++ b/src/CoreBundle/Controller/Admin/AdminController.php @@ -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; @@ -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 = []; @@ -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', [ diff --git a/src/CoreBundle/Entity/ResourceFile.php b/src/CoreBundle/Entity/ResourceFile.php index 950c2e7b3d1..56deb87c2cc 100644 --- a/src/CoreBundle/Entity/ResourceFile.php +++ b/src/CoreBundle/Entity/ResourceFile.php @@ -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; @@ -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'])] diff --git a/src/CoreBundle/Repository/ResourceFileRepository.php b/src/CoreBundle/Repository/ResourceFileRepository.php new file mode 100644 index 00000000000..63a2f07f2cf --- /dev/null +++ b/src/CoreBundle/Repository/ResourceFileRepository.php @@ -0,0 +1,81 @@ +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(); + } +}