Skip to content

Commit

Permalink
Merge pull request genemu#52 from kusmierz/2.0
Browse files Browse the repository at this point in the history
Sync with 2.0 branch
  • Loading branch information
genemu committed Feb 29, 2012
2 parents e7f32bc + e52ae88 commit 97fa633
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Controller/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class ImageController extends Controller
*/
public function changeAction(Request $request)
{
$rootDir = $this->container->getParameter('genemu.form.file.root_dir');
$folder = $this->container->getParameter('genemu.form.file.folder');
$rootDir = rtrim($this->container->getParameter('genemu.form.file.root_dir'), '/\\') . DIRECTORY_SEPARATOR;
$folder = rtrim($this->container->getParameter('genemu.form.file.folder'), '/\\') . DIRECTORY_SEPARATOR;

$file = $request->get('image');
$handle = new Image($rootDir . $this->stripQueryString($file));
Expand Down
22 changes: 22 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function getConfigTreeBuilder()
$this->addFile($rootNode);
$this->addImage($rootNode);
$this->addAutocompleter($rootNode);
$this->addTokeninput($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -258,6 +259,27 @@ private function addImage(ArrayNodeDefinition $rootNode)
;
}

/**
* Add configuration Tokeninput
*
* @param ArrayNodeDefinition $rootNode
*/
private function addTokeninput(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('tokeninput')
->canBeUnset()
->addDefaultsIfNotSet()
->children()
->booleanNode('doctrine')->defaultTrue()->end()
->booleanNode('mongodb')->defaultFalse()->end()
->end()
->end()
->end()
;
}

/**
* Add configuration Autocompleter
*
Expand Down
8 changes: 6 additions & 2 deletions DependencyInjection/GenemuFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('model.xml');
$loader->load('jquery.xml');

if (false === empty($configs['autocompleter']['doctrine'])) {
if (!empty($configs['autocompleter']['doctrine']) ||
!empty($configs['tokeninput']['doctrine'])
) {
$loader->load('entity.xml');
}

if (false === empty($configs['autocompleter']['mongodb'])) {
if (!empty($configs['autocompleter']['mongodb']) ||
!empty($configs['tokeninput']['mongodb'])
) {
$loader->load('mongodb.xml');
}

Expand Down
80 changes: 80 additions & 0 deletions Form/Core/Type/PlainType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Genemu\Bundle\FormBundle\Form\Core\Type;

use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

/**
* A Form type that just renders the field as a p tag. This is useful for forms where certain field
* need to be shown but not editable.
*
* @author Adam Kuśmierz <adam@kusmierz.be>
*/
class PlainType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function getDefaultOptions(array $options)
{
$defaultOptions = array(
'widget' => 'field',
'configs' => array(),
'read_only' => true,
'attr' => array(
'class' => $this->getName()
)
//'property_path' => false,
);

return array_replace_recursive($defaultOptions, $options);
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form)
{
$value = $form->getClientData();

// set string representation
if (true === $value) {
$value = 'true';
} else if (false === $value) {
$value = 'false';
} else if (null === $value) {
$value = 'null';
} else if (is_array($value)) {
$value = implode(', ', $value);
} else if ($value instanceof \DateTime) {
$value = $value->format('Y-m-d H:i:s');
} else if (is_object($value)) {
if (method_exists($value, '__toString')) {
$value = $value->__toString();
} else {
$value = get_class($value);
}
}

$view->set('value', (string) $value);
}

/**
* {@inheritdoc}
*/
public function getParent(array $options)
{
return 'field';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'genemu_plain';
}
}
174 changes: 174 additions & 0 deletions Form/JQuery/Type/TokeninputType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Olivier Chauvel <olivier@generation-multiple.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Genemu\Bundle\FormBundle\Form\JQuery\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

use Genemu\Bundle\FormBundle\Form\Core\ChoiceList\AjaxArrayChoiceList;
use Genemu\Bundle\FormBundle\Form\Core\DataTransformer\ChoiceToJsonTransformer;

/**
* @author Adam Kuśmierz <adam@kusmierz.be>
*/
class TokeninputType extends AbstractType
{
/**
* Available options to set
*
* @var array
*/
protected $_availableTokeninputOptions = array(
'method',
'queryParam',
'searchDelay',
'minChars',
'propertyToSearch',
'jsonContainer',
'crossDomain',
'prePopulate',
'hintText',
'noResultsText',
'searchingText',
'deleteText',
'animateDropdown',
'theme',
'resultsFormatter',
'tokenFormatter',
'tokenLimit',
'tokenDelimiter',
'preventDuplicates',
'tokenValue'
);

/**
* {@inheritdoc}
*/
public function buildForm(FormBuilder $builder, array $options)
{
if (true === empty($options['choice_list'])) {
$options['choice_list'] = new AjaxArrayChoiceList($options['choices'], $options['ajax']);
}

$builder
->appendClientTransformer(new ChoiceToJsonTransformer(
$options['choice_list'],
$options['widget'],
$options['multiple'],
$options['ajax']
))
->setAttribute('choice_list', $options['choice_list'])
->setAttribute('widget', $options['widget'])
->setAttribute('route_name', $options['route_name']);

foreach ($this->_availableTokeninputOptions as $option) {
if (isset($options[$option])) {
$builder->setAttribute($option, $options[$option]);
}
}
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form)
{
$datas = json_decode($form->getClientData(), true);
$value = '';

if (false === empty($datas)) {
if (true === $form->getAttribute('multiple')) {
foreach ($datas as $data) {
$value .= $data['label'] . ', ';
}
} else {
$value = $datas['label'];
}
}

$view
->set('tokeninput_value', $value)
->set('route_name', $form->getAttribute('route_name'));

foreach ($this->_availableTokeninputOptions as $option) {
if ($form->hasAttribute($option)) {
$view->set($option, $form->getAttribute($option));
}
}
}

/**
* {@inheritdoc}
*/
public function getDefaultOptions(array $options)
{
$defaultOptions = array(
'widget' => 'choice',
'route_name' => null,
'ajax' => false,
'multiple' => true,
'queryParam' => 'term',
'preventDuplicates' => true,
'tokenValue' => 'value',
'propertyToSearch' => 'label',
'theme' => 'facebook'
);

if (false === empty($options['route_name'])) {
$options['ajax'] = true;
}

$options = array_replace($defaultOptions, $options);

return $options;
}

/**
* {@inheritdoc}
*/
public function getAllowedOptionValues(array $options)
{
return array(
'widget' => array(
'choice',
'language',
'country',
'locale',
'entity',
'document',
'model',
)
);
}

/**
* {@inheritdoc}
*/
public function getParent(array $options)
{
if (true === in_array($options['widget'], array('entity', 'document', 'model'), true)) {
return 'genemu_ajax' . $options['widget'];
}

return $options['widget'];
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'genemu_jquerytokeninput';
}
}
3 changes: 3 additions & 0 deletions Resources/config/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<argument type="service" id="genemu.gd.captcha" />
<argument>%genemu.form.captcha.options%</argument>
</service>
<service id="genemu.form.core.type.plain" class="Genemu\Bundle\FormBundle\Form\Core\Type\PlainType">
<tag name="form.type" alias="genemu_plain" />
</service>
<service id="genemu.form.core.type.tinymce" class="Genemu\Bundle\FormBundle\Form\Core\Type\TinymceType">
<tag name="form.type" alias="genemu_tinymce" />
<argument>%genemu.form.tinymce.configs%</argument>
Expand Down
3 changes: 3 additions & 0 deletions Resources/config/jquery.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
<service id="genemu.form.jquery.type.slider" class="Genemu\Bundle\FormBundle\Form\JQuery\Type\SliderType">
<tag name="form.type" alias="genemu_jqueryslider" />
</service>
<service id="genemu.form.jquery.type.tokeninput" class="Genemu\Bundle\FormBundle\Form\JQuery\Type\TokeninputType">
<tag name="form.type" alias="genemu_jquerytokeninput" />
</service>
</services>

</container>
17 changes: 17 additions & 0 deletions Resources/views/Form/div_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,20 @@
</div>
{% endspaceless %}
{% endblock genemu_jqueryimage_widget %}

{% block genemu_jquerytokeninput_widget %}
{% spaceless %}
{{ block("hidden_widget") }}

{% set id = "tokeninput_" ~ id %}
{% set full_name = "tokeninput_" ~ full_name %}
{% set value = tokeninput_value %}
{{ block("field_widget") }}
{% endspaceless %}
{% endblock genemu_jquerytokeninput_widget %}

{% block genemu_plain_widget %}
<div {{ block('container_attributes') }}>
<p {{ block('widget_attributes') }}>{{ value|escape }}</p>
</div>
{% endblock %}
Loading

0 comments on commit 97fa633

Please sign in to comment.