CBlog::setDateCreation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Entity;
8
9
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
10
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
11
use ApiPlatform\Metadata\ApiFilter;
12
use ApiPlatform\Metadata\ApiResource;
13
use ApiPlatform\Metadata\Delete;
14
use ApiPlatform\Metadata\Get;
15
use ApiPlatform\Metadata\GetCollection;
16
use ApiPlatform\Metadata\Patch;
17
use ApiPlatform\Metadata\Post;
18
use ApiPlatform\Metadata\Put;
19
use ApiPlatform\OpenApi\Model\Operation;
20
use ApiPlatform\OpenApi\Model\RequestBody;
21
use ArrayObject;
22
use Chamilo\CoreBundle\Controller\Api\CreateCBlogAction;
23
use Chamilo\CoreBundle\Controller\Api\UpdateVisibilityBlog;
24
use Chamilo\CoreBundle\Entity\AbstractResource;
25
use Chamilo\CoreBundle\Entity\ResourceInterface;
26
use Chamilo\CoreBundle\Entity\ResourceLink;
27
use Chamilo\CourseBundle\Repository\CBlogRepository;
28
use DateTime;
29
use Doctrine\Common\Collections\ArrayCollection;
30
use Doctrine\Common\Collections\Collection;
31
use Doctrine\ORM\Mapping as ORM;
32
use Stringable;
33
use Symfony\Component\Serializer\Annotation\Groups;
34
use Symfony\Component\Validator\Constraints as Assert;
35
36
#[ORM\Entity(repositoryClass: CBlogRepository::class)]
37
#[ORM\Table(name: 'c_blog')]
38
#[ApiResource(
39
    operations: [
40
        new GetCollection(),
41
        new Post(
42
            controller: CreateCBlogAction::class,
43
            openapi: new Operation(
44
                summary: 'Create a new blog project',
45
                requestBody: new RequestBody(
46
                    content: new ArrayObject([
47
                        'application/json' => [
48
                            'schema' => [
49
                                'type' => 'object',
50
                                'properties' => [
51
                                    'title' => ['type' => 'string'],
52
                                    'blogSubtitle' => ['type' => 'string'],
53
                                    'parentResourceNodeId' => ['type' => 'integer'],
54
                                    'resourceLinkList' => [
55
                                        'oneOf' => [
56
                                            ['type' => 'array'],
57
                                            ['type' => 'string'],
58
                                        ],
59
                                    ],
60
                                    'showOnHomepage' => ['type' => 'boolean'],
61
                                ],
62
                                'required' => ['title'],
63
                            ],
64
                        ],
65
                    ]),
66
                ),
67
            ),
68
            security: "is_granted('ROLE_CURRENT_COURSE_TEACHER') or is_granted('ROLE_CURRENT_COURSE_SESSION_TEACHER') or is_granted('ROLE_TEACHER')",
69
            validationContext: ['groups' => ['Default', 'blog:write']],
70
            deserialize: false
71
        ),
72
        new Get(security: "is_granted('VIEW', object.resourceNode)"),
73
        new Patch(security: "is_granted('ROLE_USER')"),
74
        new Delete(security: "is_granted('ROLE_USER')"),
75
        new Put(
76
            uriTemplate: '/c_blogs/{iid}/toggle_visibility',
77
            controller: UpdateVisibilityBlog::class,
78
            security: "is_granted('EDIT', object.resourceNode)",
79
            deserialize: false
80
        ),
81
    ],
82
    normalizationContext: ['groups' => ['blog:read']],
83
    denormalizationContext: ['groups' => ['blog:write', 'resource_node:write']],
84
    paginationEnabled: true
85
)]
86
#[ApiFilter(SearchFilter::class, properties: [
87
    'title' => 'partial',
88
    'blogSubtitle' => 'partial',
89
])]
90
#[ApiFilter(OrderFilter::class, properties: [
91
    'dateCreation',
92
    'title',
93
], arguments: ['orderParameterName' => 'order'])]
94
class CBlog extends AbstractResource implements ResourceInterface, Stringable
95
{
96
    #[Groups(['blog:read'])]
97
    #[ORM\Id, ORM\Column(name: 'iid', type: 'integer'), ORM\GeneratedValue]
98
    protected ?int $iid = null;
99
100
    #[Assert\NotBlank]
101
    #[Groups(['blog:read', 'blog:write'])]
102
    #[ORM\Column(name: 'title', type: 'text', nullable: false)]
103
    protected string $title;
104
105
    #[Groups(['blog:read', 'blog:write'])]
106
    #[ORM\Column(name: 'blog_subtitle', type: 'string', length: 250, nullable: true)]
107
    protected ?string $blogSubtitle = null;
108
109
    #[Groups(['blog:read'])]
110
    #[ORM\Column(name: 'date_creation', type: 'datetime', nullable: false)]
111
    protected DateTime $dateCreation;
112
113
    #[Groups(['blog:read'])]
114
    #[ORM\OneToMany(mappedBy: 'blog', targetEntity: CBlogAttachment::class, cascade: ['persist', 'remove'])]
115
    private Collection $attachments;
116
117
    public function __construct()
118
    {
119
        $this->attachments = new ArrayCollection();
120
        $this->dateCreation = new DateTime();
121
    }
122
123
    public function getIid(): ?int
124
    {
125
        return $this->iid;
126
    }
127
128
    public function getTitle(): string
129
    {
130
        return $this->title;
131
    }
132
    public function setTitle(string $title): self
133
    {
134
        $this->title = $title;
135
136
        return $this;
137
    }
138
139
    public function getBlogSubtitle(): ?string
140
    {
141
        return $this->blogSubtitle;
142
    }
143
    public function setBlogSubtitle(?string $blogSubtitle): self
144
    {
145
        $this->blogSubtitle = $blogSubtitle;
146
147
        return $this;
148
    }
149
150
    public function getDateCreation(): DateTime
151
    {
152
        return $this->dateCreation;
153
    }
154
    public function setDateCreation(DateTime $dateCreation): self
155
    {
156
        $this->dateCreation = $dateCreation;
157
158
        return $this;
159
    }
160
161
    /**
162
     * @return Collection<int, CBlogAttachment>
163
     */
164
    public function getAttachments(): Collection
165
    {
166
        return $this->attachments;
167
    }
168
    public function addAttachment(CBlogAttachment $attachment): self
169
    {
170
        if (!$this->attachments->contains($attachment)) {
171
            $this->attachments->add($attachment);
172
            $attachment->setBlog($this);
173
        }
174
175
        return $this;
176
    }
177
    public function removeAttachment(CBlogAttachment $attachment): self
178
    {
179
        if ($this->attachments->removeElement($attachment)) {
180
            if ($attachment->getBlog() === $this) {
181
                $attachment->setBlog(null);
182
            }
183
        }
184
185
        return $this;
186
    }
187
188
    #[Groups(['blog:read'])]
189
    public function getCreatedAt(): string
190
    {
191
        return $this->getDateCreation()->format('Y-m-d');
192
    }
193
194
    #[Groups(['blog:read'])]
195
    public function getOwner(): array
196
    {
197
        if (method_exists($this, 'getCreator') && null !== $this->getCreator()) {
198
            $u = $this->getCreator();
199
            $name = method_exists($u, 'getFullName') ? $u->getFullName()
200
                : (method_exists($u, 'getUsername') ? $u->getUsername() : 'Owner');
201
202
            return [
203
                'id' => method_exists($u, 'getId') ? $u->getId() : null,
204
                'name' => $name,
205
            ];
206
        }
207
208
        return ['id' => null, 'name' => 'Owner'];
209
    }
210
211
    #[Groups(['blog:read', 'blog:write'])]
212
    public function getVisible(): bool
213
    {
214
        $link = $this->getFirstResourceLink();
215
        if (!$link instanceof ResourceLink) {
216
            return true;
217
        }
218
219
        return ResourceLink::VISIBILITY_PUBLISHED === $link->getVisibility();
220
    }
221
222
    public function setVisible(bool $visible): self
223
    {
224
        $link = $this->getFirstResourceLink();
225
        if ($link instanceof ResourceLink) {
226
            $link->setVisibility(
227
                $visible ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_DRAFT
228
            );
229
        }
230
231
        return $this;
232
    }
233
234
    #[Groups(['blog:read'])]
235
    public function getVisibilityName(): string
236
    {
237
        $link = $this->getFirstResourceLink();
238
        if ($link instanceof ResourceLink) {
239
            return (string) $link->getVisibilityName();
240
        }
241
242
        return 'published';
243
    }
244
245
    // === ResourceInterface ===
246
    public function getResourceIdentifier(): int
247
    {
248
        return (int) ($this->getIid() ?? 0);
249
    }
250
    public function getResourceName(): string
251
    {
252
        return $this->getTitle();
253
    }
254
    public function setResourceName(string $name): self
255
    {
256
        return $this->setTitle($name);
257
    }
258
259
    public function __toString(): string
260
    {
261
        return $this->getTitle();
262
    }
263
}
264