Completed
Push — master ( 24096d...e49e51 )
by Julito
08:45
created

ResourceNode::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Entity\Resource;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Entity\Session;
9
use Chamilo\UserBundle\Entity\User;
10
use Doctrine\Common\Collections\ArrayCollection;
11
use Doctrine\Common\Collections\Criteria;
12
use Doctrine\ORM\Mapping as ORM;
13
use Gedmo\Mapping\Annotation as Gedmo;
14
use Gedmo\Timestampable\Traits\TimestampableEntity;
15
use JMS\Serializer\Annotation as JMS;
16
use JMS\Serializer\Annotation\Groups;
17
use Symfony\Component\Routing\RouterInterface;
18
use Symfony\Component\Validator\Constraints as Assert;
19
20
/**
21
 * Base entity for all resources.
22
 *
23
 * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\ResourceNodeRepository")
24
 *
25
 * @ORM\Table(name="resource_node")
26
 *
27
 * @Gedmo\Tree(type="materializedPath")
28
 */
29
class ResourceNode
30
{
31
    public const PATH_SEPARATOR = '`';
32
    use TimestampableEntity;
33
34
    /**
35
     * @Groups({"list"})
36
     * @ORM\Id
37
     * @ORM\Column(type="integer")
38
     * @ORM\GeneratedValue(strategy="AUTO")
39
     */
40
    protected $id;
41
42
    /**
43
     * @Assert\NotBlank()
44
     * @Groups({"list"})
45
     * @Gedmo\TreePathSource
46
     *
47
     * @ORM\Column(name="title", type="string", length=255, nullable=false)
48
     */
49
    protected $title;
50
51
    /**
52
     * @Assert\NotBlank()
53
     *
54
     * @Gedmo\Slug(fields={"title"})
55
     * @ORM\Column(name="slug", type="string", length=255, nullable=false)
56
     */
57
    protected $slug;
58
59
    /**
60
     * @ORM\ManyToOne(targetEntity="ResourceType", inversedBy="resourceNodes")
61
     * @ORM\JoinColumn(name="resource_type_id", referencedColumnName="id", nullable=false)
62
     */
63
    protected $resourceType;
64
65
    /**
66
     * @var ResourceLink[]
67
     *
68
     * @ORM\OneToMany(targetEntity="ResourceLink", mappedBy="resourceNode", cascade={"remove"})
69
     */
70
    protected $resourceLinks;
71
72
    /**
73
     * @var ResourceFile
74
     * @Groups({"list"})
75
     *
76
     * @ORM\OneToOne(targetEntity="ResourceFile", inversedBy="resourceNode", orphanRemoval=true)
77
     * @ORM\JoinColumn(name="resource_file_id", referencedColumnName="id", onDelete="CASCADE")
78
     */
79
    protected $resourceFile;
80
81
    /**
82
     * @Groups({"list"})
83
     * @ORM\ManyToOne(
84
     *     targetEntity="Chamilo\UserBundle\Entity\User", inversedBy="resourceNodes"
85
     * )
86
     * @ORM\JoinColumn(name="creator_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
87
     */
88
    protected $creator;
89
90
    /**
91
     * @Gedmo\TreeParent
92
     *
93
     * @ORM\ManyToOne(
94
     *     targetEntity="ResourceNode",
95
     *     inversedBy="children"
96
     * )
97
     * @ORM\JoinColumns({@ORM\JoinColumn(onDelete="CASCADE")})
98
     */
99
    protected $parent;
100
101
    /**
102
     * @Gedmo\TreeLevel
103
     *
104
     * @ORM\Column(name="level", type="integer", nullable=true)
105
     */
106
    protected $level;
107
108
    /**
109
     * @var ResourceNode[]
110
     *
111
     * @ORM\OneToMany(
112
     *     targetEntity="ResourceNode",
113
     *     mappedBy="parent"
114
     * )
115
     * @ORM\OrderBy({"id" = "ASC"})
116
     */
117
    protected $children;
118
119
    /**
120
     * @Gedmo\TreePath(appendId=true,separator="`")
121
     *
122
     * @ORM\Column(name="path", type="text", nullable=true)
123
     */
124
    protected $path;
125
126
    /**
127
     * Shortcut to access Course resource from ResourceNode.
128
     *
129
     * ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", mappedBy="resourceNode")
130
     */
131
    //protected $course;
132
133
    /**
134
     * Shortcut to access Course resource from ResourceNode.
135
     *
136
     * ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Illustration", mappedBy="resourceNode")
137
     */
138
    //protected $illustration;
139
140
    /**
141
     * @var ResourceComment[]|ArrayCollection
142
     *
143
     * @ORM\OneToMany(targetEntity="ResourceComment", mappedBy="resourceNode", cascade={"persist", "remove"})
144
     */
145
    protected $comments;
146
147
    /**
148
     * @var \DateTime
149
     *
150
     * @Groups({"list"})
151
     * @Gedmo\Timestampable(on="create")
152
     * @ORM\Column(type="datetime")
153
     * @JMS\Type("DateTime")
154
     */
155
    protected $createdAt;
156
157
    /**
158
     * @var \DateTime
159
     *
160
     * @Groups({"list"})
161
     * @Gedmo\Timestampable(on="update")
162
     * @ORM\Column(type="datetime")
163
     * @JMS\Type("DateTime")
164
     */
165
    protected $updatedAt;
166
167
    /**
168
     * Constructor.
169
     */
170
    public function __construct()
171
    {
172
        $this->children = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Doctrine\Common\Collections\ArrayCollection() of type Doctrine\Common\Collections\ArrayCollection is incompatible with the declared type Chamilo\CoreBundle\Entity\Resource\ResourceNode[] of property $children.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
173
        $this->resourceLinks = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Doctrine\Common\Collections\ArrayCollection() of type Doctrine\Common\Collections\ArrayCollection is incompatible with the declared type Chamilo\CoreBundle\Entity\Resource\ResourceLink[] of property $resourceLinks.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
174
        $this->comments = new ArrayCollection();
175
        $this->createdAt = new \DateTime();
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function __toString()
182
    {
183
        return (string) $this->getPathForDisplay();
184
    }
185
186
    /**
187
     * @return Course
188
     */
189
    public function getCourse(): ?Course
190
    {
191
        return $this->course;
192
    }
193
194
    public function isCourseNode(): bool
195
    {
196
        return null !== $this->course;
197
    }
198
199
    public function isIllustrationNode(): bool
200
    {
201
        return null !== $this->illustration;
202
    }
203
204
    /**
205
     * Returns the resource id.
206
     *
207
     * @return int
208
     */
209
    public function getId()
210
    {
211
        return $this->id;
212
    }
213
214
    /**
215
     * @param int $id
216
     *
217
     * @return $this
218
     */
219
    public function setId($id)
220
    {
221
        $this->id = $id;
222
223
        return $this;
224
    }
225
226
    /**
227
     * Returns the resource creator.
228
     *
229
     * @return User
230
     */
231
    public function getCreator(): ?User
232
    {
233
        return $this->creator;
234
    }
235
236
    public function setCreator(User $creator = null)
237
    {
238
        $this->creator = $creator;
239
240
        return $this;
241
    }
242
243
    /**
244
     * Returns the children resource instances.
245
     *
246
     * @return ResourceNode[]|ArrayCollection
247
     */
248
    public function getChildren()
249
    {
250
        return $this->children;
251
    }
252
253
    /**
254
     * Sets the parent resource.
255
     *
256
     * @param ResourceNode $parent
257
     *
258
     * @return $this
259
     */
260
    public function setParent(self $parent = null)
261
    {
262
        $this->parent = $parent;
263
264
        return $this;
265
    }
266
267
    /**
268
     * Returns the parent resource.
269
     *
270
     * @return ResourceNode
271
     */
272
    public function getParent()
273
    {
274
        return $this->parent;
275
    }
276
277
    /**
278
     * Return the lvl value of the resource in the tree.
279
     *
280
     * @return int
281
     */
282
    public function getLevel()
283
    {
284
        return $this->level;
285
    }
286
287
    /**
288
     * Returns the "raw" path of the resource
289
     * (the path merge names and ids of all items).
290
     * Eg.: "Root-1/subdir-2/file.txt-3/".
291
     *
292
     * @return string
293
     */
294
    public function getPath()
295
    {
296
        return $this->path;
297
    }
298
299
    /**
300
     * @return ResourceComment[]|ArrayCollection
301
     */
302
    public function getComments()
303
    {
304
        return $this->comments;
305
    }
306
307
    public function addComment(ResourceComment $comment)
308
    {
309
        $comment->setResourceNode($this);
310
311
        return $this->comments->add($comment);
312
    }
313
314
    /**
315
     * Returns the path cleaned from its ids.
316
     * Eg.: "Root/subdir/file.txt".
317
     *
318
     * @return string
319
     */
320
    public function getPathForDisplay()
321
    {
322
        return self::convertPathForDisplay($this->path);
323
    }
324
325
    public function getPathForDisplayToArray($baseRoot = null)
326
    {
327
        $parts = explode(self::PATH_SEPARATOR, $this->path);
328
        $list = [];
329
        foreach ($parts as $part) {
330
            $parts = explode('-', $part);
331
            if (empty($parts[1])) {
332
                continue;
333
            }
334
335
            $value = $parts[0];
336
            $id = $parts[1];
337
338
            if (!empty($baseRoot)) {
339
                if ($id < $baseRoot) {
340
                    continue;
341
                }
342
            }
343
            $list[$id] = $value;
344
        }
345
346
        return $list;
347
    }
348
349
    /**
350
     * @return string
351
     */
352
    public function getPathForDisplayRemoveBase(string $base)
353
    {
354
        $path = str_replace($base, '', $this->path);
355
356
        return self::convertPathForDisplay($path);
357
    }
358
359
    public function getSlug()
360
    {
361
        return $this->slug;
362
    }
363
364
    public function getTitle()
365
    {
366
        return $this->title;
367
    }
368
369
    public function setTitle(string $title)
370
    {
371
        $this->title = $title;
372
373
        return $this;
374
    }
375
376
    /**
377
     * @return ResourceNode
378
     */
379
    public function setSlug(string $slug)
380
    {
381
        if (false !== strpos(self::PATH_SEPARATOR, $slug)) {
382
            throw new \InvalidArgumentException('Invalid character "'.self::PATH_SEPARATOR.'" in resource name.');
383
        }
384
385
        $this->slug = $slug;
386
387
        return $this;
388
    }
389
390
    /**
391
     * Convert a path for display: remove ids.
392
     *
393
     * @param string $path
394
     *
395
     * @return string
396
     */
397
    public static function convertPathForDisplay($path)
398
    {
399
        /*$pathForDisplay = preg_replace(
400
            '/-\d+'.self::PATH_SEPARATOR.'/',
401
            ' / ',
402
            $path
403
        );
404
        if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) {
405
            $pathForDisplay = substr_replace($pathForDisplay, '', -3);
406
        }
407
        */
408
        $pathForDisplay = preg_replace(
409
            '/-\d+'.self::PATH_SEPARATOR.'/',
410
            '/',
411
            $path
412
        );
413
414
        if (null !== $pathForDisplay && strlen($pathForDisplay) > 0) {
415
            $pathForDisplay = substr_replace($pathForDisplay, '', -1);
416
        }
417
418
        return $pathForDisplay;
419
    }
420
421
    /**
422
     * @return ResourceType
423
     */
424
    public function getResourceType()
425
    {
426
        return $this->resourceType;
427
    }
428
429
    /**
430
     * @param ResourceType $resourceType
431
     *
432
     * @return ResourceNode
433
     */
434
    public function setResourceType($resourceType)
435
    {
436
        $this->resourceType = $resourceType;
437
438
        return $this;
439
    }
440
441
    /**
442
     * @return ArrayCollection|ResourceLink[]
443
     */
444
    public function getResourceLinks()
445
    {
446
        return $this->resourceLinks;
447
    }
448
449
    /**
450
     * @return ResourceNode
451
     */
452
    public function setResourceLinks($resourceLinks)
453
    {
454
        $this->resourceLinks = $resourceLinks;
455
456
        return $this;
457
    }
458
459
    /**
460
     * @param Session $session
461
     *
462
     * @return ArrayCollection
463
     */
464
    public function hasSession(Session $session = null)
465
    {
466
        $links = $this->getResourceLinks();
467
        $criteria = Criteria::create();
468
469
        $criteria->andWhere(
470
            Criteria::expr()->eq('session', $session)
471
        );
472
473
        return $links->matching($criteria);
474
    }
475
476
    /**
477
     * @return bool
478
     */
479
    public function hasResourceFile()
480
    {
481
        return null !== $this->resourceFile;
482
    }
483
484
    /**
485
     * @return ResourceFile
486
     */
487
    public function getResourceFile(): ?ResourceFile
488
    {
489
        return $this->resourceFile;
490
    }
491
492
    /**
493
     * @return bool
494
     */
495
    public function hasEditableContent()
496
    {
497
        if ($this->hasResourceFile()) {
498
            $mimeType = $this->getResourceFile()->getMimeType();
499
            if (false !== strpos($mimeType, 'text')) {
500
                return true;
501
            }
502
        }
503
504
        return false;
505
    }
506
507
    /**
508
     * @return bool
509
     */
510
    public function isResourceFileAnImage()
511
    {
512
        if ($this->hasResourceFile()) {
513
            $mimeType = $this->getResourceFile()->getMimeType();
514
            if (false !== strpos($mimeType, 'image')) {
515
                return true;
516
            }
517
        }
518
519
        return false;
520
    }
521
522
    /**
523
     * @return bool
524
     */
525
    public function isResourceFileAVideo()
526
    {
527
        if ($this->hasResourceFile()) {
528
            $mimeType = $this->getResourceFile()->getMimeType();
529
            if (false !== strpos($mimeType, 'video')) {
530
                return true;
531
            }
532
        }
533
534
        return false;
535
    }
536
537
    public function setResourceFile(ResourceFile $resourceFile = null): self
538
    {
539
        $this->resourceFile = $resourceFile;
540
541
        return $this;
542
    }
543
544
    /**
545
     * @return string
546
     */
547
    public function getIcon()
548
    {
549
        $class = 'fa fa-folder';
550
        if ($this->hasResourceFile()) {
551
            $class = 'far fa-file';
552
553
            if ($this->isResourceFileAnImage()) {
554
                $class = 'far fa-file-image';
555
            }
556
            if ($this->isResourceFileAVideo()) {
557
                $class = 'far fa-file-video';
558
            }
559
        }
560
561
        return '<i class="'.$class.'"></i>';
562
    }
563
564
    /**
565
     * @return string
566
     */
567
    public function getThumbnail(RouterInterface $router)
568
    {
569
        $size = 'fa-3x';
570
        $class = "fa fa-folder $size";
571
        if ($this->hasResourceFile()) {
572
            $class = "far fa-file $size";
573
574
            if ($this->isResourceFileAnImage()) {
575
                $class = "far fa-file-image $size";
576
577
                $params = [
578
                    'id' => $this->getId(),
579
                    'tool' => $this->getResourceType()->getTool(),
580
                    'type' => $this->getResourceType()->getName(),
581
                    'filter' => 'editor_thumbnail',
582
                ];
583
                $url = $router->generate(
584
                    'chamilo_core_resource_view_file',
585
                    $params
586
                );
587
588
                return "<img src='$url'/>";
589
            }
590
            if ($this->isResourceFileAVideo()) {
591
                $class = "far fa-file-video $size";
592
            }
593
        }
594
595
        return '<i class="'.$class.'"></i>';
596
    }
597
}
598