Completed
Push — master ( edf824...39509c )
by Julito
10:53
created

ResourceNode::getResourceFile()   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\UserBundle\Entity\User;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\ORM\Mapping as ORM;
9
use Gedmo\Mapping\Annotation as Gedmo;
10
use Gedmo\Timestampable\Traits\TimestampableEntity;
11
use Symfony\Component\Validator\Constraints as Assert;
12
13
/**
14
 * Base entity for all resources.
15
 *
16
 * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\MaterializedPathRepository")
17
 *
18
 * @ORM\Table(name="resource_node")
19
 *
20
 * @Gedmo\Tree(type="materializedPath")
21
 */
22
class ResourceNode
23
{
24
    use TimestampableEntity;
25
26
    public const PATH_SEPARATOR = '`';
27
28
    /**
29
     * @ORM\Id
30
     * @ORM\Column(type="integer")
31
     * @ORM\GeneratedValue(strategy="AUTO")
32
     */
33
    protected $id;
34
35
    /**
36
     * @Gedmo\TreePathSource
37
     *
38
     * @ORM\Column()
39
     *
40
     * @Assert\NotBlank()
41
     */
42
    protected $name;
43
44
    /**
45
     * @var string
46
     *
47
     * @ORM\Column(name="description", type="text", nullable = true)
48
     */
49
    protected $description;
50
51
    /**
52
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceType", inversedBy="resourceNodes")
53
     * @ORM\JoinColumn(name="resource_type_id", referencedColumnName="id", nullable=false)
54
     */
55
    protected $resourceType;
56
57
    /**
58
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceLink", mappedBy="resourceNode", cascade={"remove"})
59
     */
60
    protected $resourceLinks;
61
62
    /**
63
     * @var ResourceFile
64
     *
65
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceFile", inversedBy="resourceNode", cascade={"remove"})
66
     * @ORM\JoinColumn(name="resource_file_id", referencedColumnName="id")
67
     */
68
    protected $resourceFile;
69
70
    /**
71
     * @ORM\ManyToOne(
72
     *     targetEntity="Chamilo\UserBundle\Entity\User",
73
     *     inversedBy="resourceNodes",
74
     *     cascade={"persist"}
75
     * )
76
     * @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
77
     */
78
    protected $creator;
79
80
    /**
81
     * @Gedmo\TreeParent
82
     *
83
     * @ORM\ManyToOne(
84
     *     targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode",
85
     *     inversedBy="children"
86
     * )
87
     * @ORM\JoinColumns({@ORM\JoinColumn(onDelete="CASCADE")})
88
     */
89
    protected $parent;
90
91
    /**
92
     * @Gedmo\TreeLevel
93
     *
94
     * @ORM\Column(name="level", type="integer", nullable=true)
95
     */
96
    protected $level;
97
98
    /**
99
     * @ORM\OneToMany(
100
     *     targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode",
101
     *     mappedBy="parent"
102
     * )
103
     * @ORM\OrderBy({"id" = "ASC"})
104
     */
105
    protected $children;
106
107
    /**
108
     * @Gedmo\TreePath(separator="`")
109
     *
110
     * @ORM\Column(name="path", type="string", length=3000, nullable=true)
111
     */
112
    protected $path;
113
114
    //protected $pathForCreationLog = '';
115
116
    /**
117
     * Constructor.
118
     */
119
    public function __construct()
120
    {
121
        $this->children = new ArrayCollection();
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function __toString()
128
    {
129
        return (string) $this->getName();
130
    }
131
132
    /**
133
     * Returns the resource id.
134
     *
135
     * @return int
136
     */
137
    public function getId()
138
    {
139
        return $this->id;
140
    }
141
142
    /**
143
     * @param int $id
144
     *
145
     * @return $this
146
     */
147
    public function setId($id)
148
    {
149
        $this->id = $id;
150
151
        return $this;
152
    }
153
154
    /**
155
     * Returns the resource creator.
156
     *
157
     * @return User
158
     */
159
    public function getCreator()
160
    {
161
        return $this->creator;
162
    }
163
164
    /**
165
     * Sets the resource creator.
166
     *
167
     * @param User $creator
168
     *
169
     * @return $this
170
     */
171
    public function setCreator(User $creator)
172
    {
173
        $this->creator = $creator;
174
175
        return $this;
176
    }
177
178
    /**
179
     * Returns the children resource instances.
180
     *
181
     * @return ArrayCollection
182
     */
183
    public function getChildren()
184
    {
185
        return $this->children;
186
    }
187
188
    /**
189
     * Sets the parent resource.
190
     *
191
     * @param ResourceNode $parent
192
     *
193
     * @return $this
194
     */
195
    public function setParent(ResourceNode $parent = null)
196
    {
197
        $this->parent = $parent;
198
199
        return $this;
200
    }
201
202
    /**
203
     * Returns the parent resource.
204
     *
205
     * @return AbstractResource
206
     */
207
    public function getParent()
208
    {
209
        return $this->parent;
210
    }
211
212
    /**
213
     * Return the lvl value of the resource in the tree.
214
     *
215
     * @return int
216
     */
217
    public function getLevel()
218
    {
219
        return $this->level;
220
    }
221
222
    /**
223
     * Returns the "raw" path of the resource
224
     * (the path merge names and ids of all items).
225
     * Eg.: "Root-1/subdir-2/file.txt-3/".
226
     *
227
     * @return string
228
     */
229
    public function getPath()
230
    {
231
        return $this->path;
232
    }
233
234
    /**
235
     * Returns the path cleaned from its ids.
236
     * Eg.: "Root/subdir/file.txt".
237
     *
238
     * @return
239
     */
240
    public function getPathForDisplay()
241
    {
242
        return self::convertPathForDisplay($this->path);
243
    }
244
245
    /**
246
     * Sets the resource name.
247
     *
248
     * @param string $name
249
     *
250
     * @throws an exception if the name contains the path separator ('/')
251
     *
252
     * @return $this
253
     */
254
    public function setName($name)
255
    {
256
        if (strpos(self::PATH_SEPARATOR, $name) !== false) {
257
            throw new \InvalidArgumentException(
258
                'Invalid character "'.self::PATH_SEPARATOR.'" in resource name.'
259
            );
260
        }
261
262
        $this->name = $name;
263
264
        return $this;
265
    }
266
267
    /**
268
     * Returns the resource name.
269
     *
270
     * @return string
271
     */
272
    public function getName()
273
    {
274
        return $this->name;
275
    }
276
277
    /**
278
     * Convert a path for display: remove ids.
279
     *
280
     * @param string $path
281
     *
282
     * @return string
283
     */
284
    public static function convertPathForDisplay($path)
285
    {
286
        $pathForDisplay = preg_replace(
287
            '/-\d+'.self::PATH_SEPARATOR.'/',
288
            ' / ',
289
            $path
290
        );
291
292
        if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) {
293
            $pathForDisplay = substr_replace($pathForDisplay, "", -3);
294
        }
295
296
        return $pathForDisplay;
297
    }
298
299
    /**
300
     * This is required for logging the resource path at the creation.
301
     * Do not use this function otherwise.
302
     *
303
     * @return type
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
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
     * @return ResourceFile
363
     */
364
    public function getResourceFile()
365
    {
366
        return $this->resourceFile;
367
    }
368
369
    /**
370
     * @param ResourceFile $resourceFile
371
     *
372
     * @return ResourceNode
373
     */
374
    public function setResourceFile(ResourceFile $resourceFile): ResourceNode
375
    {
376
        $this->resourceFile = $resourceFile;
377
378
        return $this;
379
    }
380
381
    /**
382
     * @return string
383
     */
384
    public function getDescription(): string
385
    {
386
        return (string) $this->description;
387
    }
388
389
    /**
390
     * @param string $description
391
     *
392
     * @return ResourceNode
393
     */
394
    public function setDescription(string $description): ResourceNode
395
    {
396
        $this->description = $description;
397
398
        return $this;
399
    }
400
}
401