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

Added doctrine_mongo_autocomplete model filter #137

Closed
wants to merge 5 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 37 additions & 3 deletions Builder/DatagridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@

namespace Sonata\DoctrineMongoDBAdminBundle\Builder;

use Sonata\AdminBundle\Admin\AdminInterface;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
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 @@ -82,6 +83,10 @@ 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);
}
}

/**
Expand Down Expand Up @@ -116,6 +121,16 @@ public function addFilter(DatagridInterface $datagrid, $type = null, FieldDescri
$admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);

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

if ($type === 'doctrine_mongo_autocomplete') {
$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 @@ -133,7 +148,7 @@ public function addFilter(DatagridInterface $datagrid, $type = null, FieldDescri
*/
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 +160,23 @@ 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 \Sonata\AdminBundle\Datagrid\PagerInterface
*
* @throws \RuntimeException If invalid pager type is set.
*/
protected function getPager($pagerType)
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the use case of this new extension point?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to keep the same logic of MySQL PHP file for be easier to mantain both projects by just comparing them -- till common areas such this one become merged into Admin bundle

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

{
switch ($pagerType) {
case Pager::TYPE_DEFAULT:
return new Pager();
default:
throw new \RuntimeException(sprintf('Unknown pager type "%s".', $pagerType));
}
}
}
21 changes: 21 additions & 0 deletions Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ public function apply($queryBuilder, $value)
$this->filter($queryBuilder, null, $this->getFieldName(), $value);
}

public function getFieldName()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add some PHPDoc please?

{
$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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use simple quotes (run PHP CS Fixer).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was before the PHP CS Fixer

} 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}
*/
Expand Down
162 changes: 162 additions & 0 deletions Filter/ModelAutocompleteFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <josluis.lopes@gmail.com>
*
* 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;

class ModelAutocompleteFilter extends Filter
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add some PHPDoc?

{

public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
{
if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be simplified by if (!isset($data['value'])) {

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

}

/**
*
* @param ProxyQueryInterface $queryBuilder
Copy link
Contributor

Choose a reason for hiding this comment

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

Please fix PHPDoc.

* @param type $alias
* @param type $field
* @param type $data
* @return type
*/
protected function handleMultiple(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
Copy link
Contributor

Choose a reason for hiding this comment

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

array $data (typehint)

{
if (count($data['value']) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Always use strict operators (===) and yoda style (0 === count($data['value'])).

return;
}

$ids = array();
foreach ($data['value'] as $value) {
$ids[] = self::fixIdentifier($value->getId());
}

if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here (===)

$queryBuilder->field($field)->notIn($ids);
} else {
$queryBuilder->field($field)->in($ids);
}

$this->active = true;
}

/**
*
Copy link
Contributor

Choose a reason for hiding this comment

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

Correct the PHPDoc.

* @param ProxyQueryInterface $queryBuilder
* @param type $alias
* @param type $field
* @param type $data
* @return type
*/
protected function handleScalar(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
Copy link
Contributor

Choose a reason for hiding this comment

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

data should be typehinted array $data

{
if (empty($data['value'])) {
return;
}

$id = self::fixIdentifier($data['value']->getId());

if (isset($data['type']) && $data['type'] == EqualType::TYPE_IS_NOT_EQUAL) {
Copy link
Contributor

Choose a reason for hiding this comment

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

===

$queryBuilder->field($field)->notEqual($id);
} else {
$queryBuilder->field($field)->equals($id);
}

$this->active = true;
}

/**
* {@inheritdoc}
*/
protected function association(ProxyQueryInterface $queryBuilder, $data)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this extension point necessary? Make the function private if it isn't.

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't mind my previous comment. It's inherited.

{
$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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why static?

{
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
{

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you delete this docblock?

/**
* @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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"require-dev": {
"jmikola/geojson": "~1.0",
"phpunit/phpunit": "~4.0",
"symfony/phpunit-bridge": "~2.7|~3.0",
"fabpot/php-cs-fixer": "~0.5|~1.0"
},
Expand Down