forked from genemu/GenemuFormBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MongoDB to JQueryAutocompleter and doc
- Loading branch information
Showing
27 changed files
with
936 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
/** | ||
* @author Olivier Chauvel <[email protected]> | ||
*/ | ||
class AjaxChoiceList extends EntityChoiceList | ||
class AjaxEntityChoiceList extends EntityChoiceList | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
## Usage: | ||
|
||
``` php | ||
<?php | ||
// ... | ||
public function buildForm(FormBuilder $builder, array $options) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
## Usage: | ||
|
||
``` php | ||
<?php | ||
// ... | ||
public function buildForm(FormBuilder $builder, array $options) | ||
{ | ||
|
Oops, something went wrong.