Passed
Push — master ( 845a96...e5a491 )
by Julito
09:08
created

CDocument::postPersist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A CDocument::setSession() 0 5 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CourseBundle\Entity;
6
7
use ApiPlatform\Core\Annotation\ApiFilter;
8
use ApiPlatform\Core\Annotation\ApiResource;
9
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
10
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
11
use APY\DataGridBundle\Grid\Mapping as GRID;
12
use Chamilo\CoreBundle\Controller\CreateResourceNodeFileAction;
13
use Chamilo\CoreBundle\Entity\AbstractResource;
14
use Chamilo\CoreBundle\Entity\Course;
15
use Chamilo\CoreBundle\Entity\ResourceInterface;
16
use Chamilo\CoreBundle\Entity\ResourceToCourseInterface;
17
use Chamilo\CoreBundle\Entity\Session;
18
use Chamilo\CourseBundle\Traits\ShowCourseResourcesInSessionTrait;
19
use Doctrine\ORM\Mapping as ORM;
20
use Symfony\Component\Serializer\Annotation\Groups;
21
22
//*      attributes={"security"="is_granted('ROLE_ADMIN')"},
23
/**
24
 * @ApiResource(
25
 *      shortName="Documents",
26
 *      normalizationContext={"groups"={"document:read", "resource_node:read"}},
27
 *      denormalizationContext={"groups"={"document:write"}},
28
 *      collectionOperations={
29
 *         "post"={
30
 *             "controller"=CreateResourceNodeFileAction::class,
31
 *             "deserialize"=false,
32
 *             "security"="is_granted('ROLE_USER')",
33
 *             "validation_groups"={"Default", "media_object_create", "document:write"},
34
 *             "openapi_context"={
35
 *                 "requestBody"={
36
 *                     "content"={
37
 *                         "multipart/form-data"={
38
 *                             "schema"={
39
 *                                 "type"="object",
40
 *                                 "properties"={
41
 *                                     "title"={
42
 *                                         "type"="string",
43
 *                                     },
44
 *                                     "filetype"={
45
 *                                         "type"="string",
46
 *                                         "enum"={"folder", "file"},
47
 *                                     },
48
 *                                     "comment"={
49
 *                                         "type"="string",
50
 *                                     },
51
 *                                     "uploadFile"={
52
 *                                         "type"="string",
53
 *                                         "format"="binary"
54
 *                                     },
55
 *                                     "parentResourceNodeId"={
56
 *                                         "type"="integer",
57
 *                                     },
58
 *                                     "resourceLinks"={
59
 *                                         "type"="array",
60
 *                                         "items": {
61
 *                                              "type": "object",
62
 *                                              "properties"={
63
 *                                                  "visibility"={
64
 *                                                       "type"="integer",
65
 *                                                   },
66
 *                                                  "c_id"={
67
 *                                                       "type"="integer",
68
 *                                                   },
69
 *                                                   "session_id"={
70
 *                                                       "type"="integer",
71
 *                                                   },
72
 *                                              }
73
 *                                         }
74
 *                                     },
75
 *                                 }
76
 *                             }
77
 *                         }
78
 *                     }
79
 *                 }
80
 *             }
81
 *         },
82
 *         "get"
83
 *     },
84
 * )
85
 * @ApiFilter(SearchFilter::class, properties={"title": "partial", "resourceNode.parent": "exact"})
86
 * @ApiFilter(
87
 *     OrderFilter::class,
88
 *     properties={
89
 *          "id",
90
 *          "filetype",
91
 *          "resourceNode.title",
92
 *          "resourceNode.createdAt",
93
 *          "resourceNode.resourceFile.size",
94
 *          "resourceNode.updatedAt"
95
 *      }
96
 * )
97
 *
98
 * @ORM\Table(
99
 *  name="c_document",
100
 *  indexes={
101
 *      @ORM\Index(name="course", columns={"c_id"}),
102
 *      @ORM\Index(name="idx_cdoc_path", columns={"path"}),
103
 *      @ORM\Index(name="idx_cdoc_size", columns={"size"}),
104
 *      @ORM\Index(name="idx_cdoc_id", columns={"id"}),
105
 *      @ORM\Index(name="idx_cdoc_type", columns={"filetype"}),
106
 *      @ORM\Index(name="idx_cdoc_sid", columns={"session_id"}),
107
 *  }
108
 * )
109
 * @GRID\Source(columns="iid, title, resourceNode.createdAt", filterable=false, groups={"resource"})
110
 * @GRID\Source(columns="iid, title", filterable=false, groups={"editor"})
111
 * @ORM\Entity
112
 */
113
class CDocument extends AbstractResource implements ResourceInterface, ResourceToCourseInterface
114
{
115
    use ShowCourseResourcesInSessionTrait;
116
117
    /**
118
     * @var int
119
     * @Groups({"document:read"})
120
     * @ORM\Column(name="iid", type="integer")
121
     * @ORM\Id
122
     * @ORM\GeneratedValue
123
     */
124
    protected $iid;
125
126
    /**
127
     * @var int
128
     *
129
     * @ORM\Column(name="id", type="integer", nullable=true)
130
     */
131
    protected $id;
132
133
    /**
134
     * @var string
135
     *
136
     * @ORM\Column(name="path", type="string", length=255, nullable=true)
137
     */
138
    protected $path;
139
140
    /**
141
     * @var string
142
     * @Groups({"document:read", "document:write"})
143
     * @ORM\Column(name="title", type="string", length=255, nullable=true)
144
     */
145
    protected $title;
146
147
    /**
148
     * @var string
149
     * @Groups({"document:read", "document:write"})
150
     * @ORM\Column(name="comment", type="text", nullable=true)
151
     */
152
    protected $comment;
153
154
    /**
155
     * @var string File type, it can be 'folder' or 'file'
156
     * @Groups({"document:read", "document:write"})
157
     * @ORM\Column(name="filetype", type="string", length=10, nullable=false)
158
     */
159
    protected $filetype;
160
161
    /**
162
     * @var int
163
     *
164
     * @ORM\Column(name="size", type="integer", nullable=false)
165
     */
166
    protected $size;
167
168
    /**
169
     * @var bool
170
     *
171
     * @ORM\Column(name="readonly", type="boolean", nullable=false)
172
     */
173
    protected $readonly;
174
175
    /**
176
     * @var bool
177
     *
178
     * @ORM\Column(name="template", type="boolean", nullable=false)
179
     */
180
    protected $template;
181
182
    /**
183
     * @var Course
184
     *
185
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", cascade={"persist"})
186
     * @ORM\JoinColumn(name="c_id", referencedColumnName="id", onDelete="CASCADE" )
187
     */
188
    protected $course;
189
190
    /**
191
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session", cascade={"persist"})
192
     * @ORM\JoinColumn(name="session_id", referencedColumnName="id", onDelete="CASCADE" )
193
     */
194
    protected $session;
195
196
    /**
197
     * CDocument constructor.
198
     */
199
    public function __construct()
200
    {
201
        $this->id = 0;
202
        $this->size = 0;
203
        $this->filetype = 'folder';
204
        $this->readonly = false;
205
        $this->template = false;
206
    }
207
208
    public function __toString(): string
209
    {
210
        return $this->getTitle();
211
    }
212
213
    public function isTemplate(): bool
214
    {
215
        return $this->template;
216
    }
217
218
    public function setTemplate(bool $template): self
219
    {
220
        $this->template = $template;
221
222
        return $this;
223
    }
224
225
    /**
226
     * Set path.
227
     *
228
     * @param string $path
229
     *
230
     * @return CDocument
231
     */
232
    public function setPath($path)
233
    {
234
        $this->path = $path;
235
236
        return $this;
237
    }
238
239
    /**
240
     * Get path.
241
     *
242
     * @return string
243
     */
244
    public function getPath()
245
    {
246
        return $this->path;
247
    }
248
249
    /**
250
     * Set comment.
251
     *
252
     * @param string $comment
253
     *
254
     * @return CDocument
255
     */
256
    public function setComment($comment)
257
    {
258
        $this->comment = $comment;
259
260
        return $this;
261
    }
262
263
    /**
264
     * Get comment.
265
     *
266
     * @return string
267
     */
268
    public function getComment()
269
    {
270
        return $this->comment;
271
    }
272
273
    /**
274
     * Set title.
275
     *
276
     * @param string $title
277
     *
278
     * @return CDocument
279
     */
280
    public function setTitle($title)
281
    {
282
        $this->title = $title;
283
284
        return $this;
285
    }
286
287
    /**
288
     * Get title.
289
     *
290
     * @return string
291
     */
292
    public function getTitle()
293
    {
294
        return (string) $this->title;
295
    }
296
297
    /**
298
     * Set filetype.
299
     *
300
     * @param string $filetype
301
     *
302
     * @return CDocument
303
     */
304
    public function setFiletype($filetype)
305
    {
306
        $this->filetype = $filetype;
307
308
        return $this;
309
    }
310
311
    /**
312
     * Get filetype.
313
     *
314
     * @return string
315
     */
316
    public function getFiletype()
317
    {
318
        return $this->filetype;
319
    }
320
321
    /**
322
     * Set size.
323
     *
324
     * @return CDocument
325
     */
326
    public function setSize(int $size)
327
    {
328
        $this->size = $size ?: 0;
329
330
        return $this;
331
    }
332
333
    /**
334
     * Get size.
335
     *
336
     * @return int
337
     */
338
    public function getSize()
339
    {
340
        return $this->size;
341
    }
342
343
    /**
344
     * Set readonly.
345
     *
346
     * @param bool $readonly
347
     *
348
     * @return CDocument
349
     */
350
    public function setReadonly($readonly)
351
    {
352
        $this->readonly = $readonly;
353
354
        return $this;
355
    }
356
357
    /**
358
     * Get readonly.
359
     *
360
     * @return bool
361
     */
362
    public function getReadonly()
363
    {
364
        return $this->readonly;
365
    }
366
367
    /**
368
     * Set id.
369
     *
370
     * @param int $id
371
     *
372
     * @return CDocument
373
     */
374
    public function setId($id)
375
    {
376
        $this->id = $id;
377
378
        return $this;
379
    }
380
381
    public function getCourse(): Course
382
    {
383
        return $this->course;
384
    }
385
386
    /**
387
     * @param Course $course
388
     *
389
     * @return CDocument
390
     */
391
    public function setCourse($course)
392
    {
393
        $this->course = $course;
394
395
        return $this;
396
    }
397
398
    /**
399
     * @return int
400
     */
401
    public function getIid()
402
    {
403
        return $this->iid;
404
    }
405
406
    /**
407
     * @return Session
408
     */
409
    public function getSession()
410
    {
411
        return $this->session;
412
    }
413
414
    /**
415
     * @param Session $session
416
     *
417
     * @return CDocument
418
     */
419
    public function setSession($session)
420
    {
421
        $this->session = $session;
422
423
        return $this;
424
    }
425
426
    /*public function postPersist(LifecycleEventArgs $args)
427
    {
428
        // Update id with iid value
429
        $em = $args->getEntityManager();
430
        $em->persist($this);
431
        $em->flush();
432
    }*/
433
434
    /**
435
     * Resource identifier.
436
     */
437
    public function getResourceIdentifier(): int
438
    {
439
        return $this->getIid();
440
    }
441
442
    public function getResourceName(): string
443
    {
444
        return $this->getTitle();
445
    }
446
}
447