Skip to content

Commit

Permalink
Separate radio table remove and radio station bulk remove screens
Browse files Browse the repository at this point in the history
Issue #116
  • Loading branch information
TomaszGasior committed Apr 20, 2022
1 parent 8d29e8e commit b34be0b
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 105 deletions.
13 changes: 13 additions & 0 deletions assets/css/radio-station-bulk-remove.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* radio station list */

.radio-stations-remove {
max-height: 430px;
overflow-y: auto;
box-sizing: border-box;
padding: 0.05em 1em;
background: #F9FCF9;
border: 1px solid #D6EAD0;
}
.radio-stations-remove fieldset {
margin: 0;
}
15 changes: 0 additions & 15 deletions assets/css/radio-table-remove.css
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/* radio station list */

.radio-stations-remove {
max-height: 430px;
overflow-y: auto;
box-sizing: border-box;
padding: 0.05em 1em;
background: #F9FCF9;
border: 1px solid #D6EAD0;
}
.radio-stations-remove fieldset {
margin: 0;
}


/* radio table warning */

.radio-table-remove-confirm {
Expand Down
35 changes: 35 additions & 0 deletions src/Controller/RadioStationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Entity\RadioStation;
use App\Entity\RadioTable;
use App\Form\RadioStationEditType;
use App\Form\RadioStationBulkRemoveType;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
Expand Down Expand Up @@ -96,4 +97,38 @@ public function copy(RadioStation $radioStation): Response
'template' => $template,
]);
}

/**
* @Route({"pl": "/wykaz/{id}/usun-stacje", "en": "/list/{id}/delete-stations"}, name="radio_station.bulk_remove")
* @IsGranted("IS_AUTHENTICATED_REMEMBERED")
* @IsGranted("RADIO_TABLE_MODIFY", subject="radioTable", statusCode=404)
*/
public function bulkRemove(RadioTable $radioTable, Request $request, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(RadioStationBulkRemoveType::class, null, ['radio_table' => $radioTable]);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$chosenToRemove = $form->getData()['chosenToRemove'];

if (count($chosenToRemove) > 0) {
foreach ($chosenToRemove as $radioStation) {
$entityManager->remove($radioStation);
}
$entityManager->flush();

$this->addFlash('notice', 'radio_station.bulk_remove.notification.bulk_removed');

// Form needs to be reloaded to not display removed radio stations.
return $this->redirectToRoute('radio_station.bulk_remove', [
'id' => $radioTable->getId(),
]);
}
}

return $this->render('radio_station/bulk_remove.html.twig', [
'form' => $form->createView(),
'radio_table' => $radioTable,
]);
}
}
45 changes: 6 additions & 39 deletions src/Controller/RadioTableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

namespace App\Controller;

use App\Entity\RadioStation;
use App\Entity\RadioTable;
use App\Export\RadioTableExporterProvider;
use App\Form\RadioStationRemoveType;
use App\Form\RadioTableCreateType;
use App\Form\RadioTableRemoveType;
use App\Form\RadioTableSettingsType;
use App\Repository\RadioStationRepository;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -125,27 +122,17 @@ public function export(RadioTable $radioTable): Response
}

/**
* This action handles both radio table removing and radio station removing.
*
* @Route({"pl": "/wykaz/{id}/usun", "en": "/list/{id}/delete"}, name="radio_table.remove")
* @ParamConverter("radioStationToRemove", class="stdClass")
* @IsGranted("IS_AUTHENTICATED_REMEMBERED")
* @IsGranted("RADIO_TABLE_MODIFY", subject="radioTable", statusCode=404)
*/
public function remove(RadioTable $radioTable, Request $request, EntityManagerInterface $entityManager,
RadioStation $radioStationToRemove = null): Response
public function remove(RadioTable $radioTable, Request $request, EntityManagerInterface $entityManager): Response
{
$form_RadioTable = $this->createForm(RadioTableRemoveType::class);
$form_RadioTable->handleRequest($request);

$form_RadioStation = $this->createForm(RadioStationRemoveType::class,
$radioStationToRemove ? ['chosenToRemove' => [$radioStationToRemove]] : null,
['radio_table' => $radioTable]
);
$form_RadioStation->handleRequest($request);
$form = $this->createForm(RadioTableRemoveType::class);
$form->handleRequest($request);

if ($form_RadioTable->isSubmitted() && $form_RadioTable->isValid()) {
$confirmed = (true === $form_RadioTable->getData()['confirm']);
if ($form->isSubmitted() && $form->isValid()) {
$confirmed = (true === $form->getData()['confirm']);

if ($confirmed) {
$entityManager->remove($radioTable);
Expand All @@ -158,29 +145,9 @@ public function remove(RadioTable $radioTable, Request $request, EntityManagerIn
$this->addFlash('error', 'radio_table.remove.notification.not_yet');
}
}
elseif ($form_RadioStation->isSubmitted() && $form_RadioStation->isValid()) {
$chosenToRemove = $form_RadioStation->getData()['chosenToRemove'];

if (count($chosenToRemove) > 0) {
foreach ($chosenToRemove as $radioStation) {
$entityManager->remove($radioStation);
}
$entityManager->flush();

$this->addFlash('notice', 'radio_station.remove.notification.bulk_removed');

// Redirect to after successful radio stations removing.
// * Form needs to be reloaded to not display removed radio stations.
// * URL needs to be changed to avoid 404 error if page was forwarded from RadioStationController.
return $this->redirectToRoute('radio_table.remove', [
'id' => $radioTable->getId(),
]);
}
}

return $this->render('radio_table/remove.html.twig', [
'form_radio_table' => $form_RadioTable->createView(),
'form_radio_station' => $form_RadioStation->createView(),
'form' => $form->createView(),
'radio_table' => $radioTable,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RadioStationRemoveType extends AbstractType
class RadioStationBulkRemoveType extends AbstractType
{
private $radioStationRepository;

Expand Down Expand Up @@ -53,7 +53,7 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setRequired(['radio_table']);
$resolver->setAllowedTypes('radio_table', RadioTable::class);
$resolver->setDefaults([
'label_format' => 'radio_station.remove.form.%name%',
'label_format' => 'radio_station.bulk_remove.form.%name%',
]);
}
}
62 changes: 62 additions & 0 deletions templates/radio_station/bulk_remove.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{% extends 'layout.html.twig' %}

{% block page_title %}{{ 'radio_station.bulk_remove.title'|trans }}{% endblock %}

{% block head_closing %}
{{ encore_entry_link_tags('radio-station-bulk-remove') }}
{% endblock %}

{% block context_menu %}
<ul>
<li>
<a href="{{ path('radio_table.show', {id: radio_table.id}) }}">
{{ 'radio_table.show.title'|trans }}
</a>
</li>
<li>
<a href="{{ path('radio_table.settings', {id: radio_table.id}) }}">
{{ 'radio_table.settings.title'|trans }}
</a>
</li>
</ul>
{% endblock %}

{% form_theme form _self %}
{% block _radio_station_bulk_remove_chosenToRemove_entry_label %}
{% set label = '%s (%s %s)'|format(label, frequency|format_number({min_fraction_digit: 2, grouping_used: false}), get_frequency_label(frequency_unit)) %}
{{ block('form_label') }}
{% endblock %}

{% block page_content %}
<header>
<h1 class="separated-title">
<span>{{ 'radio_station.bulk_remove.title'|trans }}</span>
<span class="additional">
<span class="sr-only">{{ 'radio_station.edit.text.for_radio_table_a11y_subheading'|trans }}</span>
{{ radio_table.name }}
</span>
</h1>
</header>

<div class="tabbed-ui">
<div>
<h2>{{ 'radio_station.bulk_remove.heading.remove_radio_station'|trans }}</h2>

{% if radio_table.radioStationsCount > 0 %}
{{ form_start(form) }}

<div class="radio-stations-remove">
{{ form_row(form.chosenToRemove) }}
</div>

<button>{{ 'radio_station.bulk_remove.title'|trans }}</button>

{{ form_end(form) }}
{% else %}
<p class="information">
{{ 'radio_station.bulk_remove.information.empty_cannot_remove_radio_station'|trans }}
</p>
{% endif %}
</div>
</div>
{% endblock %}
33 changes: 4 additions & 29 deletions templates/radio_table/remove.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@
</ul>
{% endblock %}

{% form_theme form_radio_station _self %}
{% block _radio_station_remove_chosenToRemove_entry_label %}
{% set label = '%s (%s %s)'|format(label, frequency|format_number({min_fraction_digit: 2, grouping_used: false}), get_frequency_label(frequency_unit)) %}
{{ block('form_label') }}
{% endblock %}

{% block page_content %}
<header>
<h1 class="separated-title">
Expand All @@ -42,45 +36,26 @@
</header>

<div class="tabbed-ui">
<div>
<h2>{{ 'radio_table.remove.heading.remove_radio_station'|trans }}</h2>

{% if radio_table.radioStationsCount > 0 %}
{{ form_start(form_radio_station) }}

<div class="radio-stations-remove">
{{ form_row(form_radio_station.chosenToRemove) }}
</div>

<button>{{ 'radio_table.remove.action.selected_radio_stations'|trans }}</button>

{{ form_end(form_radio_station) }}
{% else %}
<p class="information">
{{ 'radio_table.remove.information.empty_cannot_remove_radio_station'|trans }}
</p>
{% endif %}
</div>
<div>
<h2>{{ 'radio_table.remove.heading.remove_radio_table'|trans }}</h2>

<p class="information">
{{ 'radio_table.remove.information.cannot_be_undone'|trans }}
</p>

{{ form_start(form_radio_table) }}
{{ form_start(form) }}

{{ form_row(form_radio_table.confirm, {
{{ form_row(form.confirm, {
label_translation_parameters: { '%name%': radio_table.name },
attr: {
'class': 'radio-table-remove-confirm',
'data-confirm-message': 'radio_table.remove.text.confirm_alert'|trans,
},
}) }}

<button>{{ 'radio_table.remove.action.radio_table'|trans }}</button>
<button>{{ 'radio_table.remove.title'|trans }}</button>

{{ form_end(form_radio_table) }}
{{ form_end(form) }}
</div>
</div>
{% endblock %}
5 changes: 5 additions & 0 deletions templates/radio_table/settings.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
{{ 'radio_station.add.title'|trans }}
</a>
</li>
<li>
<a href="{{ path('radio_station.bulk_remove', {id: radio_table.id}) }}">
{{ 'radio_station.bulk_remove.title'|trans }}
</a>
</li>
<li>
<a href="{{ path('radio_table.remove', {id: radio_table.id}) }}">
{{ 'radio_table.remove.title'|trans }}
Expand Down
9 changes: 5 additions & 4 deletions tests/Controller/RadioStationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ public function testCopyRadioStation(): void
$this->assertStringContainsString('COPIED_RADIO_STATION_NAME', $content);
}

public function testRemoveRadioStation(): void
public function testBulkRemoveRadioStation(): void
{
$crawler = $this->client->request('GET', '/wykaz/1/usun-stacje/1');
$form = $crawler->filter('form[name="radio_station_remove"]')->form();
$this->client->submit($form); // Radiostation is selected automatically.
$crawler = $this->client->request('GET', '/wykaz/1/usun-stacje');
$form = $crawler->filter('form[name="radio_station_bulk_remove"]')->form();
$form['radio_station_bulk_remove[chosenToRemove][1]']->tick(); // Checkbox is chosen by order, not by input value.
$this->client->submit($form);

$this->client->request('GET', '/wykaz/1');
$content = $this->client->getResponse()->getContent();
Expand Down
4 changes: 2 additions & 2 deletions tests/Form/FormsCompilationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Entity\RadioTable;
use App\Entity\User;
use App\Form\RadioStationEditType;
use App\Form\RadioStationRemoveType;
use App\Form\RadioStationBulkRemoveType;
use App\Form\RadioTableCreateType;
use App\Form\RadioTableRemoveType;
use App\Form\RadioTableSearchType;
Expand Down Expand Up @@ -40,7 +40,7 @@ public function formTypeAndEntityProvider(): iterable
self::ensureKernelShutdown();

yield 'RadioStationEditType' => [RadioStationEditType::class, $radioStation];
yield 'RadioStationRemoveType' => [RadioStationRemoveType::class, null, ['radio_table' => $radioTable]];
yield 'RadioStationBulkRemoveType' => [RadioStationBulkRemoveType::class, null, ['radio_table' => $radioTable]];
yield 'RadioTableCreateType' => [RadioTableCreateType::class, $radioTable];
yield 'RadioTableRemoveType' => [RadioTableRemoveType::class];
yield 'RadioTableSearchType' => [RadioTableSearchType::class];
Expand Down
Loading

0 comments on commit b34be0b

Please sign in to comment.