@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
])
|
||||
*/
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user