Skip to content

Commit 9a00115

Browse files
committed
Add multi-delete for team categories.
Also handle plural correctly of entities ending with "y". Part of #229.
1 parent e81158c commit 9a00115

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

webapp/src/Controller/Jury/TeamCategoryController.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\HttpFoundation\RedirectResponse;
2121
use Symfony\Component\HttpFoundation\Request;
2222
use Symfony\Component\HttpFoundation\Response;
23+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2324
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2425
use Symfony\Component\HttpKernel\KernelInterface;
2526
use Symfony\Component\PropertyAccess\PropertyAccess;
@@ -65,13 +66,30 @@ public function indexAction(): Response
6566
'allow_self_registration' => ['title' => 'self-registration', 'sort' => true],
6667
];
6768

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+
}
75+
6876
$propertyAccessor = PropertyAccess::createPropertyAccessor();
6977
$team_categories_table = [];
7078
foreach ($teamCategories as $teamCategoryData) {
7179
/** @var TeamCategory $teamCategory */
7280
$teamCategory = $teamCategoryData[0];
7381
$categorydata = [];
7482
$categoryactions = [];
83+
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+
}
92+
7593
// Get whatever fields we can from the category object itself.
7694
foreach ($table_fields as $k => $v) {
7795
if ($propertyAccessor->isReadable($teamCategory, $k)) {
@@ -230,6 +248,20 @@ public function addAction(Request $request): Response
230248
]);
231249
}
232250

251+
#[IsGranted('ROLE_ADMIN')]
252+
#[Route(path: '/delete-multiple', name: 'jury_team_category_delete_multiple', methods: ['GET', 'POST'])]
253+
public function deleteMultipleAction(Request $request): Response
254+
{
255+
$ids = $request->query->all('ids');
256+
if (empty($ids)) {
257+
throw new BadRequestHttpException('No IDs specified for deletion');
258+
}
259+
260+
$categories = $this->em->getRepository(TeamCategory::class)->findBy(['categoryid' => $ids]);
261+
262+
return $this->deleteEntities($request, $categories, $this->generateUrl('jury_team_categories'));
263+
}
264+
233265
#[Route(path: '/{categoryId<\d+>}/request-remaining', name: 'jury_team_category_request_remaining')]
234266
public function requestRemainingRunsWholeTeamCategoryAction(string $categoryId): RedirectResponse
235267
{

webapp/templates/jury/delete_modal.html.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
{% block title %}
44
{% if count > 1 %}
5-
Delete {{ count }} {{ type }}s
5+
Delete {{ count }} {% if type|slice(-1) == 'y' %}{{ type|slice(0, -1) }}ies{% else %}{{ type }}s{% endif %}
66
{% else %}
77
Delete {{ type }} {{ primaryKey }} - "{{ description }}"
88
{% endif %}
@@ -16,7 +16,7 @@
1616
</div>
1717
{% else %}
1818
{% if count > 1 %}
19-
<p>You're about to delete the following {{ count }} {{ type }}s:</p>
19+
<p>You're about to delete the following {{ count }} {% if type|slice(-1) == 'y' %}{{ type|slice(0, -1) }}ies{% else %}{{ type }}s{% endif %}:</p>
2020
<ul>
2121
{% for desc in description|split(',') %}
2222
<li>{{ desc|trim }}</li>
@@ -46,4 +46,4 @@
4646
{% if isError %}OK{% else %}Cancel{% endif %}
4747
{% endblock %}
4848

49-
{% block buttonText %}Delete{% endblock %}
49+
{% block buttonText %}Delete{% endblock %}

webapp/templates/jury/team_categories.html.twig

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,24 @@
1515
{{ macros.table(team_categories, table_fields) }}
1616

1717
{% if is_granted('ROLE_ADMIN') %}
18-
<p>
18+
<p class="mt-4">
19+
{% include 'jury/partials/_delete_button.html.twig' with {'entities': team_categories} %}
1920
{{ button(path('jury_team_category_add'), 'Add new category', 'primary', 'plus') }}
2021
</p>
2122
{% endif %}
2223

2324
{% endblock %}
25+
26+
{% block extrafooter %}
27+
{{ parent() }}
28+
<script src="{{ asset('js/multi-delete.js') }}"></script>
29+
<script>
30+
$(function() {
31+
initializeMultiDelete({
32+
buttonSelector: '#delete-selected-button',
33+
checkboxClass: 'category-checkbox',
34+
deleteUrl: '{{ path('jury_team_category_delete_multiple') }}'
35+
});
36+
});
37+
</script>
38+
{% endblock %}

0 commit comments

Comments
 (0)