toolbar - always allow to choose global activities (#410)
This commit is contained in:
@@ -19,8 +19,11 @@ use App\Form\Type\VisibilityType;
|
||||
use App\Repository\ActivityRepository;
|
||||
use App\Repository\CustomerRepository;
|
||||
use App\Repository\ProjectRepository;
|
||||
use App\Repository\Query\ActivityQuery;
|
||||
use App\Repository\Query\CustomerQuery;
|
||||
use App\Repository\Query\ProjectQuery;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\DateType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
@@ -137,6 +140,12 @@ abstract class AbstractToolbarForm extends AbstractType
|
||||
*/
|
||||
protected function addProjectChoice(FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('project', ChoiceType::class, [
|
||||
'group_by' => null,
|
||||
'required' => false,
|
||||
'label' => 'label.project',
|
||||
]);
|
||||
|
||||
$builder->addEventListener(
|
||||
FormEvents::PRE_SUBMIT,
|
||||
function (FormEvent $event) {
|
||||
@@ -149,10 +158,12 @@ abstract class AbstractToolbarForm extends AbstractType
|
||||
'group_by' => null,
|
||||
'required' => false,
|
||||
'query_builder' => function (ProjectRepository $repo) use ($data) {
|
||||
$qb = $repo->builderForEntityType();
|
||||
$qb->andWhere('p.customer = :customer')->setParameter('customer', $data['customer']);
|
||||
$query = new ProjectQuery();
|
||||
$query->setCustomer($data['customer']);
|
||||
$query->setResultType(ProjectQuery::RESULT_TYPE_QUERYBUILDER);
|
||||
$query->setVisibility(ProjectQuery::SHOW_BOTH);
|
||||
|
||||
return $qb;
|
||||
return $repo->findByQuery($query);
|
||||
},
|
||||
]);
|
||||
}
|
||||
@@ -164,6 +175,20 @@ abstract class AbstractToolbarForm extends AbstractType
|
||||
*/
|
||||
protected function addActivityChoice(FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('activity', ActivityType::class, [
|
||||
'group_by' => null,
|
||||
'required' => false,
|
||||
'query_builder' => function (ActivityRepository $repo) {
|
||||
$query = new ActivityQuery();
|
||||
$query->setResultType(ActivityQuery::RESULT_TYPE_QUERYBUILDER);
|
||||
$query->setGlobalsOnly(true);
|
||||
$query->setOrderGlobalsFirst(true);
|
||||
$query->setVisibility(ActivityQuery::SHOW_BOTH);
|
||||
|
||||
return $repo->findByQuery($query);
|
||||
},
|
||||
]);
|
||||
|
||||
$builder->addEventListener(
|
||||
FormEvents::PRE_SUBMIT,
|
||||
function (FormEvent $event) {
|
||||
@@ -176,9 +201,13 @@ abstract class AbstractToolbarForm extends AbstractType
|
||||
'group_by' => null,
|
||||
'required' => false,
|
||||
'query_builder' => function (ActivityRepository $repo) use ($data) {
|
||||
$qb = $repo->builderForEntityType(null, $data['project']);
|
||||
$query = new ActivityQuery();
|
||||
$query->setResultType(ActivityQuery::RESULT_TYPE_QUERYBUILDER);
|
||||
$query->setProject($data['project']);
|
||||
$query->setOrderGlobalsFirst(true);
|
||||
$query->setVisibility(ActivityQuery::SHOW_BOTH);
|
||||
|
||||
return $qb;
|
||||
return $repo->findByQuery($query);
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -40,6 +40,27 @@ class ActivityType extends AbstractType
|
||||
return $activity->getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Activity $choiceValue
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return array
|
||||
*/
|
||||
public function choiceAttr($choiceValue, $key, $value)
|
||||
{
|
||||
$project = null;
|
||||
|
||||
if (!($choiceValue instanceof Activity)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (null !== $choiceValue->getProject()) {
|
||||
$project = $choiceValue->getProject()->getId();
|
||||
}
|
||||
|
||||
return ['data-project' => $project];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -50,17 +71,10 @@ class ActivityType extends AbstractType
|
||||
'class' => Activity::class,
|
||||
'choice_label' => [$this, 'choiceLabel'],
|
||||
'group_by' => [$this, 'groupBy'],
|
||||
'choice_attr' => [$this, 'choiceAttr'],
|
||||
'query_builder' => function (ActivityRepository $repo) {
|
||||
return $repo->builderForEntityType();
|
||||
},
|
||||
'choice_attr' => function (Activity $activity, $key, $value) {
|
||||
$attributes = [];
|
||||
if (null !== $activity->getProject()) {
|
||||
$attributes['data-project'] = $activity->getProject()->getId();
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
},
|
||||
//'attr' => ['class' => 'selectpicker', 'data-size' => 10, 'data-live-search' => true, 'data-width' => '100%']
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,27 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
*/
|
||||
class ProjectType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* @param Project $choiceValue
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return array
|
||||
*/
|
||||
public function choiceAttr($choiceValue, $key, $value)
|
||||
{
|
||||
$customer = null;
|
||||
|
||||
if (!($choiceValue instanceof Project)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (null !== $choiceValue->getCustomer()) {
|
||||
$customer = $choiceValue->getCustomer()->getId();
|
||||
}
|
||||
|
||||
return ['data-customer' => $customer];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -29,6 +50,7 @@ class ProjectType extends AbstractType
|
||||
'label' => 'label.project',
|
||||
'class' => Project::class,
|
||||
'choice_label' => 'name',
|
||||
'choice_attr' => [$this, 'choiceAttr'],
|
||||
'group_by' => function (Project $project, $key, $index) {
|
||||
return $project->getCustomer()->getName();
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user