|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/* For licensing terms, see /license.txt */ |
|
5
|
|
|
|
|
6
|
|
|
namespace Chamilo\CoreBundle\Controller\Api; |
|
7
|
|
|
|
|
8
|
|
|
use Chamilo\CoreBundle\Entity\Course; |
|
9
|
|
|
use Chamilo\CoreBundle\Entity\Session; |
|
10
|
|
|
use Chamilo\CoreBundle\Entity\User; |
|
11
|
|
|
use Chamilo\CourseBundle\Entity\CBlog; |
|
12
|
|
|
use Chamilo\CourseBundle\Repository\CBlogRepository; |
|
13
|
|
|
use Chamilo\CourseBundle\Repository\CShortcutRepository; |
|
14
|
|
|
use Doctrine\ORM\EntityManagerInterface as EntityManager; |
|
15
|
|
|
use Symfony\Bundle\SecurityBundle\Security; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController; |
|
18
|
|
|
|
|
19
|
|
|
#[AsController] |
|
20
|
|
|
class CreateCBlogAction extends BaseResourceFileAction |
|
21
|
|
|
{ |
|
22
|
|
|
public function __invoke( |
|
23
|
|
|
Request $request, |
|
24
|
|
|
CBlogRepository $repo, |
|
25
|
|
|
EntityManager $em, |
|
26
|
|
|
CShortcutRepository $shortcutRepository, |
|
27
|
|
|
Security $security |
|
28
|
|
|
): CBlog { |
|
29
|
|
|
$data = json_decode($request->getContent(), true) ?: []; |
|
30
|
|
|
|
|
31
|
|
|
$title = (string) ($data['title'] ?? ''); |
|
32
|
|
|
$subtitle = $data['blogSubtitle'] ?? null; |
|
33
|
|
|
$parentResourceNodeId = $data['parentResourceNodeId'] ?? null; |
|
34
|
|
|
$resourceLinkListRaw = $data['resourceLinkList'] ?? []; |
|
35
|
|
|
$showOnHomepage = isset($data['showOnHomepage']) ? (bool) $data['showOnHomepage'] : false; |
|
36
|
|
|
|
|
37
|
|
|
if (\is_string($resourceLinkListRaw)) { |
|
38
|
|
|
$decoded = json_decode($resourceLinkListRaw, true); |
|
39
|
|
|
$resourceLinkList = \is_array($decoded) ? $decoded : []; |
|
40
|
|
|
} else { |
|
41
|
|
|
$resourceLinkList = \is_array($resourceLinkListRaw) ? $resourceLinkListRaw : []; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$blog = (new CBlog()) |
|
45
|
|
|
->setTitle($title) |
|
46
|
|
|
->setBlogSubtitle($subtitle); |
|
47
|
|
|
|
|
48
|
|
|
if (!empty($parentResourceNodeId)) { |
|
49
|
|
|
$blog->setParentResourceNode((int) $parentResourceNodeId); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!empty($resourceLinkList)) { |
|
53
|
|
|
$blog->setResourceLinkArray($resourceLinkList); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$em->persist($blog); |
|
57
|
|
|
$em->flush(); |
|
58
|
|
|
|
|
59
|
|
|
// Optional: create shortcut on homepage (same behavior as links) |
|
60
|
|
|
$this->handleShortcutCreation($resourceLinkList, $em, $security, $blog, $shortcutRepository, $showOnHomepage); |
|
61
|
|
|
|
|
62
|
|
|
return $blog; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function handleShortcutCreation( |
|
66
|
|
|
array $resourceLinkList, |
|
67
|
|
|
EntityManager $em, |
|
68
|
|
|
Security $security, |
|
69
|
|
|
CBlog $blog, |
|
70
|
|
|
CShortcutRepository $shortcutRepository, |
|
71
|
|
|
bool $onHomepage |
|
72
|
|
|
): void { |
|
73
|
|
|
if (!$onHomepage || empty($resourceLinkList)) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$first = reset($resourceLinkList); |
|
78
|
|
|
$sid = (int) ($first['sid'] ?? 0); |
|
79
|
|
|
$cid = (int) ($first['cid'] ?? 0); |
|
80
|
|
|
|
|
81
|
|
|
$course = $cid ? $em->getRepository(Course::class)->find($cid) : null; |
|
82
|
|
|
$session = $sid ? $em->getRepository(Session::class)->find($sid) : null; |
|
83
|
|
|
|
|
84
|
|
|
/** @var User $currentUser */ |
|
85
|
|
|
$currentUser = $security->getUser(); |
|
86
|
|
|
if ($currentUser) { |
|
|
|
|
|
|
87
|
|
|
$shortcutRepository->addShortCut($blog, $currentUser, $course, $session); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|