From 5dda66342aa82b83d3514ad7ec7c151ef8448ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 14 Nov 2016 23:00:13 +0100 Subject: [PATCH] add an "active" field on activity type --- Entity/ActivityType.php | 38 +++++++++++++++++++ Form/ActivityType.php | 3 +- Form/ActivityTypeType.php | 14 +++++-- Form/Type/TranslatableActivityType.php | 33 ++++++++-------- .../config/doctrine/ActivityType.orm.yml | 3 ++ .../migrations/Version20161114085659.php | 29 ++++++++++++++ Resources/views/ActivityType/index.html.twig | 18 ++++++--- .../Type/TranslatableActivityTypeTest.php | 9 +++-- 8 files changed, 116 insertions(+), 31 deletions(-) create mode 100644 Resources/migrations/Version20161114085659.php diff --git a/Entity/ActivityType.php b/Entity/ActivityType.php index 2af64f861..9e0065358 100644 --- a/Entity/ActivityType.php +++ b/Entity/ActivityType.php @@ -34,6 +34,8 @@ class ActivityType * @var array */ private $name; + + private $active = true; /** @@ -82,5 +84,41 @@ class ActivityType return $this->name; } } + + /** + * get active + * + * return true if the type is active. + * + * @return boolean true if the type is active + */ + public function getActive() { + return $this->active; + } + + /** + * get active + * + * return true if the type is active + * + * @return boolean true if the type is active + */ + public function isActive() { + return $this->getActive(); + } + + /** + * set active + * + * set to true if the type is active + * + * @param boolean $active + * @return \Chill\ActivityBundle\Entity\ActivityType + */ + public function setActive($active) { + $this->active = $active; + return $this; + } + } diff --git a/Form/ActivityType.php b/Form/ActivityType.php index 23e829bb7..f3f92377d 100644 --- a/Form/ActivityType.php +++ b/Form/ActivityType.php @@ -114,7 +114,8 @@ class ActivityType extends AbstractType 'required' => false )) ->add('type', TranslatableActivityType::class, array( - 'placeholder' => 'Choose a type' + 'placeholder' => 'Choose a type', + 'active_only' => true )) ; diff --git a/Form/ActivityTypeType.php b/Form/ActivityTypeType.php index 278c23a9a..146f0abca 100644 --- a/Form/ActivityTypeType.php +++ b/Form/ActivityTypeType.php @@ -4,7 +4,8 @@ namespace Chill\ActivityBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; class ActivityTypeType extends AbstractType { @@ -16,13 +17,20 @@ class ActivityTypeType extends AbstractType { $builder ->add('name', 'translatable_string') - ; + ->add('active', ChoiceType::class, array( + 'choices' => array( + 'Yes' => true, + 'No' => false + ), + 'choices_as_values' => true, + 'expanded' => true + )); } /** * @param OptionsResolverInterface $resolver */ - public function setDefaultOptions(OptionsResolverInterface $resolver) + public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'Chill\ActivityBundle\Entity\ActivityType' diff --git a/Form/Type/TranslatableActivityType.php b/Form/Type/TranslatableActivityType.php index 6e4865bb7..89399e551 100644 --- a/Form/Type/TranslatableActivityType.php +++ b/Form/Type/TranslatableActivityType.php @@ -65,30 +65,27 @@ class TranslatableActivityType extends AbstractType return EntityType::class; } + public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options) { + /* @var $qb \Doctrine\ORM\QueryBuilder */ + $qb = $options['query_builder']; + + if ($options['active_only'] === true) { + $qb->where($qb->expr()->eq('at.active', ':active')); + $qb->setParameter('active', true, \Doctrine\DBAL\Types\Type::BOOLEAN); + } + } + public function configureOptions(OptionsResolver $resolver) { - // create a local copy for use in closures - $translatableStringHelper = $this->translatableStringHelper; - $types = $this->activityTypeRepository->findAll(); - - // sort by alphabetical order - usort($types, function(ActivityType $typeA, ActivityType $typeB) use ($translatableStringHelper) { - $strA = $translatableStringHelper->localize($typeA->getName()); - $strB = $translatableStringHelper->localize($typeB->getName()); - - if ($strA === $strB) { - return 0; - } - - return $strA < $strB ? -1 : 1; - }); $resolver->setDefaults( array( 'class' => 'ChillActivityBundle:ActivityType', - 'choices' => $types, - 'choice_label' => function (ActivityType $type) use ($translatableStringHelper) { - return $translatableStringHelper->localize($type->getName()); + 'active_only' => true, + 'query_builder' => $this->activityTypeRepository + ->createQueryBuilder('at'), + 'choice_label' => function (ActivityType $type) { + return $this->translatableStringHelper->localize($type->getName()); } ) ); diff --git a/Resources/config/doctrine/ActivityType.orm.yml b/Resources/config/doctrine/ActivityType.orm.yml index fb3878343..5965cd9a0 100644 --- a/Resources/config/doctrine/ActivityType.orm.yml +++ b/Resources/config/doctrine/ActivityType.orm.yml @@ -10,4 +10,7 @@ Chill\ActivityBundle\Entity\ActivityType: fields: name: type: json_array + active: + type: boolean + default: true lifecycleCallbacks: { } diff --git a/Resources/migrations/Version20161114085659.php b/Resources/migrations/Version20161114085659.php new file mode 100644 index 000000000..cd23b4ffa --- /dev/null +++ b/Resources/migrations/Version20161114085659.php @@ -0,0 +1,29 @@ +addSql('ALTER TABLE activitytype ADD active BOOLEAN NOT NULL DEFAULT \'t\''); + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + $this->addSql('ALTER TABLE ActivityType DROP active'); + } +} diff --git a/Resources/views/ActivityType/index.html.twig b/Resources/views/ActivityType/index.html.twig index e1b714888..7c4a77c89 100644 --- a/Resources/views/ActivityType/index.html.twig +++ b/Resources/views/ActivityType/index.html.twig @@ -23,6 +23,7 @@ {{ 'Name'|trans }} + {{ 'Active'|trans }} {{ 'Actions'|trans }} @@ -30,13 +31,20 @@ {% for entity in entities %} {{ entity.name|localize_translatable_string }} + + {%- if entity.active -%} + + {%- else -%} + + {%- endif -%} + -