Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autcomplete feature #149

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions Builder/DatagridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@

namespace Sonata\DoctrineMongoDBAdminBundle\Builder;

use Sonata\AdminBundle\Admin\AdminInterface;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
use Sonata\AdminBundle\Datagrid\PagerInterface;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\Pager;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
use Sonata\AdminBundle\Datagrid\Datagrid;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Filter\FilterFactoryInterface;
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\Pager;
use Symfony\Component\Form\FormFactory;

class DatagridBuilder implements DatagridBuilderInterface
Expand Down Expand Up @@ -52,8 +54,8 @@ public function __construct(FormFactory $formFactory, FilterFactoryInterface $fi
}

/**
* @param \Sonata\AdminBundle\Admin\AdminInterface $admin
* @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
* @param AdminInterface $admin
* @param FieldDescriptionInterface $fieldDescription
*/
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
{
Expand Down Expand Up @@ -82,13 +84,17 @@ public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInter

$fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName()));
$fieldDescription->setOption('name', $fieldDescription->getOption('name', $fieldDescription->getName()));

if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE, ClassMetadataInfo::MANY, ClassMetadataInfo::REFERENCE_MANY, ClassMetadataInfo::REFERENCE_ONE ))) {
$admin->attachAdminClass($fieldDescription);
}
}

/**
* @param \Sonata\AdminBundle\Datagrid\DatagridInterface $datagrid
* @param DatagridInterface $datagrid
* @param null $type
* @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
* @param \Sonata\AdminBundle\Admin\AdminInterface $admin
* @param FieldDescriptionInterface $fieldDescription
* @param AdminInterface $admin
*/
public function addFilter(DatagridInterface $datagrid, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
{
Expand Down Expand Up @@ -116,6 +122,16 @@ public function addFilter(DatagridInterface $datagrid, $type = null, FieldDescri
$admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);

$fieldDescription->mergeOption('field_options', array('required' => false));

if ('doctrine_mongo_autocomplete' === $type) {
$fieldDescription->mergeOption('field_options', array(
'class' => $fieldDescription->getTargetEntity(),
'model_manager' => $fieldDescription->getAdmin()->getModelManager(),
'admin_code' => $admin->getCode(),
'context' => 'filter',
));
}

$filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());

if (false !== $filter->getLabel() && !$filter->getLabel()) {
Expand All @@ -126,14 +142,14 @@ public function addFilter(DatagridInterface $datagrid, $type = null, FieldDescri
}

/**
* @param \Sonata\AdminBundle\Admin\AdminInterface $admin
* @param AdminInterface $admin
* @param array $values
*
* @return \Sonata\AdminBundle\Datagrid\DatagridInterface
* @return DatagridInterface
*/
public function getBaseDatagrid(AdminInterface $admin, array $values = array())
{
$pager = new Pager();
$pager = $this->getPager($admin->getPagerType());
$pager->setCountColumn($admin->getModelManager()->getIdentifierFieldNames($admin->getClass()));

$defaultOptions = array();
Expand All @@ -145,4 +161,22 @@ public function getBaseDatagrid(AdminInterface $admin, array $values = array())

return new Datagrid($admin->createQuery(), $admin->getList(), $pager, $formBuilder, $values);
}

/**
* Get pager by pagerType
*
* @param string $pagerType
*
* @return PagerInterface
* @throws \RuntimeException If invalid pager type is set.
*/
protected function getPager($pagerType)
{
switch ($pagerType) {
case Pager::TYPE_DEFAULT:
return new Pager();
default:
throw new \RuntimeException(sprintf('Unknown pager type "%s".', $pagerType));
}
}
}
24 changes: 21 additions & 3 deletions Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,35 @@ abstract class Filter extends BaseFilter
protected $active = false;

/**
* {@inheritdoc}
* @param mixed $queryBuilder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removing inheritdoc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason, should i put it back ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course. If this method as a parent, it must have this tag.

* @param mixed $value
*/
public function apply($queryBuilder, $value)
{
$this->value = $value;

$this->filter($queryBuilder, null, $this->getFieldName(), $value);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing doc block.

public function getFieldName()
{
$fieldName = $this->getOption('field_name');
if (is_array($this->getOption('parent_association_mappings'))) {
foreach($this->getOption('parent_association_mappings') as $map) {
if(!empty($map['name'])) {
$fieldName = $map['name'] . "." . $fieldName;
} elseif (!empty($map['fieldName'])) {
$fieldName = $map['fieldName'] . $fieldName;
}
}
}
if (!$fieldName) {
throw new \RuntimeException(sprintf('The option `field_name` must be set for field: `%s`', $this->getName()));
}
return $fieldName;
}

/**
* {@inheritdoc}
* @return bool
*/
public function isActive()
{
Expand Down
159 changes: 159 additions & 0 deletions Filter/ModelAutocompleteFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <[email protected]>
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineMongoDBAdminBundle\Filter;

use Sonata\CoreBundle\Form\Type\EqualType;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Doctrine\Common\Collections\Collection;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing class comment.

/**
* Class ModelAutocompleteFilter
* @author Jose Lopes <[email protected]>
* @package Sonata\DoctrineMongoDBAdminBundle\Filter
*/
class ModelAutocompleteFilter extends Filter
{

public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing doc block.

{
if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
return;
}
if ($data['value'] instanceof Collection) {
$data['value'] = $data['value']->toArray();
}
$field = $this->getIdentifierField($field);
if (is_array($data['value'])) {
$this->handleMultiple($queryBuilder, $alias, $field, $data);
} else {
$this->handleScalar($queryBuilder, $alias, $field, $data);
}
}

/**
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line not needed.

* @param ProxyQueryInterface $queryBuilder
* @param type $alias
* @param type $field
* @param type $data
* @return type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line needed before return.

*/
protected function handleMultiple(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
{
if (count($data['value']) == 0) {
return;
}
$ids = array();
foreach ($data['value'] as $value) {
$ids[] = self::fixIdentifier($value->getId());
}

if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) {
$queryBuilder->field($field)->notIn($ids);
} else {
$queryBuilder->field($field)->in($ids);
}
$this->active = true;
}

/**
*
* @param ProxyQueryInterface $queryBuilder
* @param type $alias
* @param type $field
* @param type $data
* @return type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line needed before return.

*/
protected function handleScalar(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
{
if (empty($data['value'])) {
return;
}
$id = self::fixIdentifier($data['value']->getId());
if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) {
$queryBuilder->field($field)->notEqual($id);
} else {
$queryBuilder->field($field)->equals($id);
}
$this->active = true;
}

/**
* {@inheritdoc}
*/
protected function association(ProxyQueryInterface $queryBuilder, $data)
{
$associationMappings = $this->getParentAssociationMappings();
$associationMappings[] = $this->getAssociationMapping();
$alias = $queryBuilder->entityJoin($associationMappings);

return array($alias, false);
}

/**
* Return \MongoId if $id is MongoId in string representation, otherwise custom string
*
* @param mixed $id
* @return \MongoId|string
*/
protected static function fixIdentifier($id)
{
try {
return new \MongoId($id);
} catch (\MongoException $ex) {
return $id;
}
}

/**
* Identifier field name is 'field' if mapping type is simple; otherwise, it's 'field.$id'
*
* @param string $field
* @return string
*/
protected function getIdentifierField($field)
{
$field_mapping = $this->getFieldMapping();

return (true === $field_mapping['simple']) ? $field : $field . '.$id';
}

/**
* {@inheritdoc}
*/
public function getDefaultOptions()
{
return array(
'field_name' => false,
'field_type' => 'sonata_type_model_autocomplete',
'field_options' => array(),
'operator_type' => 'sonata_type_equal',
'operator_options' => array(),
);
}

/**
* {@inheritdoc}
*/
public function getRenderSettings()
{
return array('sonata_type_filter_default', array(
'field_type' => $this->getFieldType(),
'field_options' => $this->getFieldOptions(),
'operator_type' => $this->getOption('operator_type'),
'operator_options' => $this->getOption('operator_options'),
'label' => $this->getLabel()
));
}
}

8 changes: 4 additions & 4 deletions Filter/ModelFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

class ModelFilter extends Filter
{

/**
* @param ProxyQueryInterface $queryBuilder
* @param string $alias
* @param string $field
* @param mixed $data
* @param string $alias
* @param string $field
* @param mixed $data
*
* @return
*/
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
{
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/doctrine_mongodb_filter_types.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<tag name="sonata.admin.filter.type" alias="doctrine_mongo_choice" />
</service>

<service id="sonata.admin.orm.filter.type.model_autocomplete" class="Sonata\DoctrineMongoDBAdminBundle\Filter\ModelAutocompleteFilter">
<tag name="sonata.admin.filter.type" alias="doctrine_mongo_autocomplete" />
</service>

<service id="sonata.admin.odm.filter.type.model" class="%sonata.admin.odm.filter.type.model.class%">
<tag name="sonata.admin.filter.type" alias="doctrine_mongo_model" />
</service>
Expand Down