Completed
Push — master ( 4874d1...5ea477 )
by Julito
10:24
created

ResourceNode::getPathForCreationLog()   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
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Entity\Resource;
5
6
use Chamilo\CoreBundle\Entity\Session;
7
use Chamilo\UserBundle\Entity\User;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\Common\Collections\Criteria;
10
use Doctrine\ORM\Mapping as ORM;
11
use Gedmo\Mapping\Annotation as Gedmo;
12
use Gedmo\Timestampable\Traits\TimestampableEntity;
13
use Symfony\Component\Validator\Constraints as Assert;
14
15
/**
16
 * Base entity for all resources.
17
 *
18
 * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\ResourceNodeRepository")
19
 *
20
 * @ORM\Table(name="resource_node")
21
 *
22
 * @Gedmo\Tree(type="materializedPath")
23
 */
24
class ResourceNode
25
{
26
    use TimestampableEntity;
27
28
    public const PATH_SEPARATOR = '`';
29
30
    /**
31
     * @ORM\Id
32
     * @ORM\Column(type="integer")
33
     * @ORM\GeneratedValue(strategy="AUTO")
34
     */
35
    protected $id;
36
37
    /**
38
     * @Gedmo\TreePathSource
39
     * @ORM\Column()
40
     *
41
     * @Assert\NotBlank()
42
     */
43
    protected $name;
44
45
    /**
46
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceType", inversedBy="resourceNodes")
47
     * @ORM\JoinColumn(name="resource_type_id", referencedColumnName="id", nullable=false)
48
     */
49
    protected $resourceType;
50
51
    /**
52
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceLink", mappedBy="resourceNode",
53
     *                                                                                cascade={"remove"})
54
     */
55
    protected $resourceLinks;
56
57
    /**
58
     * @var ResourceFile
59
     *
60
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceFile", inversedBy="resourceNode", orphanRemoval=true)
61
     * @ORM\JoinColumn(name="resource_file_id", referencedColumnName="id", onDelete="CASCADE")
62
     */
63
    protected $resourceFile;
64
65
    /**
66
     * @ORM\ManyToOne(
67
     *     targetEntity="Chamilo\UserBundle\Entity\User",
68
     *     inversedBy="resourceNodes",
69
     *     cascade={"persist"}
70
     * )
71
     * @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
72
     */
73
    protected $creator;
74
75
    /**
76
     * @Gedmo\TreeParent
77
     *
78
     * @ORM\ManyToOne(
79
     *     targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode",
80
     *     inversedBy="children"
81
     * )
82
     * @ORM\JoinColumns({@ORM\JoinColumn(onDelete="CASCADE")})
83
     */
84
    protected $parent;
85
86
    /**
87
     * @Gedmo\TreeLevel
88
     *
89
     * @ORM\Column(name="level", type="integer", nullable=true)
90
     */
91
    protected $level;
92
93
    /**
94
     * @var ResourceNode[]
95
     *
96
     * @ORM\OneToMany(
97
     *     targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode",
98
     *     mappedBy="parent"
99
     * )
100
     * @ORM\OrderBy({"id" = "ASC"})
101
     */
102
    protected $children;
103
104
    /**
105
     * @Gedmo\TreePath(separator="`")
106
     *
107
     * @ORM\Column(name="path", type="string", length=3000, nullable=true)
108
     */
109
    protected $path;
110
111
    //protected $pathForCreationLog = '';
112
113
    /**
114
     * Constructor.
115
     */
116
    public function __construct()
117
    {
118
        $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...
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function __toString()
125
    {
126
        return (string) $this->getName();
127
    }
128
129
    /**
130
     * Returns the resource id.
131
     *
132
     * @return int
133
     */
134
    public function getId()
135
    {
136
        return $this->id;
137
    }
138
139
    /**
140
     * @param int $id
141
     *
142
     * @return $this
143
     */
144
    public function setId($id)
145
    {
146
        $this->id = $id;
147
148
        return $this;
149
    }
150
151
    /**
152
     * Returns the resource creator.
153
     *
154
     * @return User
155
     */
156
    public function getCreator()
157
    {
158
        return $this->creator;
159
    }
160
161
    /**
162
     * Sets the resource creator.
163
     *
164
     * @return $this
165
     */
166
    public function setCreator(User $creator)
167
    {
168
        $this->creator = $creator;
169
170
        return $this;
171
    }
172
173
    /**
174
     * Returns the children resource instances.
175
     *
176
     * @return ArrayCollection
177
     */
178
    public function getChildren()
179
    {
180
        return $this->children;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->children returns the type Chamilo\CoreBundle\Entity\Resource\ResourceNode[] which is incompatible with the documented return type Doctrine\Common\Collections\ArrayCollection.
Loading history...
181
    }
182
183
    /**
184
     * Sets the parent resource.
185
     *
186
     * @param ResourceNode $parent
187
     *
188
     * @return $this
189
     */
190
    public function setParent(ResourceNode $parent = null)
191
    {
192
        $this->parent = $parent;
193
194
        return $this;
195
    }
196
197
    /**
198
     * Returns the parent resource.
199
     *
200
     * @return ResourceNode
201
     */
202
    public function getParent()
203
    {
204
        return $this->parent;
205
    }
206
207
    /**
208
     * Return the lvl value of the resource in the tree.
209
     *
210
     * @return int
211
     */
212
    public function getLevel()
213
    {
214
        return $this->level;
215
    }
216
217
    /**
218
     * Returns the "raw" path of the resource
219
     * (the path merge names and ids of all items).
220
     * Eg.: "Root-1/subdir-2/file.txt-3/".
221
     *
222
     * @return string
223
     */
224
    public function getPath()
225
    {
226
        return $this->path;
227
    }
228
229
    /**
230
     * Returns the path cleaned from its ids.
231
     * Eg.: "Root/subdir/file.txt".
232
     *
233
     * @return string
234
     */
235
    public function getPathForDisplay()
236
    {
237
        return self::convertPathForDisplay($this->path);
238
    }
239
240
    /**
241
     * Sets the resource name.
242
     *
243
     * @param string $name
244
     *
245
     * @throws an exception if the name contains the path separator ('/')
246
     *
247
     * @return $this
248
     */
249
    public function setName($name)
250
    {
251
        if (strpos(self::PATH_SEPARATOR, $name) !== false) {
252
            throw new \InvalidArgumentException('Invalid character "'.self::PATH_SEPARATOR.'" in resource name.');
253
        }
254
255
        $this->name = $name;
256
257
        return $this;
258
    }
259
260
    /**
261
     * Returns the resource name.
262
     *
263
     * @return string
264
     */
265
    public function getName()
266
    {
267
        return $this->name;
268
    }
269
270
    /**
271
     * Convert a path for display: remove ids.
272
     *
273
     * @param string $path
274
     *
275
     * @return string
276
     */
277
    public static function convertPathForDisplay($path)
278
    {
279
        /*$pathForDisplay = preg_replace(
280
            '/-\d+'.self::PATH_SEPARATOR.'/',
281
            ' / ',
282
            $path
283
        );
284
        if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) {
285
            $pathForDisplay = substr_replace($pathForDisplay, '', -3);
286
        }
287
        */
288
        $pathForDisplay = preg_replace(
289
            '/-\d+'.self::PATH_SEPARATOR.'/',
290
            '/',
291
            $path
292
        );
293
294
        if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) {
295
            $pathForDisplay = substr_replace($pathForDisplay, '', -1);
296
        }
297
298
        return $pathForDisplay;
299
    }
300
301
    /**
302
     * This is required for logging the resource path at the creation.
303
     * Do not use this function otherwise.
304
     */
305
    public function setPathForCreationLog($path)
306
    {
307
        $this->pathForCreationLog = $path;
0 ignored issues
show
Bug Best Practice introduced by
The property pathForCreationLog does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
308
    }
309
310
    /**
311
     * This is required for logging the resource path at the creation.
312
     * Do not use this function otherwise.
313
     *
314
     * @return type
315
     */
316
    public function getPathForCreationLog()
317
    {
318
        return $this->pathForCreationLog;
319
    }
320
321
    /**
322
     * @return ResourceType
323
     */
324
    public function getResourceType()
325
    {
326
        return $this->resourceType;
327
    }
328
329
    /**
330
     * @param ResourceType $resourceType
331
     *
332
     * @return ResourceNode
333
     */
334
    public function setResourceType($resourceType)
335
    {
336
        $this->resourceType = $resourceType;
337
338
        return $this;
339
    }
340
341
    /**
342
     * @return ArrayCollection|ResourceLink[]
343
     */
344
    public function getResourceLinks()
345
    {
346
        return $this->resourceLinks;
347
    }
348
349
    /**
350
     * @param mixed $resourceLinks
351
     *
352
     * @return ResourceNode
353
     */
354
    public function setResourceLinks($resourceLinks)
355
    {
356
        $this->resourceLinks = $resourceLinks;
357
358
        return $this;
359
    }
360
361
    /**
362
     * @param Session $session
363
     *
364
     * @return ArrayCollection
365
     */
366
    public function hasSession(Session $session = null)
367
    {
368
        $links = $this->getResourceLinks();
369
        $criteria = Criteria::create();
370
371
        $criteria->andWhere(
372
            Criteria::expr()->eq('session', $session)
373
        );
374
375
        $result = $links->matching($criteria);
376
377
        return $result;
378
    }
379
380
    /**
381
     * @return bool
382
     */
383
    public function hasResourceFile()
384
    {
385
        return $this->resourceFile !== null;
386
    }
387
388
    /**
389
     * @return ResourceFile
390
     */
391
    public function getResourceFile(): ?ResourceFile
392
    {
393
        return $this->resourceFile;
394
    }
395
396
    /**
397
     * @return bool
398
     */
399
    public function isEditable()
400
    {
401
        if ($this->hasResourceFile()) {
402
            $mimeType = $this->getResourceFile()->getMimeType();
403
            if (strpos($mimeType, 'text') !== false) {
404
                return true;
405
            }
406
        }
407
408
        return false;
409
    }
410
411
    /**
412
     * @return bool
413
     */
414
    public function isResourceFileAnImage()
415
    {
416
        if ($this->hasResourceFile()) {
417
418
            $mimeType = $this->getResourceFile()->getMimeType();
419
            if (strpos($mimeType, 'image') !== false) {
420
                return true;
421
            }
422
        }
423
424
        return false;
425
    }
426
427
    /**
428
     * @param ResourceFile $resourceFile
429
     *
430
     * @return ResourceNode
431
     */
432
    public function setResourceFile(ResourceFile $resourceFile): ResourceNode
433
    {
434
        $this->resourceFile = $resourceFile;
435
436
        return $this;
437
    }
438
}
439