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