Skip to content

Commit

Permalink
Add MongoDB to JQueryAutocompleter and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
genemu committed Nov 23, 2011
1 parent b0e0cf8 commit 36fd64e
Show file tree
Hide file tree
Showing 27 changed files with 936 additions and 17 deletions.
28 changes: 28 additions & 0 deletions Form/ChoiceList/AjaxDocumentChoiceList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Olivier Chauvel <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Genemu\Bundle\FormBundle\Form\ChoiceList;

use Symfony\Bundle\DoctrineMongoDBBundle\Form\ChoiceList\DocumentChoiceList;

/**
* @author Olivier Chauvel <[email protected]>
*/
class AjaxDocumentChoiceList extends DocumentChoiceList
{
/**
* {@inheritdoc}
*/
protected function load()
{
return array();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @author Olivier Chauvel <[email protected]>
*/
class AjaxChoiceList extends EntityChoiceList
class AjaxEntityChoiceList extends EntityChoiceList
{
/**
* {@inheritdoc}
Expand Down
85 changes: 85 additions & 0 deletions Form/DataTransformer/DocumentIdToJsonTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Olivier Chauvel <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Genemu\Bundle\FormBundle\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Bundle\DoctrineMongoDBBundle\Form\ChoiceList\DocumentChoiceList;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\FormException;

/**
* DocumentIdToJsonTransform
*
* @author Olivier Chauvel <[email protected]>
*/

class DocumentIdToJsonTransformer implements DataTransformerInterface
{
protected $choiceList;
protected $rooteName;

/**
* Construct
*
* @param DocumentChoiceList $choiceList
*/
public function __construct(DocumentChoiceList $choiceList, $routeName = null)
{
$this->choiceList = $choiceList;
$this->routeName = $routeName;
}

/**
* {@inheritdoc}
*/
public function transform($id)
{
if (null === $id || !$id) {
return null;
}

if (!is_integer($id) && !is_string($id)) {
throw new UnexpectedTypeException($id, 'integer or string');
}

if ($this->routeName) {
$entity = $this->choiceList->getDocument($id);

if (!method_exists($entity, '__toString')) {
throw new FormException('Entities passed to the choice field must have a "__toString()" method defined because use Ajax.');
}

return json_encode(array(
'label' => $entity->__toString(),
'value' => $id
));
} else {
$choices = $this->choiceList->getChoices();

return json_encode(array(
'label' => $choices[$id],
'value' => $id
));
}
}

/**
* {@inheritdoc}
*/
public function reverseTransform($value)
{
$value = is_array($value) ? current($value) : $value;
$value = json_decode($value, true);

return $value['value'];
}
}
96 changes: 96 additions & 0 deletions Form/DataTransformer/DocumentIdsToJsonTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Olivier Chauvel <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Genemu\Bundle\FormBundle\Form\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Bundle\DoctrineMongoDBBundle\Form\ChoiceList\DocumentChoiceList;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\FormException;

/**
* DocumentIdsToJsonTransform
*
* @author Olivier Chauvel <[email protected]>
*/

class DocumentIdsToJsonTransformer implements DataTransformerInterface
{
protected $choiceList;
protected $routeName;

/**
* Construct
*
* @param DocumentChoiceList $choiceList
*/
public function __construct(DocumentChoiceList $choiceList, $routeName = null)
{
$this->choiceList = $choiceList;
$this->routeName = $routeName;
}

/**
* {@inheritdoc}
*/
public function transform($ids)
{
if (null === $ids || !$ids) {
return null;
}

if (!is_array($ids)) {
throw new UnexpectedTypeException($ids, 'array');
}

$array = array();
if ($this->routeName) {
foreach ($ids as $id) {
$entity = $this->choiceList->getDocument($id);

if (!method_exists($entity, '__toString')) {
throw new FormException('Entities passed to the choice field must have a "__toString()" method defined because use Ajax.');
}

$array[] = array(
'label' => $entity->__toString(),
'value' => $id
);
}
} else {
$choices = $this->choiceList->getChoices();
foreach ($ids as $id) {
$array[] = array(
'label' => $choices[$id],
'value' => $id
);
}
}

return json_encode($array);
}

/**
* {@inheritdoc}
*/
public function reverseTransform($values)
{
$values = is_array($values) ? current($values) : $values;
$values = json_decode($values, true);

$array = array();
foreach ($values as $value) {
$array[] = $value['value'];
}

return $array;
}
}
4 changes: 2 additions & 2 deletions Form/DataTransformer/EntityIdToJsonTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function transform($id)
return null;
}

if (!is_integer($id)) {
throw new UnexpectedTypeException($id, 'integer');
if (!is_integer($id) && !is_string($id)) {
throw new UnexpectedTypeException($id, 'integer or string');
}

if ($this->routeName) {
Expand Down
42 changes: 37 additions & 5 deletions Form/Type/JQueryAutocompleterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
use Genemu\Bundle\FormBundle\Form\DataTransformer\EntityIdsToJsonTransformer;
use Genemu\Bundle\FormBundle\Form\DataTransformer\EntityIdToJsonTransformer;

use Genemu\Bundle\FormBundle\Form\ChoiceList\AjaxChoiceList;
use Genemu\Bundle\FormBundle\Form\DataTransformer\DocumentIdsToJsonTransformer;
use Genemu\Bundle\FormBundle\Form\DataTransformer\DocumentIdToJsonTransformer;

use Genemu\Bundle\FormBundle\Form\ChoiceList\AjaxEntityChoiceList;
use Genemu\Bundle\FormBundle\Form\ChoiceList\AjaxDocumentChoiceList;

/**
* JQueryAutocompleterType
Expand Down Expand Up @@ -55,7 +59,7 @@ public function __construct($registry)
*/
public function buildForm(FormBuilder $builder, array $options)
{
if (!$options['route_name'] && 'entity' !== $options['widget']) {
if (!$options['route_name'] && !in_array($options['widget'], array('entity', 'document'))) {
$choiceList = null;

if (isset($options['choices']) && $options['choices']) {
Expand Down Expand Up @@ -87,6 +91,9 @@ public function buildForm(FormBuilder $builder, array $options)
case 'entity':
$transformer = new EntityIdsToJsonTransformer($options['choice_list'], $options['route_name']);
break;
case 'document':
$transformer = new DocumentIdsToJsonTransformer($options['choice_list'], $options['route_name']);
break;
case 'choice':
if (isset($options['choice_list']) && $options['choice_list']) {
$transformer = new ChoiceToJsonTransformer($options['choice_list']);
Expand All @@ -107,6 +114,8 @@ public function buildForm(FormBuilder $builder, array $options)
$builder->appendClientTransformer(new FieldToJsonTransformer());
} elseif ('entity' === $options['widget']) {
$builder->appendClientTransformer(new EntityIdToJsonTransformer($options['choice_list'], $options['route_name']));
} elseif ('document' === $options['widget']) {
$builder->appendClientTransformer(new DocumentIdToJsonTransformer($options['choice_list'], $options['route_name']));
}

$builder->setAttribute('multiple', false);
Expand All @@ -128,7 +137,7 @@ public function buildView(FormView $view, FormInterface $form)
if (
$form->getAttribute('multiple') ||
$form->getAttribute('route_name') ||
'entity' === $form->getAttribute('widget')
in_array($form->getAttribute('widget'), array('entity', 'document'))
) {
$data = json_decode($data, true);
}
Expand All @@ -143,7 +152,7 @@ public function buildView(FormView $view, FormInterface $form)
if ($form->hasAttribute('choice_list') && $form->getAttribute('choice_list')) {
$choices = $form->getAttribute('choice_list')->getChoices();

if ('entity' === $form->getAttribute('widget')) {
if (in_array($form->getAttribute('widget'), array('entity', 'document'))) {
$value = $data['label'];
} else {
foreach ($choices as $choice) {
Expand Down Expand Up @@ -189,6 +198,18 @@ public function getDefaultOptions(array $options)
'choices' => array(),
'group_by' => null
));
} elseif (isset($options['widget']) && 'document' === $options['widget']) {
$defaultOptions = array_merge($defaultOptions, array(
'choices' => array(),
'class' => null,
'document_manager' => null,
'expended' => null,
'multiple' => false,
'preferred_choices' => array(),
'property' => null,
'query_builder' => null,
'template' => 'choice'
));
}

$options = array_replace($defaultOptions, $options);
Expand All @@ -208,14 +229,25 @@ public function getDefaultOptions(array $options)
if ('entity' === $options['widget'] && $options['route_name']) {
$method = $this->registry instanceof ManagerRegistry ? 'getManager' : 'getEntityManager';

$options['choice_list'] = new AjaxChoiceList(
$options['choice_list'] = new AjaxEntityChoiceList(
$this->registry->$method($options['em']),
$options['class'],
$options['property'],
$options['query_builder'],
$options['choices'],
$options['group_by']
);
} elseif ('document' === $options['widget'] && $options['route_name']) {
$method = $this->registry instanceof ManagerRegistry ? 'getManager' : 'getEntityManager';

$options['choice_list'] = new AjaxDocumentChoiceList(
$this->registry->$method($options['document_manager']),
$options['class'],
$options['property'],
$options['query_builder'],
$options['choices']
);

}

return $options;
Expand Down
5 changes: 5 additions & 0 deletions Resources/doc/captcha_gd/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ genemu_form:
## Default Usage:
``` php
<?php
// ...
public function buildForm(FormBuilder $builder, array $options)
{
Expand All @@ -19,3 +20,7 @@ public function buildForm(FormBuilder $builder, array $options)
->add('captcha', 'genemu_captcha');
}
```

## Extra:

[Default configuration](https://github.com/genemu/GenemuFormBundle/blob/master/Resources/doc/captcha_gd/default_configuration.md)
1 change: 1 addition & 0 deletions Resources/doc/jquery/autocomplete/choices.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Usage:

``` php
<?php
// ...
public function buildForm(FormBuilder $builder, array $options)
{
Expand Down
4 changes: 3 additions & 1 deletion Resources/doc/jquery/autocomplete/choices_ajax.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Usage:

``` php
<?php
// ...
public function buildForm(FormBuilder $builder, array $options)
{
Expand All @@ -20,13 +21,14 @@ public function buildForm(FormBuilder $builder, array $options)
## Add function ajaxAction to Controller:

``` php
<?php
namespace MyNamespace;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class MyClassAjax extends Controller
class MyClassAjaxController extends Controller
{
/**
* @Route("/ajax", name="ajax")
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/jquery/autocomplete/entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Usage:

``` php
<?php
// ...
public function buildForm(FormBuilder $builder, array $options)
{
Expand Down
Loading

0 comments on commit 36fd64e

Please sign in to comment.