Passed
Push — master ( 6f8b91...26ffea )
by Julito
10:53
created

IllustrationRepository::getResources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 5
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository\Node;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\Illustration;
11
use Chamilo\CoreBundle\Entity\ResourceFile;
12
use Chamilo\CoreBundle\Entity\ResourceIllustrationInterface;
13
use Chamilo\CoreBundle\Entity\ResourceInterface;
14
use Chamilo\CoreBundle\Entity\ResourceNode;
15
use Chamilo\CoreBundle\Entity\Session;
16
use Chamilo\CoreBundle\Entity\User;
17
use Chamilo\CoreBundle\Form\Resource\IllustrationType;
18
use Chamilo\CoreBundle\Repository\ResourceRepository;
19
use Chamilo\CoreBundle\Repository\UploadInterface;
20
use Chamilo\CourseBundle\Entity\CGroup;
21
use Doctrine\ORM\QueryBuilder;
22
use Doctrine\Persistence\ManagerRegistry;
23
use Symfony\Component\HttpFoundation\File\UploadedFile;
24
25
final class IllustrationRepository extends ResourceRepository implements UploadInterface
26
{
27
    public function __construct(ManagerRegistry $registry)
28
    {
29
        parent::__construct($registry, Illustration::class);
30
    }
31
32
    /*public function getResources(User $user, ResourceNode $parentNode, Course $course = null, Session $session = null, CGroup $group = null): QueryBuilder
33
    {
34
        $qb = $this->createQueryBuilder('resource')
35
            ->select('resource')
36
            ->innerJoin(
37
                'resource.resourceNode',
38
                'node'
39
            )
40
        ;
41
42
        $qb->andWhere('node.creator = :creator');
43
        $qb->setParameter('creator', $user);
44
45
        return $qb;
46
    }*/
47
48
    public function saveUpload(UploadedFile $file): ResourceInterface
49
    {
50
        $resource = new Illustration();
51
        $resource->setName($file->getClientOriginalName());
52
53
        return $resource;
54
    }
55
56
    /**
57
     * @param ResourceInterface|User $resource
58
     */
59
    public function addIllustration(
60
        ResourceInterface $resource,
61
        User $creator,
62
        UploadedFile $uploadFile = null,
63
        string $crop = ''
64
    ): ?ResourceFile {
65
        if (null === $uploadFile) {
66
            return null;
67
        }
68
69
        $illustrationNode = $this->getIllustrationNodeFromParent($resource->getResourceNode());
70
        $em = $this->getEntityManager();
71
72
        if (null === $illustrationNode) {
73
            $illustration = (new Illustration())
74
                ->setCreator($creator)
75
                ->setParent($resource)
76
            ;
77
            $em->persist($illustration);
78
        } else {
79
            $illustration = $this->findOneBy([
80
                'resourceNode' => $illustrationNode,
81
            ]);
82
        }
83
84
        $file = $this->addFile($illustration, $uploadFile);
85
        if (null !== $file) {
86
            if (!empty($crop)) {
87
                $file->setCrop($crop);
88
            }
89
            $em->persist($file);
90
        }
91
        $em->flush();
92
93
        return $file;
94
    }
95
96
    public function getIllustrationNodeFromParent(ResourceNode $resourceNode): ?ResourceNode
97
    {
98
        $nodeRepo = $this->getResourceNodeRepository();
99
        $name = $this->getResourceTypeName();
100
101
        $qb = $nodeRepo->createQueryBuilder('n')
102
            ->select('node')
103
            ->from(ResourceNode::class, 'node')
104
            ->innerJoin('node.resourceType', 'type')
105
            ->innerJoin('node.resourceFile', 'file')
106
            ->where('node.parent = :parent')
107
            ->andWhere('type.name = :name')
108
            ->setParameters([
109
                'parent' => $resourceNode->getId(),
110
                'name' => $name,
111
            ])
112
            ->setMaxResults(1)
113
        ;
114
115
        return $qb->getQuery()->getOneOrNullResult();
116
    }
117
118
    public function deleteIllustration(ResourceInterface $resource): void
119
    {
120
        $node = $this->getIllustrationNodeFromParent($resource->getResourceNode());
121
122
        if (null !== $node) {
123
            $this->getEntityManager()->remove($node);
124
            $this->getEntityManager()->flush();
125
        }
126
    }
127
128
    public function hasIllustration(ResourceIllustrationInterface $resource): bool
129
    {
130
        $node = $this->getIllustrationNodeFromParent($resource->getResourceNode());
131
132
        return null !== $node;
133
    }
134
135
    /**
136
     * @param string $filter See: services.yaml parameter "glide_media_filters" to see the list of filters.
137
     */
138
    public function getIllustrationUrl(
139
        ResourceIllustrationInterface $resource,
140
        string $filter = '',
141
        int $size = 32
142
    ): string {
143
        $illustration = $this->getIllustrationUrlFromNode($resource->getResourceNode(), $filter);
144
145
        if (empty($illustration)) {
146
            $illustration = $resource->getDefaultIllustration($size);
147
        }
148
149
        return $illustration;
150
    }
151
152
    public function getResourceFormType(): string
153
    {
154
        return IllustrationType::class;
155
    }
156
157
    private function getIllustrationUrlFromNode(ResourceNode $node, string $filter = ''): string
158
    {
159
        $node = $this->getIllustrationNodeFromParent($node);
160
161
        if (null !== $node) {
162
            $params = [
163
                'id' => $node->getId(),
164
                'tool' => $node->getResourceType()->getTool(),
165
                'type' => $node->getResourceType()->getName(),
166
            ];
167
168
            if (!empty($filter)) {
169
                $params['filter'] = $filter;
170
            }
171
172
            return $this->getRouter()->generate('chamilo_core_resource_view', $params);
173
        }
174
175
        return '';
176
    }
177
}
178