-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathAddressBookController.php
More file actions
135 lines (108 loc) 路 5.47 KB
/
Copy pathAddressBookController.php
File metadata and controls
135 lines (108 loc) 路 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace App\Controller\Admin;
use App\Entity\AddressBook;
use App\Entity\Principal;
use App\Entity\User;
use App\Form\AddressBookType;
use App\Services\BirthdayService;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
#[Route('/addressbooks', name: 'addressbook_')]
class AddressBookController extends AbstractController
{
#[Route('/{userId}', name: 'index')]
public function addressBooks(ManagerRegistry $doctrine, #[MapEntity(id: 'userId')] User $user, int $userId): Response
{
$principalUri = $user->getPrincipalUri();
$principal = $doctrine->getRepository(Principal::class)->findOneByUri($principalUri);
$addressbooks = $doctrine->getRepository(AddressBook::class)->findByPrincipalUri($principalUri);
return $this->render('addressbooks/index.html.twig', [
'addressbooks' => $addressbooks,
'principal' => $principal,
'userId' => $userId,
]);
}
#[Route('/{userId}/new', name: 'create')]
#[Route('/{userId}/edit/{id}', name: 'edit', requirements: ['id' => "\d+"])]
public function addressbookCreate(ManagerRegistry $doctrine, Request $request, #[MapEntity(id: 'userId')] User $user, int $userId, ?int $id, TranslatorInterface $trans, BirthdayService $birthdayService): Response
{
$username = $user->getUsername();
$principalUri = $user->getPrincipalUri();
$principal = $doctrine->getRepository(Principal::class)->findOneByUri($principalUri);
if (!$principal) {
throw $this->createNotFoundException('User not found');
}
if ($id) {
$addressbook = $doctrine->getRepository(AddressBook::class)->findOneBy(['id' => $id, 'principalUri' => $principalUri]);
if (!$addressbook) {
throw $this->createNotFoundException('Address book not found');
}
} else {
$addressbook = new AddressBook();
// The owner is given by the URL, never by the submitted form
$addressbook->setPrincipalUri($principalUri);
}
$isBirthdayCalendarEnabled = $this->getParameter('caldav_enabled') && $this->getParameter('carddav_enabled');
$form = $this->createForm(AddressBookType::class, $addressbook, ['new' => !$id, 'birthday_calendar_enabled' => $isBirthdayCalendarEnabled]);
if ($isBirthdayCalendarEnabled) {
$form->get('includedInBirthdayCalendar')->setData($addressbook->isIncludedInBirthdayCalendar());
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $doctrine->getManager();
$entityManager->persist($addressbook);
$entityManager->flush();
$this->addFlash('success', $trans->trans('addressbooks.saved'));
if ($isBirthdayCalendarEnabled && true === $form->get('includedInBirthdayCalendar')->getData()) {
$addressbook->setIncludedInBirthdayCalendar(true);
} else {
$addressbook->setIncludedInBirthdayCalendar(false);
}
if ($isBirthdayCalendarEnabled) {
// Let's sync the user birthday calendar if needed
$birthdayService->syncUser($username);
}
return $this->redirectToRoute('addressbook_index', ['userId' => $userId]);
}
return $this->render('addressbooks/edit.html.twig', [
'form' => $form->createView(),
'principal' => $principal,
'userId' => $userId,
'addressbook' => $addressbook,
]);
}
#[Route('/{userId}/delete/{id}', name: 'delete', requirements: ['id' => "\d+"], methods: ['POST'])]
public function addressbookDelete(ManagerRegistry $doctrine, Request $request, #[MapEntity(id: 'userId')] User $user, int $userId, string $id, TranslatorInterface $trans, BirthdayService $birthdayService): Response
{
if (!$this->isCsrfTokenValid('admin_action', $request->getPayload()->getString('_token'))) {
throw $this->createAccessDeniedException('Invalid CSRF token.');
}
$username = $user->getUsername();
$principalUri = $user->getPrincipalUri();
$addressbook = $doctrine->getRepository(AddressBook::class)->findOneBy(['id' => $id, 'principalUri' => $principalUri]);
if (!$addressbook) {
throw $this->createNotFoundException('Address Book not found');
}
$entityManager = $doctrine->getManager();
foreach ($addressbook->getCards() ?? [] as $card) {
$entityManager->remove($card);
}
foreach ($addressbook->getChanges() ?? [] as $change) {
$entityManager->remove($change);
}
$entityManager->remove($addressbook);
$entityManager->flush();
$this->addFlash('success', $trans->trans('addressbooks.deleted'));
$isBirthdayCalendarEnabled = $this->getParameter('caldav_enabled') && $this->getParameter('carddav_enabled');
if ($isBirthdayCalendarEnabled) {
// Let's sync the user birthday calendar if needed
$birthdayService->syncUser($username);
}
return $this->redirectToRoute('addressbook_index', ['userId' => $userId]);
}
}