Skip to content

Commit 29b2d5a

Browse files
author
tchapi
committed
chore
chore chore chore chore chore
1 parent 64bdaa7 commit 29b2d5a

2 files changed

Lines changed: 37 additions & 12 deletions

File tree

src/Controller/Admin/CalendarController.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use App\Entity\CalendarInstance;
77
use App\Entity\CalendarSubscription;
88
use App\Entity\Principal;
9-
use App\Entity\SchedulingObject;
109
use App\Entity\User;
1110
use App\Form\CalendarInstanceType;
1211
use Doctrine\Persistence\ManagerRegistry;
@@ -240,18 +239,24 @@ public function calendarShareAdd(ManagerRegistry $doctrine, Request $request, in
240239
#[Route('/{userId}/delete/{id}', name: 'delete', requirements: ['id' => "\d+"])]
241240
public function calendarDelete(ManagerRegistry $doctrine, int $userId, string $id, TranslatorInterface $trans): Response
242241
{
242+
$user = $doctrine->getRepository(User::class)->findOneById($userId);
243+
if (!$user) {
244+
throw $this->createNotFoundException('User not found');
245+
}
246+
$principalUri = Principal::PREFIX.$user->getUsername();
247+
243248
$instance = $doctrine->getRepository(CalendarInstance::class)->findOneById($id);
244249
if (!$instance) {
245250
throw $this->createNotFoundException('Calendar not found');
246251
}
247252

248253
$entityManager = $doctrine->getManager();
249254

250-
$schedulingObjects = $doctrine->getRepository(SchedulingObject::class)->findByPrincipalUri($instance->getPrincipalUri());
251-
foreach ($schedulingObjects ?? [] as $object) {
255+
// Scheduling objects attached to the calendar objects of the calendar
256+
$schedulingObjectsOfCalendarObjects = $doctrine->getRepository(CalendarInstance::class)->findAllSchedulingObjectsForCalendar($instance->getId(), $principalUri);
257+
foreach ($schedulingObjectsOfCalendarObjects ?? [] as $object) {
252258
$entityManager->remove($object);
253259
}
254-
255260
foreach ($instance->getCalendar()->getObjects() ?? [] as $object) {
256261
$entityManager->remove($object);
257262
}

src/Repository/CalendarInstanceRepository.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace App\Repository;
44

5+
use App\Entity\Calendar;
56
use App\Entity\CalendarInstance;
7+
use App\Entity\CalendarObject;
68
use App\Entity\Principal;
9+
use App\Entity\SchedulingObject;
710
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
811
use Doctrine\Persistence\ManagerRegistry;
912

@@ -37,11 +40,11 @@ public function findSharedInstancesOfInstance(int $calendarId, bool $withCalenda
3740
return $query->addSelect('p.displayName', 'p.email')
3841
->getQuery()
3942
->getArrayResult();
40-
} else {
41-
// Returns CalendarInstances as objects
42-
return $query->getQuery()
43-
->getResult();
4443
}
44+
45+
// Returns CalendarInstances as objects
46+
return $query->getQuery()
47+
->getResult();
4548
}
4649

4750
/**
@@ -74,6 +77,23 @@ public function hasDifferentOwner(int $calendarId, string $principalUri): bool
7477
->getSingleScalarResult() > 0;
7578
}
7679

80+
81+
public function findAllSchedulingObjectsForCalendar(int $calendarInstanceId, string $principalUri): array
82+
{
83+
$objectRepository = $this->getEntityManager()->getRepository(SchedulingObject::class);
84+
return $objectRepository->createQueryBuilder('s')
85+
->leftJoin(CalendarObject::class, 'c', \Doctrine\ORM\Query\Expr\Join::WITH, 'c.uri = s.uri')
86+
->leftJoin(CalendarInstance::class, 'ci', \Doctrine\ORM\Query\Expr\Join::WITH, 'ci.calendar = c.calendar')
87+
->where('ci.id = :id')
88+
// uri is not unique across calendars — two different calendars can have objects with the same uri.
89+
// The join should also filter by principaluri as a consequence
90+
->andWhere('s.principalUri = :principalUri')
91+
->setParameter('id', $calendarInstanceId)
92+
->setParameter('principalUri', $principalUri)
93+
->getQuery()
94+
->getResult();
95+
}
96+
7797
/**
7898
* Get counts of calendar objects by component type for a calendar instance.
7999
*
@@ -83,7 +103,7 @@ public function hasDifferentOwner(int $calendarId, string $principalUri): bool
83103
*/
84104
public function getObjectCountsByComponentType(int $calendarId): array
85105
{
86-
$objectRepository = $this->getEntityManager()->getRepository(\App\Entity\CalendarObject::class);
106+
$objectRepository = $this->getEntityManager()->getRepository(CalendarObject::class);
87107

88108
// Instead of three separate queries, get all counts in a single query
89109
$results = $objectRepository->createQueryBuilder('o')
@@ -95,9 +115,9 @@ public function getObjectCountsByComponentType(int $calendarId): array
95115
->getResult();
96116

97117
$componentTypeMap = [
98-
\App\Entity\Calendar::COMPONENT_EVENTS => 'events',
99-
\App\Entity\Calendar::COMPONENT_NOTES => 'notes',
100-
\App\Entity\Calendar::COMPONENT_TODOS => 'tasks',
118+
Calendar::COMPONENT_EVENTS => 'events',
119+
Calendar::COMPONENT_NOTES => 'notes',
120+
Calendar::COMPONENT_TODOS => 'tasks',
101121
];
102122

103123
$counts = [

0 commit comments

Comments
 (0)