mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 21:34:25 +00:00
Merge branch 'master' of git://github.com/Chill-project/CustomFields
Conflicts: Resources/config/services.yml
This commit is contained in:
commit
52b7045cc3
1
.gitignore
vendored
1
.gitignore
vendored
@ -18,6 +18,7 @@ composer.phar
|
||||
/nbproject/private/
|
||||
parameters.yml
|
||||
app/config/parameters.yml
|
||||
Tests/Fixtures/App/app/config/parameters.yml
|
||||
.DS_Store
|
||||
*bower_components
|
||||
bin/*
|
||||
|
@ -7,6 +7,7 @@ namespace Chill\CustomFieldsBundle\CustomFields;
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -16,6 +17,13 @@ use Symfony\Component\Form\FormBuilderInterface;
|
||||
class CustomFieldText implements CustomFieldInterface
|
||||
{
|
||||
|
||||
private $requestStack;
|
||||
|
||||
public function __construct(RequestStack $requestStack)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
const MAX_LENGTH = 'maxLength';
|
||||
|
||||
/**
|
||||
@ -33,7 +41,7 @@ class CustomFieldText implements CustomFieldInterface
|
||||
: 'textarea';
|
||||
|
||||
$builder->add($customField->getSlug(), $type, array(
|
||||
'label' => $customField->getLabel()
|
||||
'label' => $customField->getName()[$this->requestStack->getCurrentRequest()->getLocale()]
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -12,10 +12,6 @@ class CustomField
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $label;
|
||||
|
||||
private $slug;
|
||||
|
||||
@ -45,11 +41,6 @@ class CustomField
|
||||
*/
|
||||
private $ordering;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $relation = 1;
|
||||
|
||||
const ONE_TO_ONE = 1;
|
||||
const ONE_TO_MANY = 2;
|
||||
@ -74,38 +65,11 @@ class CustomField
|
||||
return $this->slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set label
|
||||
*
|
||||
* @param string $label
|
||||
*
|
||||
* @return CustomField
|
||||
*/
|
||||
public function setLabel($label)
|
||||
{
|
||||
$this->label = $label;
|
||||
|
||||
if ($this->slug === NULL) {
|
||||
$this->slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $label);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get label
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLabel()
|
||||
{
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set type
|
||||
@ -130,18 +94,6 @@ class CustomField
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
function getRelation()
|
||||
{
|
||||
return $this->relation;
|
||||
}
|
||||
|
||||
function setRelation($relation)
|
||||
{
|
||||
$this->relation = $relation;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@ -254,29 +206,6 @@ class CustomField
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set customFieldGroup
|
||||
*
|
||||
* @param \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup $customFieldGroup
|
||||
*
|
||||
* @return CustomField
|
||||
*/
|
||||
public function setCustomFieldGroup(\Chill\CustomFieldsBundle\Entity\CustomFieldsGroup $customFieldGroup = null)
|
||||
{
|
||||
$this->customFieldGroup = $customFieldGroup;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customFieldGroup
|
||||
*
|
||||
* @return \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup
|
||||
*/
|
||||
public function getCustomFieldGroup()
|
||||
{
|
||||
return $this->customFieldGroup;
|
||||
}
|
||||
|
||||
public function setSlug($slug)
|
||||
{
|
||||
|
@ -18,6 +18,7 @@ use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Chill\CustomFieldsBundle\Form\AdressType;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
|
||||
class CustomFieldType extends AbstractType
|
||||
{
|
||||
@ -44,68 +45,19 @@ class CustomFieldType extends AbstractType
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$customFields = $this->om
|
||||
->getRepository('ChillCustomFieldsBundle:CustomField')
|
||||
->findAll();
|
||||
|
||||
foreach ($customFields as $cf) {
|
||||
|
||||
//$builder->add(
|
||||
//$builder->create(
|
||||
//$cf->getSlug(),
|
||||
$this->customFieldCompiler
|
||||
->getCustomFieldByType($cf->getType())
|
||||
->buildForm($builder, $cf);
|
||||
/* )
|
||||
->addModelTransformer(new CustomFieldDataTransformer(
|
||||
$this->customFieldCompiler
|
||||
->getCustomFieldByType($cf->getType()),
|
||||
$cf)
|
||||
)*/
|
||||
//);
|
||||
|
||||
// if($cf->getType() === 'ManyToOne(Adress)') {
|
||||
// $builder->add($cf->getLabel(), 'entity', array(
|
||||
// 'class' => 'ChillCustomFieldsBundle:Adress',
|
||||
// 'property' => 'data'
|
||||
// ));
|
||||
// } else if ($cf->getType() === 'ManyToOnePersist(Adress)') {
|
||||
// $builder->add($cf->getLabel(), new AdressType());
|
||||
// } else if($cf->getType() === 'ManyToMany(Adress)') {
|
||||
//
|
||||
// $adress = $this->om
|
||||
// ->getRepository('ChillCustomFieldsBundle:Adress')
|
||||
// ->findAll();
|
||||
//
|
||||
// $adressId = array_map(
|
||||
// function($e) { return $e->getId(); },
|
||||
// $adress);
|
||||
//
|
||||
// $adressLabel = array_map(
|
||||
// function($e) { return (string) $e; },
|
||||
// $adress);
|
||||
//
|
||||
// $addressChoices = array_combine($adressId, $adressLabel);
|
||||
//
|
||||
//
|
||||
// $builder->add($cf->getLabel(), 'choice', array(
|
||||
// 'choices' => $addressChoices,
|
||||
// 'multiple' => true
|
||||
// ));
|
||||
// }
|
||||
// else {
|
||||
// $builder->add($cf->getLabel(), $cf->getType());
|
||||
// }
|
||||
foreach ($options['group']->getCustomFields() as $cf) {
|
||||
$this->customFieldCompiler
|
||||
->getCustomFieldByType($cf->getType())
|
||||
->buildForm($builder, $cf);
|
||||
}
|
||||
|
||||
//$builder->addViewTransformer(new JsonCustomFieldToArrayTransformer($this->om));
|
||||
}
|
||||
|
||||
public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver
|
||||
//->addAllowedTypes(array('context' => 'string'))
|
||||
//->setRequired(array('context'))
|
||||
->setRequired(array('group'))
|
||||
->addAllowedTypes(array('group' =>
|
||||
array('Chill\CustomFieldsBundle\Entity\CustomFieldsGroup')))
|
||||
;
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,6 @@ services:
|
||||
class: Chill\CustomFieldsBundle\Form\CustomFieldType
|
||||
arguments:
|
||||
- "@chill.custom_field.provider"
|
||||
|
||||
tags:
|
||||
- { name: 'form.type', alias: 'custom_field_choice' }
|
||||
|
||||
@ -38,6 +37,8 @@ services:
|
||||
|
||||
chill.custom_field.text:
|
||||
class: Chill\CustomFieldsBundle\CustomFields\CustomFieldText
|
||||
arguments:
|
||||
- "@request_stack"
|
||||
tags:
|
||||
- { name: 'chill.custom_field', type: 'text' }
|
||||
|
||||
|
86
Tests/CustomFieldTestHelper.php
Normal file
86
Tests/CustomFieldTestHelper.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2014 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Tests;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
|
||||
/**
|
||||
* Give useful method to prepare tests regarding custom fields
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class CustomFieldTestHelper
|
||||
{
|
||||
/**
|
||||
* Prepare a crawler containing the rendering of a customField
|
||||
*
|
||||
* @internal This method will mock a customFieldGroup containing $field, and
|
||||
* rendering the customfield, using Type\CustomFieldType, to a simple form row
|
||||
*
|
||||
* @param CustomField $field
|
||||
* @param KernelTestCase $testCase
|
||||
* @param KernelInterface $kernel
|
||||
* @param type $locale
|
||||
* @return Crawler
|
||||
*/
|
||||
public static function getCrawlerForField(CustomField $field, KernelTestCase $testCase, KernelInterface $kernel, $locale = 'en')
|
||||
{
|
||||
//check a kernel is accessible
|
||||
|
||||
|
||||
$customFieldsGroup = $testCase->getMock('Chill\CustomFieldsBundle\Entity\CustomFieldsGroup');
|
||||
$customFieldsGroup->expects($testCase->once())
|
||||
->method('getCustomFields')
|
||||
->will($testCase->returnValue(array($field)))
|
||||
;
|
||||
|
||||
$request = $testCase->getMock('Symfony\Component\HttpFoundation\Request');
|
||||
$request->expects($testCase->any())
|
||||
->method('getLocale')
|
||||
->will($testCase->returnValue($locale))
|
||||
;
|
||||
$kernel->getContainer()->get('request_stack')->push($request);
|
||||
|
||||
$builder = $kernel->getContainer()->get('form.factory')->createBuilder();
|
||||
$form = $builder->add('tested', 'custom_field',
|
||||
array('group' => $customFieldsGroup))
|
||||
->getForm()
|
||||
;
|
||||
|
||||
$kernel->getContainer()->get('twig.loader')
|
||||
->addPath(__DIR__.'/Fixtures/App/app/Resources/views/',
|
||||
$namespace = 'test');
|
||||
$content = $kernel
|
||||
->getContainer()->get('templating')
|
||||
->render('@test/CustomField/simple_form_render.html.twig', array(
|
||||
'form' => $form->createView(),
|
||||
'inputKeys' => array('tested')
|
||||
));
|
||||
|
||||
$crawler = new Crawler();
|
||||
$crawler->addHtmlContent($content);
|
||||
|
||||
return $crawler;
|
||||
}
|
||||
}
|
102
Tests/CustomFields/CustomFieldsTextTest.php
Normal file
102
Tests/CustomFields/CustomFieldsTextTest.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2014 Champs-Libres.coop
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Tests;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldText;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\DomCrawler\Crawler;
|
||||
use Chill\CustomFieldsBundle\Tests\CustomFieldTestHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class CustomFieldsTextTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var \Chill\CustomFieldsBundle\Service\CustomFieldProvider
|
||||
*/
|
||||
private $customFieldProvider;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
static::bootKernel();
|
||||
$this->customFieldProvider = static::$kernel->getContainer()
|
||||
->get('chill.custom_field.provider');
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function testCustomFieldsTextExists()
|
||||
{
|
||||
$customField = $this->customFieldProvider->getCustomFieldByType('text');
|
||||
|
||||
$this->assertInstanceOf('Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface',
|
||||
$customField);
|
||||
$this->assertInstanceOf('Chill\CustomFieldsBundle\CustomFields\CustomFieldText',
|
||||
$customField);
|
||||
}
|
||||
|
||||
public function testPublicFormRenderingLengthLessThan256()
|
||||
{
|
||||
$customField = new CustomField();
|
||||
$customField->setType('text')
|
||||
->setOptions(array(CustomFieldText::MAX_LENGTH => 255))
|
||||
->setSlug('slug')
|
||||
->setOrdering(10)
|
||||
->setActive(true)
|
||||
->setName(array('en' => 'my label'))
|
||||
;
|
||||
|
||||
$crawler = CustomFieldTestHelper::getCrawlerForField($customField, $this, static::$kernel);
|
||||
|
||||
$this->assertCount(1, $crawler->filter("input[type=text]"));
|
||||
$this->assertCount(1, $crawler->filter("label:contains('my label')"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function testPublicFormRenderingLengthMoreThan25()
|
||||
{
|
||||
$customField = new CustomField();
|
||||
$customField->setType('text')
|
||||
->setOptions(array(CustomFieldText::MAX_LENGTH => 256))
|
||||
->setSlug('slug')
|
||||
->setOrdering(10)
|
||||
->setActive(true)
|
||||
->setName(array('en' => 'my label'))
|
||||
;
|
||||
|
||||
$crawler = CustomFieldTestHelper::getCrawlerForField($customField, $this, static::$kernel);
|
||||
|
||||
$this->assertCount(1, $crawler->filter("textarea"));
|
||||
$this->assertCount(1, $crawler->filter("label:contains('my label')"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -13,7 +13,8 @@ class AppKernel extends Kernel
|
||||
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
|
||||
new Symfony\Bundle\TwigBundle\TwigBundle(),
|
||||
new \Symfony\Bundle\AsseticBundle\AsseticBundle(),
|
||||
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle()
|
||||
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
|
||||
new \Chill\MainBundle\ChillMainBundle,
|
||||
#add here all the required bundle (some bundle are not required)
|
||||
);
|
||||
}
|
||||
|
@ -0,0 +1,3 @@
|
||||
{% for key in inputKeys %}
|
||||
{{ form_row(form[key]) }}
|
||||
{% endfor %}
|
@ -24,4 +24,7 @@ doctrine:
|
||||
charset: UTF8
|
||||
orm:
|
||||
auto_generate_proxy_classes: "%kernel.debug%"
|
||||
auto_mapping: true
|
||||
auto_mapping: true
|
||||
|
||||
chill_main:
|
||||
available_languages: [ fr, nl, en ]
|
@ -3,4 +3,5 @@ parameters:
|
||||
database_port: 5432
|
||||
database_name: test0
|
||||
database_user: postgres
|
||||
database_password: postgres
|
||||
database_password: postgres
|
||||
locale: fr
|
@ -3,4 +3,5 @@ parameters:
|
||||
database_port: 5434
|
||||
database_name: symfony
|
||||
database_user: symfony
|
||||
database_password: symfony
|
||||
database_password: symfony
|
||||
locale: fr
|
@ -26,7 +26,8 @@
|
||||
"symfony/swiftmailer-bundle": "~2.3",
|
||||
"symfony/monolog-bundle": "~2.4",
|
||||
"sensio/distribution-bundle": "~3.0",
|
||||
"sensio/framework-extra-bundle": "~3.0"
|
||||
"sensio/framework-extra-bundle": "~3.0",
|
||||
"chill-project/main": "*@dev"
|
||||
},
|
||||
"scripts": {
|
||||
"post-install-cmd": [
|
||||
|
1626
composer.lock
generated
1626
composer.lock
generated
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user