add an "active" field on activity type

This commit is contained in:
Julien Fastré 2016-11-14 23:00:13 +01:00
parent 79dd4bfac8
commit 5dda66342a
8 changed files with 116 additions and 31 deletions

View File

@ -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;
}
}

View File

@ -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
))
;

View File

@ -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'

View File

@ -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());
}
)
);

View File

@ -10,4 +10,7 @@ Chill\ActivityBundle\Entity\ActivityType:
fields:
name:
type: json_array
active:
type: boolean
default: true
lifecycleCallbacks: { }

View File

@ -0,0 +1,29 @@
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Type;
/**
* Add an "active" column on activitytype table
*/
class Version20161114085659 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$this->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');
}
}

View File

@ -23,6 +23,7 @@
<thead>
<tr>
<th>{{ 'Name'|trans }}</th>
<th>{{ 'Active'|trans }}</th>
<th>{{ 'Actions'|trans }}</th>
</tr>
</thead>
@ -30,13 +31,20 @@
{% for entity in entities %}
<tr>
<td><a href="{{ path('chill_activity_activitytype_show', { 'id': entity.id }) }}">{{ entity.name|localize_translatable_string }}</a></td>
<td style="text-align:center;">
{%- if entity.active -%}
<i class="fa fa-check-square-o"></i>
{%- else -%}
<i class="fa fa-square-o"></i>
{%- endif -%}
</td>
<td>
<ul>
<ul class="record_actions">
<li>
<a href="{{ path('chill_activity_activitytype_show', { 'id': entity.id }) }}">{{ 'show'|trans }}</a>
<a href="{{ path('chill_activity_activitytype_show', { 'id': entity.id }) }}" class="sc-button bt-reset">{{ 'show'|trans|capitalize }}</a>
</li>
<li>
<a href="{{ path('chill_activity_activitytype_edit', { 'id': entity.id }) }}">{{ 'edit'|trans }}</a>
<a href="{{ path('chill_activity_activitytype_edit', { 'id': entity.id }) }}" class="sc-button bt-edit">{{ 'edit'|trans|capitalize }}</a>
</li>
</ul>
</td>
@ -45,9 +53,9 @@
</tbody>
</table>
<ul>
<ul class="record_actions">
<li>
<a href="{{ path('chill_activity_activitytype_new') }}">
<a href="{{ path('chill_activity_activitytype_new') }}" class="sc-button bt-create">
{{ 'Create a new activity type'|trans }}
</a>
</li>

View File

@ -67,11 +67,11 @@ class TranslatableActivityTypeTest extends KernelTestCase
*
* @return \Chill\ActivityBundle\Entity\ActivityType
*/
protected function getRandomType()
protected function getRandomType($active = true)
{
$types = $this->container->get('doctrine.orm.entity_manager')
->getRepository('ChillActivityBundle:ActivityType')
->findAll();
->findBy(array('active' => $active));
return $types[array_rand($types)];
}
@ -93,7 +93,8 @@ class TranslatableActivityTypeTest extends KernelTestCase
$this->assertEquals($type->getId(), $form->getData()['type']->getId());
// test the ordering of the types in the form
$view = $form->createView();
// since 2016-11-14 the types are not alphabetically ordered, skipping
/*$view = $form->createView();
$this->assertGreaterThan(0, count($view['type']->vars['choices']),
"test that there are at least one choice");
@ -106,7 +107,7 @@ class TranslatableActivityTypeTest extends KernelTestCase
$this->assertTrue($previous < $choice->label);
$previous = $choice->label;
}
}
}*/
}