diff --git a/src/TimesheetBundle/Controller/Admin/CustomerController.php b/src/TimesheetBundle/Controller/Admin/CustomerController.php index 9437c851..29ae972b 100644 --- a/src/TimesheetBundle/Controller/Admin/CustomerController.php +++ b/src/TimesheetBundle/Controller/Admin/CustomerController.php @@ -20,7 +20,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use TimesheetBundle\Form\CustomerEditForm; -use TimesheetBundle\Form\CustomerToolbarForm; +use TimesheetBundle\Form\Toolbar\CustomerToolbarForm; use TimesheetBundle\Repository\Query\CustomerQuery; /** @@ -40,7 +40,7 @@ class CustomerController extends AbstractController */ protected function getQueryForRequest(Request $request) { - $visibility = $request->get('visibility'); + $visibility = $request->get('visibility', CustomerQuery::SHOW_VISIBLE); if (strlen($visibility) == 0 || (int)$visibility != $visibility) { $visibility = CustomerQuery::SHOW_BOTH; } diff --git a/src/TimesheetBundle/Form/ProjectEditForm.php b/src/TimesheetBundle/Form/ProjectEditForm.php index 3422d21a..6185d9dd 100644 --- a/src/TimesheetBundle/Form/ProjectEditForm.php +++ b/src/TimesheetBundle/Form/ProjectEditForm.php @@ -21,6 +21,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; use TimesheetBundle\Entity\Customer; use TimesheetBundle\Entity\Project; use TimesheetBundle\Form\Type\CustomerType; +use TimesheetBundle\Repository\CustomerRepository; /** * Defines the form used to edit Projects. @@ -35,6 +36,14 @@ class ProjectEditForm extends AbstractType */ public function buildForm(FormBuilderInterface $builder, array $options) { + /** @var Project $entry */ + $entry = $options['data']; + + $customer = null; + if ($entry->getId() !== null) { + $customer = $entry->getCustomer(); + } + $builder // string - length 255 ->add('name', TextType::class, [ @@ -47,6 +56,9 @@ class ProjectEditForm extends AbstractType // customer ->add('customer', CustomerType::class, [ 'label' => 'label.customer', + 'query_builder' => function (CustomerRepository $repo) use ($customer) { + return $repo->builderForEntityType($customer); + }, ]) // boolean ->add('visible', VisibilityType::class, [ @@ -57,17 +69,6 @@ class ProjectEditForm extends AbstractType 'label' => 'label.budget', 'currency' => $builder->getOption('currency'), ]) - // do not allow activity selection as this causes headaches: - // 1. it is a bad UX - // 2. what should happen if they are detached? - /* - ->add('activities', EntityType::class, [ - 'label' => 'label.activity', - 'class' => 'TimesheetBundle:Activity', - 'multiple' => true, - 'expanded' => true - ]) - */ ; } diff --git a/src/TimesheetBundle/Form/Type/CustomerType.php b/src/TimesheetBundle/Form/Type/CustomerType.php index 6dce49fe..8e4f800b 100644 --- a/src/TimesheetBundle/Form/Type/CustomerType.php +++ b/src/TimesheetBundle/Form/Type/CustomerType.php @@ -35,10 +35,7 @@ class CustomerType extends AbstractType 'class' => 'TimesheetBundle:Customer', 'choice_label' => 'name', 'query_builder' => function (CustomerRepository $repo) { - $query = new CustomerQuery(); - $query->setVisibility(CustomerQuery::SHOW_BOTH); - $query->setResultType(CustomerQuery::RESULT_TYPE_QUERYBUILDER); - return $repo->findByQuery($query); + return $repo->builderForEntityType(null); }, ]); } diff --git a/src/TimesheetBundle/Repository/CustomerRepository.php b/src/TimesheetBundle/Repository/CustomerRepository.php index 648a4a83..f00983de 100644 --- a/src/TimesheetBundle/Repository/CustomerRepository.php +++ b/src/TimesheetBundle/Repository/CustomerRepository.php @@ -50,6 +50,20 @@ class CustomerRepository extends AbstractRepository return $stats; } + /** + * Returns a query builder that is used for CustomerType and your own 'query_builder' option. + * + * @param Customer|null $entity + * @return \Doctrine\ORM\QueryBuilder + */ + public function builderForEntityType(Customer $entity = null) + { + $query = new CustomerQuery(); + $query->setHiddenEntity($entity); + $query->setResultType(CustomerQuery::RESULT_TYPE_QUERYBUILDER); + return $this->findByQuery($query); + } + /** * @param CustomerQuery $query * @return \Doctrine\ORM\QueryBuilder|\Pagerfanta\Pagerfanta @@ -64,6 +78,12 @@ class CustomerRepository extends AbstractRepository if ($query->getVisibility() == CustomerQuery::SHOW_VISIBLE) { $qb->andWhere('c.visible = 1'); + + /** @var Customer $entity */ + $entity = $query->getHiddenEntity(); + if ($entity!== null) { + $qb->orWhere('c.id = :customer')->setParameter('customer', $entity); + } } elseif ($query->getVisibility() == CustomerQuery::SHOW_HIDDEN) { $qb->andWhere('c.visible = 0'); }