|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Controller\Api; |
|
8
|
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Helpers\CidReqHelper; |
|
10
|
|
|
use Chamilo\CoreBundle\Entity\Course; |
|
11
|
|
|
use Chamilo\CoreBundle\Entity\Session; |
|
12
|
|
|
use Chamilo\CoreBundle\Entity\User; |
|
13
|
|
|
use Chamilo\CoreBundle\Entity\ResourceLink; |
|
14
|
|
|
use Chamilo\CourseBundle\Entity\CBlog; |
|
15
|
|
|
use Chamilo\CourseBundle\Repository\CBlogRepository; |
|
16
|
|
|
use Chamilo\CourseBundle\Repository\CShortcutRepository; |
|
17
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
19
|
|
|
use Symfony\Bundle\SecurityBundle\Security; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController; |
|
21
|
|
|
|
|
22
|
|
|
#[AsController] |
|
23
|
|
|
class UpdateVisibilityBlog extends AbstractController |
|
24
|
|
|
{ |
|
25
|
|
|
public function __construct( |
|
26
|
|
|
private readonly CidReqHelper $cidReqHelper, |
|
27
|
|
|
private readonly Security $security, |
|
28
|
|
|
private readonly EntityManagerInterface $em, |
|
29
|
|
|
private readonly CShortcutRepository $shortcutRepository, |
|
30
|
|
|
) {} |
|
31
|
|
|
|
|
32
|
|
|
public function __invoke(CBlog $blog, CBlogRepository $repo): CBlog |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var Course|null $course */ |
|
35
|
|
|
$course = $this->cidReqHelper->getDoctrineCourseEntity(); |
|
36
|
|
|
/** @var Session|null $session */ |
|
37
|
|
|
$session = $this->cidReqHelper->getDoctrineSessionEntity(); |
|
38
|
|
|
/** @var User|null $currentUser */ |
|
39
|
|
|
$currentUser = $this->security->getUser(); |
|
40
|
|
|
|
|
41
|
|
|
if (!$course || !$currentUser) { |
|
42
|
|
|
return $blog; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (!$blog->hasResourceNode()) { |
|
46
|
|
|
$repo->addResourceNode($blog, $currentUser, $course); |
|
47
|
|
|
} |
|
48
|
|
|
if (null === $blog->getResourceNode()->getParent()) { |
|
49
|
|
|
$blog->getResourceNode()->setParent($course->getResourceNode()); |
|
50
|
|
|
$this->em->persist($blog->getResourceNode()); |
|
51
|
|
|
} |
|
52
|
|
|
if (!$blog->getFirstResourceLinkFromCourseSession($course, $session)) { |
|
53
|
|
|
$blog->addCourseLink($course, $session); |
|
54
|
|
|
$this->em->persist($blog); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$link = $blog->getFirstResourceLinkFromCourseSession($course, $session); |
|
58
|
|
|
$wasVisible = $link && ResourceLink::VISIBILITY_PUBLISHED === $link->getVisibility(); |
|
59
|
|
|
|
|
60
|
|
|
if ($wasVisible) { |
|
61
|
|
|
$this->shortcutRepository->hardDeleteShortcutsForCourse($blog, $course); |
|
62
|
|
|
$this->em->flush(); |
|
63
|
|
|
|
|
64
|
|
|
$repo->setVisibilityDraft($blog, $course, $session); |
|
65
|
|
|
} else { |
|
66
|
|
|
$repo->setVisibilityPublished($blog, $course, $session); |
|
67
|
|
|
$this->shortcutRepository->addShortCut($blog, $currentUser, $course, $session); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->em->flush(); |
|
71
|
|
|
|
|
72
|
|
|
return $blog; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|