Skip to content

Commit

Permalink
Add age categories
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Jan 21, 2024
1 parent 89eddde commit ba7fabb
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 3 deletions.
33 changes: 33 additions & 0 deletions migrations/Version20240121162659.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240121162659 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE book ADD age_category INT DEFAULT NULL');
$this->addSql('ALTER TABLE user ADD birthday DATE DEFAULT NULL, ADD max_age_category INT DEFAULT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE book DROP age_category');
$this->addSql('ALTER TABLE `user` DROP birthday, DROP max_age_category');
}
}
15 changes: 15 additions & 0 deletions src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class Book
#[ORM\ManyToMany(targetEntity: Shelf::class, mappedBy: 'books', cascade: ['persist'])]
private Collection $shelves;

#[ORM\Column(nullable: true)]
private ?int $ageCategory = null;

public function __construct()
{
$this->bookInteractions = new ArrayCollection();
Expand Down Expand Up @@ -470,4 +473,16 @@ public function removeShelf(Shelf $shelf): static

return $this;
}

public function getAgeCategory(): ?int
{
return $this->ageCategory;
}

public function setAgeCategory(?int $ageCategory): static
{
$this->ageCategory = $ageCategory;

return $this;
}
}
37 changes: 37 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
Expand All @@ -13,6 +14,12 @@
#[ORM\Table(name: '`user`')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
public const AGE_CATEGORIES = [
'0-10 ans' => '1',
'11-15 ans' => '3',
'16-18 ans' => '4',
'18 ans et+' => '5',
];
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
Expand Down Expand Up @@ -63,6 +70,12 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(options: ['default' => true])]
private bool $displayAllBooks = true;

#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
private ?\DateTimeInterface $birthday = null;

#[ORM\Column(nullable: true)]
private ?int $maxAgeCategory = null;

public function __construct()
{
$this->bookInteractions = new ArrayCollection();
Expand Down Expand Up @@ -270,4 +283,28 @@ public function setDisplayAllBooks(bool $displayAllBooks): static

return $this;
}

public function getBirthday(): ?\DateTimeInterface
{
return $this->birthday;
}

public function setBirthday(?\DateTimeInterface $birthday): static
{
$this->birthday = $birthday;

return $this;
}

public function getMaxAgeCategory(): ?int
{
return $this->maxAgeCategory;
}

public function setMaxAgeCategory(?int $maxAgeCategory): static
{
$this->maxAgeCategory = $maxAgeCategory;

return $this;
}
}
17 changes: 17 additions & 0 deletions src/Form/BookFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Form;

use App\Entity\Book;
use App\Entity\User;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type;
Expand Down Expand Up @@ -232,6 +233,22 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
},
]);

$builder->add('age', Type\ChoiceType::class, [
'choices' => User::AGE_CATEGORIES + ['Not set' => 'null'],
'required' => false,
'mapped' => false,
'expanded' => true,
'multiple' => true,
'target_callback' => function (QueryBuilder $qb, array $readValue): void {
if (in_array('null', $readValue, true)) {
$qb->andWhere('book.ageCategory is null');
} elseif (count($readValue) > 0) {
$qb->andWhere($qb->expr()->in('book.ageCategory', ':ageCategory'));
$qb->setParameter('ageCategory', $readValue);
}
},
]);

$builder->add('favorite', Type\ChoiceType::class, [
'choices' => [
'Any' => '',
Expand Down
3 changes: 3 additions & 0 deletions src/Form/UserType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
Expand All @@ -23,6 +24,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'multiple' => true,
'expanded' => true,
])
->add('birthday', BirthdayType::class, ['widget' => 'single_text'])
->add('maxAgeCategory', ChoiceType::class, ['choices' => User::AGE_CATEGORIES])
->add('plainPassword', PasswordType::class, [
'mapped' => false,
'required' => false,
Expand Down
1 change: 1 addition & 0 deletions src/Service/FilteredBookUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class FilteredBookUrlGenerator
'extension' => '',
'verified' => '',
'orderBy' => '',
'age' => [],
'submit' => '',
];

Expand Down
2 changes: 1 addition & 1 deletion src/Twig/InlineEditBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class InlineEditBook extends AbstractController
use ValidatableComponentTrait;
use ComponentToolsTrait;

#[LiveProp(writable: ['title', 'serie', 'serieIndex', 'publisher', 'verified', 'summary', 'authors', 'tags'])]
#[LiveProp(writable: ['title', 'serie', 'serieIndex', 'publisher', 'verified', 'summary', 'authors', 'tags', 'ageCategory'])]
public Book $book;

#[LiveProp()]
Expand Down
3 changes: 3 additions & 0 deletions templates/book/_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
{% endif %}
{{ component('FieldGuesser',{'book':book}) }}
</div>
<div>
{{ component('InlineEditBook', {'book':book, 'field':'ageCategory'}) }}
</div>
</td>
<td>
{% include 'book/_interaction.html.twig' %}
Expand Down
4 changes: 4 additions & 0 deletions templates/book/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
<th>{{ 'publisher'|trans }}</th><td>{{ component('InlineEditBook', {'book':book, 'field':'publisher', suggestions:suggestions}) }}</td>
</tr>

<tr>
<th>{{ 'Age'|trans }}</th><td>{{ component('InlineEditBook', {'book':book, 'field':'ageCategory'}) }}</td>
</tr>

<tr>
<th>{{ 'tags'|trans }}</th><td>{{ component('InlineEditBook', {'book':book, 'field':'tags', suggestions:suggestions}) }}</td>
</tr>
Expand Down
18 changes: 18 additions & 0 deletions templates/components/InlineEditBook.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@
<option value="{{ tag }}" selected>{{ tag }}</option>
{% endfor %}
</select>
{% elseif field=='ageCategory' %}
<select
class="form-select"
id="book_{{ book.id }}_{{ field }}"
data-model="norender|book.{{ field }}"
>

{% for value,ageCat in constant('App\\Entity\\User::AGE_CATEGORIES') %}
<option value="{{ ageCat }}" {{ value==book.ageCategory??'selected' }}>{{ value }}</option>
{% endfor %}
<option value="">Not set</option>
</select>
{% else %}
<input
type="text"
Expand Down Expand Up @@ -106,6 +118,12 @@
<a href="{{ filter_book_url({'serie':[book.serie]}) }}" class="text-decoration-none p-1">
<i class="bi bi-list-ol"></i>&nbsp;{{ book.serie }}
</a>
{% elseif field=='ageCategory' %}
{% for key, value in constant('App\\Entity\\User::AGE_CATEGORIES')|filter((v,k)=>v==book.ageCategory) %}
{{ key }}
{% else %}
-
{% endfor %}
{% else %}
{{ attribute(book, "get"~field) }}
{% endif %}
Expand Down
2 changes: 0 additions & 2 deletions templates/user/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@
{{ include('user/_form.html.twig', {'button_label': 'Update'}) }}

<a href="{{ path('app_user_index') }}">back to list</a>

{{ include('user/_delete_form.html.twig') }}
{% endblock %}

0 comments on commit ba7fabb

Please sign in to comment.