Skip to content

Commit 1fed90f

Browse files
committed
Deduplicate multi entity deletion code.
1 parent 7cbb96d commit 1fed90f

File tree

6 files changed

+52
-111
lines changed

6 files changed

+52
-111
lines changed

webapp/src/Controller/BaseController.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ protected function deleteEntities(
455455
}
456456

457457
/**
458-
* @param array<string, array<array{'target': string, 'targetColumn': string, 'type': string}>> $relations
459-
* @return string[]
458+
* @template T of object
459+
* @param class-string<T> $entityClass
460460
*/
461461
protected function deleteMultiple(
462462
Request $request,
@@ -471,7 +471,9 @@ protected function deleteMultiple(
471471
throw new BadRequestHttpException('No IDs specified for deletion');
472472
}
473473

474-
$entities = $this->em->getRepository($entityClass)->findBy([$idProperty => $ids]);
474+
/** @var \Doctrine\ORM\EntityRepository<T> $repository */
475+
$repository = $this->em->getRepository($entityClass);
476+
$entities = $repository->findBy([$idProperty => $ids]);
475477

476478
if ($filter) {
477479
$entities = array_filter($entities, $filter);
@@ -485,7 +487,10 @@ protected function deleteMultiple(
485487
return $this->deleteEntities($request, $entities, $this->generateUrl($redirectRoute));
486488
}
487489

488-
490+
/**
491+
* @param array<string, array<array{'target': string, 'targetColumn': string, 'type': string}>> $relations
492+
* @return string[]
493+
*/
489494
protected function getDependentEntities(string $entityClass, array $relations): array
490495
{
491496
$result = [];
@@ -514,6 +519,39 @@ protected function getDependentEntities(string $entityClass, array $relations):
514519
return $result;
515520
}
516521

522+
/**
523+
* @param array<string, array<string, mixed>> $table_fields
524+
*/
525+
protected function addSelectAllCheckbox(array &$table_fields, string $title): void
526+
{
527+
if ($this->isGranted('ROLE_ADMIN')) {
528+
$table_fields = array_merge(
529+
['checkbox' => ['title' => sprintf('<input type="checkbox" class="select-all" title="Select all %s">', $title), 'sort' => false, 'search' => false, 'raw' => true]],
530+
$table_fields
531+
);
532+
}
533+
}
534+
535+
/**
536+
* @param array<string, mixed> $data
537+
*/
538+
protected function addEntityCheckbox(array &$data, object $entity, mixed $identifierValue, string $checkboxClass, ?callable $condition = null): void
539+
{
540+
if ($this->isGranted('ROLE_ADMIN')) {
541+
if ($condition !== null && !$condition($entity)) {
542+
$data['checkbox'] = ['value' => ''];
543+
return;
544+
}
545+
$data['checkbox'] = [
546+
'value' => sprintf(
547+
'<input type="checkbox" name="ids[]" value="%s" class="%s">',
548+
$identifierValue,
549+
$checkboxClass
550+
)
551+
];
552+
}
553+
}
554+
517555
/**
518556
* Get the contests that an event for the given entity should be triggered on
519557
*

webapp/src/Controller/Jury/ProblemController.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,7 @@ public function indexAction(): Response
8888
'type' => ['title' => 'type', 'sort' => true],
8989
];
9090

91-
if ($this->isGranted('ROLE_ADMIN')) {
92-
$table_fields = array_merge(
93-
['checkbox' => ['title' => '<input type="checkbox" class="select-all" title="Select all problems">', 'sort' => false, 'search' => false, 'raw' => true]],
94-
$table_fields
95-
);
96-
}
91+
$this->addSelectAllCheckbox($table_fields, 'problems');
9792

9893
$contestCountData = $this->em->createQueryBuilder()
9994
->from(ContestProblem::class, 'cp')
@@ -117,26 +112,7 @@ public function indexAction(): Response
117112
$problemdata = [];
118113
$problemactions = [];
119114

120-
if ($this->isGranted('ROLE_ADMIN')) {
121-
$problemIsLocked = false;
122-
foreach ($p->getContestProblems() as $contestProblem) {
123-
if ($contestProblem->getContest()->isLocked()) {
124-
$problemIsLocked = true;
125-
break;
126-
}
127-
}
128-
129-
if (!$problemIsLocked) {
130-
$problemdata['checkbox'] = [
131-
'value' => sprintf(
132-
'<input type="checkbox" name="ids[]" value="%s" class="problem-checkbox">',
133-
$p->getProbid()
134-
)
135-
];
136-
} else {
137-
$problemdata['checkbox'] = ['value' => ''];
138-
}
139-
}
115+
$this->addEntityCheckbox($problemdata, $p, $p->getProbid(), 'problem-checkbox', fn(Problem $problem) => !$problem->isLocked());
140116

141117
// Get whatever fields we can from the problem object itself.
142118
foreach ($table_fields as $k => $v) {

webapp/src/Controller/Jury/TeamAffiliationController.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,7 @@ public function indexAction(
6060
'name' => ['title' => 'name', 'sort' => true, 'default_sort' => true],
6161
];
6262

63-
if ($this->isGranted('ROLE_ADMIN')) {
64-
$table_fields = array_merge(
65-
['checkbox' => ['title' => '<input type="checkbox" class="select-all" title="Select all affiliations">', 'sort' => false, 'search' => false, 'raw' => true]],
66-
$table_fields
67-
);
68-
}
63+
$this->addSelectAllCheckbox($table_fields, 'affiliations');
6964

7065
if ($showFlags) {
7166
$table_fields['country'] = ['title' => 'country', 'sort' => true];
@@ -82,14 +77,7 @@ public function indexAction(
8277
$affiliationdata = [];
8378
$affiliationactions = [];
8479

85-
if ($this->isGranted('ROLE_ADMIN')) {
86-
$affiliationdata['checkbox'] = [
87-
'value' => sprintf(
88-
'<input type="checkbox" name="ids[]" value="%s" class="affiliation-checkbox">',
89-
$teamAffiliation->getAffilid()
90-
)
91-
];
92-
}
80+
$this->addEntityCheckbox($affiliationdata, $teamAffiliation, $teamAffiliation->getAffilid(), 'affiliation-checkbox');
9381

9482
// Get whatever fields we can from the affiliation object itself.
9583
foreach ($table_fields as $k => $v) {

webapp/src/Controller/Jury/TeamCategoryController.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,7 @@ public function indexAction(): Response
6666
'allow_self_registration' => ['title' => 'self-registration', 'sort' => true],
6767
];
6868

69-
if ($this->isGranted('ROLE_ADMIN')) {
70-
$table_fields = array_merge(
71-
['checkbox' => ['title' => '<input type="checkbox" class="select-all" title="Select all categories">', 'sort' => false, 'search' => false, 'raw' => true]],
72-
$table_fields
73-
);
74-
}
69+
$this->addSelectAllCheckbox($table_fields, 'categories');
7570

7671
$propertyAccessor = PropertyAccess::createPropertyAccessor();
7772
$team_categories_table = [];
@@ -81,14 +76,7 @@ public function indexAction(): Response
8176
$categorydata = [];
8277
$categoryactions = [];
8378

84-
if ($this->isGranted('ROLE_ADMIN')) {
85-
$categorydata['checkbox'] = [
86-
'value' => sprintf(
87-
'<input type="checkbox" name="ids[]" value="%s" class="category-checkbox">',
88-
$teamCategory->getCategoryid()
89-
)
90-
];
91-
}
79+
$this->addEntityCheckbox($categorydata, $teamCategory, $teamCategory->getCategoryid(), 'category-checkbox');
9280

9381
// Get whatever fields we can from the category object itself.
9482
foreach ($table_fields as $k => $v) {

webapp/src/Controller/Jury/TeamController.php

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,7 @@ public function indexAction(): Response
102102
'stats' => ['title' => 'stats', 'sort' => true,],
103103
];
104104

105-
if ($this->isGranted('ROLE_ADMIN')) {
106-
$table_fields = array_merge(
107-
['checkbox' => ['title' => '<input type="checkbox" class="select-all" title="Select all teams">', 'sort' => false, 'search' => false, 'raw' => true]],
108-
$table_fields
109-
);
110-
}
105+
$this->addSelectAllCheckbox($table_fields, 'teams');
111106

112107
$userDataPerTeam = $this->em->createQueryBuilder()
113108
->from(Team::class, 't', 't.teamid')
@@ -123,34 +118,7 @@ public function indexAction(): Response
123118
$teamdata = [];
124119
$teamactions = [];
125120

126-
if ($this->isGranted('ROLE_ADMIN')) {
127-
$isLocked = false;
128-
foreach ($t->getContests() as $contest) {
129-
if ($contest->isLocked()) {
130-
$isLocked = true;
131-
break;
132-
}
133-
}
134-
if (!$isLocked && $t->getCategory()) {
135-
foreach ($t->getCategory()->getContests() as $contest) {
136-
if ($contest->isLocked()) {
137-
$isLocked = true;
138-
break;
139-
}
140-
}
141-
}
142-
143-
if (!$isLocked) {
144-
$teamdata['checkbox'] = [
145-
'value' => sprintf(
146-
'<input type="checkbox" name="ids[]" value="%s" class="team-checkbox">',
147-
$t->getTeamid()
148-
)
149-
];
150-
} else {
151-
$teamdata['checkbox'] = ['value' => ''];
152-
}
153-
}
121+
$this->addEntityCheckbox($teamdata, $t, $t->getTeamid(), 'team-checkbox', fn(Team $team) => !$team->isLocked());
154122

155123
// Get whatever fields we can from the team object itself.
156124
foreach ($table_fields as $k => $v) {

webapp/src/Controller/Jury/UserController.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,7 @@ public function indexAction(): Response
7272
'team' => ['title' => 'team', 'sort' => true],
7373
];
7474

75-
if ($this->isGranted('ROLE_ADMIN')) {
76-
$table_fields = array_merge(
77-
['checkbox' => ['title' => '<input type="checkbox" class="select-all" title="Select all users">', 'sort' => false, 'search' => false, 'raw' => true]],
78-
$table_fields
79-
);
80-
}
75+
$this->addSelectAllCheckbox($table_fields, 'users');
8176

8277
if (in_array('ipaddress', $this->config->get('auth_methods'))) {
8378
$table_fields['ip_address'] = ['title' => 'autologin IP', 'sort' => true];
@@ -93,19 +88,7 @@ public function indexAction(): Response
9388
$userdata = [];
9489
$useractions = [];
9590

96-
if ($this->isGranted('ROLE_ADMIN')) {
97-
$canBeDeleted = $u->getUserid() !== $this->dj->getUser()->getUserid();
98-
if ($canBeDeleted) {
99-
$userdata['checkbox'] = [
100-
'value' => sprintf(
101-
'<input type="checkbox" name="ids[]" value="%s" class="user-checkbox">',
102-
$u->getUserid()
103-
)
104-
];
105-
} else {
106-
$userdata['checkbox'] = ['value' => ''];
107-
}
108-
}
91+
$this->addEntityCheckbox($userdata, $u, $u->getUserid(), 'user-checkbox', fn(User $user) => $user->getUserid() !== $this->dj->getUser()->getUserid());
10992

11093
// Get whatever fields we can from the user object itself.
11194
foreach ($table_fields as $k => $v) {

0 commit comments

Comments
 (0)