Add requestStack in repository services

pull/3904/head
Julio Montoya 4 years ago
parent b1e54dcd6f
commit 56fe63bb98
  1. 165
      src/CoreBundle/Repository/ResourceRepository.php
  2. 2
      src/CoreBundle/Resources/config/repositories.yml
  3. 1
      src/CourseBundle/Resources/config/services.yml
  4. 33
      src/CourseBundle/Traits/NonResourceRepository.php

@ -19,14 +19,12 @@ use Chamilo\CoreBundle\Entity\ResourceType;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
use Chamilo\CoreBundle\ToolChain;
use Chamilo\CourseBundle\Entity\CGroup;
use Cocur\Slugify\SlugifyInterface;
use Chamilo\CourseBundle\Traits\NonResourceRepository;
use DateTime;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Exception;
@ -36,92 +34,19 @@ use Symfony\Component\Form\FormFactory;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Throwable;
/**
* Class ResourceRepository.
* Extends EntityRepository is needed to process settings.
*/
abstract class ResourceRepository extends ServiceEntityRepository
{
protected EntityRepository $repository;
use NonResourceRepository;
protected EntityManager $entityManager;
protected ?RouterInterface $router = null;
protected ?ResourceNodeRepository $resourceNodeRepository = null;
protected ?AuthorizationCheckerInterface $authorizationChecker = null;
protected ?SlugifyInterface $slugify = null;
protected ?ToolChain $toolChain = null;
/**
* The entity class FQN.
*/
protected string $className;
protected Settings $settings;
protected Template $templates;
protected ?ResourceType $resourceType = null;
/*public function __construct(
AuthorizationCheckerInterface $authorizationChecker,
EntityManager $entityManager,
RouterInterface $router,
SlugifyInterface $slugify,
ToolChain $toolChain,
ResourceNodeRepository $resourceNodeRepository
) {
$className = $this->getClassName();
$this->repository = $entityManager->getRepository($className);
$this->authorizationChecker = $authorizationChecker;
$this->router = $router;
$this->resourceNodeRepository = $resourceNodeRepository;
$this->slugify = $slugify;
$this->toolChain = $toolChain;
$this->settings = new Settings();
$this->templates = new Template();
}*/
public function setResourceNodeRepository(ResourceNodeRepository $resourceNodeRepository): self
{
$this->resourceNodeRepository = $resourceNodeRepository;
return $this;
}
public function setRouter(RouterInterface $router): self
{
$this->router = $router;
return $this;
}
public function setAuthorizationChecker(AuthorizationCheckerInterface $authorizationChecker): self
{
$this->authorizationChecker = $authorizationChecker;
return $this;
}
public function setSlugify(SlugifyInterface $slugify): self
{
$this->slugify = $slugify;
return $this;
}
public function setToolChain(ToolChain $toolChain): self
{
$this->toolChain = $toolChain;
return $this;
}
public function setSettings(Settings $settings): self
{
$this->settings = $settings;
@ -159,22 +84,12 @@ abstract class ResourceRepository extends ServiceEntityRepository
return $class;
}
public function getAuthorizationChecker(): AuthorizationCheckerInterface
{
return $this->authorizationChecker;
}
public function getRouter(): RouterInterface
public function getCount(QueryBuilder $qb): int
{
return $this->router;
}
$qb->select('count(resource)');
$qb->setMaxResults(1);
/**
* @return ResourceNodeRepository
*/
public function getResourceNodeRepository()
{
return $this->resourceNodeRepository;
return (int) $qb->getQuery()->getSingleScalarResult();
}
/**
@ -253,6 +168,10 @@ abstract class ResourceRepository extends ServiceEntityRepository
public function update(AbstractResource $resource, bool $andFlush = true): void
{
if (!$resource->hasResourceNode()) {
throw new Exception('Resource needs a resource node');
}
$resource->getResourceNode()->setUpdatedAt(new DateTime());
$resource->getResourceNode()->setTitle($resource->getResourceName());
$this->getEntityManager()->persist($resource);
@ -301,7 +220,7 @@ abstract class ResourceRepository extends ServiceEntityRepository
return $resourceNode;
}
public function findResourceByTitle(
public function findCourseResourceByTitle(
string $title,
ResourceNode $parentNode,
Course $course,
@ -315,15 +234,7 @@ abstract class ResourceRepository extends ServiceEntityRepository
return $qb->getQuery()->getOneOrNullResult();
}
public function getCount(QueryBuilder $qb): int
{
$qb->select('count(resource)');
$qb->setMaxResults(1);
return (int) $qb->getQuery()->getSingleScalarResult();
}
public function findResourcesByTitle(
public function findCourseResourcesByTitle(
string $title,
ResourceNode $parentNode,
Course $course,
@ -336,7 +247,35 @@ abstract class ResourceRepository extends ServiceEntityRepository
return $qb->getQuery()->getResult();
}
public function addFile(ResourceInterface $resource, UploadedFile $file, string $description = ''): ?ResourceFile
public function addFileFromString(ResourceInterface $resource, string $fileName, string $mimeType, string $content, bool $flush = true): ?ResourceFile
{
$handle = tmpfile();
fwrite($handle, $content);
$meta = stream_get_meta_data($handle);
$file = new UploadedFile($meta['uri'], $fileName, $mimeType, null, true);
return $this->addFile($resource, $file, '', $flush);
}
public function addFileFromFileRequest(ResourceInterface $resource, string $fileKey, bool $flush = true): ?ResourceFile
{
$request = $this->getRequest();
if ($request->files->has($fileKey)) {
$file = $request->files->get($fileKey);
if (null !== $file) {
$resourceFile = $this->addFile($resource, $file);
if ($flush) {
$this->getEntityManager()->flush();
}
return $resourceFile;
}
}
return null;
}
public function addFile(ResourceInterface $resource, UploadedFile $file, string $description = '', bool $flush = false): ?ResourceFile
{
$resourceNode = $resource->getResourceNode();
@ -360,6 +299,10 @@ abstract class ResourceRepository extends ServiceEntityRepository
$resourceNode->setResourceFile($resourceFile);
$em->persist($resourceNode);
if ($flush) {
$em->flush();
}
return $resourceFile;
}
@ -514,6 +457,18 @@ abstract class ResourceRepository extends ServiceEntityRepository
return $qb;
}
public function getResourceByCreatorFromTitle(
string $title,
User $user,
ResourceNode $parentNode,
): ?ResourceInterface {
$qb = $this->getResourcesByCreator($user, $parentNode);
$this->addTitleQueryBuilder($title, $qb);
$qb->setMaxResults(1);
return $qb->getQuery()->getOneOrNullResult();
}
public function getResourcesByCreator(User $user, ResourceNode $parentNode = null): QueryBuilder
{
$qb = $this->createQueryBuilder('resource')
@ -1007,13 +962,7 @@ abstract class ResourceRepository extends ServiceEntityRepository
->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)
->setResourceLink($link)
;
//$rights[] = $resourceRight;
$link->addResourceRight($resourceRight);
/*if (!empty($rights)) {
$link->setResourceRights($rights);
}*/
} else {
$link->setResourceRights(new ArrayCollection());
}

@ -22,6 +22,7 @@ services:
- setSlugify: ['@cocur_slugify']
- setToolChain: ['@Chamilo\CoreBundle\ToolChain']
- setResourceNodeRepository: ['@Chamilo\CoreBundle\Repository\ResourceNodeRepository']
- setRequestStack: ['@request_stack']
tags: ['doctrine.repository_service']
# User repository
@ -33,3 +34,4 @@ services:
- setSlugify: ['@cocur_slugify']
- setToolChain: ['@Chamilo\CoreBundle\ToolChain']
- setResourceNodeRepository: ['@Chamilo\CoreBundle\Repository\ResourceNodeRepository']
- setRequestStack: ['@request_stack']

@ -35,4 +35,5 @@ services:
- setSlugify: ['@cocur_slugify']
- setToolChain: ['@Chamilo\CoreBundle\ToolChain']
- setResourceNodeRepository: ['@Chamilo\CoreBundle\Repository\ResourceNodeRepository']
- setRequestStack: ['@request_stack']
exclude: '../../Repository/CCourseSettingRepository.php'

@ -11,6 +11,8 @@ use Chamilo\CoreBundle\ToolChain;
use Cocur\Slugify\SlugifyInterface;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
@ -30,6 +32,13 @@ trait NonResourceRepository
protected ?ToolChain $toolChain = null;
protected ?RequestStack $requestStack;
public function getAuthorizationChecker(): AuthorizationCheckerInterface
{
return $this->authorizationChecker;
}
public function setAuthorizationChecker(AuthorizationCheckerInterface $authorizationChecker): self
{
$this->authorizationChecker = $authorizationChecker;
@ -37,6 +46,11 @@ trait NonResourceRepository
return $this;
}
public function getRouter(): RouterInterface
{
return $this->router;
}
public function setRouter(RouterInterface $router): self
{
$this->router = $router;
@ -58,10 +72,29 @@ trait NonResourceRepository
return $this;
}
/**
* @return ResourceNodeRepository
*/
public function getResourceNodeRepository()
{
return $this->resourceNodeRepository;
}
public function setResourceNodeRepository(ResourceNodeRepository $resourceNodeRepository): self
{
$this->resourceNodeRepository = $resourceNodeRepository;
return $this;
}
public function setRequestStack(RequestStack $requestStack): self
{
$this->requestStack = $requestStack;
return $this;
}
public function getRequest(): Request
{
return $this->requestStack->getCurrentRequest();
}
}

Loading…
Cancel
Save