mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-29 19:13:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle;
|
||||
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
use Chill\CustomFieldsBundle\DependencyInjection\CustomFieldCompilerPass;
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
|
||||
class ChillCustomFieldsBundle extends Bundle
|
||||
{
|
||||
|
@@ -1,56 +1,48 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2014 Champs-Libres Coopérative <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Command;
|
||||
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Helper\Table;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use Symfony\Component\Yaml\Parser;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Symfony\Component\Yaml\Parser;
|
||||
|
||||
/**
|
||||
* Class for the command 'chill:custom_fields:populate_group' that
|
||||
* Create custom fields from a yml file
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @author Marc Ducobu <marc.ducobu@champs-libres.coop>
|
||||
* Create custom fields from a yml file.
|
||||
*/
|
||||
class CreateFieldsOnGroupCommand extends Command
|
||||
{
|
||||
const ARG_PATH = 'path';
|
||||
const ARG_DELETE = 'delete';
|
||||
public const ARG_DELETE = 'delete';
|
||||
|
||||
public const ARG_PATH = 'path';
|
||||
|
||||
private $availableLanguages;
|
||||
|
||||
/**
|
||||
* @var CustomFieldProvider
|
||||
*/
|
||||
private $customFieldProvider;
|
||||
|
||||
private $customizablesEntities;
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
@@ -61,15 +53,9 @@ class CreateFieldsOnGroupCommand extends Command
|
||||
*/
|
||||
private $validator;
|
||||
|
||||
private $availableLanguages;
|
||||
private $customizablesEntities;
|
||||
|
||||
/**
|
||||
* CreateFieldsOnGroupCommand constructor.
|
||||
*
|
||||
* @param CustomFieldProvider $customFieldProvider
|
||||
* @param EntityManager $entityManager
|
||||
* @param ValidatorInterface $validator
|
||||
* @param $availableLanguages
|
||||
* @param $customizablesEntities
|
||||
*/
|
||||
@@ -92,14 +78,21 @@ class CreateFieldsOnGroupCommand extends Command
|
||||
{
|
||||
$this->setName('chill:custom_fields:populate_group')
|
||||
->setDescription('Create custom fields from a yml file')
|
||||
->addArgument(self::ARG_PATH, InputOption::VALUE_REQUIRED,
|
||||
'Path to description file')
|
||||
->addOption(self::ARG_DELETE, null, InputOption::VALUE_NONE,
|
||||
'If set, delete existing fields');
|
||||
->addArgument(
|
||||
self::ARG_PATH,
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Path to description file'
|
||||
)
|
||||
->addOption(
|
||||
self::ARG_DELETE,
|
||||
null,
|
||||
InputOption::VALUE_NONE,
|
||||
'If set, delete existing fields'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the existing custom fields for a given customFieldGroup
|
||||
* Delete the existing custom fields for a given customFieldGroup.
|
||||
*
|
||||
* @param CustomFieldsGroup $customFieldsGroup : The custom field group
|
||||
*/
|
||||
@@ -113,9 +106,7 @@ class CreateFieldsOnGroupCommand extends Command
|
||||
}
|
||||
|
||||
/**
|
||||
* @param InputInterface $input
|
||||
* @param OutputInterface $output
|
||||
* @return int|null|void
|
||||
* @return int|void|null
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
@@ -139,108 +130,62 @@ class CreateFieldsOnGroupCommand extends Command
|
||||
$this->availableLanguages
|
||||
))
|
||||
->setRows($this->_prepareRows($customFieldsGroups))
|
||||
->render()
|
||||
;
|
||||
->render();
|
||||
|
||||
$question = new Question(
|
||||
"Enter the customfieldGroup's id on which the custom fields should be added: ");
|
||||
"Enter the customfieldGroup's id on which the custom fields should be added: "
|
||||
);
|
||||
$question->setNormalizer(
|
||||
function($answer) use ($customFieldsGroups) {
|
||||
function ($answer) use ($customFieldsGroups) {
|
||||
foreach ($customFieldsGroups as $customFieldsGroup) {
|
||||
if ($answer == $customFieldsGroup->getId()) {
|
||||
if ($customFieldsGroup->getId() == $answer) {
|
||||
return $customFieldsGroup;
|
||||
}
|
||||
}
|
||||
throw new \RuntimeException('The id does not match an existing CustomFieldsGroup');
|
||||
|
||||
throw new RuntimeException('The id does not match an existing CustomFieldsGroup');
|
||||
}
|
||||
);
|
||||
$customFieldsGroup = $helper->ask($input, $output, $question);
|
||||
|
||||
|
||||
if ($input->getOption(self::ARG_DELETE)) {
|
||||
$this->deleteFieldsForCFGroup($customFieldsGroup);
|
||||
}
|
||||
|
||||
$fieldsInput = $this->_parse($input->getArgument(self::ARG_PATH),
|
||||
$output);
|
||||
$fieldsInput = $this->_parse(
|
||||
$input->getArgument(self::ARG_PATH),
|
||||
$output
|
||||
);
|
||||
|
||||
$fields = $this->_addFields($customFieldsGroup, $fieldsInput, $output);
|
||||
}
|
||||
|
||||
private function _prepareRows ($customFieldsGroups)
|
||||
{
|
||||
$rows = array();
|
||||
$languages = $this->availableLanguages;
|
||||
//gather entitites and create an array to access them easily
|
||||
$customizableEntities = array();
|
||||
foreach ($this->customizablesEntities as $entry) {
|
||||
$customizableEntities[$entry['class']] = $entry['name'];
|
||||
}
|
||||
|
||||
array_walk($customFieldsGroups,
|
||||
function(CustomFieldsGroup $customFieldGroup, $key)
|
||||
use ($languages, &$rows, $customizableEntities) {
|
||||
//set id and entity
|
||||
$row = array(
|
||||
$customFieldGroup->getId(),
|
||||
$customizableEntities[$customFieldGroup->getEntity()]
|
||||
);
|
||||
|
||||
foreach ($languages as $lang) {
|
||||
//todo replace with service to find lang when available
|
||||
$row[] = (isset($customFieldGroup->getName()[$lang])) ?
|
||||
$customFieldGroup->getName()[$lang] :
|
||||
'Not available in this language';
|
||||
}
|
||||
$rows[] = $row;
|
||||
}
|
||||
);
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
private function _parse($path, OutputInterface $output)
|
||||
{
|
||||
$parser = new Parser();
|
||||
|
||||
if (!file_exists($path)) {
|
||||
throw new \RuntimeException("file does not exist");
|
||||
}
|
||||
|
||||
try {
|
||||
$values = $parser->parse(file_get_contents($path));
|
||||
} catch (ParseException $ex) {
|
||||
throw new \RuntimeException("The yaml file is not valid", 0, $ex);
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
private function _addFields(CustomFieldsGroup $group, $values, OutputInterface $output)
|
||||
{
|
||||
|
||||
$em = $this->entityManager;
|
||||
|
||||
$languages = $this->availableLanguages;
|
||||
|
||||
foreach($values['fields'] as $slug => $field) {
|
||||
foreach ($values['fields'] as $slug => $field) {
|
||||
//check the cf type exists
|
||||
$cfType = $this->customFieldProvider->getCustomFieldByType($field['type']);
|
||||
if ($cfType === NULL) {
|
||||
throw new \RuntimeException('the type '.$field['type'].' '
|
||||
|
||||
if (null === $cfType) {
|
||||
throw new RuntimeException('the type ' . $field['type'] . ' '
|
||||
. 'does not exists');
|
||||
}
|
||||
|
||||
$cf = new CustomField();
|
||||
$cf->setSlug($slug)
|
||||
->setName($field['name'])
|
||||
->setOptions(isset($field['options']) ? $field['options'] : array() )
|
||||
->setOptions($field['options'] ?? [])
|
||||
->setOrdering($field['ordering'])
|
||||
->setType($field['type'])
|
||||
->setCustomFieldsGroup($group);
|
||||
|
||||
//add to table
|
||||
$names = array();
|
||||
$names = [];
|
||||
|
||||
foreach ($languages as $lang) {
|
||||
//todo replace with service to find lang when available
|
||||
$names[] = (isset($cf->getName()[$lang])) ?
|
||||
@@ -250,14 +195,64 @@ class CreateFieldsOnGroupCommand extends Command
|
||||
|
||||
if ($this->validator->validate($cf)) {
|
||||
$em->persist($cf);
|
||||
$output->writeln("<info>Adding Custom Field of type "
|
||||
.$cf->getType()."\t with slug ".$cf->getSlug().
|
||||
"\t and names : ".implode($names, ', ')."</info>");
|
||||
$output->writeln('<info>Adding Custom Field of type '
|
||||
. $cf->getType() . "\t with slug " . $cf->getSlug() .
|
||||
"\t and names : " . implode($names, ', ') . '</info>');
|
||||
} else {
|
||||
throw new \RuntimeException("Error in field ".$slug);
|
||||
throw new RuntimeException('Error in field ' . $slug);
|
||||
}
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
private function _parse($path, OutputInterface $output)
|
||||
{
|
||||
$parser = new Parser();
|
||||
|
||||
if (!file_exists($path)) {
|
||||
throw new RuntimeException('file does not exist');
|
||||
}
|
||||
|
||||
try {
|
||||
$values = $parser->parse(file_get_contents($path));
|
||||
} catch (ParseException $ex) {
|
||||
throw new RuntimeException('The yaml file is not valid', 0, $ex);
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
private function _prepareRows($customFieldsGroups)
|
||||
{
|
||||
$rows = [];
|
||||
$languages = $this->availableLanguages;
|
||||
//gather entitites and create an array to access them easily
|
||||
$customizableEntities = [];
|
||||
|
||||
foreach ($this->customizablesEntities as $entry) {
|
||||
$customizableEntities[$entry['class']] = $entry['name'];
|
||||
}
|
||||
|
||||
array_walk(
|
||||
$customFieldsGroups,
|
||||
function (CustomFieldsGroup $customFieldGroup, $key) use ($languages, &$rows, $customizableEntities) {
|
||||
//set id and entity
|
||||
$row = [
|
||||
$customFieldGroup->getId(),
|
||||
$customizableEntities[$customFieldGroup->getEntity()],
|
||||
];
|
||||
|
||||
foreach ($languages as $lang) {
|
||||
//todo replace with service to find lang when available
|
||||
$row[] = (isset($customFieldGroup->getName()[$lang])) ?
|
||||
$customFieldGroup->getName()[$lang] :
|
||||
'Not available in this language';
|
||||
}
|
||||
$rows[] = $row;
|
||||
}
|
||||
);
|
||||
|
||||
return $rows;
|
||||
}
|
||||
}
|
||||
|
@@ -1,33 +1,19 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2015 Champs Libres <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Class AdminController
|
||||
* Controller for the custom fields configuration section (in admin section)
|
||||
*
|
||||
* @package Chill\CustomFieldsBundle\Controller
|
||||
* Controller for the custom fields configuration section (in admin section).
|
||||
*/
|
||||
class AdminController extends AbstractController
|
||||
{
|
||||
@@ -35,4 +21,4 @@ class AdminController extends AbstractController
|
||||
{
|
||||
return $this->render('ChillCustomFieldsBundle:Admin:layout.html.twig');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,26 +1,28 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\CustomFieldsBundle\Form\CustomFieldType;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Class CustomFieldController
|
||||
*
|
||||
* @package Chill\CustomFieldsBundle\Controller
|
||||
* Class CustomFieldController.
|
||||
*/
|
||||
class CustomFieldController extends AbstractController
|
||||
{
|
||||
|
||||
/**
|
||||
* Creates a new CustomField entity.
|
||||
*
|
||||
*/
|
||||
public function createAction(Request $request)
|
||||
{
|
||||
@@ -34,95 +36,27 @@ class CustomFieldController extends AbstractController
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->get('translator')
|
||||
->trans('The custom field has been created'));
|
||||
->trans('The custom field has been created'));
|
||||
|
||||
return $this->redirect($this->generateUrl('customfieldsgroup_show',
|
||||
array('id' => $entity->getCustomFieldsGroup()->getId())));
|
||||
return $this->redirect($this->generateUrl(
|
||||
'customfieldsgroup_show',
|
||||
['id' => $entity->getCustomFieldsGroup()->getId()]
|
||||
));
|
||||
}
|
||||
|
||||
$this->addFlash('error', $this->get('translator')
|
||||
->trans("The custom field form contains errors"));
|
||||
->trans('The custom field form contains errors'));
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:new.html.twig', array(
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:new.html.twig', [
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to create a CustomField entity.
|
||||
*
|
||||
* @param CustomField $entity The entity
|
||||
* @param string
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createCreateForm(CustomField $entity, $type)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldType::class, $entity, array(
|
||||
'action' => $this->generateUrl('customfield_create',
|
||||
array('type' => $type)),
|
||||
'method' => 'POST',
|
||||
'type' => $type,
|
||||
'group_widget' => ($entity->getCustomFieldsGroup()) ? 'hidden' :'entity'
|
||||
));
|
||||
|
||||
$form->add('submit', SubmitType::class, array('label' => 'Create'));
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a form to create a new CustomField entity.
|
||||
*
|
||||
*/
|
||||
public function newAction(Request $request)
|
||||
{
|
||||
$entity = new CustomField();
|
||||
|
||||
//add the custom field group if defined in URL
|
||||
$cfGroupId = $request->query->get('customFieldsGroup', null);
|
||||
|
||||
if ($cfGroupId !== null) {
|
||||
$cfGroup = $this->getDoctrine()->getManager()
|
||||
->getRepository(CustomFieldsGroup::class)
|
||||
->find($cfGroupId);
|
||||
if (!$cfGroup) {
|
||||
throw $this->createNotFoundException('CustomFieldsGroup with id '
|
||||
. $cfGroupId.' is not found !');
|
||||
}
|
||||
$entity->setCustomFieldsGroup($cfGroup);
|
||||
}
|
||||
|
||||
$form = $this->createCreateForm($entity, $request->query->get('type'));
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:new.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and displays a CustomField entity.
|
||||
*
|
||||
* @deprecated is not used since there is no link to show action
|
||||
*/
|
||||
public function showAction($id)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$entity = $em->getRepository(CustomField::class)->find($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find CustomField entity.');
|
||||
}
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:show.html.twig', array(
|
||||
'entity' => $entity, ));
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a form to edit an existing CustomField entity.
|
||||
*
|
||||
* @param mixed $id
|
||||
*/
|
||||
public function editAction($id)
|
||||
{
|
||||
@@ -136,35 +70,67 @@ class CustomFieldController extends AbstractController
|
||||
|
||||
$editForm = $this->createEditForm($entity, $entity->getType());
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:edit.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
));
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:edit.html.twig', [
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to edit a CustomField entity.
|
||||
*
|
||||
* @param CustomField $entity The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createEditForm(CustomField $entity, $type)
|
||||
* Displays a form to create a new CustomField entity.
|
||||
*/
|
||||
public function newAction(Request $request)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldType::class, $entity, array(
|
||||
'action' => $this->generateUrl('customfield_update', array('id' => $entity->getId())),
|
||||
'method' => 'PUT',
|
||||
'type' => $type,
|
||||
'group_widget' => 'hidden'
|
||||
));
|
||||
$entity = new CustomField();
|
||||
|
||||
$form->add('submit', SubmitType::class, array('label' => 'Update'));
|
||||
//add the custom field group if defined in URL
|
||||
$cfGroupId = $request->query->get('customFieldsGroup', null);
|
||||
|
||||
return $form;
|
||||
if (null !== $cfGroupId) {
|
||||
$cfGroup = $this->getDoctrine()->getManager()
|
||||
->getRepository(CustomFieldsGroup::class)
|
||||
->find($cfGroupId);
|
||||
|
||||
if (!$cfGroup) {
|
||||
throw $this->createNotFoundException('CustomFieldsGroup with id '
|
||||
. $cfGroupId . ' is not found !');
|
||||
}
|
||||
$entity->setCustomFieldsGroup($cfGroup);
|
||||
}
|
||||
|
||||
$form = $this->createCreateForm($entity, $request->query->get('type'));
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:new.html.twig', [
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and displays a CustomField entity.
|
||||
*
|
||||
* @deprecated is not used since there is no link to show action
|
||||
*
|
||||
* @param mixed $id
|
||||
*/
|
||||
public function showAction($id)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$entity = $em->getRepository(CustomField::class)->find($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find CustomField entity.');
|
||||
}
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:show.html.twig', [
|
||||
'entity' => $entity, ]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits an existing CustomField entity.
|
||||
*
|
||||
* @param mixed $id
|
||||
*/
|
||||
public function updateAction(Request $request, $id)
|
||||
{
|
||||
@@ -183,17 +149,65 @@ class CustomFieldController extends AbstractController
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->get('translator')
|
||||
->trans("The custom field has been updated"));
|
||||
->trans('The custom field has been updated'));
|
||||
|
||||
return $this->redirect($this->generateUrl('customfield_edit', array('id' => $id)));
|
||||
return $this->redirect($this->generateUrl('customfield_edit', ['id' => $id]));
|
||||
}
|
||||
|
||||
$this->addFlash('error', $this->get('translator')
|
||||
->trans("The custom field form contains errors"));
|
||||
->trans('The custom field form contains errors'));
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:edit.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
));
|
||||
return $this->render('ChillCustomFieldsBundle:CustomField:edit.html.twig', [
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to create a CustomField entity.
|
||||
*
|
||||
* @param CustomField $entity The entity
|
||||
* @param string
|
||||
* @param mixed $type
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createCreateForm(CustomField $entity, $type)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldType::class, $entity, [
|
||||
'action' => $this->generateUrl(
|
||||
'customfield_create',
|
||||
['type' => $type]
|
||||
),
|
||||
'method' => 'POST',
|
||||
'type' => $type,
|
||||
'group_widget' => ($entity->getCustomFieldsGroup()) ? 'hidden' : 'entity',
|
||||
]);
|
||||
|
||||
$form->add('submit', SubmitType::class, ['label' => 'Create']);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to edit a CustomField entity.
|
||||
*
|
||||
* @param CustomField $entity The entity
|
||||
* @param mixed $type
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createEditForm(CustomField $entity, $type)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldType::class, $entity, [
|
||||
'action' => $this->generateUrl('customfield_update', ['id' => $entity->getId()]),
|
||||
'method' => 'PUT',
|
||||
'type' => $type,
|
||||
'group_widget' => 'hidden',
|
||||
]);
|
||||
|
||||
$form->add('submit', SubmitType::class, ['label' => 'Update']);
|
||||
|
||||
return $form;
|
||||
}
|
||||
}
|
||||
|
@@ -1,47 +1,47 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Controller;
|
||||
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsDefaultGroup;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\CustomFieldsBundle\Form\CustomFieldsGroupType;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldsGroupToIdTransformer;
|
||||
use Chill\CustomFieldsBundle\Form\Type\CustomFieldType as FormTypeCustomField;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Doctrine\ORM\Query;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Doctrine\ORM\Query;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldsGroupToIdTransformer;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsDefaultGroup;
|
||||
use Chill\CustomFieldsBundle\Form\CustomFieldsGroupType;
|
||||
use Chill\CustomFieldsBundle\Form\CustomFieldType;
|
||||
use Chill\CustomFieldsBundle\Form\Type\CustomFieldType as FormTypeCustomField;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* Class CustomFieldsGroupController
|
||||
*
|
||||
* @package Chill\CustomFieldsBundle\Controller
|
||||
* Class CustomFieldsGroupController.
|
||||
*/
|
||||
class CustomFieldsGroupController extends AbstractController
|
||||
{
|
||||
|
||||
/**
|
||||
* @var CustomFieldProvider
|
||||
*/
|
||||
private $customfieldProvider;
|
||||
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
private $translator;
|
||||
|
||||
|
||||
/**
|
||||
* CustomFieldsGroupController constructor.
|
||||
*
|
||||
* @param CustomFieldProvider $customFieldProvider
|
||||
* @param TranslatorInterface $translator
|
||||
*/
|
||||
public function __construct(
|
||||
CustomFieldProvider $customFieldProvider,
|
||||
@@ -50,76 +50,9 @@ class CustomFieldsGroupController extends AbstractController
|
||||
$this->customfieldProvider = $customFieldProvider;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all CustomFieldsGroup entities.
|
||||
*
|
||||
*/
|
||||
public function indexAction()
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$cfGroups = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->findAll();
|
||||
$defaultGroups = $this->getDefaultGroupsId();
|
||||
|
||||
$makeDefaultFormViews = array();
|
||||
foreach ($cfGroups as $group) {
|
||||
if (!in_array($group->getId(), $defaultGroups)){
|
||||
$makeDefaultFormViews[$group->getId()] = $this->createMakeDefaultForm($group)->createView();
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:index.html.twig', array(
|
||||
'entities' => $cfGroups,
|
||||
'default_groups' => $defaultGroups,
|
||||
'make_default_forms' => $makeDefaultFormViews
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of CustomFieldsGroupId which are marked as default
|
||||
* for their entity
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function getDefaultGroupsId()
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$customFieldsGroupIds = $em->createQuery('SELECT g.id FROM '
|
||||
. 'ChillCustomFieldsBundle:CustomFieldsDefaultGroup d '
|
||||
. 'JOIN d.customFieldsGroup g')
|
||||
->getResult(Query::HYDRATE_SCALAR);
|
||||
|
||||
$result = array();
|
||||
foreach ($customFieldsGroupIds as $row) {
|
||||
$result[] = $row['id'];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* create a form to make the group default
|
||||
*
|
||||
* @param CustomFieldsGroup $group
|
||||
* @return \Symfony\Component\Form\Form
|
||||
*/
|
||||
private function createMakeDefaultForm(CustomFieldsGroup $group = null)
|
||||
{
|
||||
return $this->createFormBuilder($group, array(
|
||||
'method' => 'POST',
|
||||
'action' => $this->generateUrl('customfieldsgroup_makedefault')
|
||||
))
|
||||
->add('id', HiddenType::class)
|
||||
->add('submit', SubmitType::class, array('label' => 'Make default'))
|
||||
->getForm();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new CustomFieldsGroup entity.
|
||||
*
|
||||
*/
|
||||
public function createAction(Request $request)
|
||||
{
|
||||
@@ -133,101 +66,24 @@ class CustomFieldsGroupController extends AbstractController
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->translator
|
||||
->trans("The custom fields group has been created"));
|
||||
->trans('The custom fields group has been created'));
|
||||
|
||||
return $this->redirect($this->generateUrl('customfieldsgroup_show', array('id' => $entity->getId())));
|
||||
return $this->redirect($this->generateUrl('customfieldsgroup_show', ['id' => $entity->getId()]));
|
||||
}
|
||||
|
||||
$this->addFlash('error', $this->translator
|
||||
->trans("The custom fields group form contains errors"));
|
||||
->trans('The custom fields group form contains errors'));
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:new.html.twig', array(
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:new.html.twig', [
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to create a CustomFieldsGroup entity.
|
||||
*
|
||||
* @param CustomFieldsGroup $entity The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createCreateForm(CustomFieldsGroup $entity)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldsGroupType::class, $entity, array(
|
||||
'action' => $this->generateUrl('customfieldsgroup_create'),
|
||||
'method' => 'POST',
|
||||
));
|
||||
|
||||
$form->add('submit', SubmitType::class, array('label' => 'Create'));
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a form to create a new CustomFieldsGroup entity.
|
||||
*
|
||||
*/
|
||||
public function newAction()
|
||||
{
|
||||
$entity = new CustomFieldsGroup();
|
||||
$form = $this->createCreateForm($entity);
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:new.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and displays a CustomFieldsGroup entity.
|
||||
*
|
||||
*/
|
||||
public function showAction($id)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$entity = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.');
|
||||
}
|
||||
|
||||
$options = $this->getOptionsAvailable($entity->getEntity());
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:show.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'create_field_form' => $this->createCreateFieldForm($entity)->createView(),
|
||||
'options' => $options
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of available key option for custom fields group
|
||||
* on the given entity
|
||||
*
|
||||
* @param string $entity the entity to filter
|
||||
*/
|
||||
private function getOptionsAvailable($entity)
|
||||
{
|
||||
$options = $this->getParameter('chill_custom_fields.'
|
||||
. 'customizables_entities');
|
||||
|
||||
foreach($options as $key => $definition) {
|
||||
if ($definition['class'] == $entity) {
|
||||
foreach ($definition['options'] as $key => $value) {
|
||||
yield $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
// [$entity->getEntity()];
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a form to edit an existing CustomFieldsGroup entity.
|
||||
*
|
||||
* @param mixed $id
|
||||
*/
|
||||
public function editAction($id)
|
||||
{
|
||||
@@ -241,102 +97,42 @@ class CustomFieldsGroupController extends AbstractController
|
||||
|
||||
$editForm = $this->createEditForm($entity);
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:edit.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
));
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:edit.html.twig', [
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to edit a CustomFieldsGroup entity.
|
||||
*
|
||||
* @param CustomFieldsGroup $entity The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createEditForm(CustomFieldsGroup $entity)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldsGroupType::class, $entity, array(
|
||||
'action' => $this->generateUrl('customfieldsgroup_update', array('id' => $entity->getId())),
|
||||
'method' => 'PUT',
|
||||
));
|
||||
$form->add('submit', SubmitType::class, array('label' => 'Update'));
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
private function createCreateFieldForm(CustomFieldsGroup $customFieldsGroup)
|
||||
{
|
||||
|
||||
$fieldChoices = array();
|
||||
foreach ($this->customfieldProvider->getAllFields()
|
||||
as $key => $customType) {
|
||||
$fieldChoices[$key] = $customType->getName();
|
||||
}
|
||||
|
||||
$customfield = (new CustomField())
|
||||
->setCustomFieldsGroup($customFieldsGroup);
|
||||
|
||||
$builder = $this->get('form.factory')
|
||||
->createNamedBuilder(null, FormType::class, $customfield, array(
|
||||
'method' => 'GET',
|
||||
'action' => $this->generateUrl('customfield_new'),
|
||||
'csrf_protection' => false
|
||||
))
|
||||
->add('type', ChoiceType::class, array(
|
||||
'choices' => array_combine(array_values($fieldChoices),array_keys($fieldChoices)),
|
||||
))
|
||||
->add('customFieldsGroup', HiddenType::class)
|
||||
->add('submit', SubmitType::class);
|
||||
$builder->get('customFieldsGroup')
|
||||
->addViewTransformer(new CustomFieldsGroupToIdTransformer(
|
||||
$this->getDoctrine()->getManager()));
|
||||
|
||||
return $builder->getForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits an existing CustomFieldsGroup entity.
|
||||
*
|
||||
* Lists all CustomFieldsGroup entities.
|
||||
*/
|
||||
public function updateAction(Request $request, $id)
|
||||
public function indexAction()
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$entity = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($id);
|
||||
$cfGroups = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->findAll();
|
||||
$defaultGroups = $this->getDefaultGroupsId();
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.');
|
||||
$makeDefaultFormViews = [];
|
||||
|
||||
foreach ($cfGroups as $group) {
|
||||
if (!in_array($group->getId(), $defaultGroups)) {
|
||||
$makeDefaultFormViews[$group->getId()] = $this->createMakeDefaultForm($group)->createView();
|
||||
}
|
||||
}
|
||||
|
||||
$editForm = $this->createEditForm($entity);
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
if ($editForm->isValid()) {
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->translator
|
||||
->trans("The custom fields group has been updated"));
|
||||
|
||||
return $this->redirect($this->generateUrl('customfieldsgroup_edit', array('id' => $id)));
|
||||
}
|
||||
|
||||
$this->addFlash('error', $this->translator
|
||||
->trans("The custom fields group form contains errors"));
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:edit.html.twig', array(
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
|
||||
));
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:index.html.twig', [
|
||||
'entities' => $cfGroups,
|
||||
'default_groups' => $defaultGroups,
|
||||
'make_default_forms' => $makeDefaultFormViews,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the CustomField Group with id $cFGroupId as default
|
||||
* Set the CustomField Group with id $cFGroupId as default.
|
||||
*/
|
||||
public function makeDefaultAction(Request $request)
|
||||
{
|
||||
|
||||
$form = $this->createMakeDefaultForm(null);
|
||||
$form->handleRequest($request);
|
||||
|
||||
@@ -346,16 +142,16 @@ class CustomFieldsGroupController extends AbstractController
|
||||
|
||||
$cFGroup = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->findOneById($cFGroupId);
|
||||
|
||||
if(!$cFGroup) {
|
||||
if (!$cFGroup) {
|
||||
throw $this
|
||||
->createNotFoundException("customFieldsGroup not found with "
|
||||
. "id $cFGroupId");
|
||||
->createNotFoundException('customFieldsGroup not found with '
|
||||
. "id {$cFGroupId}");
|
||||
}
|
||||
|
||||
$cFDefaultGroup = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsDefaultGroup')
|
||||
->findOneByEntity($cFGroup->getEntity());
|
||||
|
||||
if($cFDefaultGroup) {
|
||||
if ($cFDefaultGroup) {
|
||||
$em->remove($cFDefaultGroup);
|
||||
$em->flush(); /*this is necessary, if not doctrine
|
||||
* will not remove old entity before adding a new one,
|
||||
@@ -372,11 +168,25 @@ class CustomFieldsGroupController extends AbstractController
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->translator
|
||||
->trans("The default custom fields group has been changed"));
|
||||
->trans('The default custom fields group has been changed'));
|
||||
|
||||
return $this->redirect($this->generateUrl('customfieldsgroup'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a form to create a new CustomFieldsGroup entity.
|
||||
*/
|
||||
public function newAction()
|
||||
{
|
||||
$entity = new CustomFieldsGroup();
|
||||
$form = $this->createCreateForm($entity);
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:new.html.twig', [
|
||||
'entity' => $entity,
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function render the customFieldsGroup as a form.
|
||||
*
|
||||
@@ -397,33 +207,223 @@ class CustomFieldsGroupController extends AbstractController
|
||||
throw $this->createNotFoundException('Unable to find CustomFieldsGroups entity.');
|
||||
}
|
||||
|
||||
$form = $this->createForm(FormTypeCustomField::class, null, array('group' => $entity));
|
||||
$form->add('submit_dump', SubmitType::class, array('label' => 'POST AND DUMP'));
|
||||
$form->add('submit_render', SubmitType::class, array('label' => 'POST AND RENDER'));
|
||||
$form = $this->createForm(FormTypeCustomField::class, null, ['group' => $entity]);
|
||||
$form->add('submit_dump', SubmitType::class, ['label' => 'POST AND DUMP']);
|
||||
$form->add('submit_render', SubmitType::class, ['label' => 'POST AND RENDER']);
|
||||
$form->handleRequest($request);
|
||||
|
||||
$this->get('twig.loader')
|
||||
->addPath(__DIR__.'/../Tests/Fixtures/App/app/Resources/views/',
|
||||
$namespace = 'test');
|
||||
->addPath(
|
||||
__DIR__ . '/../Tests/Fixtures/App/app/Resources/views/',
|
||||
$namespace = 'test'
|
||||
);
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
if ($form->get('submit_render')->isClicked()) {
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:render_for_test.html.twig', array(
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:render_for_test.html.twig', [
|
||||
'fields' => $form->getData(),
|
||||
'customFieldsGroup' => $entity
|
||||
));
|
||||
'customFieldsGroup' => $entity,
|
||||
]);
|
||||
}
|
||||
|
||||
//dump($form->getData());
|
||||
//dump(json_enccode($form->getData()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
return $this
|
||||
->render('@test/CustomField/simple_form_render.html.twig', array(
|
||||
'form' => $form->createView()
|
||||
));
|
||||
->render('@test/CustomField/simple_form_render.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and displays a CustomFieldsGroup entity.
|
||||
*
|
||||
* @param mixed $id
|
||||
*/
|
||||
public function showAction($id)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$entity = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.');
|
||||
}
|
||||
|
||||
$options = $this->getOptionsAvailable($entity->getEntity());
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:show.html.twig', [
|
||||
'entity' => $entity,
|
||||
'create_field_form' => $this->createCreateFieldForm($entity)->createView(),
|
||||
'options' => $options,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits an existing CustomFieldsGroup entity.
|
||||
*
|
||||
* @param mixed $id
|
||||
*/
|
||||
public function updateAction(Request $request, $id)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$entity = $em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')->find($id);
|
||||
|
||||
if (!$entity) {
|
||||
throw $this->createNotFoundException('Unable to find CustomFieldsGroup entity.');
|
||||
}
|
||||
|
||||
$editForm = $this->createEditForm($entity);
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
if ($editForm->isValid()) {
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->translator
|
||||
->trans('The custom fields group has been updated'));
|
||||
|
||||
return $this->redirect($this->generateUrl('customfieldsgroup_edit', ['id' => $id]));
|
||||
}
|
||||
|
||||
$this->addFlash('error', $this->translator
|
||||
->trans('The custom fields group form contains errors'));
|
||||
|
||||
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:edit.html.twig', [
|
||||
'entity' => $entity,
|
||||
'edit_form' => $editForm->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function createCreateFieldForm(CustomFieldsGroup $customFieldsGroup)
|
||||
{
|
||||
$fieldChoices = [];
|
||||
|
||||
foreach ($this->customfieldProvider->getAllFields()
|
||||
as $key => $customType) {
|
||||
$fieldChoices[$key] = $customType->getName();
|
||||
}
|
||||
|
||||
$customfield = (new CustomField())
|
||||
->setCustomFieldsGroup($customFieldsGroup);
|
||||
|
||||
$builder = $this->get('form.factory')
|
||||
->createNamedBuilder(null, FormType::class, $customfield, [
|
||||
'method' => 'GET',
|
||||
'action' => $this->generateUrl('customfield_new'),
|
||||
'csrf_protection' => false,
|
||||
])
|
||||
->add('type', ChoiceType::class, [
|
||||
'choices' => array_combine(array_values($fieldChoices), array_keys($fieldChoices)),
|
||||
])
|
||||
->add('customFieldsGroup', HiddenType::class)
|
||||
->add('submit', SubmitType::class);
|
||||
$builder->get('customFieldsGroup')
|
||||
->addViewTransformer(new CustomFieldsGroupToIdTransformer(
|
||||
$this->getDoctrine()->getManager()
|
||||
));
|
||||
|
||||
return $builder->getForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to create a CustomFieldsGroup entity.
|
||||
*
|
||||
* @param CustomFieldsGroup $entity The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createCreateForm(CustomFieldsGroup $entity)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldsGroupType::class, $entity, [
|
||||
'action' => $this->generateUrl('customfieldsgroup_create'),
|
||||
'method' => 'POST',
|
||||
]);
|
||||
|
||||
$form->add('submit', SubmitType::class, ['label' => 'Create']);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to edit a CustomFieldsGroup entity.
|
||||
*
|
||||
* @param CustomFieldsGroup $entity The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createEditForm(CustomFieldsGroup $entity)
|
||||
{
|
||||
$form = $this->createForm(CustomFieldsGroupType::class, $entity, [
|
||||
'action' => $this->generateUrl('customfieldsgroup_update', ['id' => $entity->getId()]),
|
||||
'method' => 'PUT',
|
||||
]);
|
||||
$form->add('submit', SubmitType::class, ['label' => 'Update']);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* create a form to make the group default.
|
||||
*
|
||||
* @param CustomFieldsGroup $group
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form
|
||||
*/
|
||||
private function createMakeDefaultForm(?CustomFieldsGroup $group = null)
|
||||
{
|
||||
return $this->createFormBuilder($group, [
|
||||
'method' => 'POST',
|
||||
'action' => $this->generateUrl('customfieldsgroup_makedefault'),
|
||||
])
|
||||
->add('id', HiddenType::class)
|
||||
->add('submit', SubmitType::class, ['label' => 'Make default'])
|
||||
->getForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of CustomFieldsGroupId which are marked as default
|
||||
* for their entity.
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function getDefaultGroupsId()
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$customFieldsGroupIds = $em->createQuery('SELECT g.id FROM '
|
||||
. 'ChillCustomFieldsBundle:CustomFieldsDefaultGroup d '
|
||||
. 'JOIN d.customFieldsGroup g')
|
||||
->getResult(Query::HYDRATE_SCALAR);
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($customFieldsGroupIds as $row) {
|
||||
$result[] = $row['id'];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of available key option for custom fields group
|
||||
* on the given entity.
|
||||
*
|
||||
* @param string $entity the entity to filter
|
||||
*/
|
||||
private function getOptionsAvailable($entity)
|
||||
{
|
||||
$options = $this->getParameter('chill_custom_fields.'
|
||||
. 'customizables_entities');
|
||||
|
||||
foreach ($options as $key => $definition) {
|
||||
if ($definition['class'] == $entity) {
|
||||
foreach ($definition['options'] as $key => $value) {
|
||||
yield $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
// [$entity->getEntity()];
|
||||
}
|
||||
}
|
||||
|
@@ -1,37 +1,20 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\CustomFields;
|
||||
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
abstract class AbstractCustomField implements CustomFieldInterface
|
||||
{
|
||||
public function isEmptyValue($value, CustomField $customField)
|
||||
{
|
||||
return (empty($value) and $value !== FALSE);
|
||||
return empty($value) and false !== $value;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,57 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\CustomFields;
|
||||
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
|
||||
use Chill\CustomFieldsBundle\Form\Type\ChoicesListType;
|
||||
use Chill\CustomFieldsBundle\Form\Type\ChoicesType;
|
||||
use Chill\CustomFieldsBundle\Form\Type\ChoiceWithOtherType;
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
|
||||
use Symfony\Bridge\Twig\TwigEngine;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use LogicException;
|
||||
use Symfony\Bridge\Twig\TwigEngine;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use function LogicException;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @author Marc Ducobu <marc@champs-libes.coop>
|
||||
*/
|
||||
class CustomFieldChoice extends AbstractCustomField
|
||||
{
|
||||
const ALLOW_OTHER = 'other';
|
||||
const OTHER_VALUE_LABEL = 'otherValueLabel';
|
||||
const MULTIPLE = 'multiple';
|
||||
const EXPANDED = 'expanded';
|
||||
const CHOICES = 'choices';
|
||||
public const ALLOW_OTHER = 'other';
|
||||
|
||||
public const CHOICES = 'choices';
|
||||
|
||||
public const EXPANDED = 'expanded';
|
||||
|
||||
public const MULTIPLE = 'multiple';
|
||||
|
||||
public const OTHER_VALUE_LABEL = 'otherValueLabel';
|
||||
|
||||
private $defaultLocales;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TwigEngine
|
||||
*/
|
||||
private $templating;
|
||||
@@ -60,48 +46,51 @@ class CustomFieldChoice extends AbstractCustomField
|
||||
* @var TranslatableStringHelper Helper that find the string in current locale from an array of translation
|
||||
*/
|
||||
private $translatableStringHelper;
|
||||
|
||||
|
||||
/**
|
||||
* CustomFieldChoice constructor.
|
||||
*
|
||||
* @param TranslatorInterface $translator
|
||||
* @param TwigEngine $templating
|
||||
* @param TranslatableStringHelper $translatableStringHelper
|
||||
*/
|
||||
public function __construct(
|
||||
TranslatorInterface $translator,
|
||||
TwigEngine $templating,
|
||||
TranslatableStringHelper $translatableStringHelper)
|
||||
TranslatableStringHelper $translatableStringHelper
|
||||
)
|
||||
{
|
||||
$this->defaultLocales = $translator->getFallbackLocales();
|
||||
$this->templating = $templating;
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
public function allowOtherChoice(CustomField $cf)
|
||||
{
|
||||
return $cf->getOptions()[self::ALLOW_OTHER];
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, CustomField $customField)
|
||||
{
|
||||
//prepare choices
|
||||
$choices = array();
|
||||
$choices = [];
|
||||
$customFieldOptions = $customField->getOptions();
|
||||
|
||||
foreach($customFieldOptions[self::CHOICES] as $persistedChoices) {
|
||||
if ($persistedChoices['active']){
|
||||
foreach ($customFieldOptions[self::CHOICES] as $persistedChoices) {
|
||||
if ($persistedChoices['active']) {
|
||||
$choices[$persistedChoices['slug']] = $this->translatableStringHelper->localize($persistedChoices['name']);
|
||||
}
|
||||
}
|
||||
|
||||
//prepare $options
|
||||
$options = array(
|
||||
$options = [
|
||||
'multiple' => $customFieldOptions[self::MULTIPLE],
|
||||
'choices' => array_combine(array_values($choices),array_keys($choices)),
|
||||
'choices' => array_combine(array_values($choices), array_keys($choices)),
|
||||
'required' => $customField->isRequired(),
|
||||
'label' => $this->translatableStringHelper->localize($customField->getName())
|
||||
);
|
||||
'label' => $this->translatableStringHelper->localize($customField->getName()),
|
||||
];
|
||||
|
||||
//if allow_other = true
|
||||
if ($customFieldOptions[self::ALLOW_OTHER] == true) {
|
||||
if (true == $customFieldOptions[self::ALLOW_OTHER]) {
|
||||
$otherValueLabel = null;
|
||||
if(array_key_exists(self::OTHER_VALUE_LABEL, $customFieldOptions)) {
|
||||
|
||||
if (array_key_exists(self::OTHER_VALUE_LABEL, $customFieldOptions)) {
|
||||
$otherValueLabel = $this->translatableStringHelper->localize(
|
||||
$customFieldOptions[self::OTHER_VALUE_LABEL]
|
||||
);
|
||||
@@ -113,10 +102,10 @@ class CustomFieldChoice extends AbstractCustomField
|
||||
$customField->getSlug(),
|
||||
ChoiceWithOtherType::class,
|
||||
$options,
|
||||
array('other_value_label'=> $otherValueLabel)
|
||||
)
|
||||
->addModelTransformer(new CustomFieldDataTransformer($this, $customField)));
|
||||
|
||||
['other_value_label' => $otherValueLabel]
|
||||
)
|
||||
->addModelTransformer(new CustomFieldDataTransformer($this, $customField))
|
||||
);
|
||||
} else { //if allow_other = false
|
||||
//we add the 'expanded' to options
|
||||
$options['expanded'] = $customFieldOptions[self::EXPANDED];
|
||||
@@ -131,41 +120,41 @@ class CustomFieldChoice extends AbstractCustomField
|
||||
public function buildOptionsForm(FormBuilderInterface $builder)
|
||||
{
|
||||
$builder
|
||||
->add(self::MULTIPLE, ChoiceType::class, array(
|
||||
->add(self::MULTIPLE, ChoiceType::class, [
|
||||
'expanded' => true,
|
||||
'multiple' => false,
|
||||
'choices' => array(
|
||||
'choices' => [
|
||||
'Multiple' => '1',
|
||||
'Unique' => '0'),
|
||||
'Unique' => '0', ],
|
||||
'empty_data' => '0',
|
||||
'label' => 'Multiplicity'
|
||||
))
|
||||
->add(self::EXPANDED, ChoiceType::class, array(
|
||||
'label' => 'Multiplicity',
|
||||
])
|
||||
->add(self::EXPANDED, ChoiceType::class, [
|
||||
'expanded' => true,
|
||||
'multiple' => false,
|
||||
'choices' => array(
|
||||
'choices' => [
|
||||
'Expanded' => '1',
|
||||
'Non expanded' => '0'),
|
||||
'Non expanded' => '0', ],
|
||||
'empty_data' => '0',
|
||||
'label' => 'Choice display'
|
||||
))
|
||||
->add(self::ALLOW_OTHER, ChoiceType::class, array(
|
||||
'label' => 'Choice display',
|
||||
])
|
||||
->add(self::ALLOW_OTHER, ChoiceType::class, [
|
||||
'label' => 'Allow other',
|
||||
'choices' => array(
|
||||
'choices' => [
|
||||
'No' => '0',
|
||||
'Yes' => '1'),
|
||||
'Yes' => '1', ],
|
||||
'empty_data' => '0',
|
||||
'expanded' => true,
|
||||
'multiple' => false
|
||||
))
|
||||
->add(self::OTHER_VALUE_LABEL, TranslatableStringFormType::class, array(
|
||||
'label' => 'Other value label (empty if use by default)'))
|
||||
->add(self::CHOICES, ChoicesType::class, array(
|
||||
'multiple' => false,
|
||||
])
|
||||
->add(self::OTHER_VALUE_LABEL, TranslatableStringFormType::class, [
|
||||
'label' => 'Other value label (empty if use by default)', ])
|
||||
->add(self::CHOICES, ChoicesType::class, [
|
||||
'entry_type' => ChoicesListType::class,
|
||||
'allow_add' => true
|
||||
));
|
||||
'allow_add' => true,
|
||||
]);
|
||||
|
||||
return $builder;
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function deserialize($serialized, CustomField $customField)
|
||||
@@ -175,12 +164,183 @@ class CustomFieldChoice extends AbstractCustomField
|
||||
|
||||
if ($options[self::MULTIPLE]) {
|
||||
return $this->deserializeToMultiple($serialized, $options[self::ALLOW_OTHER]);
|
||||
} else {
|
||||
return $this->deserializeToUnique($serialized, $options[self::ALLOW_OTHER]);
|
||||
}
|
||||
|
||||
return $this->deserializeToUnique($serialized, $options[self::ALLOW_OTHER]);
|
||||
|
||||
return $serialized;
|
||||
}
|
||||
|
||||
public function extractOtherValue(CustomField $cf, ?array $data = null)
|
||||
{
|
||||
return $data['_other'];
|
||||
}
|
||||
|
||||
public function getChoices(CustomField $cf)
|
||||
{
|
||||
if ($cf->getOptions()[self::MULTIPLE]) {
|
||||
$choices = [];
|
||||
|
||||
foreach ($cf->getOptions()[self::CHOICES] as $choice) {
|
||||
if (false === $choices['active']) {
|
||||
continue;
|
||||
}
|
||||
$choices[$choice['slug']] = $this->translatableStringHelper
|
||||
->localize($choice['name']);
|
||||
}
|
||||
|
||||
if ($this->allowOtherChoice($cf)) {
|
||||
$labels = $cf->getOptions()[self::OTHER_VALUE_LABEL];
|
||||
|
||||
if (!is_array($labels) or count($labels) === 0) {
|
||||
$labels['back'] = 'other value';
|
||||
}
|
||||
$choices['_other'] = $this->translatableStringHelper
|
||||
->localize($labels);
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
return [
|
||||
$cf->getSlug() => $this->translatableStringHelper->localize($cf->getName()),
|
||||
];
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'Choices';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the choice given in $choiceSlug is checked inside $data.
|
||||
*
|
||||
* Used in list exports.
|
||||
*
|
||||
* @param string $choiceSlug the slug of the choice we want to know if it was checked
|
||||
* @param array|string $data the data of the field
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isChecked(CustomField $cf, $choiceSlug, $data)
|
||||
{
|
||||
if (null === $data) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($cf->getOptions()[self::MULTIPLE]) {
|
||||
if ($cf->getOptions()[self::ALLOW_OTHER]) {
|
||||
return \in_array($choiceSlug, $this->deserialize($data, $cf)['_choices']);
|
||||
}
|
||||
|
||||
return \in_array($choiceSlug, $this->deserialize($data, $cf));
|
||||
}
|
||||
|
||||
if ($cf->getOptions()[self::ALLOW_OTHER]) {
|
||||
return $this->deserialize($data, $cf)['_choices'] === $choiceSlug;
|
||||
}
|
||||
|
||||
return $this->deserialize($data, $cf) === $choiceSlug;
|
||||
}
|
||||
|
||||
public function isEmptyValue($value, CustomField $customField)
|
||||
{
|
||||
if (null === $value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// if multiple choice OR multiple/single choice with other
|
||||
if (is_array($value)) {
|
||||
// if allow other
|
||||
if (array_key_exists('_choices', $value)) {
|
||||
if (null === $value['_choices']) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return empty($value['_choices']);
|
||||
} // we do not have 'allow other'
|
||||
|
||||
if (count($value) === 1) {
|
||||
return empty($value[0]);
|
||||
}
|
||||
|
||||
return empty($value);
|
||||
}
|
||||
|
||||
return empty($value);
|
||||
|
||||
throw LogicException('This case is not expected.');
|
||||
}
|
||||
|
||||
public function isMultiple(CustomField $cf)
|
||||
{
|
||||
return $cf->getOptions()[self::MULTIPLE];
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal this function is able to receive data whichever is the value of "other", "multiple"
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param mixed $documentType
|
||||
*
|
||||
* @return string html representation
|
||||
*/
|
||||
public function render($value, CustomField $customField, $documentType = 'html')
|
||||
{
|
||||
//extract the data. They are under a _choice key if they are stored with allow_other
|
||||
$data = (isset($value['_choices'])) ? $value['_choices'] : $value;
|
||||
$selected = (is_array($data)) ? $data : [$data];
|
||||
$choices = $customField->getOptions()[self::CHOICES];
|
||||
|
||||
if (in_array('_other', $selected)) {
|
||||
$choices[] = ['name' => $value['_other'], 'slug' => '_other'];
|
||||
}
|
||||
|
||||
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice.html.twig';
|
||||
|
||||
if ('csv' == $documentType) {
|
||||
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice.csv.twig';
|
||||
}
|
||||
|
||||
return $this->templating
|
||||
->render(
|
||||
$template,
|
||||
[
|
||||
'choices' => $choices,
|
||||
'selected' => $selected,
|
||||
'multiple' => $customField->getOptions()[self::MULTIPLE],
|
||||
'expanded' => $customField->getOptions()[self::EXPANDED],
|
||||
'locales' => $this->defaultLocales,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function serialize($value, CustomField $customField)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* deserialized the data from the database to a multiple
|
||||
* field.
|
||||
*
|
||||
* @param mixed $serialized
|
||||
* @param bool $allowOther
|
||||
*/
|
||||
private function deserializeToMultiple($serialized, $allowOther)
|
||||
{
|
||||
$value = $this->guessValue($serialized);
|
||||
|
||||
// set in an array : we want a multiple
|
||||
$fixedValue = is_array($value) ? $value : [$value];
|
||||
|
||||
if ($allowOther) {
|
||||
return $this->deserializeWithAllowOther($serialized, $fixedValue);
|
||||
}
|
||||
|
||||
return $fixedValue;
|
||||
}
|
||||
|
||||
private function deserializeToUnique($serialized, $allowOther)
|
||||
{
|
||||
$value = $this->guessValue($serialized);
|
||||
@@ -194,40 +354,19 @@ class CustomFieldChoice extends AbstractCustomField
|
||||
|
||||
if ($allowOther) {
|
||||
return $this->deserializeWithAllowOther($serialized, $fixedValue);
|
||||
} else {
|
||||
return $fixedValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* deserialized the data from the database to a multiple
|
||||
* field
|
||||
*
|
||||
* @param mixed $serialized
|
||||
* @param boolean $allowOther
|
||||
*/
|
||||
private function deserializeToMultiple($serialized, $allowOther)
|
||||
{
|
||||
$value = $this->guessValue($serialized);
|
||||
|
||||
// set in an array : we want a multiple
|
||||
$fixedValue = is_array($value) ? $value : array($value);
|
||||
|
||||
if ($allowOther) {
|
||||
return $this->deserializeWithAllowOther($serialized, $fixedValue);
|
||||
} else {
|
||||
return $fixedValue;
|
||||
}
|
||||
return $fixedValue;
|
||||
}
|
||||
|
||||
private function deserializeWithAllowOther($serialized, $value)
|
||||
{
|
||||
$existingOther = isset($serialized['_other']) ? $serialized['_other'] : '';
|
||||
$existingOther = $serialized['_other'] ?? '';
|
||||
|
||||
return array(
|
||||
return [
|
||||
'_other' => $existingOther,
|
||||
'_choices' => $value
|
||||
);
|
||||
'_choices' => $value,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -237,177 +376,27 @@ class CustomFieldChoice extends AbstractCustomField
|
||||
* **is not** the content of the _other field, but the `_other` string.
|
||||
*
|
||||
* @param array|string $value
|
||||
*
|
||||
* @throws LogicException if the case is not covered by this
|
||||
*
|
||||
* @return mixed
|
||||
* @throws \LogicException if the case is not covered by this
|
||||
*/
|
||||
private function guessValue($value)
|
||||
{
|
||||
if ($value === NULL) {
|
||||
return NULL;
|
||||
if (null === $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!is_array($value)) {
|
||||
return $value;
|
||||
} else {
|
||||
// we have a field with "allow other"
|
||||
if (array_key_exists('_choices', $value)) {
|
||||
return $value['_choices'];
|
||||
} else {
|
||||
// we have a field with "multiple"
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
throw \LogicException("This case is not expected.");
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'Choices';
|
||||
}
|
||||
|
||||
public function isEmptyValue($value, CustomField $customField)
|
||||
{
|
||||
if ($value === NULL) {
|
||||
return true;
|
||||
// we have a field with "allow other"
|
||||
if (array_key_exists('_choices', $value)) {
|
||||
return $value['_choices'];
|
||||
}
|
||||
|
||||
// if multiple choice OR multiple/single choice with other
|
||||
if (is_array($value))
|
||||
{
|
||||
// if allow other
|
||||
if (array_key_exists('_choices', $value)) {
|
||||
if ($value['_choices'] === NULL) {
|
||||
return true;
|
||||
}
|
||||
return empty($value['_choices']);
|
||||
} else { // we do not have 'allow other'
|
||||
if (count($value) === 1){
|
||||
return empty($value[0]);
|
||||
} else {
|
||||
return empty($value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return empty($value);
|
||||
}
|
||||
|
||||
throw \LogicException("This case is not expected.");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @internal this function is able to receive data whichever is the value of "other", "multiple"
|
||||
* @param mixed $value
|
||||
* @param CustomField $customField
|
||||
* @return string html representation
|
||||
*/
|
||||
public function render($value, CustomField $customField, $documentType = 'html')
|
||||
{
|
||||
//extract the data. They are under a _choice key if they are stored with allow_other
|
||||
$data = (isset($value['_choices'])) ? $value['_choices'] : $value;
|
||||
$selected = (is_array($data)) ? $data : array($data);
|
||||
$choices = $customField->getOptions()[self::CHOICES];
|
||||
|
||||
if (in_array('_other', $selected)){
|
||||
$choices[] = array('name' => $value['_other'], 'slug' => '_other');
|
||||
}
|
||||
|
||||
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice.html.twig';
|
||||
if($documentType == 'csv') {
|
||||
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice.csv.twig';
|
||||
}
|
||||
|
||||
return $this->templating
|
||||
->render($template,
|
||||
array(
|
||||
'choices' => $choices,
|
||||
'selected' => $selected,
|
||||
'multiple' => $customField->getOptions()[self::MULTIPLE],
|
||||
'expanded' => $customField->getOptions()[self::EXPANDED],
|
||||
'locales' => $this->defaultLocales
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function serialize($value, CustomField $customField)
|
||||
{
|
||||
// we have a field with "multiple"
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function getChoices(CustomField $cf)
|
||||
{
|
||||
if ($cf->getOptions()[self::MULTIPLE]) {
|
||||
$choices = array();
|
||||
|
||||
foreach ($cf->getOptions()[self::CHOICES] as $choice) {
|
||||
if ($choices['active'] === false) {
|
||||
continue;
|
||||
}
|
||||
$choices[$choice["slug"]] = $this->translatableStringHelper
|
||||
->localize($choice["name"]);
|
||||
}
|
||||
|
||||
if ($this->allowOtherChoice($cf)) {
|
||||
$labels = $cf->getOptions()[self::OTHER_VALUE_LABEL];
|
||||
if (!is_array($labels) or count($labels) === 0) {
|
||||
$labels['back'] = 'other value';
|
||||
}
|
||||
$choices['_other'] = $this->translatableStringHelper
|
||||
->localize($labels);
|
||||
}
|
||||
|
||||
return $choices;
|
||||
} else {
|
||||
return [
|
||||
$cf->getSlug() => $this->translatableStringHelper->localize($cf->getName())
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the choice given in $choiceSlug is checked inside $data.
|
||||
*
|
||||
* Used in list exports.
|
||||
*
|
||||
* @param CustomField $cf
|
||||
* @param string $choiceSlug the slug of the choice we want to know if it was checked
|
||||
* @param array|string $data the data of the field
|
||||
* @return boolean
|
||||
*/
|
||||
public function isChecked(CustomField $cf, $choiceSlug, $data)
|
||||
{
|
||||
if ($data === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($cf->getOptions()[self::MULTIPLE]) {
|
||||
if ($cf->getOptions()[self::ALLOW_OTHER]) {
|
||||
return \in_array($choiceSlug, $this->deserialize($data, $cf)['_choices']);
|
||||
} else {
|
||||
return \in_array($choiceSlug, $this->deserialize($data, $cf));
|
||||
}
|
||||
} else {
|
||||
if ($cf->getOptions()[self::ALLOW_OTHER]) {
|
||||
return $this->deserialize($data, $cf)['_choices'] === $choiceSlug;
|
||||
} else {
|
||||
return $this->deserialize($data, $cf) === $choiceSlug;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function isMultiple(CustomField $cf)
|
||||
{
|
||||
return $cf->getOptions()[self::MULTIPLE];
|
||||
}
|
||||
|
||||
public function allowOtherChoice(CustomField $cf)
|
||||
{
|
||||
return $cf->getOptions()[self::ALLOW_OTHER];
|
||||
}
|
||||
|
||||
public function extractOtherValue(CustomField $cf, array $data = null)
|
||||
{
|
||||
return $data['_other'];
|
||||
|
||||
throw LogicException('This case is not expected.');
|
||||
}
|
||||
}
|
||||
|
@@ -1,196 +1,112 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>, <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\CustomFields;
|
||||
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
|
||||
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
|
||||
use Symfony\Bundle\TwigBundle\TwigEngine;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
|
||||
use Chill\MainBundle\Form\Type\ChillDateType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Symfony\Bundle\TwigBundle\TwigEngine;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Validator\Constraints\Callback;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
|
||||
|
||||
/**
|
||||
* Create a custom date number.
|
||||
*
|
||||
*
|
||||
* The date may have a min and max value.
|
||||
*
|
||||
*
|
||||
* The date is stored as an unix timestamp.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class CustomFieldDate extends AbstractCustomField
|
||||
{
|
||||
public const DATE_FORMAT = DateTime::RFC3339;
|
||||
|
||||
public const FORMAT = 'format';
|
||||
|
||||
public const MAX = 'max';
|
||||
|
||||
/**
|
||||
* key for the minimal value of the field
|
||||
* key for the minimal value of the field.
|
||||
*/
|
||||
const MIN = 'min';
|
||||
const MAX = 'max';
|
||||
const FORMAT = 'format';
|
||||
const DATE_FORMAT = \DateTime::RFC3339;
|
||||
|
||||
public const MIN = 'min';
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TwigEngine
|
||||
*/
|
||||
private $templating = NULL;
|
||||
|
||||
private $templating;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
private $translatableStringHelper = NULL;
|
||||
|
||||
private $translatableStringHelper;
|
||||
|
||||
public function __construct(TwigEngine $templating, TranslatableStringHelper $translatableStringHelper)
|
||||
{
|
||||
$this->templating = $templating;
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, CustomField $customField)
|
||||
{
|
||||
$fieldOptions = $this->prepareFieldOptions($customField);
|
||||
|
||||
|
||||
$builder->add(
|
||||
$builder
|
||||
->create(
|
||||
$customField->getSlug(),
|
||||
ChillDateType::class,
|
||||
$fieldOptions)
|
||||
$customField->getSlug(),
|
||||
ChillDateType::class,
|
||||
$fieldOptions
|
||||
)
|
||||
->addModelTransformer(
|
||||
new CustomFieldDataTransformer($this, $customField)
|
||||
)
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* prepare the options'form field
|
||||
*
|
||||
* @param CustomField $customField
|
||||
* @param string $type
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function prepareFieldOptions(CustomField $customField)
|
||||
{
|
||||
$options = $customField->getOptions();
|
||||
|
||||
/**
|
||||
* @var mixed[] the formField options
|
||||
*/
|
||||
$fieldOptions = array();
|
||||
|
||||
// add required
|
||||
$fieldOptions['required'] = False;
|
||||
|
||||
//add label
|
||||
$fieldOptions['label'] = $this->translatableStringHelper->localize($customField->getName());
|
||||
|
||||
// add constraints if required
|
||||
if ($options[self::MIN] !== NULL) {
|
||||
$fieldOptions['constraints'][] = new Callback(
|
||||
function($timestamp, ExecutionContextInterface $context) use ($options) {
|
||||
if ($timestamp === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$value = \DateTime::createFromFormat(self::DATE_FORMAT, $timestamp);
|
||||
$after = new \DateTime($options[self::MIN]);
|
||||
|
||||
if ($value < $after) {
|
||||
$context
|
||||
->buildViolation('This date must be after or equal to %date%', [
|
||||
'%date%' => $after->format('d-m-Y')
|
||||
])
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
if ($options[self::MAX] !== NULL) {
|
||||
$fieldOptions['constraints'][] = new Callback(
|
||||
function($timestamp, ExecutionContextInterface $context) use ($options) {
|
||||
if ($timestamp === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$value = \DateTime::createFromFormat(self::DATE_FORMAT, $timestamp);
|
||||
$before = new \DateTime($options[self::MAX]);
|
||||
|
||||
if ($value > $before) {
|
||||
$context
|
||||
->buildViolation('This date must be before or equal to %date%', [
|
||||
'%date%' => $before->format('d-m-Y')
|
||||
])
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return $fieldOptions;
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function buildOptionsForm(FormBuilderInterface $builder)
|
||||
{
|
||||
$validatorFunction = function($value, ExecutionContextInterface $context) {
|
||||
$validatorFunction = function ($value, ExecutionContextInterface $context) {
|
||||
try {
|
||||
$date = new \DateTime($value);
|
||||
} catch (\Exception $e) {
|
||||
$date = new DateTime($value);
|
||||
} catch (Exception $e) {
|
||||
$context->buildViolation('The expression "%expression%" is invalid', [
|
||||
'%expression%' => $value
|
||||
'%expression%' => $value,
|
||||
])
|
||||
->addViolation()
|
||||
;
|
||||
->addViolation();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return $builder
|
||||
->add(self::MIN, TextType::class, array(
|
||||
'label' => 'Greater or equal than (expression like 1 day ago, 2 years ago, +1 month, today, tomorrow, or date with format YYYY-mm-dd)',
|
||||
'required' => false,
|
||||
'constraints' => [ new Callback($validatorFunction) ]
|
||||
))
|
||||
->add(self::MAX, TextType::class, array(
|
||||
'label' => 'Lesser or equal than (expression like 1 day ago, 2 years ago, +1 month, today, tomorrow, or date with format YYYY-mm-dd)',
|
||||
'required' => false,
|
||||
'constraints' => [ new Callback($validatorFunction) ]
|
||||
))
|
||||
->add(self::FORMAT, ChoiceType::class, [
|
||||
'label' => 'Format',
|
||||
'choices' => [
|
||||
'medium' => 'medium',
|
||||
'long' => 'long',
|
||||
'short' => 'short'
|
||||
]
|
||||
])
|
||||
;
|
||||
|
||||
->add(self::MIN, TextType::class, [
|
||||
'label' => 'Greater or equal than (expression like 1 day ago, 2 years ago, +1 month, today, tomorrow, or date with format YYYY-mm-dd)',
|
||||
'required' => false,
|
||||
'constraints' => [new Callback($validatorFunction)],
|
||||
])
|
||||
->add(self::MAX, TextType::class, [
|
||||
'label' => 'Lesser or equal than (expression like 1 day ago, 2 years ago, +1 month, today, tomorrow, or date with format YYYY-mm-dd)',
|
||||
'required' => false,
|
||||
'constraints' => [new Callback($validatorFunction)],
|
||||
])
|
||||
->add(self::FORMAT, ChoiceType::class, [
|
||||
'label' => 'Format',
|
||||
'choices' => [
|
||||
'medium' => 'medium',
|
||||
'long' => 'long',
|
||||
'short' => 'short',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function deserialize($serialized, CustomField $customField)
|
||||
@@ -198,8 +114,8 @@ class CustomFieldDate extends AbstractCustomField
|
||||
if (empty($serialized)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return \DateTime::createFromFormat(self::DATE_FORMAT, $serialized);
|
||||
|
||||
return DateTime::createFromFormat(self::DATE_FORMAT, $serialized);
|
||||
}
|
||||
|
||||
public function getName()
|
||||
@@ -212,30 +128,97 @@ class CustomFieldDate extends AbstractCustomField
|
||||
switch ($documentType) {
|
||||
case 'csv':
|
||||
$date = $this->deserialize($value, $customField);
|
||||
if (NULL === $date) {
|
||||
|
||||
if (null === $date) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return $date->format('Y-m-d');
|
||||
|
||||
default:
|
||||
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:date.'
|
||||
.$documentType.'.twig';
|
||||
. $documentType . '.twig';
|
||||
|
||||
return $this->templating
|
||||
->render($template, array(
|
||||
->render($template, [
|
||||
'value' => $this->deserialize($value, $customField),
|
||||
'format' => $customField->getOptions()[self::FORMAT]
|
||||
));
|
||||
'format' => $customField->getOptions()[self::FORMAT],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function serialize($date, CustomField $customField)
|
||||
{
|
||||
if ($date === null) {
|
||||
if (null === $date) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return $date->format(self::DATE_FORMAT);
|
||||
}
|
||||
|
||||
/**
|
||||
* prepare the options'form field.
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function prepareFieldOptions(CustomField $customField)
|
||||
{
|
||||
$options = $customField->getOptions();
|
||||
|
||||
/**
|
||||
* @var mixed[] the formField options
|
||||
*/
|
||||
$fieldOptions = [];
|
||||
|
||||
// add required
|
||||
$fieldOptions['required'] = false;
|
||||
|
||||
//add label
|
||||
$fieldOptions['label'] = $this->translatableStringHelper->localize($customField->getName());
|
||||
|
||||
// add constraints if required
|
||||
if (null !== $options[self::MIN]) {
|
||||
$fieldOptions['constraints'][] = new Callback(
|
||||
function ($timestamp, ExecutionContextInterface $context) use ($options) {
|
||||
if (null === $timestamp) {
|
||||
return;
|
||||
}
|
||||
|
||||
$value = DateTime::createFromFormat(self::DATE_FORMAT, $timestamp);
|
||||
$after = new DateTime($options[self::MIN]);
|
||||
|
||||
if ($value < $after) {
|
||||
$context
|
||||
->buildViolation('This date must be after or equal to %date%', [
|
||||
'%date%' => $after->format('d-m-Y'),
|
||||
])
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (null !== $options[self::MAX]) {
|
||||
$fieldOptions['constraints'][] = new Callback(
|
||||
function ($timestamp, ExecutionContextInterface $context) use ($options) {
|
||||
if (null === $timestamp) {
|
||||
return;
|
||||
}
|
||||
|
||||
$value = DateTime::createFromFormat(self::DATE_FORMAT, $timestamp);
|
||||
$before = new DateTime($options[self::MAX]);
|
||||
|
||||
if ($value > $before) {
|
||||
$context
|
||||
->buildViolation('This date must be before or equal to %date%', [
|
||||
'%date%' => $before->format('d-m-Y'),
|
||||
])
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return $fieldOptions;
|
||||
}
|
||||
}
|
||||
|
@@ -1,69 +1,74 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\CustomFields;
|
||||
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
interface CustomFieldInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Return a form type to edit the custom field. This form is shown to the
|
||||
* user.
|
||||
* Return a form type to edit the custom field. This form is shown to the
|
||||
* user.
|
||||
*
|
||||
* @param \Chill\CustomFieldsBundle\CustomField\FormBuilderInterface $builder
|
||||
* @param \Chill\CustomFieldsBundle\CustomField\CustomField $customField
|
||||
*
|
||||
* @return \Symfony\Component\Form\FormTypeInterface the form type
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, CustomField $customField);
|
||||
|
||||
/**
|
||||
* Transform the value into a format that can be stored in DB
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param \Chill\CustomFieldsBundle\CustomField\CustomField $customField
|
||||
* Return a formType which allow to edit option for the custom type.
|
||||
* This FormType is shown in admin.
|
||||
*
|
||||
* @param \Chill\CustomFieldsBundle\CustomField\FormBuilderInterface $builder
|
||||
*
|
||||
* @return \Symfony\Component\Form\FormTypeInterface|null the form type
|
||||
*/
|
||||
public function serialize($value, CustomField $customField);
|
||||
public function buildOptionsForm(FormBuilderInterface $builder);
|
||||
|
||||
/**
|
||||
* Transform the representation of the value, stored in db, into the
|
||||
* value which may be used in the process.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @param \Chill\CustomFieldsBundle\CustomField\CustomField $customField
|
||||
* @param mixed $serialized
|
||||
*/
|
||||
public function deserialize($serialized, CustomField $customField);
|
||||
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* Return if the value can be considered as empty.
|
||||
*
|
||||
* @param mixed $value the value passed throug the deserialize function
|
||||
*/
|
||||
public function isEmptyValue($value, CustomField $customField);
|
||||
|
||||
/**
|
||||
* Return a repsentation of the value of the CustomField.
|
||||
*
|
||||
*
|
||||
* @param mixed $value the raw value, **not deserialized** (= as stored in the db)
|
||||
* @param \Chill\CustomFieldsBundle\CustomField\CustomField $customField
|
||||
* @param mixed $documentType
|
||||
*
|
||||
* @return string an html representation of the value
|
||||
*/
|
||||
public function render($value, CustomField $customField, $documentType = 'html');
|
||||
|
||||
public function getName();
|
||||
|
||||
|
||||
/**
|
||||
* Return a formType which allow to edit option for the custom type.
|
||||
* This FormType is shown in admin
|
||||
*
|
||||
* @param \Chill\CustomFieldsBundle\CustomField\FormBuilderInterface $builder
|
||||
* @return \Symfony\Component\Form\FormTypeInterface|null the form type
|
||||
* Transform the value into a format that can be stored in DB.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param \Chill\CustomFieldsBundle\CustomField\CustomField $customField
|
||||
*/
|
||||
public function buildOptionsForm(FormBuilderInterface $builder);
|
||||
|
||||
/**
|
||||
* Return if the value can be considered as empty
|
||||
*
|
||||
* @param mixed $value the value passed throug the deserialize function
|
||||
* @param CustomField $customField
|
||||
*/
|
||||
public function isEmptyValue($value, CustomField $customField);
|
||||
public function serialize($value, CustomField $customField);
|
||||
}
|
||||
|
@@ -1,28 +1,36 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\CustomFieldsBundle\CustomFields;
|
||||
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\EntityRepository\CustomFieldLongChoice\OptionRepository;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option;
|
||||
use Chill\CustomFieldsBundle\EntityRepository\CustomFieldLongChoice\OptionRepository;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
|
||||
use Symfony\Bridge\Twig\TwigEngine;
|
||||
use Chill\MainBundle\Form\Type\Select2ChoiceType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use LogicException;
|
||||
use Symfony\Bridge\Twig\TwigEngine;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
class CustomFieldLongChoice extends AbstractCustomField
|
||||
{
|
||||
private OptionRepository $optionRepository;
|
||||
public const KEY = 'key';
|
||||
|
||||
private TranslatableStringHelper $translatableStringHelper;
|
||||
private OptionRepository $optionRepository;
|
||||
|
||||
private TwigEngine $templating;
|
||||
|
||||
public const KEY = 'key';
|
||||
private TranslatableStringHelper $translatableStringHelper;
|
||||
|
||||
public function __construct(
|
||||
OptionRepository $optionRepository,
|
||||
@@ -37,29 +45,32 @@ class CustomFieldLongChoice extends AbstractCustomField
|
||||
public function buildForm(FormBuilderInterface $builder, CustomField $customField)
|
||||
{
|
||||
$options = $customField->getOptions();
|
||||
$entries = $this->optionRepository->findFilteredByKey($options[self::KEY],
|
||||
false, true);
|
||||
$entries = $this->optionRepository->findFilteredByKey(
|
||||
$options[self::KEY],
|
||||
false,
|
||||
true
|
||||
);
|
||||
//create a local copy of translatable string helper
|
||||
$translatableStringHelper = $this->translatableStringHelper;
|
||||
$builder->add($customField->getSlug(), Select2ChoiceType::class, array(
|
||||
$builder->add($customField->getSlug(), Select2ChoiceType::class, [
|
||||
'choices' => $entries,
|
||||
'choice_label' => function(Option $option) use ($translatableStringHelper) {
|
||||
'choice_label' => function (Option $option) use ($translatableStringHelper) {
|
||||
return $translatableStringHelper->localize($option->getText());
|
||||
},
|
||||
'choice_value' => static fn (Option $key): ?int => $key === null ? null : $key->getId(),
|
||||
'choice_value' => static fn (Option $key): ?int => null === $key ? null : $key->getId(),
|
||||
'multiple' => false,
|
||||
'expanded' => false,
|
||||
'required' => $customField->isRequired(),
|
||||
'placeholder' => 'Choose a value',
|
||||
'group_by' => function(Option $option) use ($translatableStringHelper) {
|
||||
'group_by' => function (Option $option) use ($translatableStringHelper) {
|
||||
if ($option->hasParent()) {
|
||||
return $translatableStringHelper->localize($option->getParent()->getText());
|
||||
}
|
||||
|
||||
return $translatableStringHelper->localize($option->getText());
|
||||
},
|
||||
'label' => $translatableStringHelper->localize($customField->getName())
|
||||
));
|
||||
'label' => $translatableStringHelper->localize($customField->getName()),
|
||||
]);
|
||||
|
||||
$builder
|
||||
->get($customField->getSlug())
|
||||
@@ -70,25 +81,24 @@ class CustomFieldLongChoice extends AbstractCustomField
|
||||
{
|
||||
//create a selector between different keys
|
||||
$keys = $this->optionRepository->getKeys();
|
||||
$choices = array();
|
||||
$choices = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$choices[$key] = $key;
|
||||
}
|
||||
|
||||
return $builder->add(self::KEY, ChoiceType::class, array(
|
||||
'choices' => array_combine(array_values($choices),array_keys($choices)),
|
||||
return $builder->add(self::KEY, ChoiceType::class, [
|
||||
'choices' => array_combine(array_values($choices), array_keys($choices)),
|
||||
'label' => 'Options key',
|
||||
));
|
||||
|
||||
]);
|
||||
}
|
||||
|
||||
public function deserialize($serialized, \Chill\CustomFieldsBundle\Entity\CustomField $customField)
|
||||
public function deserialize($serialized, CustomField $customField)
|
||||
{
|
||||
if ($serialized === NULL) {
|
||||
return NULL;
|
||||
if (null === $serialized) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return $this->optionRepository->find($serialized);
|
||||
}
|
||||
|
||||
@@ -97,32 +107,31 @@ class CustomFieldLongChoice extends AbstractCustomField
|
||||
return 'Long choice field';
|
||||
}
|
||||
|
||||
public function render($value, \Chill\CustomFieldsBundle\Entity\CustomField $customField, $documentType = 'html')
|
||||
public function render($value, CustomField $customField, $documentType = 'html')
|
||||
{
|
||||
$option = $this->deserialize($value, $customField);
|
||||
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:choice_long.'
|
||||
.$documentType.'.twig';
|
||||
. $documentType . '.twig';
|
||||
|
||||
return $this->templating
|
||||
->render($template, array(
|
||||
'values' => $option === NULL ? array() : array($option)
|
||||
));
|
||||
->render($template, [
|
||||
'values' => null === $option ? [] : [$option],
|
||||
]);
|
||||
}
|
||||
|
||||
public function serialize($value, \Chill\CustomFieldsBundle\Entity\CustomField $customField)
|
||||
public function serialize($value, CustomField $customField)
|
||||
{
|
||||
if ($value === NULL) {
|
||||
return NULL;
|
||||
if (null === $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$value instanceof Option) {
|
||||
throw new \LogicException('the value should be an instance of '
|
||||
throw new LogicException('the value should be an instance of '
|
||||
. 'Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option, '
|
||||
. is_object($value) ? get_class($value) : gettype($value).' given');
|
||||
. is_object($value) ? get_class($value) : gettype($value) . ' given');
|
||||
}
|
||||
|
||||
// we place the id in array, to allow in the future multiple select
|
||||
return $value->getId();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,69 +1,52 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>, <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\CustomFields;
|
||||
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
|
||||
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Bundle\TwigBundle\TwigEngine;
|
||||
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Bundle\TwigBundle\TwigEngine;
|
||||
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
|
||||
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
|
||||
|
||||
/**
|
||||
* Create a custom field number.
|
||||
*
|
||||
* This number may have a min and max value, and a precision.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @author Marc Ducobu <marc@champs-libres.coop>
|
||||
*/
|
||||
class CustomFieldNumber extends AbstractCustomField
|
||||
{
|
||||
/**
|
||||
* key for the minimal value of the field
|
||||
*/
|
||||
const MIN = 'min';
|
||||
const MAX = 'max';
|
||||
const SCALE = 'scale';
|
||||
const POST_TEXT = 'post_text';
|
||||
public const MAX = 'max';
|
||||
|
||||
/**
|
||||
* key for the minimal value of the field.
|
||||
*/
|
||||
public const MIN = 'min';
|
||||
|
||||
public const POST_TEXT = 'post_text';
|
||||
|
||||
public const SCALE = 'scale';
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TwigEngine
|
||||
*/
|
||||
private $templating = NULL;
|
||||
private $templating;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
private $translatableStringHelper = NULL;
|
||||
private $translatableStringHelper;
|
||||
|
||||
public function __construct(TwigEngine $templating, TranslatableStringHelper $translatableStringHelper)
|
||||
{
|
||||
@@ -76,82 +59,38 @@ class CustomFieldNumber extends AbstractCustomField
|
||||
$options = $customField->getOptions();
|
||||
|
||||
//select the type depending to the SCALE
|
||||
$type = ($options[self::SCALE] === 0 or $options[self::SCALE] === NULL)?
|
||||
$type = (0 === $options[self::SCALE] or null === $options[self::SCALE]) ?
|
||||
IntegerType::class : NumberType::class;
|
||||
//'integer' : 'number';
|
||||
//'integer' : 'number';
|
||||
|
||||
$fieldOptions = $this->prepareFieldOptions($customField, $type);
|
||||
|
||||
$builder->add($customField->getSlug(), $type, $fieldOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* prepare the options'form field
|
||||
*
|
||||
* @param CustomField $customField
|
||||
* @param string $type
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function prepareFieldOptions(CustomField $customField, $type)
|
||||
{
|
||||
$options = $customField->getOptions();
|
||||
|
||||
/**
|
||||
* @var mixed[] the formField options
|
||||
*/
|
||||
$fieldOptions = array();
|
||||
|
||||
// add required
|
||||
$fieldOptions['required'] = False;
|
||||
|
||||
//add label
|
||||
$fieldOptions['label'] = $this->translatableStringHelper->localize($customField->getName());
|
||||
|
||||
// add constraints if required
|
||||
if ($options[self::MIN] !== NULL) {
|
||||
$fieldOptions['constraints'][] = new GreaterThanOrEqual(array('value' => $options[self::MIN]));
|
||||
}
|
||||
if ($options[self::MAX] !== NULL) {
|
||||
$fieldOptions['constraints'][] = new LessThanOrEqual(array('value' => $options[self::MAX]));
|
||||
}
|
||||
|
||||
// add precision to options if required
|
||||
if ($type === 'number') {
|
||||
$fieldOptions['scale'] = $options[self::SCALE];
|
||||
}
|
||||
|
||||
if (!empty($options[self::POST_TEXT])) {
|
||||
$fieldOptions['post_text'] = $options[self::POST_TEXT];
|
||||
}
|
||||
|
||||
return $fieldOptions;
|
||||
}
|
||||
|
||||
public function buildOptionsForm(FormBuilderInterface $builder)
|
||||
{
|
||||
return $builder
|
||||
->add(self::MIN, NumberType::class, array(
|
||||
'scale' => 2,
|
||||
'label' => 'Greater or equal than',
|
||||
'required' => false
|
||||
))
|
||||
->add(self::MAX, NumberType::class, array(
|
||||
'scale' => 2,
|
||||
'label' => 'Lesser or equal than',
|
||||
'required' => false
|
||||
))
|
||||
->add(self::SCALE, IntegerType::class, array(
|
||||
'scale' => 0,
|
||||
'label' => 'Precision',
|
||||
'constraints' => array(
|
||||
new GreaterThanOrEqual(array('value' => 0))
|
||||
)
|
||||
))
|
||||
->add(self::POST_TEXT, TextType::class, array(
|
||||
'label' => 'Text after the field'
|
||||
))
|
||||
;
|
||||
|
||||
->add(self::MIN, NumberType::class, [
|
||||
'scale' => 2,
|
||||
'label' => 'Greater or equal than',
|
||||
'required' => false,
|
||||
])
|
||||
->add(self::MAX, NumberType::class, [
|
||||
'scale' => 2,
|
||||
'label' => 'Lesser or equal than',
|
||||
'required' => false,
|
||||
])
|
||||
->add(self::SCALE, IntegerType::class, [
|
||||
'scale' => 0,
|
||||
'label' => 'Precision',
|
||||
'constraints' => [
|
||||
new GreaterThanOrEqual(['value' => 0]),
|
||||
],
|
||||
])
|
||||
->add(self::POST_TEXT, TextType::class, [
|
||||
'label' => 'Text after the field',
|
||||
]);
|
||||
}
|
||||
|
||||
public function deserialize($serialized, CustomField $customField)
|
||||
@@ -167,15 +106,15 @@ class CustomFieldNumber extends AbstractCustomField
|
||||
public function render($value, CustomField $customField, $documentType = 'html')
|
||||
{
|
||||
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:number.'
|
||||
.$documentType.'.twig';
|
||||
. $documentType . '.twig';
|
||||
$options = $customField->getOptions();
|
||||
|
||||
return $this->templating
|
||||
->render($template, array(
|
||||
->render($template, [
|
||||
'number' => $value,
|
||||
'scale' => $options[self::SCALE],
|
||||
'post' => $options[self::POST_TEXT]
|
||||
));
|
||||
'post' => $options[self::POST_TEXT],
|
||||
]);
|
||||
}
|
||||
|
||||
public function serialize($value, CustomField $customField)
|
||||
@@ -183,4 +122,46 @@ class CustomFieldNumber extends AbstractCustomField
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* prepare the options'form field.
|
||||
*
|
||||
* @param string $type
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function prepareFieldOptions(CustomField $customField, $type)
|
||||
{
|
||||
$options = $customField->getOptions();
|
||||
|
||||
/**
|
||||
* @var mixed[] the formField options
|
||||
*/
|
||||
$fieldOptions = [];
|
||||
|
||||
// add required
|
||||
$fieldOptions['required'] = false;
|
||||
|
||||
//add label
|
||||
$fieldOptions['label'] = $this->translatableStringHelper->localize($customField->getName());
|
||||
|
||||
// add constraints if required
|
||||
if (null !== $options[self::MIN]) {
|
||||
$fieldOptions['constraints'][] = new GreaterThanOrEqual(['value' => $options[self::MIN]]);
|
||||
}
|
||||
|
||||
if (null !== $options[self::MAX]) {
|
||||
$fieldOptions['constraints'][] = new LessThanOrEqual(['value' => $options[self::MAX]]);
|
||||
}
|
||||
|
||||
// add precision to options if required
|
||||
if ('number' === $type) {
|
||||
$fieldOptions['scale'] = $options[self::SCALE];
|
||||
}
|
||||
|
||||
if (!empty($options[self::POST_TEXT])) {
|
||||
$fieldOptions['post_text'] = $options[self::POST_TEXT];
|
||||
}
|
||||
|
||||
return $fieldOptions;
|
||||
}
|
||||
}
|
||||
|
@@ -1,49 +1,33 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>, <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\CustomFields;
|
||||
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Bundle\TwigBundle\TwigEngine;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Bundle\TwigBundle\TwigEngine;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
|
||||
/**
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @author Marc Ducobu <marc@champs-libres.coop>
|
||||
*/
|
||||
class CustomFieldText extends AbstractCustomField
|
||||
{
|
||||
public const MAX_LENGTH = 'maxLength';
|
||||
|
||||
public const MULTIPLE_CF_INLINE = 'multipleCFInline';
|
||||
|
||||
private $requestStack;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TwigEngine
|
||||
*/
|
||||
private $templating;
|
||||
@@ -53,62 +37,56 @@ class CustomFieldText extends AbstractCustomField
|
||||
*/
|
||||
private $translatableStringHelper;
|
||||
|
||||
|
||||
public function __construct(RequestStack $requestStack, TwigEngine $templating,
|
||||
TranslatableStringHelper $translatableStringHelper)
|
||||
public function __construct(
|
||||
RequestStack $requestStack,
|
||||
TwigEngine $templating,
|
||||
TranslatableStringHelper $translatableStringHelper
|
||||
)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->templating = $templating;
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
const MAX_LENGTH = 'maxLength';
|
||||
const MULTIPLE_CF_INLINE ='multipleCFInline';
|
||||
|
||||
/**
|
||||
* Create a form according to the maxLength option
|
||||
* Create a form according to the maxLength option.
|
||||
*
|
||||
* if maxLength < 256 THEN the form type is 'text'
|
||||
* if not, THEN the form type is textarea
|
||||
*
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param CustomField $customField
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, CustomField $customField)
|
||||
{
|
||||
$options = $customField->getOptions();
|
||||
$options = $customField->getOptions();
|
||||
|
||||
$type = ($options[self::MAX_LENGTH] < 256) ? TextType::class
|
||||
$type = (256 > $options[self::MAX_LENGTH]) ? TextType::class
|
||||
: TextareaType::class;
|
||||
|
||||
$attrArray = array();
|
||||
$attrArray = [];
|
||||
|
||||
if(array_key_exists(self::MULTIPLE_CF_INLINE, $options) and
|
||||
$options[self::MULTIPLE_CF_INLINE]) {
|
||||
if (array_key_exists(self::MULTIPLE_CF_INLINE, $options)
|
||||
and $options[self::MULTIPLE_CF_INLINE]) {
|
||||
$attrArray['class'] = 'multiple-cf-inline';
|
||||
}
|
||||
|
||||
$builder->add($customField->getSlug(), $type, array(
|
||||
$builder->add($customField->getSlug(), $type, [
|
||||
'label' => $this->translatableStringHelper->localize($customField->getName()),
|
||||
'required' => false,
|
||||
'attr' => $attrArray
|
||||
));
|
||||
'attr' => $attrArray,
|
||||
]);
|
||||
}
|
||||
|
||||
public function render($value, CustomField $customField, $documentType = 'html')
|
||||
public function buildOptionsForm(FormBuilderInterface $builder)
|
||||
{
|
||||
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:text.html.twig';
|
||||
if($documentType == 'csv') {
|
||||
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:text.csv.twig';
|
||||
}
|
||||
|
||||
return $this->templating
|
||||
->render($template, array('text' => $value));
|
||||
}
|
||||
|
||||
public function serialize($value, CustomField $customField)
|
||||
{
|
||||
return $value;
|
||||
return $builder
|
||||
->add(self::MAX_LENGTH, IntegerType::class, ['empty_data' => 256])
|
||||
->add(self::MULTIPLE_CF_INLINE, ChoiceType::class, [
|
||||
'choices' => [
|
||||
'Multiple boxes on the line' => '1',
|
||||
'One box on the line' => '0',
|
||||
],
|
||||
'label' => 'Box appearance',
|
||||
'expanded' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function deserialize($serialized, CustomField $customField)
|
||||
@@ -121,18 +99,20 @@ class CustomFieldText extends AbstractCustomField
|
||||
return 'Text field';
|
||||
}
|
||||
|
||||
public function buildOptionsForm(FormBuilderInterface $builder)
|
||||
public function render($value, CustomField $customField, $documentType = 'html')
|
||||
{
|
||||
return $builder
|
||||
->add(self::MAX_LENGTH, IntegerType::class, array('empty_data' => 256))
|
||||
->add(self::MULTIPLE_CF_INLINE, ChoiceType::class, array(
|
||||
'choices' => array(
|
||||
'Multiple boxes on the line' => '1',
|
||||
'One box on the line' => '0'
|
||||
),
|
||||
'label' => 'Box appearance',
|
||||
'expanded' => True
|
||||
))
|
||||
;
|
||||
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:text.html.twig';
|
||||
|
||||
if ('csv' == $documentType) {
|
||||
$template = 'ChillCustomFieldsBundle:CustomFieldsRendering:text.csv.twig';
|
||||
}
|
||||
|
||||
return $this->templating
|
||||
->render($template, ['text' => $value]);
|
||||
}
|
||||
|
||||
public function serialize($value, CustomField $customField)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
@@ -1,45 +1,33 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\CustomFields;
|
||||
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Form\Type\CustomFieldsTitleType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Bundle\TwigBundle\TwigEngine;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Bundle\TwigBundle\TwigEngine;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Chill\CustomFieldsBundle\Form\Type\CustomFieldsTitleType;
|
||||
|
||||
class CustomFieldTitle extends AbstractCustomField
|
||||
{
|
||||
const TYPE = 'type';
|
||||
const TYPE_TITLE = 'title';
|
||||
const TYPE_SUBTITLE = 'subtitle';
|
||||
public const TYPE = 'type';
|
||||
|
||||
public const TYPE_SUBTITLE = 'subtitle';
|
||||
|
||||
public const TYPE_TITLE = 'title';
|
||||
|
||||
private $requestStack;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TwigEngine
|
||||
*/
|
||||
private $templating;
|
||||
@@ -49,8 +37,11 @@ class CustomFieldTitle extends AbstractCustomField
|
||||
*/
|
||||
private $translatableStringHelper;
|
||||
|
||||
public function __construct(RequestStack $requestStack, TwigEngine $templating,
|
||||
TranslatableStringHelper $translatableStringHelper)
|
||||
public function __construct(
|
||||
RequestStack $requestStack,
|
||||
TwigEngine $templating,
|
||||
TranslatableStringHelper $translatableStringHelper
|
||||
)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->templating = $templating;
|
||||
@@ -59,30 +50,29 @@ class CustomFieldTitle extends AbstractCustomField
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, CustomField $customField)
|
||||
{
|
||||
$builder->add($customField->getSlug(), CustomFieldsTitleType::class, array(
|
||||
$builder->add($customField->getSlug(), CustomFieldsTitleType::class, [
|
||||
'label' => false,
|
||||
'attr' => array(
|
||||
'attr' => [
|
||||
'class' => 'cf-title',
|
||||
'title' => $this->translatableStringHelper->localize($customField->getName()),
|
||||
self::TYPE => $customField->getOptions()[self::TYPE ]
|
||||
)
|
||||
));
|
||||
self::TYPE => $customField->getOptions()[self::TYPE],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function render($value, CustomField $customField, $documentType = 'html')
|
||||
public function buildOptionsForm(FormBuilderInterface $builder)
|
||||
{
|
||||
return $this->templating
|
||||
->render('ChillCustomFieldsBundle:CustomFieldsRendering:title.html.twig',
|
||||
array(
|
||||
'title' => $customField->getName(),
|
||||
'type' => $customField->getOptions()[self::TYPE]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function serialize($value, CustomField $customField)
|
||||
{
|
||||
return $value;
|
||||
return $builder->add(
|
||||
self::TYPE,
|
||||
ChoiceType::class,
|
||||
[
|
||||
'choices' => [
|
||||
'Main title' => self::TYPE_TITLE,
|
||||
'Subtitle' => self::TYPE_SUBTITLE,
|
||||
],
|
||||
'label' => 'Title level',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function deserialize($serialized, CustomField $customField)
|
||||
@@ -100,16 +90,20 @@ class CustomFieldTitle extends AbstractCustomField
|
||||
return false;
|
||||
}
|
||||
|
||||
public function buildOptionsForm(FormBuilderInterface $builder)
|
||||
public function render($value, CustomField $customField, $documentType = 'html')
|
||||
{
|
||||
return $builder->add(self::TYPE, ChoiceType::class,
|
||||
array(
|
||||
'choices' => array(
|
||||
'Main title' => self::TYPE_TITLE,
|
||||
'Subtitle' => self::TYPE_SUBTITLE
|
||||
),
|
||||
'label' => 'Title level',
|
||||
)
|
||||
);
|
||||
return $this->templating
|
||||
->render(
|
||||
'ChillCustomFieldsBundle:CustomFieldsRendering:title.html.twig',
|
||||
[
|
||||
'title' => $customField->getName(),
|
||||
'type' => $customField->getOptions()[self::TYPE],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function serialize($value, CustomField $customField)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
@@ -1,56 +1,42 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\DataFixtures\ORM;
|
||||
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option;
|
||||
use Doctrine\Common\DataFixtures\AbstractFixture;
|
||||
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
|
||||
error_reporting(0);
|
||||
/**
|
||||
* Load some Options
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* Load some Options.
|
||||
*/
|
||||
class LoadOption extends AbstractFixture implements OrderedFixtureInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var \Faker\Generator
|
||||
*/
|
||||
public $fakerFr;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Faker\Generator
|
||||
*/
|
||||
public $fakerEn;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Faker\Generator
|
||||
*/
|
||||
public $fakerFr;
|
||||
|
||||
/**
|
||||
* @var \Faker\Generator
|
||||
*/
|
||||
public $fakerNl;
|
||||
|
||||
private $counter = 0;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->fakerFr = \Faker\Factory::create('fr_FR');
|
||||
@@ -72,110 +58,99 @@ class LoadOption extends AbstractFixture implements OrderedFixtureInterface
|
||||
$this->loadingWords($manager);
|
||||
|
||||
$manager->flush();
|
||||
|
||||
}
|
||||
|
||||
private function loadingWords(\Doctrine\Persistence\ObjectManager $manager)
|
||||
{
|
||||
echo "Loading some words...\n";
|
||||
|
||||
$parents = array(
|
||||
array(
|
||||
'fr' => 'Categorie 1',
|
||||
'nl' => 'Categorie 1',
|
||||
'en' => 'Category 1'
|
||||
),
|
||||
array(
|
||||
'fr' => 'Categorie 2',
|
||||
'nl' => 'Categorie 2',
|
||||
'en' => 'Category 2'
|
||||
)
|
||||
);
|
||||
|
||||
foreach ($parents as $text) {
|
||||
$parent = (new Option())
|
||||
->setText($text)
|
||||
->setKey('word')
|
||||
;
|
||||
$manager->persist($parent);
|
||||
|
||||
//Load children
|
||||
$expected_nb_children = rand(10, 50);
|
||||
for ($i=0; $i < $expected_nb_children; $i++) {
|
||||
$manager->persist($this->createChildOption($parent, array(
|
||||
'fr' => $this->fakerFr->word,
|
||||
'nl' => $this->fakerNl->word,
|
||||
'en' => $this->fakerEn->word
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function loadingCompanies(\Doctrine\Persistence\ObjectManager $manager)
|
||||
{
|
||||
echo "Loading companies \n";
|
||||
$companiesParents = array(
|
||||
array(
|
||||
'fr' => 'Grandes Entreprises',
|
||||
'nl' => 'Grotes Bedrijven',
|
||||
'en' => 'Big Companies'
|
||||
),
|
||||
array(
|
||||
'fr' => 'Moyennes Entreprises',
|
||||
'nl' => 'Middelbare Bedrijven',
|
||||
'en' => 'Middle Companies'
|
||||
),
|
||||
array(
|
||||
'fr' => 'Petites Entreprises',
|
||||
'nl' => 'Kleine Bedrijven',
|
||||
'en' => 'Little Companies'
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
foreach ($companiesParents as $text) {
|
||||
|
||||
$parent = (new Option())
|
||||
->setText($text)
|
||||
->setKey('company')
|
||||
;
|
||||
$manager->persist($parent);
|
||||
|
||||
//Load children
|
||||
$expected_nb_children = rand(10, 50);
|
||||
for ($i=0; $i < $expected_nb_children; $i++) {
|
||||
|
||||
$companyName = $this->fakerFr->company;
|
||||
|
||||
$manager->persist(
|
||||
$this->createChildOption($parent, array(
|
||||
'fr' => $companyName,
|
||||
'nl' => $companyName,
|
||||
'en' => $companyName
|
||||
)));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private $counter = 0;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Option $parent
|
||||
* @param array $text
|
||||
* @return Option
|
||||
*/
|
||||
private function createChildOption(Option $parent, array $text)
|
||||
{
|
||||
$this->counter ++;
|
||||
++$this->counter;
|
||||
|
||||
return (new Option())
|
||||
->setText($text)
|
||||
->setParent($parent)
|
||||
->setActive(true)
|
||||
->setInternalKey($parent->getKey().'-'.$this->counter);
|
||||
;
|
||||
->setText($text)
|
||||
->setParent($parent)
|
||||
->setActive(true)
|
||||
->setInternalKey($parent->getKey() . '-' . $this->counter);
|
||||
}
|
||||
|
||||
private function loadingCompanies(ObjectManager $manager)
|
||||
{
|
||||
echo "Loading companies \n";
|
||||
$companiesParents = [
|
||||
[
|
||||
'fr' => 'Grandes Entreprises',
|
||||
'nl' => 'Grotes Bedrijven',
|
||||
'en' => 'Big Companies',
|
||||
],
|
||||
[
|
||||
'fr' => 'Moyennes Entreprises',
|
||||
'nl' => 'Middelbare Bedrijven',
|
||||
'en' => 'Middle Companies',
|
||||
],
|
||||
[
|
||||
'fr' => 'Petites Entreprises',
|
||||
'nl' => 'Kleine Bedrijven',
|
||||
'en' => 'Little Companies',
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($companiesParents as $text) {
|
||||
$parent = (new Option())
|
||||
->setText($text)
|
||||
->setKey('company');
|
||||
$manager->persist($parent);
|
||||
|
||||
//Load children
|
||||
$expected_nb_children = rand(10, 50);
|
||||
|
||||
for ($i = 0; $i < $expected_nb_children; ++$i) {
|
||||
$companyName = $this->fakerFr->company;
|
||||
|
||||
$manager->persist(
|
||||
$this->createChildOption($parent, [
|
||||
'fr' => $companyName,
|
||||
'nl' => $companyName,
|
||||
'en' => $companyName,
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function loadingWords(ObjectManager $manager)
|
||||
{
|
||||
echo "Loading some words...\n";
|
||||
|
||||
$parents = [
|
||||
[
|
||||
'fr' => 'Categorie 1',
|
||||
'nl' => 'Categorie 1',
|
||||
'en' => 'Category 1',
|
||||
],
|
||||
[
|
||||
'fr' => 'Categorie 2',
|
||||
'nl' => 'Categorie 2',
|
||||
'en' => 'Category 2',
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($parents as $text) {
|
||||
$parent = (new Option())
|
||||
->setText($text)
|
||||
->setKey('word');
|
||||
$manager->persist($parent);
|
||||
|
||||
//Load children
|
||||
$expected_nb_children = rand(10, 50);
|
||||
|
||||
for ($i = 0; $i < $expected_nb_children; ++$i) {
|
||||
$manager->persist($this->createChildOption($parent, [
|
||||
'fr' => $this->fakerFr->word,
|
||||
'nl' => $this->fakerNl->word,
|
||||
'en' => $this->fakerEn->word,
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,29 +1,33 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
use Symfony\Component\DependencyInjection\Loader;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
|
||||
use Symfony\Component\DependencyInjection\Loader;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
|
||||
/**
|
||||
* This is the class that loads and manages your bundle configuration
|
||||
* This is the class that loads and manages your bundle configuration.
|
||||
*
|
||||
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
|
||||
*/
|
||||
class ChillCustomFieldsExtension extends Extension implements PrependExtensionInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$configuration = new Configuration();
|
||||
$config = $this->processConfiguration($configuration, $configs);
|
||||
|
||||
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../config'));
|
||||
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../config'));
|
||||
$loader->load('services.yaml');
|
||||
$loader->load('services/fixtures.yaml');
|
||||
$loader->load('services/controller.yaml');
|
||||
@@ -34,15 +38,19 @@ class ChillCustomFieldsExtension extends Extension implements PrependExtensionIn
|
||||
// && $config['customizables_entities'] !== FALSE)
|
||||
// ? $config['customizables_entities'] : array();
|
||||
|
||||
$container->setParameter('chill_custom_fields.customizables_entities',
|
||||
$config['customizables_entities']);
|
||||
$container->setParameter('chill_custom_fields.show_empty_values',
|
||||
$config['show_empty_values_in_views']);
|
||||
$container->setParameter(
|
||||
'chill_custom_fields.customizables_entities',
|
||||
$config['customizables_entities']
|
||||
);
|
||||
$container->setParameter(
|
||||
'chill_custom_fields.show_empty_values',
|
||||
$config['show_empty_values_in_views']
|
||||
);
|
||||
}
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see \Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface::prepend()
|
||||
*/
|
||||
/* (non-PHPdoc)
|
||||
* @see \Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface::prepend()
|
||||
*/
|
||||
public function prepend(ContainerBuilder $container)
|
||||
{
|
||||
// add form layout to twig resources
|
||||
@@ -54,12 +62,12 @@ class ChillCustomFieldsExtension extends Extension implements PrependExtensionIn
|
||||
$container->prependExtensionConfig('twig', $twigConfig);
|
||||
|
||||
//add routes for custom bundle
|
||||
$container->prependExtensionConfig('chill_main', array(
|
||||
'routing' => array(
|
||||
'resources' => array(
|
||||
'@ChillCustomFieldsBundle/config/routes.yaml'
|
||||
)
|
||||
)
|
||||
));
|
||||
$container->prependExtensionConfig('chill_main', [
|
||||
'routing' => [
|
||||
'resources' => [
|
||||
'@ChillCustomFieldsBundle/config/routes.yaml',
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -1,70 +1,73 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||
|
||||
/**
|
||||
* This is the class that validates and merges configuration from
|
||||
* your app/config files
|
||||
* This is the class that validates and merges configuration from
|
||||
* your app/config files.
|
||||
*
|
||||
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
|
||||
*/
|
||||
class Configuration implements ConfigurationInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getConfigTreeBuilder()
|
||||
{
|
||||
$treeBuilder = new TreeBuilder('chill_custom_fields');
|
||||
$rootNode = $treeBuilder->getRootNode('chill_custom_fields');
|
||||
|
||||
$classInfo = "The class which may receive custom fields";
|
||||
$nameInfo = "The name which will appears in the user interface. May be translatable";
|
||||
$optionsInfo = "Options available for custom fields groups referencing this class";
|
||||
$optionsFormType = "The name of the form to append";
|
||||
$optionsFormOptionsInfos = "the arguments to pass the form";
|
||||
$customizableEntitiesInfo = "A list of customizable entities";
|
||||
|
||||
$classInfo = 'The class which may receive custom fields';
|
||||
$nameInfo = 'The name which will appears in the user interface. May be translatable';
|
||||
$optionsInfo = 'Options available for custom fields groups referencing this class';
|
||||
$optionsFormType = 'The name of the form to append';
|
||||
$optionsFormOptionsInfos = 'the arguments to pass the form';
|
||||
$customizableEntitiesInfo = 'A list of customizable entities';
|
||||
|
||||
$rootNode
|
||||
->children()
|
||||
->arrayNode('customizables_entities')
|
||||
->info($customizableEntitiesInfo)
|
||||
->defaultValue(array())
|
||||
->prototype('array')
|
||||
->children()
|
||||
->scalarNode('class')->isRequired()->info($classInfo)
|
||||
->end()
|
||||
->scalarNode('name') ->isRequired()->info($nameInfo)
|
||||
->end()
|
||||
->arrayNode('options')
|
||||
->info($optionsInfo)
|
||||
->defaultValue(array())
|
||||
->useAttributeAsKey('key')
|
||||
->prototype('array')
|
||||
->children()
|
||||
->scalarNode('form_type')
|
||||
->isRequired()
|
||||
->info($optionsFormType)
|
||||
->end()
|
||||
->variableNode('form_options')
|
||||
->info($optionsFormOptionsInfos)
|
||||
->defaultValue(array())
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->arrayNode('customizables_entities')
|
||||
->info($customizableEntitiesInfo)
|
||||
->defaultValue([])
|
||||
->prototype('array')
|
||||
->children()
|
||||
->scalarNode('class')->isRequired()->info($classInfo)
|
||||
->end()
|
||||
->scalarNode('name')->isRequired()->info($nameInfo)
|
||||
->end()
|
||||
->arrayNode('options')
|
||||
->info($optionsInfo)
|
||||
->defaultValue([])
|
||||
->useAttributeAsKey('key')
|
||||
->prototype('array')
|
||||
->children()
|
||||
->scalarNode('form_type')
|
||||
->isRequired()
|
||||
->info($optionsFormType)
|
||||
->end()
|
||||
->variableNode('form_options')
|
||||
->info($optionsFormOptionsInfos)
|
||||
->defaultValue([])
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->booleanNode('show_empty_values_in_views')
|
||||
->info('Show the empty value for custom fields in the views, timeline, ...')
|
||||
->defaultValue(true)
|
||||
->info('Show the empty value for custom fields in the views, timeline, ...')
|
||||
->defaultValue(true)
|
||||
->end()
|
||||
->end()
|
||||
;
|
||||
->end();
|
||||
|
||||
return $treeBuilder;
|
||||
}
|
||||
|
@@ -1,43 +1,43 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use LogicException;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class CustomFieldCompilerPass implements CompilerPassInterface
|
||||
{
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
if (!$container->hasDefinition('chill.custom_field.provider')) {
|
||||
throw new \LogicException('service chill.custom_field.provider '
|
||||
throw new LogicException('service chill.custom_field.provider '
|
||||
. 'is not defined.');
|
||||
}
|
||||
|
||||
|
||||
$definition = $container->getDefinition(
|
||||
'chill.custom_field.provider'
|
||||
);
|
||||
|
||||
$taggedServices = $container->findTaggedServiceIds(
|
||||
'chill.custom_field'
|
||||
);
|
||||
|
||||
);
|
||||
|
||||
foreach ($taggedServices as $id => $tagAttributes) {
|
||||
foreach ($tagAttributes as $attributes) {
|
||||
$definition->addMethodCall(
|
||||
'addCustomField',
|
||||
array(new Reference($id), $attributes["type"])
|
||||
[new Reference($id), $attributes['type']]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,22 +1,10 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Entity;
|
||||
@@ -24,16 +12,36 @@ namespace Chill\CustomFieldsBundle\Entity;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* CustomField
|
||||
* CustomField.
|
||||
*
|
||||
* @ORM\Entity()
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(name="customfield")
|
||||
* @ORM\HasLifecycleCallbacks()
|
||||
* @ORM\HasLifecycleCallbacks
|
||||
*/
|
||||
class CustomField
|
||||
{
|
||||
public const ONE_TO_MANY = 2;
|
||||
|
||||
public const ONE_TO_ONE = 1;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
* @var bool
|
||||
*
|
||||
* @ORM\Column(type="boolean")
|
||||
*/
|
||||
private $active = true;
|
||||
|
||||
/**
|
||||
* @var CustomFieldsGroup
|
||||
*
|
||||
* @ORM\ManyToOne(
|
||||
* targetEntity="Chill\CustomFieldsBundle\Entity\CustomFieldsGroup",
|
||||
* inversedBy="customFields")
|
||||
*/
|
||||
private $customFieldGroup;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*
|
||||
* @ORM\Id
|
||||
* @ORM\Column(name="id", type="integer")
|
||||
@@ -41,6 +49,34 @@ class CustomField
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*
|
||||
* @ORM\Column(type="json")
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*
|
||||
* @ORM\Column(type="json")
|
||||
*/
|
||||
private $options = [];
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*
|
||||
* @ORM\Column(type="float")
|
||||
*/
|
||||
private $ordering;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*
|
||||
* @ORM\Column(type="boolean")
|
||||
*/
|
||||
private $required = false;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
@@ -56,57 +92,19 @@ class CustomField
|
||||
private $type;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
* Get customFieldGroup.
|
||||
*
|
||||
* @ORM\Column(type="boolean")
|
||||
* @return CustomFieldsGroup
|
||||
*/
|
||||
private $active = true;
|
||||
public function getCustomFieldsGroup()
|
||||
{
|
||||
return $this->customFieldGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* Get id.
|
||||
*
|
||||
* @ORM\Column(type="json")
|
||||
*/
|
||||
private $options = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*
|
||||
* @ORM\Column(type="json")
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*
|
||||
* @ORM\Column(type="float")
|
||||
*/
|
||||
private $ordering;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*
|
||||
* @ORM\Column(type="boolean")
|
||||
*/
|
||||
private $required = FALSE;
|
||||
|
||||
const ONE_TO_ONE = 1;
|
||||
const ONE_TO_MANY = 2;
|
||||
|
||||
/**
|
||||
* @var CustomFieldsGroup
|
||||
*
|
||||
* @ORM\ManyToOne(
|
||||
* targetEntity="Chill\CustomFieldsBundle\Entity\CustomFieldsGroup",
|
||||
* inversedBy="customFields")
|
||||
*/
|
||||
private $customFieldGroup;
|
||||
|
||||
|
||||
/**
|
||||
* Get id
|
||||
*
|
||||
* @return integer
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
@@ -114,36 +112,69 @@ class CustomField
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* Get name.
|
||||
*
|
||||
* @param mixed|null $locale
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getSlug()
|
||||
public function getName($locale = null)
|
||||
{
|
||||
return $this->slug;
|
||||
if ($locale) {
|
||||
if (isset($this->name[$locale])) {
|
||||
return $this->name[$locale];
|
||||
}
|
||||
|
||||
foreach ($this->name as $name) {
|
||||
if (!empty($name)) {
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
function getOptions()
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set type
|
||||
* Get order.
|
||||
*
|
||||
* @param string $type
|
||||
* @return CustomField
|
||||
* @return float
|
||||
*/
|
||||
public function setType($type)
|
||||
public function getOrdering()
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
return $this;
|
||||
return $this->ordering;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get type
|
||||
* alias for isRequired.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getRequired()
|
||||
{
|
||||
return $this->isRequired();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSlug()
|
||||
{
|
||||
return $this->slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@@ -152,11 +183,31 @@ class CustomField
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the custom field is active.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isActive()
|
||||
{
|
||||
return $this->active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set active
|
||||
* return true if the field required.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isRequired()
|
||||
{
|
||||
return $this->required;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set active.
|
||||
*
|
||||
* @param bool $active
|
||||
*
|
||||
* @param boolean $active
|
||||
* @return CustomField
|
||||
*/
|
||||
public function setActive($active)
|
||||
@@ -167,32 +218,13 @@ class CustomField
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the custom field is active
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isActive()
|
||||
{
|
||||
return $this->active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customFieldGroup
|
||||
*
|
||||
* @return CustomFieldsGroup
|
||||
*/
|
||||
public function getCustomFieldsGroup()
|
||||
{
|
||||
return $this->customFieldGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set customFieldGroup
|
||||
* Set customFieldGroup.
|
||||
*
|
||||
* @param CustomFieldsGroup $customFieldGroup
|
||||
*
|
||||
* @return CustomField
|
||||
*/
|
||||
public function setCustomFieldsGroup(CustomFieldsGroup $customFieldGroup = null)
|
||||
public function setCustomFieldsGroup(?CustomFieldsGroup $customFieldGroup = null)
|
||||
{
|
||||
$this->customFieldGroup = $customFieldGroup;
|
||||
|
||||
@@ -200,9 +232,10 @@ class CustomField
|
||||
}
|
||||
|
||||
/**
|
||||
* Set name
|
||||
* Set name.
|
||||
*
|
||||
* @param array $name
|
||||
*
|
||||
* @return CustomField
|
||||
*/
|
||||
public function setName($name)
|
||||
@@ -213,57 +246,8 @@ class CustomField
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name
|
||||
* Set options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getName($locale = null)
|
||||
{
|
||||
if ($locale) {
|
||||
if (isset($this->name[$locale])) {
|
||||
return $this->name[$locale];
|
||||
} else {
|
||||
foreach ($this->name as $name) {
|
||||
if (!empty($name)) {
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
|
||||
} else {
|
||||
return $this->name;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Set order
|
||||
*
|
||||
* @param float $order
|
||||
* @return CustomField
|
||||
*/
|
||||
public function setOrdering($order)
|
||||
{
|
||||
$this->ordering = $order;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get order
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getOrdering()
|
||||
{
|
||||
return $this->ordering;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set options
|
||||
*
|
||||
* @param array $options
|
||||
* @return CustomField
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
@@ -274,39 +258,49 @@ class CustomField
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $slug
|
||||
* @return $this
|
||||
* Set order.
|
||||
*
|
||||
* @param float $order
|
||||
*
|
||||
* @return CustomField
|
||||
*/
|
||||
public function setSlug($slug)
|
||||
public function setOrdering($order)
|
||||
{
|
||||
$this->slug = $slug;
|
||||
$this->ordering = $order;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* alias for isRequired
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getRequired()
|
||||
{
|
||||
return $this->isRequired();
|
||||
}
|
||||
|
||||
/**
|
||||
* return true if the field required
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isRequired()
|
||||
{
|
||||
return $this->required;
|
||||
}
|
||||
|
||||
public function setRequired($required)
|
||||
{
|
||||
$this->required = $required;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $slug
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setSlug($slug)
|
||||
{
|
||||
$this->slug = $slug;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set type.
|
||||
*
|
||||
* @param string $type
|
||||
*
|
||||
* @return CustomField
|
||||
*/
|
||||
public function setType($type)
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@@ -1,20 +1,10 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice;
|
||||
@@ -24,15 +14,27 @@ use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* @ORM\Entity(
|
||||
* repositoryClass="Chill\CustomFieldsBundle\EntityRepository\CustomFieldLongChoice\OptionRepository")
|
||||
* repositoryClass="Chill\CustomFieldsBundle\EntityRepository\CustomFieldLongChoice\OptionRepository")
|
||||
* @ORM\Table(name="custom_field_long_choice_options")
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class Option
|
||||
{
|
||||
/**
|
||||
* @var integer
|
||||
* @var bool
|
||||
* @ORM\Column(type="boolean")
|
||||
*/
|
||||
private $active = true;
|
||||
|
||||
/**
|
||||
* @var Collection
|
||||
* @ORM\OneToMany(
|
||||
* targetEntity="Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option",
|
||||
* mappedBy="parent")
|
||||
*/
|
||||
private $children;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*
|
||||
* @ORM\Id
|
||||
* @ORM\Column(name="id", type="integer")
|
||||
@@ -40,6 +42,12 @@ class Option
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @ORM\Column(type="string", length=50, name="internal_key")
|
||||
*/
|
||||
private $internalKey = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @ORM\Column(type="string", length=15)
|
||||
@@ -47,7 +55,16 @@ class Option
|
||||
private $key;
|
||||
|
||||
/**
|
||||
* A json representation of text (multilingual)
|
||||
* @var Option
|
||||
* @ORM\ManyToOne(
|
||||
* targetEntity="Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option",
|
||||
* inversedBy="children")
|
||||
* @ORM\JoinColumn(nullable=true)
|
||||
*/
|
||||
private $parent;
|
||||
|
||||
/**
|
||||
* A json representation of text (multilingual).
|
||||
*
|
||||
* @var array
|
||||
* @ORM\Column(type="json")
|
||||
@@ -55,33 +72,20 @@ class Option
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* @var Collection
|
||||
* @ORM\OneToMany(
|
||||
* targetEntity="Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option",
|
||||
* mappedBy="parent")
|
||||
* @return bool
|
||||
*/
|
||||
private $children;
|
||||
public function getActive()
|
||||
{
|
||||
return $this->isActive();
|
||||
}
|
||||
|
||||
/**
|
||||
* @var Option
|
||||
* @ORM\ManyToOne(
|
||||
* targetEntity="Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option",
|
||||
* inversedBy="children")
|
||||
* @ORM\JoinColumn(nullable=true)
|
||||
* @return Collection
|
||||
*/
|
||||
private $parent;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @ORM\Column(type="string", length=50, name="internal_key")
|
||||
*/
|
||||
private $internalKey = '';
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
* @ORM\Column(type="boolean")
|
||||
*/
|
||||
private $active = true;
|
||||
public function getChildren()
|
||||
{
|
||||
return $this->children;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
@@ -91,6 +95,14 @@ class Option
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getInternalKey()
|
||||
{
|
||||
return $this->internalKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@@ -99,22 +111,6 @@ class Option
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getText()
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getChildren()
|
||||
{
|
||||
return $this->children;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Option
|
||||
*/
|
||||
@@ -124,51 +120,19 @@ class Option
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @return $this
|
||||
* @return array
|
||||
*/
|
||||
public function setKey($key)
|
||||
public function getText()
|
||||
{
|
||||
$this->key = $key;
|
||||
return $this;
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $text
|
||||
* @return $this
|
||||
*/
|
||||
public function setText(array $text)
|
||||
{
|
||||
$this->text = $text;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Option|null $parent
|
||||
* @return $this
|
||||
*/
|
||||
public function setParent(Option $parent = null)
|
||||
{
|
||||
$this->parent = $parent;
|
||||
$this->key = $parent->getKey();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return boolean
|
||||
* @return bool
|
||||
*/
|
||||
public function hasParent()
|
||||
{
|
||||
return $this->parent === NULL ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getInternalKey()
|
||||
{
|
||||
return $this->internalKey;
|
||||
return null === $this->parent ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,32 +143,60 @@ class Option
|
||||
return $this->active;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getActive()
|
||||
{
|
||||
return $this->isActive();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $internal_key
|
||||
* @return $this
|
||||
*/
|
||||
public function setInternalKey($internal_key)
|
||||
{
|
||||
$this->internalKey = $internal_key;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $active
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setActive($active)
|
||||
{
|
||||
$this->active = $active;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $internal_key
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setInternalKey($internal_key)
|
||||
{
|
||||
$this->internalKey = $internal_key;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setKey($key)
|
||||
{
|
||||
$this->key = $key;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setParent(?Option $parent = null)
|
||||
{
|
||||
$this->parent = $parent;
|
||||
$this->key = $parent->getKey();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setText(array $text)
|
||||
{
|
||||
$this->text = $text;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@@ -1,22 +1,10 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Entity;
|
||||
@@ -24,26 +12,27 @@ namespace Chill\CustomFieldsBundle\Entity;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* CustomFieldsDefaultGroup
|
||||
* CustomFieldsDefaultGroup.
|
||||
*
|
||||
* @ORM\Entity()
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(
|
||||
* name="customfieldsdefaultgroup",
|
||||
* uniqueConstraints={@ORM\UniqueConstraint(
|
||||
* name="unique_entity",
|
||||
* columns={"entity"}
|
||||
* )})
|
||||
* name="unique_entity",
|
||||
* columns={"entity"}
|
||||
* )})
|
||||
*/
|
||||
class CustomFieldsDefaultGroup
|
||||
{
|
||||
/**
|
||||
* @var integer
|
||||
* @var CustomFieldsGroup
|
||||
*
|
||||
* @ORM\Id
|
||||
* @ORM\Column(name="id", type="integer")
|
||||
* @ORM\GeneratedValue(strategy="AUTO")
|
||||
* @ORM\ManyToOne(
|
||||
* targetEntity="Chill\CustomFieldsBundle\Entity\CustomFieldsGroup")
|
||||
*
|
||||
* sf4 check: option inversedBy="customFields" return inconsistent error mapping !!
|
||||
*/
|
||||
private $id;
|
||||
private $customFieldsGroup;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
@@ -53,40 +42,26 @@ class CustomFieldsDefaultGroup
|
||||
private $entity;
|
||||
|
||||
/**
|
||||
* @var CustomFieldsGroup
|
||||
* @var int
|
||||
*
|
||||
* @ORM\ManyToOne(
|
||||
* targetEntity="Chill\CustomFieldsBundle\Entity\CustomFieldsGroup")
|
||||
*
|
||||
* sf4 check: option inversedBy="customFields" return inconsistent error mapping !!
|
||||
* @ORM\Id
|
||||
* @ORM\Column(name="id", type="integer")
|
||||
* @ORM\GeneratedValue(strategy="AUTO")
|
||||
*/
|
||||
private $customFieldsGroup;
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* Get id
|
||||
* Get customFieldsGroup.
|
||||
*
|
||||
* @return integer
|
||||
* @return CustomFieldsGroup
|
||||
*/
|
||||
public function getId()
|
||||
public function getCustomFieldsGroup()
|
||||
{
|
||||
return $this->id;
|
||||
return $this->customFieldsGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set entity
|
||||
*
|
||||
* @param string $entity
|
||||
* @return CustomFieldsDefaultGroup
|
||||
*/
|
||||
public function setEntity($entity)
|
||||
{
|
||||
$this->entity = $entity;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get entity
|
||||
* Get entity.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@@ -96,9 +71,20 @@ class CustomFieldsDefaultGroup
|
||||
}
|
||||
|
||||
/**
|
||||
* Set customFieldsGroup
|
||||
* Get id.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set customFieldsGroup.
|
||||
*
|
||||
* @param CustomFieldsGroup $customFieldsGroup *
|
||||
*
|
||||
* @return CustomFieldsDefaultGroup
|
||||
*/
|
||||
public function setCustomFieldsGroup($customFieldsGroup)
|
||||
@@ -109,12 +95,16 @@ class CustomFieldsDefaultGroup
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customFieldsGroup
|
||||
* Set entity.
|
||||
*
|
||||
* @return CustomFieldsGroup
|
||||
* @param string $entity
|
||||
*
|
||||
* @return CustomFieldsDefaultGroup
|
||||
*/
|
||||
public function getCustomFieldsGroup()
|
||||
public function setEntity($entity)
|
||||
{
|
||||
return $this->customFieldsGroup;
|
||||
$this->entity = $entity;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@@ -1,40 +1,56 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Entity;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* CustomFieldGroup
|
||||
* CustomFieldGroup.
|
||||
*
|
||||
* @ORM\Entity()
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(name="customfieldsgroup")
|
||||
*/
|
||||
class CustomFieldsGroup
|
||||
{
|
||||
/**
|
||||
* @var integer
|
||||
* The custom fields of the group that are active.
|
||||
* This variable if null, if this informations has not been computed.
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
private $activeCustomFields;
|
||||
|
||||
/**
|
||||
* The custom fields of the group.
|
||||
* The custom fields are asc-ordered regarding to their property "ordering".
|
||||
*
|
||||
* @var Collection
|
||||
*
|
||||
* @ORM\OneToMany(
|
||||
* targetEntity="Chill\CustomFieldsBundle\Entity\CustomField",
|
||||
* mappedBy="customFieldGroup")
|
||||
* @ORM\OrderBy({"ordering": "ASC"})
|
||||
*/
|
||||
private $customFields;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @ORM\Column(type="string", length=255)
|
||||
*/
|
||||
private $entity;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*
|
||||
* @ORM\Id
|
||||
* @ORM\Column(name="id", type="integer")
|
||||
@@ -49,41 +65,12 @@ class CustomFieldsGroup
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @ORM\Column(type="string", length=255)
|
||||
*/
|
||||
private $entity;
|
||||
|
||||
/**
|
||||
* The custom fields of the group.
|
||||
* The custom fields are asc-ordered regarding to their property "ordering".
|
||||
*
|
||||
* @var Collection $customFields
|
||||
*
|
||||
* @ORM\OneToMany(
|
||||
* targetEntity="Chill\CustomFieldsBundle\Entity\CustomField",
|
||||
* mappedBy="customFieldGroup")
|
||||
* @ORM\OrderBy({"ordering" = "ASC"})
|
||||
*/
|
||||
private $customFields;
|
||||
|
||||
/**
|
||||
* The custom fields of the group that are active.
|
||||
* This variable if null, if this informations has not been computed.
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
private $activeCustomFields = null;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*
|
||||
* @ORM\Column(type="json")
|
||||
*/
|
||||
private $options = array();
|
||||
|
||||
private $options = [];
|
||||
|
||||
/**
|
||||
* CustomFieldsGroup constructor.
|
||||
@@ -94,9 +81,8 @@ class CustomFieldsGroup
|
||||
}
|
||||
|
||||
/**
|
||||
* Add customField
|
||||
* Add customField.
|
||||
*
|
||||
* @param CustomField $customField
|
||||
* @return CustomFieldsGroup
|
||||
*/
|
||||
public function addCustomField(CustomField $customField)
|
||||
@@ -107,34 +93,17 @@ class CustomFieldsGroup
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove customField
|
||||
*
|
||||
* @param CustomField $customField
|
||||
*/
|
||||
public function removeCustomField(CustomField $customField)
|
||||
{
|
||||
$this->customFields->removeElement($customField);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getCustomFields()
|
||||
{
|
||||
return $this->customFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the custom
|
||||
* Get all the custom.
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getActiveCustomFields()
|
||||
{
|
||||
if($this->activeCustomFields === null) {
|
||||
$this->activeCustomFields = array();
|
||||
if (null === $this->activeCustomFields) {
|
||||
$this->activeCustomFields = [];
|
||||
|
||||
foreach ($this->customFields as $cf) {
|
||||
if($cf->isActive()) {
|
||||
if ($cf->isActive()) {
|
||||
array_push($this->activeCustomFields, $cf);
|
||||
}
|
||||
}
|
||||
@@ -144,9 +113,27 @@ class CustomFieldsGroup
|
||||
}
|
||||
|
||||
/**
|
||||
* Get id
|
||||
* @return Collection
|
||||
*/
|
||||
public function getCustomFields()
|
||||
{
|
||||
return $this->customFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get entity.
|
||||
*
|
||||
* @return integer
|
||||
* @return string
|
||||
*/
|
||||
public function getEntity()
|
||||
{
|
||||
return $this->entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get id.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
@@ -154,48 +141,55 @@ class CustomFieldsGroup
|
||||
}
|
||||
|
||||
/**
|
||||
* Set name
|
||||
* Get name.
|
||||
*
|
||||
* @param array $name
|
||||
* @return CustomFieldsGroup
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name
|
||||
* @param mixed|null $language
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getName($language = null)
|
||||
{
|
||||
//TODO set this in a service, PLUS twig function
|
||||
if ($language) {
|
||||
if (isset($this->name[$language])) {
|
||||
return $this->name[$language];
|
||||
} else {
|
||||
foreach ($this->name as $name) {
|
||||
//TODO set this in a service, PLUS twig function
|
||||
if ($language) {
|
||||
if (isset($this->name[$language])) {
|
||||
return $this->name[$language];
|
||||
}
|
||||
|
||||
foreach ($this->name as $name) {
|
||||
if (!empty($name)) {
|
||||
return $name;
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
return '';
|
||||
}
|
||||
|
||||
} else {
|
||||
return $this->name;
|
||||
}
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set entity
|
||||
* get options array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove customField.
|
||||
*/
|
||||
public function removeCustomField(CustomField $customField)
|
||||
{
|
||||
$this->customFields->removeElement($customField);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set entity.
|
||||
*
|
||||
* @param string $entity
|
||||
*
|
||||
* @return CustomFieldsGroup
|
||||
*/
|
||||
public function setEntity($entity)
|
||||
@@ -206,34 +200,28 @@ class CustomFieldsGroup
|
||||
}
|
||||
|
||||
/**
|
||||
* Get entity
|
||||
* Set name.
|
||||
*
|
||||
* @return string
|
||||
* @param array $name
|
||||
*
|
||||
* @return CustomFieldsGroup
|
||||
*/
|
||||
public function getEntity()
|
||||
public function setName($name)
|
||||
{
|
||||
return $this->entity;
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* get options array
|
||||
* set options array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* set options array
|
||||
*
|
||||
* @param array $options
|
||||
* @return CustomFieldsGroup
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
$this->options = $options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@@ -1,77 +1,62 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\EntityRepository\CustomFieldLongChoice;
|
||||
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class OptionRepository extends EntityRepository
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $includeParents
|
||||
* @param mixed $active
|
||||
*
|
||||
* @return Option[]
|
||||
*/
|
||||
public function findFilteredByKey($key, $includeParents = true, $active = true)
|
||||
{
|
||||
$qb = $this->createQueryBuilder('option');
|
||||
$qb->where('option.key = :key');
|
||||
|
||||
if ($active === true){
|
||||
|
||||
if (true === $active) {
|
||||
$qb->andWhere('option.active = true');
|
||||
}
|
||||
|
||||
if ($includeParents === false) {
|
||||
|
||||
if (false === $includeParents) {
|
||||
$qb->andWhere('option.parent IS NOT NULL');
|
||||
|
||||
if ($active === TRUE) {
|
||||
|
||||
if (true === $active) {
|
||||
$qb->join('option.parent', 'p');
|
||||
$qb->andWhere('p.active = true');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$qb->setParameter('key', $key);
|
||||
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getKeys()
|
||||
{
|
||||
$keys = $this->createQueryBuilder('option')
|
||||
->select('option.key')
|
||||
->distinct()
|
||||
->getQuery()
|
||||
->getScalarResult();
|
||||
|
||||
return array_map(function($r) {
|
||||
->select('option.key')
|
||||
->distinct()
|
||||
->getQuery()
|
||||
->getScalarResult();
|
||||
|
||||
return array_map(function ($r) {
|
||||
return $r['key'];
|
||||
}, $keys);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,43 +1,47 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldsGroupToIdTransformer;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use LogicException;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class CustomFieldType extends AbstractType
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldProvider
|
||||
*/
|
||||
private $customFieldProvider;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
private $translatableStringHelper;
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $om;
|
||||
|
||||
/**
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
private $translatableStringHelper;
|
||||
|
||||
public function __construct(
|
||||
CustomFieldProvider $compiler,
|
||||
@@ -48,14 +52,10 @@ class CustomFieldType extends AbstractType
|
||||
$this->om = $om;
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
/**
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param array $options
|
||||
*/
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
|
||||
$customFieldsList = array();
|
||||
$customFieldsList = [];
|
||||
|
||||
foreach ($this->customFieldProvider->getAllFields() as $key => $field) {
|
||||
$customFieldsList[$key] = $field->getName();
|
||||
@@ -63,33 +63,32 @@ class CustomFieldType extends AbstractType
|
||||
|
||||
$builder
|
||||
->add('name', TranslatableStringFormType::class)
|
||||
->add('active', CheckboxType::class, array('required' => false));
|
||||
->add('active', CheckboxType::class, ['required' => false]);
|
||||
|
||||
if ($options['group_widget'] === 'entity') {
|
||||
$builder->add('customFieldsGroup', EntityType::class, array(
|
||||
'class' => 'ChillCustomFieldsBundle:CustomFieldsGroup',
|
||||
'choice_label' => function($g) {
|
||||
if ('entity' === $options['group_widget']) {
|
||||
$builder->add('customFieldsGroup', EntityType::class, [
|
||||
'class' => 'ChillCustomFieldsBundle:CustomFieldsGroup',
|
||||
'choice_label' => function ($g) {
|
||||
return $this->translatableStringHelper->localize($g->getName());
|
||||
}
|
||||
));
|
||||
} elseif ($options['group_widget'] === 'hidden') {
|
||||
},
|
||||
]);
|
||||
} elseif ('hidden' === $options['group_widget']) {
|
||||
$builder->add('customFieldsGroup', HiddenType::class);
|
||||
$builder->get('customFieldsGroup')
|
||||
->addViewTransformer(new CustomFieldsGroupToIdTransformer($this->om));
|
||||
->addViewTransformer(new CustomFieldsGroupToIdTransformer($this->om));
|
||||
} else {
|
||||
throw new \LogicException('The value of group_widget is not handled');
|
||||
throw new LogicException('The value of group_widget is not handled');
|
||||
}
|
||||
|
||||
$builder
|
||||
->add('ordering', NumberType::class)
|
||||
->add('required', CheckboxType::class, array(
|
||||
->add('required', CheckboxType::class, [
|
||||
'required' => false,
|
||||
//'expanded' => TRUE,
|
||||
'label' => 'Required field'
|
||||
))
|
||||
->add('type', HiddenType::class, array('data' => $options['type']))
|
||||
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event)
|
||||
{
|
||||
'label' => 'Required field',
|
||||
])
|
||||
->add('type', HiddenType::class, ['data' => $options['type']])
|
||||
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
|
||||
$customField = $event->getData();
|
||||
$form = $event->getForm();
|
||||
|
||||
@@ -101,16 +100,15 @@ class CustomFieldType extends AbstractType
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
$builder->add(
|
||||
$this->customFieldProvider
|
||||
->getCustomFieldByType($options['type'])
|
||||
->buildOptionsForm(
|
||||
$builder
|
||||
->create('options', null, array('compound' => true))
|
||||
->setRequired(false)
|
||||
)
|
||||
);
|
||||
->create('options', null, ['compound' => true])
|
||||
->setRequired(false)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,14 +116,14 @@ class CustomFieldType extends AbstractType
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => 'Chill\CustomFieldsBundle\Entity\CustomField'
|
||||
));
|
||||
$resolver->setDefaults([
|
||||
'data_class' => 'Chill\CustomFieldsBundle\Entity\CustomField',
|
||||
]);
|
||||
|
||||
$resolver->setRequired(array('type', 'group_widget'))
|
||||
->addAllowedValues('type', array_keys($this->customFieldProvider->getAllFields()))
|
||||
->addAllowedValues('group_widget', array('hidden', 'entity'))
|
||||
->setDefault('group_widget', 'entity');
|
||||
$resolver->setRequired(['type', 'group_widget'])
|
||||
->addAllowedValues('type', array_keys($this->customFieldProvider->getAllFields()))
|
||||
->addAllowedValues('group_widget', ['hidden', 'entity'])
|
||||
->setDefault('group_widget', 'entity');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,22 +1,26 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form;
|
||||
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
|
||||
|
||||
|
||||
class CustomFieldsGroupType extends AbstractType
|
||||
{
|
||||
|
||||
|
||||
private $customizableEntities; //TODO : add comment about this variable
|
||||
|
||||
/**
|
||||
@@ -30,41 +34,37 @@ class CustomFieldsGroupType extends AbstractType
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param array $options
|
||||
*/
|
||||
//TODO : details about the function
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
//prepare translation
|
||||
$entities = array();
|
||||
$customizableEntities = array(); //TODO : change name too close than $this->customizableEntities
|
||||
$entities = [];
|
||||
$customizableEntities = []; //TODO : change name too close than $this->customizableEntities
|
||||
|
||||
foreach($this->customizableEntities as $key => $definition) {
|
||||
foreach ($this->customizableEntities as $key => $definition) {
|
||||
$entities[$definition['class']] = $this->translator->trans($definition['name']);
|
||||
$customizableEntities[$definition['class']] = $definition;
|
||||
}
|
||||
|
||||
$builder
|
||||
->add('name', TranslatableStringFormType::class)
|
||||
->add('entity', ChoiceType::class, array(
|
||||
'choices' => array_combine(array_values($entities),array_keys($entities)),
|
||||
))
|
||||
;
|
||||
->add('entity', ChoiceType::class, [
|
||||
'choices' => array_combine(array_values($entities), array_keys($entities)),
|
||||
]);
|
||||
|
||||
$builder->addEventListener(
|
||||
FormEvents::POST_SET_DATA,
|
||||
function(FormEvent $event) use ($customizableEntities, $builder) {
|
||||
function (FormEvent $event) use ($customizableEntities, $builder) {
|
||||
$form = $event->getForm();
|
||||
$group = $event->getData();
|
||||
|
||||
//stop the function if entity is not set
|
||||
if ($group->getEntity() === NULL) {
|
||||
if ($group->getEntity() === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$optionBuilder = null;
|
||||
|
||||
if (count($customizableEntities[$group->getEntity()]['options']) > 0) {
|
||||
$optionBuilder = $builder
|
||||
->getFormFactory()
|
||||
@@ -75,11 +75,11 @@ class CustomFieldsGroupType extends AbstractType
|
||||
[
|
||||
'compound' => true,
|
||||
'auto_initialize' => false,
|
||||
'required' => false
|
||||
'required' => false,
|
||||
]
|
||||
);
|
||||
|
||||
foreach($customizableEntities[$group->getEntity()]['options'] as $key => $option) {
|
||||
foreach ($customizableEntities[$group->getEntity()]['options'] as $key => $option) {
|
||||
$optionBuilder->add($key, $option['form_type'], $option['form_options']);
|
||||
}
|
||||
}
|
||||
@@ -87,7 +87,8 @@ class CustomFieldsGroupType extends AbstractType
|
||||
if ((null !== $optionBuilder) && $optionBuilder->count() > 0) {
|
||||
$form->add($optionBuilder->getForm());
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
|
@@ -1,43 +1,49 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\DataTransformer;
|
||||
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class CustomFieldDataTransformer implements DataTransformerInterface
|
||||
{
|
||||
private $customFieldDefinition;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Chill\CustomFieldsBundle\Entity\CustomField
|
||||
*/
|
||||
private $customField;
|
||||
|
||||
public function __construct(CustomFieldInterface $customFieldDefinition,
|
||||
CustomField $customField)
|
||||
|
||||
private $customFieldDefinition;
|
||||
|
||||
public function __construct(
|
||||
CustomFieldInterface $customFieldDefinition,
|
||||
CustomField $customField
|
||||
)
|
||||
{
|
||||
$this->customFieldDefinition = $customFieldDefinition;
|
||||
$this->customField = $customField;
|
||||
}
|
||||
|
||||
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
return $this->customFieldDefinition->serialize($value,
|
||||
$this->customField);
|
||||
return $this->customFieldDefinition->serialize(
|
||||
$value,
|
||||
$this->customField
|
||||
);
|
||||
}
|
||||
|
||||
public function transform($value)
|
||||
{
|
||||
return $this->customFieldDefinition->deserialize($value,
|
||||
$this->customField);
|
||||
return $this->customFieldDefinition->deserialize(
|
||||
$value,
|
||||
$this->customField
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,11 +1,18 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\DataTransformer;
|
||||
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
|
||||
class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
|
||||
{
|
||||
@@ -14,43 +21,19 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
|
||||
*/
|
||||
private $om;
|
||||
|
||||
/**
|
||||
* @param ObjectManager $om
|
||||
*/
|
||||
public function __construct(ObjectManager $om)
|
||||
{
|
||||
$this->om = $om;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms an custom_field_group to a string (id)
|
||||
*
|
||||
* @param CustomFieldsGroup|null $customFieldsGroup
|
||||
* @return string
|
||||
*/
|
||||
public function transform($customFieldsGroup)
|
||||
{
|
||||
if (null === $customFieldsGroup) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (!$customFieldsGroup instanceof CustomFieldsGroup) {
|
||||
throw new TransformationFailedException(sprintf('Transformation failed: '
|
||||
. 'the expected type of the transforme function is an '
|
||||
. 'object of type Chill\CustomFieldsBundle\Entity\CustomFieldsGroup, '
|
||||
. '%s given (value : %s)', gettype($customFieldsGroup),
|
||||
$customFieldsGroup));
|
||||
}
|
||||
|
||||
return $customFieldsGroup->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a string (id) to an object (CustomFieldsGroup).
|
||||
*
|
||||
* @param string $id
|
||||
* @return CustomFieldsGroup|null
|
||||
*
|
||||
* @throws TransformationFailedException if object (report) is not found.
|
||||
*
|
||||
* @return CustomFieldsGroup|null
|
||||
*/
|
||||
public function reverseTransform($id)
|
||||
{
|
||||
@@ -61,7 +44,7 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
|
||||
if ($id instanceof CustomFieldsGroup) {
|
||||
throw new TransformationFailedException(
|
||||
sprintf(
|
||||
'The transformation failed: the expected argument on '
|
||||
'The transformation failed: the expected argument on '
|
||||
. 'reverseTransform is an object of type int,'
|
||||
. 'Chill\CustomFieldsBundle\Entity\CustomFieldsGroup, '
|
||||
. 'given'
|
||||
@@ -70,8 +53,7 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
|
||||
}
|
||||
|
||||
$customFieldsGroup = $this->om
|
||||
->getRepository(CustomFieldsGroup::class)->find($id)
|
||||
;
|
||||
->getRepository(CustomFieldsGroup::class)->find($id);
|
||||
|
||||
if (null === $customFieldsGroup) {
|
||||
throw new TransformationFailedException(
|
||||
@@ -84,4 +66,31 @@ class CustomFieldsGroupToIdTransformer implements DataTransformerInterface
|
||||
|
||||
return $customFieldsGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms an custom_field_group to a string (id).
|
||||
*
|
||||
* @param CustomFieldsGroup|null $customFieldsGroup
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function transform($customFieldsGroup)
|
||||
{
|
||||
if (null === $customFieldsGroup) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!$customFieldsGroup instanceof CustomFieldsGroup) {
|
||||
throw new TransformationFailedException(sprintf(
|
||||
'Transformation failed: '
|
||||
. 'the expected type of the transforme function is an '
|
||||
. 'object of type Chill\CustomFieldsBundle\Entity\CustomFieldsGroup, '
|
||||
. '%s given (value : %s)',
|
||||
gettype($customFieldsGroup),
|
||||
$customFieldsGroup
|
||||
));
|
||||
}
|
||||
|
||||
return $customFieldsGroup->getId();
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +1,26 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\DataTransformer;
|
||||
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
|
||||
class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
|
||||
private ObjectManager $om;
|
||||
|
||||
class JsonCustomFieldToArrayTransformer implements DataTransformerInterface
|
||||
{
|
||||
private array $customField;
|
||||
|
||||
private ObjectManager $om;
|
||||
|
||||
public function __construct(ObjectManager $om)
|
||||
{
|
||||
$this->om = $om;
|
||||
@@ -23,56 +31,15 @@ class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
|
||||
|
||||
// @TODO: in the array_map callback, CustomField::getLabel() does not exist. What do we do here?
|
||||
$customFieldsLablels = array_map(
|
||||
function($e) { return $e->getLabel(); },
|
||||
$customFields);
|
||||
function ($e) { return $e->getLabel(); },
|
||||
$customFields
|
||||
);
|
||||
|
||||
$customFieldsByLabel = array_combine($customFieldsLablels, $customFields);
|
||||
|
||||
$this->customField = $customFieldsByLabel;
|
||||
}
|
||||
|
||||
public function transform($customFieldsJSON)
|
||||
{
|
||||
echo $customFieldsJSON;
|
||||
|
||||
if($customFieldsJSON === null) {
|
||||
$customFieldsArray = [];
|
||||
} else {
|
||||
$customFieldsArray = json_decode($customFieldsJSON, true, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
$customFieldsArrayRet = array();
|
||||
|
||||
foreach ($customFieldsArray as $key => $value) {
|
||||
$traited = false;
|
||||
if(array_key_exists($key, $this->customField)) {
|
||||
$type = $this->customField[$key]->getType();
|
||||
if(strpos($type,'ManyToOne') === 0) {
|
||||
if(strpos($type,'ManyToOnePersist') ===0) {
|
||||
$entityClass = substr($type, 17, -1);
|
||||
} else {
|
||||
$entityClass = substr($type, 10, -1);
|
||||
}
|
||||
|
||||
$customFieldsArrayRet[$key] = $this->om
|
||||
->getRepository('ChillCustomFieldsBundle:' . $entityClass)
|
||||
->findOneById($value);
|
||||
$traited = true;
|
||||
} else if ($type === 'ManyToMany(Adress)') {
|
||||
$customFieldsArrayRet[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if(! $traited) {
|
||||
$customFieldsArrayRet[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
var_dump($customFieldsArrayRet);
|
||||
|
||||
return $customFieldsArrayRet;
|
||||
}
|
||||
|
||||
public function reverseTransform($customFieldsArray)
|
||||
{
|
||||
/*
|
||||
@@ -86,23 +53,25 @@ class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
|
||||
var_dump(array_keys($this->customField));
|
||||
|
||||
echo "<br> - - 9 - <br>";
|
||||
*/
|
||||
*/
|
||||
|
||||
//var_dump($customFieldsArray);
|
||||
|
||||
$customFieldsArrayRet = array();
|
||||
$customFieldsArrayRet = [];
|
||||
|
||||
foreach ($customFieldsArray as $key => $value) {
|
||||
$traited = false;
|
||||
if(array_key_exists($key, $this->customField)) {
|
||||
|
||||
if (array_key_exists($key, $this->customField)) {
|
||||
$type = $this->customField[$key]->getType();
|
||||
if(strpos($type,'ManyToOne') === 0) {
|
||||
|
||||
if (strpos($type, 'ManyToOne') === 0) {
|
||||
// pour le manytoone() faire
|
||||
// un update du form en js ? : http://symfony.com/fr/doc/current/cookbook/form/form_collections.html
|
||||
//
|
||||
//$entityClass = substr($type, 10, -1);
|
||||
//echo $entityClasss;
|
||||
if(strpos($type, 'ManyToOnePersist') === 0) {
|
||||
if (strpos($type, 'ManyToOnePersist') === 0) {
|
||||
// PEUT ETRE A FAIRE SI SEULEMENT $value->getId() ne renvoie rien...
|
||||
//
|
||||
//
|
||||
@@ -123,14 +92,57 @@ class JsonCustomFieldToArrayTransformer implements DataTransformerInterface {
|
||||
}
|
||||
}
|
||||
|
||||
if(! $traited) {
|
||||
if (!$traited) {
|
||||
$customFieldsArrayRet[$key] = $value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//echo json_encode($customFieldsArrayRet);
|
||||
|
||||
return json_encode($customFieldsArrayRet);
|
||||
}
|
||||
|
||||
public function transform($customFieldsJSON)
|
||||
{
|
||||
echo $customFieldsJSON;
|
||||
|
||||
if (null === $customFieldsJSON) {
|
||||
$customFieldsArray = [];
|
||||
} else {
|
||||
$customFieldsArray = json_decode($customFieldsJSON, true, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
$customFieldsArrayRet = [];
|
||||
|
||||
foreach ($customFieldsArray as $key => $value) {
|
||||
$traited = false;
|
||||
|
||||
if (array_key_exists($key, $this->customField)) {
|
||||
$type = $this->customField[$key]->getType();
|
||||
|
||||
if (strpos($type, 'ManyToOne') === 0) {
|
||||
if (strpos($type, 'ManyToOnePersist') === 0) {
|
||||
$entityClass = substr($type, 17, -1);
|
||||
} else {
|
||||
$entityClass = substr($type, 10, -1);
|
||||
}
|
||||
|
||||
$customFieldsArrayRet[$key] = $this->om
|
||||
->getRepository('ChillCustomFieldsBundle:' . $entityClass)
|
||||
->findOneById($value);
|
||||
$traited = true;
|
||||
} elseif ('ManyToMany(Adress)' === $type) {
|
||||
$customFieldsArrayRet[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$traited) {
|
||||
$customFieldsArrayRet[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
var_dump($customFieldsArrayRet);
|
||||
|
||||
return $customFieldsArrayRet;
|
||||
}
|
||||
}
|
||||
|
@@ -1,47 +1,29 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\Extension;
|
||||
|
||||
use Symfony\Component\Form\AbstractTypeExtension;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccess;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
* This extension create the possibility to add some text
|
||||
* after the input.
|
||||
*
|
||||
* This can be used to print the units of the field, or some text.
|
||||
*
|
||||
* This class must be extended by Extension class specifics to each input.
|
||||
* after the input.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* This can be used to print the units of the field, or some text.
|
||||
*
|
||||
* This class must be extended by Extension class specifics to each input.
|
||||
*/
|
||||
abstract class PostTextExtension extends AbstractTypeExtension
|
||||
{
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefined(array('post_text'));
|
||||
}
|
||||
|
||||
public function buildView(FormView $view, FormInterface $form, array $options)
|
||||
{
|
||||
if (array_key_exists('post_text', $options)) {
|
||||
@@ -50,4 +32,8 @@ abstract class PostTextExtension extends AbstractTypeExtension
|
||||
}
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefined(['post_text']);
|
||||
}
|
||||
}
|
||||
|
@@ -1,20 +1,10 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\Extension;
|
||||
@@ -22,15 +12,12 @@ namespace Chill\CustomFieldsBundle\Form\Extension;
|
||||
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
|
||||
|
||||
/**
|
||||
* This class add the PostTextExtension to integer fields
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* This class add the PostTextExtension to integer fields.
|
||||
*/
|
||||
class PostTextIntegerExtension extends PostTextExtension
|
||||
{
|
||||
public function getExtendedType()
|
||||
{
|
||||
return IntegerType::class;
|
||||
return IntegerType::class;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,20 +1,10 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\Extension;
|
||||
@@ -22,9 +12,7 @@ namespace Chill\CustomFieldsBundle\Form\Extension;
|
||||
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||
|
||||
/**
|
||||
* This class add the PostTextExtension to number fields
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* This class add the PostTextExtension to number fields.
|
||||
*/
|
||||
class PostTextNumberExtension extends PostTextExtension
|
||||
{
|
||||
@@ -32,5 +20,4 @@ class PostTextNumberExtension extends PostTextExtension
|
||||
{
|
||||
return NumberType::class;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,17 +1,22 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
* Return a choice widget with an "other" option
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*
|
||||
* Return a choice widget with an "other" option.
|
||||
*/
|
||||
class ChoiceWithOtherType extends AbstractType
|
||||
{
|
||||
@@ -22,7 +27,6 @@ class ChoiceWithOtherType extends AbstractType
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
|
||||
//add an 'other' entry in choices array
|
||||
$options['choices'][$this->otherValueLabel] = '_other';
|
||||
//ChoiceWithOther must always be expanded
|
||||
@@ -31,9 +35,8 @@ class ChoiceWithOtherType extends AbstractType
|
||||
$options['empty_data'] = null;
|
||||
|
||||
$builder
|
||||
->add('_other', TextType::class, array('required' => false))
|
||||
->add('_choices', ChoiceType::class, $options)
|
||||
;
|
||||
->add('_other', TextType::class, ['required' => false])
|
||||
->add('_choices', ChoiceType::class, $options);
|
||||
}
|
||||
|
||||
/* (non-PHPdoc)
|
||||
@@ -42,12 +45,11 @@ class ChoiceWithOtherType extends AbstractType
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver
|
||||
->setRequired(array('choices'))
|
||||
->setAllowedTypes('choices', array('array'))
|
||||
->setDefaults(array(
|
||||
'multiple' => false
|
||||
))
|
||||
;
|
||||
->setRequired(['choices'])
|
||||
->setAllowedTypes('choices', ['array'])
|
||||
->setDefaults([
|
||||
'multiple' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getBlockPrefix()
|
||||
|
@@ -1,35 +1,42 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\Type;
|
||||
|
||||
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
|
||||
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
|
||||
class ChoicesListType extends AbstractType
|
||||
{
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see \Symfony\Component\Form\AbstractType::buildForm()
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->add('name', TranslatableStringFormType::class)
|
||||
->add('active', CheckboxType::class, array(
|
||||
'required' => false
|
||||
))
|
||||
->add('active', CheckboxType::class, [
|
||||
'required' => false,
|
||||
])
|
||||
->add('slug', HiddenType::class)
|
||||
->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
|
||||
->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
|
||||
$form = $event->getForm();
|
||||
$data = $event->getData();
|
||||
|
||||
$formData = $form->getData();
|
||||
|
||||
if (NULL === $formData['slug']) {
|
||||
if (null === $formData['slug']) {
|
||||
$slug = uniqid(rand(), true);
|
||||
|
||||
$data['slug'] = $slug;
|
||||
@@ -38,11 +45,9 @@ class ChoicesListType extends AbstractType
|
||||
$data['slug'] = $formData['slug'];
|
||||
$event->setData($data);
|
||||
}
|
||||
})
|
||||
;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* @see \Symfony\Component\Form\FormTypeInterface::getName()
|
||||
@@ -51,5 +56,4 @@ class ChoicesListType extends AbstractType
|
||||
{
|
||||
return 'cf_choices_list';
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,15 +1,17 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*
|
||||
*/
|
||||
class ChoicesType extends AbstractType
|
||||
{
|
||||
public function getBlockPrefix()
|
||||
|
@@ -1,44 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\Type;
|
||||
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\JsonCustomFieldToArrayTransformer;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Chill\CustomFieldsBundle\Form\AdressType;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldTitle;
|
||||
|
||||
class CustomFieldType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* @var CustomFieldCompiler
|
||||
*/
|
||||
private $customFieldCompiler;
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $om;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldCompiler
|
||||
*/
|
||||
private $customFieldCompiler;
|
||||
|
||||
/**
|
||||
* @param ObjectManager $om
|
||||
*/
|
||||
public function __construct(ObjectManager $om, CustomFieldProvider $compiler)
|
||||
{
|
||||
$this->om = $om;
|
||||
@@ -55,17 +43,15 @@ class CustomFieldType extends AbstractType
|
||||
}
|
||||
}
|
||||
|
||||
public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver)
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver
|
||||
->setRequired(array('group'))
|
||||
->addAllowedTypes('group', array('Chill\CustomFieldsBundle\Entity\CustomFieldsGroup'))
|
||||
;
|
||||
->setRequired(['group'])
|
||||
->addAllowedTypes('group', ['Chill\CustomFieldsBundle\Entity\CustomFieldsGroup']);
|
||||
}
|
||||
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return 'custom_field';
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,23 +1,12 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
@@ -27,12 +16,10 @@ class CustomFieldsTitleType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return 'custom_field_title';
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,31 +1,21 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Champs-Libres Cooperative <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Form\Type;
|
||||
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
|
||||
/**
|
||||
* This type create a Choice field with custom fields as choices.
|
||||
@@ -33,19 +23,11 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
* This type can only be associated with a customFieldsGroup type. The field
|
||||
* is populated when the data (a customFieldsGroup entity) is associated with
|
||||
* the form
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class LinkedCustomFieldsType extends AbstractType
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
private $translatableStringHelper;
|
||||
|
||||
/**
|
||||
* The name for the choice field
|
||||
* The name for the choice field.
|
||||
*
|
||||
* Extracted from builder::getName
|
||||
*
|
||||
@@ -57,24 +39,71 @@ class LinkedCustomFieldsType extends AbstractType
|
||||
* the option of the form.
|
||||
*
|
||||
* @internal options are stored at the class level to be reused by appendChoice, after data are setted
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $options = array();
|
||||
private $options = [];
|
||||
|
||||
/**
|
||||
* @var TranslatableStringHelper
|
||||
*/
|
||||
private $translatableStringHelper;
|
||||
|
||||
public function __construct(TranslatableStringHelper $helper)
|
||||
{
|
||||
$this->translatableStringHelper = $helper;
|
||||
}
|
||||
|
||||
/**
|
||||
* append Choice on POST_SET_DATA event.
|
||||
*
|
||||
* Choices are extracted from custom_field_group (the data associated
|
||||
* with the root form)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function appendChoice(FormEvent $event)
|
||||
{
|
||||
$rootForm = $this->getRootForm($event->getForm());
|
||||
$group = $rootForm->getData();
|
||||
|
||||
if (null === $group) {
|
||||
return;
|
||||
}
|
||||
|
||||
$choices = [];
|
||||
|
||||
foreach ($group->getCustomFields() as $customFields) {
|
||||
$choices[$customFields->getSlug()] =
|
||||
$this->translatableStringHelper
|
||||
->localize($customFields->getName());
|
||||
}
|
||||
|
||||
$options = array_merge($this->options, [
|
||||
'choices' => $choices,
|
||||
]);
|
||||
|
||||
$event->getForm()->getParent()->add(
|
||||
$this->choiceName,
|
||||
ChoiceType::class,
|
||||
$options
|
||||
);
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$this->choiceName = $builder->getName();
|
||||
$this->options = $options;
|
||||
|
||||
$builder->addEventListener(FormEvents::POST_SET_DATA,
|
||||
array($this, 'appendChoice'))
|
||||
;
|
||||
$builder->addEventListener(
|
||||
FormEvents::POST_SET_DATA,
|
||||
[$this, 'appendChoice']
|
||||
);
|
||||
}
|
||||
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return 'custom_fields_group_linked_custom_fields';
|
||||
}
|
||||
|
||||
public function getParent()
|
||||
@@ -83,56 +112,16 @@ class LinkedCustomFieldsType extends AbstractType
|
||||
}
|
||||
|
||||
/**
|
||||
* append Choice on POST_SET_DATA event
|
||||
* Return the root form (i.e. produced from CustomFieldsGroupType::getForm).
|
||||
*
|
||||
* Choices are extracted from custom_field_group (the data associated
|
||||
* with the root form)
|
||||
*
|
||||
* @param FormEvent $event
|
||||
* @return void
|
||||
*/
|
||||
public function appendChoice(FormEvent $event)
|
||||
{
|
||||
$rootForm = $this->getRootForm($event->getForm());
|
||||
$group = $rootForm->getData();
|
||||
|
||||
if ($group === NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
$choices = array();
|
||||
foreach($group->getCustomFields() as $customFields) {
|
||||
$choices[$customFields->getSlug()] =
|
||||
$this->translatableStringHelper
|
||||
->localize($customFields->getName());
|
||||
}
|
||||
|
||||
$options = array_merge($this->options, array(
|
||||
'choices' => $choices,
|
||||
));
|
||||
|
||||
$event->getForm()->getParent()->add($this->choiceName, ChoiceType::class,
|
||||
$options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the root form (i.e. produced from CustomFieldsGroupType::getForm)
|
||||
*
|
||||
* @param FormInterface $form
|
||||
* @return FormInterface
|
||||
*/
|
||||
private function getRootForm(FormInterface $form)
|
||||
{
|
||||
if ($form->getParent() === NULL) {
|
||||
if ($form->getParent() === null) {
|
||||
return $form;
|
||||
} else {
|
||||
return $this->getRootForm($form->getParent());
|
||||
}
|
||||
}
|
||||
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return 'custom_fields_group_linked_custom_fields';
|
||||
return $this->getRootForm($form->getParent());
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,39 +1,23 @@
|
||||
<?php
|
||||
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
|
||||
class AppKernel extends Kernel
|
||||
{
|
||||
public function registerBundles()
|
||||
{
|
||||
return array(
|
||||
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
|
||||
new Chill\CustomFieldsBundle\ChillCustomFieldsBundle(),
|
||||
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
|
||||
new Symfony\Bundle\MonologBundle\MonologBundle(),
|
||||
new Symfony\Bundle\TwigBundle\TwigBundle(),
|
||||
new \Symfony\Bundle\AsseticBundle\AsseticBundle(),
|
||||
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
|
||||
new \Chill\MainBundle\ChillMainBundle,
|
||||
new \Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
|
||||
new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),
|
||||
new Chill\PersonBundle\ChillPersonBundle(),
|
||||
#add here all the required bundle (some bundle are not required)
|
||||
);
|
||||
}
|
||||
|
||||
public function registerContainerConfiguration(LoaderInterface $loader)
|
||||
{
|
||||
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCacheDir()
|
||||
{
|
||||
return sys_get_temp_dir().'/CustomFieldsBundle/cache';
|
||||
return sys_get_temp_dir() . '/CustomFieldsBundle/cache';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,7 +25,29 @@ class AppKernel extends Kernel
|
||||
*/
|
||||
public function getLogDir()
|
||||
{
|
||||
return $this->getRootDir().'/../logs';
|
||||
return $this->getRootDir() . '/../logs';
|
||||
}
|
||||
|
||||
public function registerBundles()
|
||||
{
|
||||
return [
|
||||
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
|
||||
new Chill\CustomFieldsBundle\ChillCustomFieldsBundle(),
|
||||
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
|
||||
new Symfony\Bundle\MonologBundle\MonologBundle(),
|
||||
new Symfony\Bundle\TwigBundle\TwigBundle(),
|
||||
new \Symfony\Bundle\AsseticBundle\AsseticBundle(),
|
||||
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
|
||||
new \Chill\MainBundle\ChillMainBundle(),
|
||||
new \Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
|
||||
new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),
|
||||
new Chill\PersonBundle\ChillPersonBundle(),
|
||||
//add here all the required bundle (some bundle are not required)
|
||||
];
|
||||
}
|
||||
|
||||
public function registerContainerConfiguration(LoaderInterface $loader)
|
||||
{
|
||||
$loader->load(__DIR__ . '/config/config_' . $this->getEnvironment() . '.yml');
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,11 +1,18 @@
|
||||
<?php
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationRegistry;
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Composer\Autoload\ClassLoader;
|
||||
use Doctrine\Common\Annotations\AnnotationRegistry;
|
||||
|
||||
/** @var ClassLoader $loader */
|
||||
$loader = require __DIR__.'/../../../../../vendor/autoload.php';
|
||||
$loader = require __DIR__ . '/../../../../../vendor/autoload.php';
|
||||
|
||||
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
|
||||
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
|
||||
|
||||
return $loader;
|
||||
|
@@ -1,7 +1,14 @@
|
||||
<?php
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Symfony\Component\Debug\Debug;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
// If you don't want to setup permissions the proper way, just uncomment the following PHP line
|
||||
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
|
||||
@@ -11,16 +18,17 @@ use Symfony\Component\Debug\Debug;
|
||||
// Feel free to remove this, extend it, or make something more sophisticated.
|
||||
if (isset($_SERVER['HTTP_CLIENT_IP'])
|
||||
|| isset($_SERVER['HTTP_X_FORWARDED_FOR'])
|
||||
|| !(in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1')) || php_sapi_name() === 'cli-server')
|
||||
|| !(in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', 'fe80::1', '::1']) || php_sapi_name() === 'cli-server')
|
||||
) {
|
||||
header('HTTP/1.0 403 Forbidden');
|
||||
exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
|
||||
|
||||
exit('You are not allowed to access this file. Check ' . basename(__FILE__) . ' for more information.');
|
||||
}
|
||||
|
||||
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
|
||||
$loader = require_once __DIR__ . '/../app/bootstrap.php.cache';
|
||||
Debug::enable();
|
||||
|
||||
require_once __DIR__.'/../app/AppKernel.php';
|
||||
require_once __DIR__ . '/../app/AppKernel.php';
|
||||
|
||||
$kernel = new AppKernel('dev', true);
|
||||
$kernel->loadClassCache();
|
||||
|
@@ -1,103 +1,94 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>, <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Service;
|
||||
|
||||
use LogicException;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Receive all the services tagged with 'chill.custom_field'.
|
||||
*
|
||||
*
|
||||
* The services tagged with 'chill.custom_field' are services used to declare
|
||||
* a new custom field type. The tag must contain a 'type' variable (that must
|
||||
* be unique), this type is used to identify this custom field in the form
|
||||
* declration
|
||||
*
|
||||
* declration
|
||||
*
|
||||
* For example (in services.yml) :
|
||||
* services:
|
||||
* chill.icp2.type:
|
||||
* tags:
|
||||
* - { name: 'chill.custom_field', type: 'ICPC2' }
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class CustomFieldProvider implements ContainerAwareInterface
|
||||
{
|
||||
/** @var array $servicesByType The services indexes by the type */
|
||||
private $servicesByType = array();
|
||||
|
||||
/** @var Container $container The container */
|
||||
private $container;
|
||||
|
||||
/**
|
||||
* Add a new custom field to the provider
|
||||
*
|
||||
* @var Container The container
|
||||
*/
|
||||
private $container;
|
||||
|
||||
/**
|
||||
* @var array The services indexes by the type
|
||||
*/
|
||||
private $servicesByType = [];
|
||||
|
||||
/**
|
||||
* Add a new custom field to the provider.
|
||||
*
|
||||
* @param type $serviceName The name of the service (declared in service.yml)
|
||||
* @param type $type The type of the service (that is used in the form to
|
||||
* @param type $type The type of the service (that is used in the form to
|
||||
* add this type)
|
||||
*/
|
||||
public function addCustomField($serviceName, $type)
|
||||
{
|
||||
$this->servicesByType[$type] = $serviceName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a custom field stored in the provider. The custom field is identified
|
||||
* by its type.
|
||||
*
|
||||
* @param string $type The type of the wanted service
|
||||
* @return CustomFieldInterface
|
||||
*/
|
||||
public function getCustomFieldByType($type)
|
||||
{
|
||||
if (isset($this->servicesByType[$type])) {
|
||||
return $this->servicesByType[$type];
|
||||
} else {
|
||||
throw new \LogicException('the custom field with type '.$type.' '
|
||||
. 'is not found');
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-PHPdoc)
|
||||
* @see \Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer()
|
||||
*/
|
||||
public function setContainer(ContainerInterface $container = null)
|
||||
{
|
||||
if ($container === null) {
|
||||
throw new \LogicException('container should not be null');
|
||||
}
|
||||
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the custom fields known by the provider
|
||||
*
|
||||
* Get all the custom fields known by the provider.
|
||||
*
|
||||
* @return array Array of the known custom fields indexed by the type.
|
||||
*/
|
||||
public function getAllFields()
|
||||
{
|
||||
return $this->servicesByType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a custom field stored in the provider. The custom field is identified
|
||||
* by its type.
|
||||
*
|
||||
* @param string $type The type of the wanted service
|
||||
*
|
||||
* @return CustomFieldInterface
|
||||
*/
|
||||
public function getCustomFieldByType($type)
|
||||
{
|
||||
if (isset($this->servicesByType[$type])) {
|
||||
return $this->servicesByType[$type];
|
||||
}
|
||||
|
||||
throw new LogicException('the custom field with type ' . $type . ' '
|
||||
. 'is not found');
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-PHPdoc)
|
||||
* @see \Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer()
|
||||
*/
|
||||
public function setContainer(?ContainerInterface $container = null)
|
||||
{
|
||||
if (null === $container) {
|
||||
throw new LogicException('container should not be null');
|
||||
}
|
||||
|
||||
$this->container = $container;
|
||||
}
|
||||
}
|
||||
|
@@ -1,90 +1,78 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>, <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Service;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
/**
|
||||
* Helpers for manipulating custom fields.
|
||||
*
|
||||
*
|
||||
* Herlpers for getting a certain custom field, for getting the raw value
|
||||
* of a custom field and for rendering the value of a custom field.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*
|
||||
*/
|
||||
class CustomFieldsHelper
|
||||
{
|
||||
/** @var EntityManagerInterface $em The entity manager */
|
||||
/**
|
||||
* @var EntityManagerInterface The entity manager
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/** @var CustomFieldProvider $provider Provider of all the declared custom
|
||||
|
||||
/** @var CustomFieldProvider Provider of all the declared custom
|
||||
* fields */
|
||||
private $provider;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Constructor.
|
||||
*
|
||||
* @param EntityManagerInterface $em The entity manager
|
||||
* @param CustomFieldProvider $provider The customfield provider that
|
||||
* @param CustomFieldProvider $provider The customfield provider that
|
||||
* contains all the declared custom fields
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $em,
|
||||
CustomFieldProvider $provider)
|
||||
public function __construct(
|
||||
EntityManagerInterface $em,
|
||||
CustomFieldProvider $provider
|
||||
)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->provider = $provider;
|
||||
}
|
||||
|
||||
|
||||
public function isEmptyValue(array $fields, CustomField $customField)
|
||||
{
|
||||
$slug = $customField->getSlug();
|
||||
$rawValue = (isset($fields[$slug])) ? $fields[$slug] : null;
|
||||
$customFieldType = $this->provider->getCustomFieldByType($customField->getType());
|
||||
|
||||
$customFieldType = $this->provider->getCustomFieldByType($customField->getType());
|
||||
|
||||
$deserializedValue = $customFieldType->deserialize($rawValue, $customField);
|
||||
|
||||
|
||||
return $customFieldType->isEmptyValue($deserializedValue, $customField);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Render the value of a custom field
|
||||
*
|
||||
* Render the value of a custom field.
|
||||
*
|
||||
* @param array $fields the **raw** array, as stored in the db
|
||||
* @param CustomField $customField the customField entity
|
||||
* @param string $documentType The type of document in which the rendered value is displayed ('html' or 'csv').
|
||||
*
|
||||
* @throws CustomFieldsHelperException if slug is missing
|
||||
*
|
||||
* @return The representation of the value the customField.
|
||||
*/
|
||||
public function renderCustomField(array $fields, CustomField $customField, $documentType='html')
|
||||
public function renderCustomField(array $fields, CustomField $customField, $documentType = 'html')
|
||||
{
|
||||
$slug = $customField->getSlug();
|
||||
$rawValue = (isset($fields[$slug])) ? $fields[$slug] : null;
|
||||
$customFieldType = $this->provider->getCustomFieldByType($customField->getType());
|
||||
|
||||
$customFieldType = $this->provider->getCustomFieldByType($customField->getType());
|
||||
|
||||
return $customFieldType->render($rawValue, $customField, $documentType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,36 +1,25 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>, <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Service;
|
||||
|
||||
class CustomFieldsHelperException extends \Exception
|
||||
use Exception;
|
||||
|
||||
class CustomFieldsHelperException extends Exception
|
||||
{
|
||||
public static function customFieldsGroupNotFound($entity)
|
||||
public static function customFieldsGroupNotFound($entity)
|
||||
{
|
||||
return new CustomFieldsHelperException("The customFieldsGroups associated with $entity are not found");
|
||||
return new CustomFieldsHelperException("The customFieldsGroups associated with {$entity} are not found");
|
||||
}
|
||||
|
||||
|
||||
public static function slugIsMissing()
|
||||
{
|
||||
return new CustomFieldsHelperException("The slug is missing");
|
||||
return new CustomFieldsHelperException('The slug is missing');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,72 +1,51 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>, <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Templating\Twig;
|
||||
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldsHelper;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
/**
|
||||
* Add the following Twig Extension :
|
||||
* * chill_custom_field_widget : to render the value of the custom field,
|
||||
* * chill_custom_field_label : to render the label of the custom field,
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* * chill_custom_field_label : to render the label of the custom field,.
|
||||
*/
|
||||
class CustomFieldRenderingTwig extends AbstractExtension implements ContainerAwareInterface
|
||||
{
|
||||
|
||||
/** @var Container $container The container */
|
||||
/**
|
||||
* @var Container The container
|
||||
*/
|
||||
private $container;
|
||||
|
||||
/** @var array $defaultParams The default parameters */
|
||||
private $defaultParams = array(
|
||||
'label_layout' => 'ChillCustomFieldsBundle:CustomField:render_label.html.twig'
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* @var CustomFieldsHelper
|
||||
*/
|
||||
private $customFieldsHelper;
|
||||
|
||||
|
||||
/**
|
||||
* @var array The default parameters
|
||||
*/
|
||||
private $defaultParams = [
|
||||
'label_layout' => 'ChillCustomFieldsBundle:CustomField:render_label.html.twig',
|
||||
];
|
||||
|
||||
public function __construct(CustomFieldsHelper $customFieldsHelper)
|
||||
{
|
||||
$this->customFieldsHelper = $customFieldsHelper;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-PHPdoc)
|
||||
* @see \Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer()
|
||||
*/
|
||||
public function setContainer(ContainerInterface $container = null)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-PHPdoc)
|
||||
* @see Twig_Extension::getFunctions()
|
||||
@@ -74,73 +53,82 @@ class CustomFieldRenderingTwig extends AbstractExtension implements ContainerAwa
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('chill_custom_field_widget', array(
|
||||
new TwigFunction('chill_custom_field_widget', [
|
||||
$this,
|
||||
'renderWidget'
|
||||
), array(
|
||||
'is_safe' => array(
|
||||
'html'
|
||||
)
|
||||
)),
|
||||
new TwigFunction('chill_custom_field_label', array(
|
||||
'renderWidget',
|
||||
], [
|
||||
'is_safe' => [
|
||||
'html',
|
||||
],
|
||||
]),
|
||||
new TwigFunction('chill_custom_field_label', [
|
||||
$this,
|
||||
'renderLabel'
|
||||
), array(
|
||||
'is_safe' => array(
|
||||
'html'
|
||||
)
|
||||
)),
|
||||
new TwigFunction('chill_custom_field_is_empty', array(
|
||||
'renderLabel',
|
||||
], [
|
||||
'is_safe' => [
|
||||
'html',
|
||||
],
|
||||
]),
|
||||
new TwigFunction('chill_custom_field_is_empty', [
|
||||
$this,
|
||||
'isEmptyValue'
|
||||
))
|
||||
'isEmptyValue',
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Twig_ExtensionInterface::getName()
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'chill_custom_fields_rendering';
|
||||
}
|
||||
|
||||
public function isEmptyValue($fields, CustomField $customField)
|
||||
{
|
||||
return $this->customFieldsHelper
|
||||
->isEmptyValue($fields, $customField);
|
||||
}
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Twig_ExtensionInterface::getName()
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'chill_custom_fields_rendering';
|
||||
}
|
||||
|
||||
/**
|
||||
* Twig Extension that is used to render the label of a custom field.
|
||||
*
|
||||
*
|
||||
* @param CustomField $customField Either a customField OR a customizable_entity OR the FQDN of the entity
|
||||
* @param array $params The parameters for rendering. Currently, 'label_layout' allow to choose a different label. Default is 'ChillCustomFieldsBundle:CustomField:render_label.html.twig'
|
||||
*
|
||||
* @return string HTML representation of the custom field label.
|
||||
*/
|
||||
public function renderLabel(CustomField $customField, array $params = array())
|
||||
public function renderLabel(CustomField $customField, array $params = [])
|
||||
{
|
||||
$resolvedParams = array_merge($this->defaultParams, $params);
|
||||
|
||||
|
||||
return $this->container->get('templating')
|
||||
->render($resolvedParams['label_layout'], array('customField' => $customField));
|
||||
->render($resolvedParams['label_layout'], ['customField' => $customField]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Twig extension that is used to render the value of a custom field.
|
||||
*
|
||||
*
|
||||
* The presentation of the value is influenced by the document type.
|
||||
*
|
||||
*
|
||||
* @param array $fields The array raw, as stored in the db
|
||||
* @param CustomField $customField Either a customField OR a customizable_entity OR the FQDN of the entity
|
||||
* @param string $documentType The type of the document (csv, html)
|
||||
* @param string $slug The slug of the custom field ONLY necessary if the first argument is NOT a CustomField instance
|
||||
*
|
||||
* @return string HTML representation of the custom field value, as described in the CustomFieldInterface. Is HTML safe
|
||||
*/
|
||||
public function renderWidget(array $fields, CustomField $customField, $documentType='html')
|
||||
public function renderWidget(array $fields, CustomField $customField, $documentType = 'html')
|
||||
{
|
||||
return $this->customFieldsHelper
|
||||
->renderCustomField($fields, $customField, $documentType);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-PHPdoc)
|
||||
* @see \Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer()
|
||||
*/
|
||||
public function setContainer(?ContainerInterface $container = null)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
}
|
||||
|
@@ -1,71 +1,48 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
||||
* <http://www.champs-libres.coop>, <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Templating\Twig;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
/**
|
||||
* Add the following Twig Extension :
|
||||
* * chill_custom_fields_group_widget : to render the value of a custom field
|
||||
* * group
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @author Marc Ducobu <marc.ducobu@champs-libres.coop>
|
||||
* * group.
|
||||
*/
|
||||
class CustomFieldsGroupRenderingTwig extends AbstractExtension implements ContainerAwareInterface
|
||||
{
|
||||
|
||||
/** @var Container $container The container */
|
||||
private $container;
|
||||
|
||||
/** @var array $defaultParams The default parameters */
|
||||
private $defaultParams = array(
|
||||
'layout' => 'ChillCustomFieldsBundle:CustomFieldsGroup:render.html.twig',
|
||||
'show_empty' => True
|
||||
);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param boolean $showEmptyValues whether the empty values must be rendered
|
||||
* @var Container The container
|
||||
*/
|
||||
private $container;
|
||||
|
||||
/**
|
||||
* @var array The default parameters
|
||||
*/
|
||||
private $defaultParams = [
|
||||
'layout' => 'ChillCustomFieldsBundle:CustomFieldsGroup:render.html.twig',
|
||||
'show_empty' => true,
|
||||
];
|
||||
|
||||
/**
|
||||
* @param bool $showEmptyValues whether the empty values must be rendered
|
||||
*/
|
||||
public function __construct($showEmptyValues)
|
||||
{
|
||||
$this->defaultParams['show_empty'] = $showEmptyValues;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-PHPdoc)
|
||||
* @see \Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer()
|
||||
*/
|
||||
public function setContainer(ContainerInterface $container = null)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-PHPdoc)
|
||||
* @see Twig_Extension::getFunctions()
|
||||
@@ -73,48 +50,58 @@ class CustomFieldsGroupRenderingTwig extends AbstractExtension implements Contai
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('chill_custom_fields_group_widget', array(
|
||||
new TwigFunction('chill_custom_fields_group_widget', [
|
||||
$this,
|
||||
'renderWidget'
|
||||
), array(
|
||||
'is_safe' => array(
|
||||
'html'
|
||||
)
|
||||
)),
|
||||
'renderWidget',
|
||||
], [
|
||||
'is_safe' => [
|
||||
'html',
|
||||
],
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/* (non-PHPdoc)
|
||||
* @see Twig_ExtensionInterface::getName()
|
||||
*/
|
||||
public function getName()
|
||||
public function getName()
|
||||
{
|
||||
return 'chill_custom_fields_group_rendering';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Twig extension that is used to render the value of a custom field group.
|
||||
*
|
||||
*
|
||||
* The presentation of the value is influenced by the document type.
|
||||
*
|
||||
*
|
||||
* @param array $fields The array raw, as stored in the db
|
||||
* @param CustomFieldsGroud $customFielsGroup The custom field group
|
||||
* @param string $documentType The type of the document (csv, html)
|
||||
* @param array $params The parameters for rendering :
|
||||
* @param array $params The parameters for rendering :
|
||||
* - layout : allow to choose a different layout by default :
|
||||
* ChillCustomFieldsBundle:CustomFieldsGroup:render.html.twig
|
||||
* - show_empty : force show empty field
|
||||
* @return string HTML representation of the custom field group value, as described in
|
||||
*
|
||||
* @return string HTML representation of the custom field group value, as described in
|
||||
* the CustomFieldInterface. Is HTML safe
|
||||
*/
|
||||
public function renderWidget(array $fields, $customFielsGroup, $documentType='html', array $params = array())
|
||||
public function renderWidget(array $fields, $customFielsGroup, $documentType = 'html', array $params = [])
|
||||
{
|
||||
$resolvedParams = array_merge($this->defaultParams, $params);
|
||||
|
||||
|
||||
return $this->container->get('templating')
|
||||
->render($resolvedParams['layout'], array(
|
||||
'cFGroup' => $customFielsGroup,
|
||||
'cFData' => $fields,
|
||||
'show_empty' => $resolvedParams['show_empty']));
|
||||
->render($resolvedParams['layout'], [
|
||||
'cFGroup' => $customFielsGroup,
|
||||
'cFData' => $fields,
|
||||
'show_empty' => $resolvedParams['show_empty'], ]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-PHPdoc)
|
||||
* @see \Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer()
|
||||
*/
|
||||
public function setContainer(?ContainerInterface $container = null)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,10 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.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/>.
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Tests\Config;
|
||||
@@ -23,48 +12,49 @@ namespace Chill\CustomFieldsBundle\Tests\Config;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* Test the option Customizables_entities
|
||||
* Test the option Customizables_entities.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class ConfigCustomizablesEntitiesTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
* Test that the config does work if the option
|
||||
* chill_custom_fields.customizables_entities IS NOT present
|
||||
*
|
||||
* In this case, parameter 'chill_custom_fields.customizables_entities'
|
||||
* should be an empty array in container
|
||||
*/
|
||||
public function testNotPresentInConfig()
|
||||
{
|
||||
self::bootKernel(array('environment' => 'test'));
|
||||
$customizableEntities = static::$kernel->getContainer()
|
||||
->getParameter('chill_custom_fields.customizables_entities');
|
||||
|
||||
$this->assertInternalType('array', $customizableEntities);
|
||||
$this->assertCount(1, $customizableEntities);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the 'chill_custom_fields.customizables_entities' is filled
|
||||
* correctly with a minimal configuration.
|
||||
*
|
||||
*
|
||||
* @internal use a custom config environment
|
||||
*/
|
||||
public function testNotEmptyConfig()
|
||||
{
|
||||
self::bootKernel(array('environment' => 'test_customizable_entities_test_not_empty_config'));
|
||||
self::bootKernel(['environment' => 'test_customizable_entities_test_not_empty_config']);
|
||||
$customizableEntities = static::$kernel->getContainer()
|
||||
->getParameter('chill_custom_fields.customizables_entities');
|
||||
|
||||
->getParameter('chill_custom_fields.customizables_entities');
|
||||
|
||||
$this->assertInternalType('array', $customizableEntities);
|
||||
$this->assertCount(2, $customizableEntities);
|
||||
|
||||
foreach($customizableEntities as $key => $config) {
|
||||
|
||||
foreach ($customizableEntities as $key => $config) {
|
||||
$this->assertInternalType('array', $config);
|
||||
$this->assertArrayHasKey('name', $config);
|
||||
$this->assertArrayHasKey('class', $config);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the config does work if the option
|
||||
* chill_custom_fields.customizables_entities IS NOT present.
|
||||
*
|
||||
* In this case, parameter 'chill_custom_fields.customizables_entities'
|
||||
* should be an empty array in container
|
||||
*/
|
||||
public function testNotPresentInConfig()
|
||||
{
|
||||
self::bootKernel(['environment' => 'test']);
|
||||
$customizableEntities = static::$kernel->getContainer()
|
||||
->getParameter('chill_custom_fields.customizables_entities');
|
||||
|
||||
$this->assertInternalType('array', $customizableEntities);
|
||||
$this->assertCount(1, $customizableEntities);
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,20 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Tests\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class CustomFieldControllerTest extends WebTestCase
|
||||
{
|
||||
/*
|
||||
@@ -51,5 +62,5 @@ class CustomFieldControllerTest extends WebTestCase
|
||||
$this->assertNotRegExp('/Foo/', $client->getResponse()->getContent());
|
||||
}
|
||||
|
||||
*/
|
||||
*/
|
||||
}
|
||||
|
@@ -1,21 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Tests\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Bundle\FrameworkBundle\Client;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class CustomFieldsGroupControllerTest extends WebTestCase
|
||||
{
|
||||
|
||||
public function testCompleteScenario()
|
||||
{
|
||||
self::bootKernel(array('environment' => 'test_customizable_entities_test_not_empty_config'));
|
||||
self::bootKernel(['environment' => 'test_customizable_entities_test_not_empty_config']);
|
||||
// Create a new client to browse the application
|
||||
$client = static::createClient(array(), array(
|
||||
$client = static::createClient([], [
|
||||
'PHP_AUTH_USER' => 'admin',
|
||||
'PHP_AUTH_PW' => 'olala',
|
||||
));
|
||||
'PHP_AUTH_PW' => 'olala',
|
||||
]);
|
||||
|
||||
//create the entity
|
||||
$this->createCustomFieldsGroup($client);
|
||||
@@ -28,47 +38,59 @@ class CustomFieldsGroupControllerTest extends WebTestCase
|
||||
{
|
||||
// Create a new entry in the database
|
||||
$crawler = $client->request('GET', '/fr/admin/customfieldsgroup/');
|
||||
|
||||
$this->assertEquals(200, $client->getResponse()->getStatusCode(),
|
||||
"Unexpected HTTP status code for GET /customfieldsgroup/");
|
||||
|
||||
$this->assertEquals(
|
||||
200,
|
||||
$client->getResponse()->getStatusCode(),
|
||||
'Unexpected HTTP status code for GET /customfieldsgroup/'
|
||||
);
|
||||
|
||||
$crawler = $client->click($crawler->selectLink('Créer un nouveau groupe')->link());
|
||||
|
||||
|
||||
// Fill in the form and submit it
|
||||
$form = $crawler->selectButton('Créer')->form(array(
|
||||
'custom_fields_group[name][fr]' => 'Test',
|
||||
'custom_fields_group[entity]' => 'Chill\PersonBundle\Entity\Person'
|
||||
));
|
||||
$form = $crawler->selectButton('Créer')->form([
|
||||
'custom_fields_group[name][fr]' => 'Test',
|
||||
'custom_fields_group[entity]' => 'Chill\PersonBundle\Entity\Person',
|
||||
]);
|
||||
|
||||
$crawler = $client->submit($form);
|
||||
|
||||
|
||||
$crawler = $client->followRedirect();
|
||||
|
||||
// Check data in the show view
|
||||
$this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(),
|
||||
'Missing element td:contains("Test")');
|
||||
$this->assertGreaterThan(
|
||||
0,
|
||||
$crawler->filter('td:contains("Test")')->count(),
|
||||
'Missing element td:contains("Test")'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
private function editCustomFieldsGroup(Client $client)
|
||||
{
|
||||
$crawler = $client->request('GET', '/fr/admin/customfieldsgroup/');
|
||||
$links = $crawler->selectLink('Modifier');
|
||||
|
||||
$this->assertGreaterThan(0, $links->count(),
|
||||
"We can't find a 'Modifier' link on the index page");
|
||||
|
||||
$this->assertGreaterThan(
|
||||
0,
|
||||
$links->count(),
|
||||
"We can't find a 'Modifier' link on the index page"
|
||||
);
|
||||
$crawler = $client->click($links->last()->link());
|
||||
|
||||
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||
|
||||
$form = $crawler->selectButton('Mettre à jour')->form(array(
|
||||
'custom_fields_group[name][fr]' => 'Foo',
|
||||
));
|
||||
|
||||
$form = $crawler->selectButton('Mettre à jour')->form([
|
||||
'custom_fields_group[name][fr]' => 'Foo',
|
||||
]);
|
||||
|
||||
$client->submit($form);
|
||||
$crawler = $client->followRedirect();
|
||||
|
||||
// Check the element contains an attribute with value equals "Foo"
|
||||
$this->assertGreaterThan(0, $crawler->filter('[value="Foo"]')->count(),
|
||||
'Missing element [value="Foo"]');
|
||||
$this->assertGreaterThan(
|
||||
0,
|
||||
$crawler->filter('[value="Foo"]')->count(),
|
||||
'Missing element [value="Foo"]'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -1,86 +1,71 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2014 Champs-Libres Coopérative <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
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>
|
||||
* Give useful method to prepare tests regarding custom fields.
|
||||
*/
|
||||
trait CustomFieldTestHelper
|
||||
{
|
||||
|
||||
/**
|
||||
* Prepare a crawler containing the rendering of a customField
|
||||
*
|
||||
* 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 function getCrawlerForField(CustomField $field, $locale = 'en')
|
||||
{
|
||||
$kernel = static::$kernel;
|
||||
|
||||
|
||||
//check a kernel is accessible
|
||||
$customFieldsGroup = $this->createMock('Chill\CustomFieldsBundle\Entity\CustomFieldsGroup');
|
||||
$customFieldsGroup->expects($this->once())
|
||||
->method('getActiveCustomFields')
|
||||
->will($this->returnValue(array($field)));
|
||||
|
||||
->will($this->returnValue([$field]));
|
||||
|
||||
$request = $this->createMock('Symfony\Component\HttpFoundation\Request');
|
||||
$request->expects($this->any())
|
||||
->method('getLocale')
|
||||
->will($this->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))
|
||||
$form = $builder->add(
|
||||
'tested',
|
||||
'custom_field',
|
||||
['group' => $customFieldsGroup]
|
||||
)
|
||||
->getForm();
|
||||
|
||||
|
||||
$kernel->getContainer()->get('twig.loader')
|
||||
->addPath($kernel->getContainer()->getParameter('kernel.root_dir').
|
||||
->addPath($kernel->getContainer()->getParameter('kernel.root_dir') .
|
||||
'/Resources/views/', $namespace = 'test');
|
||||
|
||||
$content = $kernel
|
||||
->getContainer()->get('templating')
|
||||
->render('@test/CustomField/simple_form_render.html.twig', array(
|
||||
->render('@test/CustomField/simple_form_render.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
'inputKeys' => array('tested')
|
||||
));
|
||||
'inputKeys' => ['tested'],
|
||||
]);
|
||||
|
||||
$crawler = new Crawler();
|
||||
$crawler->addHtmlContent($content);
|
||||
|
||||
|
||||
return $crawler;
|
||||
}
|
||||
}
|
||||
|
@@ -1,69 +1,443 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Tests;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldChoice;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* This class cover the test of CustomFieldChoice.
|
||||
*
|
||||
* Function currently covered:
|
||||
*
|
||||
*
|
||||
* Function currently covered:
|
||||
*
|
||||
* - deserialize
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class CustomFieldsChoiceTest extends KernelTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Chill\CustomFieldsBundle\Service\CustomFieldProvider
|
||||
*/
|
||||
private $cfProvider;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Chill\CustomFieldsBundle\CustomFields\CustomFieldChoice
|
||||
*/
|
||||
private $cfChoice;
|
||||
|
||||
|
||||
/**
|
||||
* @var \Chill\CustomFieldsBundle\Service\CustomFieldProvider
|
||||
*/
|
||||
private $cfProvider;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
static::bootKernel();
|
||||
|
||||
|
||||
$this->cfProvider = static::$kernel->getContainer()
|
||||
->get('chill.custom_field.provider');
|
||||
->get('chill.custom_field.provider');
|
||||
$this->cfChoice = $this->cfProvider->getCustomFieldByType('choice');
|
||||
}
|
||||
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* provide empty data in different possible representations.
|
||||
* Those data are supposed to be deserialized.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function emptyDataProvider()
|
||||
{
|
||||
return [
|
||||
// 0
|
||||
[
|
||||
// signle
|
||||
'',
|
||||
],
|
||||
// 1
|
||||
[
|
||||
// single
|
||||
null,
|
||||
],
|
||||
// 2
|
||||
[
|
||||
// signle with allow other
|
||||
['_other' => 'something', '_choices' => ''],
|
||||
],
|
||||
// 3
|
||||
[
|
||||
// multiple
|
||||
[],
|
||||
],
|
||||
// 4
|
||||
[
|
||||
// multiple with allow other
|
||||
['_other' => 'something', '_choices' => []],
|
||||
],
|
||||
// 5
|
||||
[
|
||||
// multiple with allow other
|
||||
['_other' => '', '_choices' => []],
|
||||
],
|
||||
// 6
|
||||
[
|
||||
// empty
|
||||
['_other' => null, '_choices' => null],
|
||||
],
|
||||
// 7
|
||||
[
|
||||
// empty
|
||||
[null],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function serializedRepresentationDataProvider()
|
||||
{
|
||||
return [
|
||||
[
|
||||
// multiple => false, allow_other => false
|
||||
'my-value',
|
||||
],
|
||||
[
|
||||
// multiple => true, allow_ther => false
|
||||
['my-value'],
|
||||
],
|
||||
[
|
||||
// multiple => false, allow_other => true, current value not in other
|
||||
['_other' => '', '_choices' => 'my-value'],
|
||||
],
|
||||
[
|
||||
// multiple => true, allow_other => true, current value not in other
|
||||
['_other' => '', '_choices' => ['my-value']],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the representation of the data is deserialized to an array text
|
||||
* with an "allow_other" field.
|
||||
*
|
||||
* This test does not covers the case when the selected value is `_other`
|
||||
*
|
||||
* @param type $data
|
||||
* @dataProvider serializedRepresentationDataProvider
|
||||
*/
|
||||
public function testDeserializeMultipleChoiceWithOther($data)
|
||||
{
|
||||
$customField = $this->generateCustomField([
|
||||
CustomFieldChoice::ALLOW_OTHER => true,
|
||||
CustomFieldChoice::MULTIPLE => true,
|
||||
]);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(
|
||||
['_other' => '', '_choices' => ['my-value']],
|
||||
$deserialized
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the representation of the data is deserialized to an array text
|
||||
* with an "allow_other" field.
|
||||
*
|
||||
* This test covers :
|
||||
* - the case when the selected value is `_other`
|
||||
* - result is null
|
||||
*/
|
||||
public function testDeserializeMultipleChoiceWithOtherOtherCases()
|
||||
{
|
||||
$customField = $this->generateCustomField([
|
||||
CustomFieldChoice::ALLOW_OTHER => true,
|
||||
CustomFieldChoice::MULTIPLE => true,
|
||||
]);
|
||||
|
||||
// selected value is _other
|
||||
// from single to multiple
|
||||
$data = ['_other' => 'something', '_choices' => '_other'];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(
|
||||
['_other' => 'something', '_choices' => ['_other']],
|
||||
$deserialized
|
||||
);
|
||||
|
||||
// from multiple to multiple
|
||||
$data = ['_other' => 'something', '_choices' => ['_other', 'something']];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(
|
||||
['_other' => 'something', '_choices' => ['_other', 'something']],
|
||||
$deserialized
|
||||
);
|
||||
|
||||
// test with null value
|
||||
// from single to multiple
|
||||
$data = ['_other' => '', '_choices' => ''];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(
|
||||
['_other' => '', '_choices' => ['']],
|
||||
$deserialized
|
||||
);
|
||||
|
||||
// from multiple to multiple
|
||||
$data = ['_other' => '', '_choices' => []];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(
|
||||
['_other' => '', '_choices' => []],
|
||||
$deserialized
|
||||
);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(['_other' => null, '_choices' => null], $customField);
|
||||
$this->assertSame(['_other' => '', '_choices' => [null]], $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(['_other' => null, '_choices' => ''], $customField);
|
||||
$this->assertSame(['_other' => '', '_choices' => ['']], $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize([null], $customField);
|
||||
$this->assertSame(['_other' => '', '_choices' => [null]], $deserialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the representation of the data is deserialized to an array text
|
||||
* **without** an "allow_other" field.
|
||||
*
|
||||
* @param type $data
|
||||
* @dataProvider serializedRepresentationDataProvider
|
||||
*/
|
||||
public function testDeserializeMultipleChoiceWithoutOther($data)
|
||||
{
|
||||
$customField = $this->generateCustomField([
|
||||
CustomFieldChoice::ALLOW_OTHER => false,
|
||||
CustomFieldChoice::MULTIPLE => true,
|
||||
]);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(['my-value'], $deserialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the representation of the data is deserialized to an array text
|
||||
* **without** an "allow_other" field.
|
||||
*
|
||||
* Covered cases :
|
||||
* - NULL values
|
||||
*/
|
||||
public function testDeserializeMultipleChoiceWithoutOtherOtherCases()
|
||||
{
|
||||
$customField = $this->generateCustomField([
|
||||
CustomFieldChoice::ALLOW_OTHER => false,
|
||||
CustomFieldChoice::MULTIPLE => true,
|
||||
]);
|
||||
|
||||
// from single to multiple
|
||||
$data = 'my-value';
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(['my-value'], $deserialized);
|
||||
|
||||
// from multiple to multiple
|
||||
$data = ['my-value'];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(['my-value'], $deserialized);
|
||||
|
||||
// from multiple
|
||||
$data = [null];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame([null], $deserialized);
|
||||
|
||||
$data = ['_other' => null, '_choices' => [null]];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame([null], $deserialized);
|
||||
|
||||
//from single
|
||||
$data = ['_other' => null, '_choices' => null];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame([null], $deserialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the representation of the data is deserialized to a single text
|
||||
* with an "allow_other" field.
|
||||
*
|
||||
* If the value is in _other, the _other value should be in the _other field.
|
||||
*
|
||||
* @param type $data
|
||||
* @dataProvider serializedRepresentationDataProvider
|
||||
*/
|
||||
public function testDeserializeSingleChoiceWithOther($data)
|
||||
{
|
||||
$customField = $this->generateCustomField([
|
||||
CustomFieldChoice::ALLOW_OTHER => true,
|
||||
CustomFieldChoice::MULTIPLE => false,
|
||||
]);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(['_other' => '', '_choices' => 'my-value'], $deserialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Other cases :.
|
||||
*
|
||||
* - Test if the selected value is '_other
|
||||
* - Test with null data
|
||||
*/
|
||||
public function testDeserializeSingleChoiceWithOtherOtherCases()
|
||||
{
|
||||
$customField = $this->generateCustomField([
|
||||
CustomFieldChoice::ALLOW_OTHER => true,
|
||||
CustomFieldChoice::MULTIPLE => false,
|
||||
]);
|
||||
|
||||
// from a single to a single
|
||||
$data = ['_other' => 'something', '_choices' => '_other'];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(['_other' => 'something', '_choices' => '_other'], $deserialized);
|
||||
|
||||
// from a multiple to a single
|
||||
$data = ['_other' => 'something', '_choices' => ['some', '_other']];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(['_other' => 'something', '_choices' => '_other'], $deserialized);
|
||||
|
||||
//test with null data
|
||||
//from a single to a single :
|
||||
$data = ['_other' => 'something', '_choices' => null];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(['_other' => 'something', '_choices' => null], $deserialized);
|
||||
|
||||
$data = ['_other' => 'something', '_choices' => ''];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(['_other' => 'something', '_choices' => ''], $deserialized);
|
||||
|
||||
// from a multiple to a signle
|
||||
$data = ['_other' => 'something', '_choices' => []];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(['_other' => 'something', '_choices' => ''], $deserialized);
|
||||
|
||||
$data = ['_other' => 'something', '_choices' => ['']];
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(['_other' => 'something', '_choices' => ''], $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(['_other' => null, '_choices' => null], $customField);
|
||||
$this->assertSame(['_other' => '', '_choices' => null], $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(['_other' => null, '_choices' => ''], $customField);
|
||||
$this->assertSame(['_other' => '', '_choices' => ''], $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize([null], $customField);
|
||||
$this->assertSame(['_other' => '', '_choices' => null], $deserialized);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////
|
||||
//
|
||||
// test function deserialize
|
||||
//
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Test if the representation of the data is deserialized to a single text.
|
||||
*
|
||||
* If the value is in _other, the _other value should not be returned.
|
||||
*
|
||||
* @param type $data
|
||||
* @dataProvider serializedRepresentationDataProvider
|
||||
*/
|
||||
public function testDeserializeSingleChoiceWithoutOther($data)
|
||||
{
|
||||
$customField = $this->generateCustomField([
|
||||
CustomFieldChoice::ALLOW_OTHER => false,
|
||||
CustomFieldChoice::MULTIPLE => false,
|
||||
]);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame('my-value', $deserialized);
|
||||
}
|
||||
|
||||
public function testDeserializeSingleChoiceWithoutOtherDataIsNull()
|
||||
{
|
||||
$customField = $this->generateCustomField([
|
||||
CustomFieldChoice::ALLOW_OTHER => false,
|
||||
CustomFieldChoice::MULTIPLE => false,
|
||||
]);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(null, $customField);
|
||||
$this->assertSame(null, $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize('', $customField);
|
||||
$this->assertSame('', $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize([null], $customField);
|
||||
$this->assertSame(null, $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(['_other' => null, '_choices' => null], $customField);
|
||||
$this->assertSame(null, $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(['_other' => null, '_choices' => ''], $customField);
|
||||
$this->assertSame('', $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize([null], $customField);
|
||||
$this->assertSame(null, $deserialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider emptyDataProvider
|
||||
*
|
||||
* @param mixed $data deserialized data
|
||||
*/
|
||||
public function testIsEmptyValueEmpty($data)
|
||||
{
|
||||
$customField = $this->generateCustomField([
|
||||
CustomFieldChoice::ALLOW_OTHER => false,
|
||||
CustomFieldChoice::MULTIPLE => true,
|
||||
]);
|
||||
|
||||
$isEmpty = $this->cfChoice->isEmptyValue($data, $customField);
|
||||
|
||||
$this->assertTrue($isEmpty);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////
|
||||
//
|
||||
// test function isEmptyValue
|
||||
//
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* test the not empty with the not-empty data provider.
|
||||
*
|
||||
* @param mixed $data
|
||||
* @dataProvider serializedRepresentationDataProvider
|
||||
*/
|
||||
public function testIsEmptyValueNotEmpty($data)
|
||||
{
|
||||
$customField = $this->generateCustomField([
|
||||
CustomFieldChoice::ALLOW_OTHER => false,
|
||||
CustomFieldChoice::MULTIPLE => true,
|
||||
]);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$isEmpty = $this->cfChoice->isEmptyValue($deserialized, $customField);
|
||||
|
||||
$this->assertFalse($isEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* @return CustomField
|
||||
*/
|
||||
private function generateCustomField($options)
|
||||
@@ -72,397 +446,6 @@ class CustomFieldsChoiceTest extends KernelTestCase
|
||||
->setActive(true)
|
||||
->setSlug('slug')
|
||||
->setOptions($options)
|
||||
->setType('choice')
|
||||
;
|
||||
->setType('choice');
|
||||
}
|
||||
|
||||
/////////////////////////////////////////
|
||||
//
|
||||
// test function deserialize
|
||||
//
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Test if the representation of the data is deserialized to a single text.
|
||||
*
|
||||
* If the value is in _other, the _other value should not be returned.
|
||||
*
|
||||
* @param type $data
|
||||
* @dataProvider serializedRepresentationDataProvider
|
||||
*/
|
||||
public function testDeserializeSingleChoiceWithoutOther($data)
|
||||
{
|
||||
$customField = $this->generateCustomField(array(
|
||||
CustomFieldChoice::ALLOW_OTHER => false,
|
||||
CustomFieldChoice::MULTIPLE => false
|
||||
));
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame('my-value', $deserialized);
|
||||
}
|
||||
|
||||
|
||||
public function testDeserializeSingleChoiceWithoutOtherDataIsNull()
|
||||
{
|
||||
$customField = $this->generateCustomField(array(
|
||||
CustomFieldChoice::ALLOW_OTHER => false,
|
||||
CustomFieldChoice::MULTIPLE => false
|
||||
));
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(null, $customField);
|
||||
$this->assertSame(null, $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize('', $customField);
|
||||
$this->assertSame('', $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(array(null), $customField);
|
||||
$this->assertSame(null, $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(array('_other' => null, '_choices' => null), $customField);
|
||||
$this->assertSame(null, $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(array('_other' => null, '_choices' => ''), $customField);
|
||||
$this->assertSame('', $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(array(null), $customField);
|
||||
$this->assertSame(null, $deserialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the representation of the data is deserialized to a single text
|
||||
* with an "allow_other" field.
|
||||
*
|
||||
* If the value is in _other, the _other value should be in the _other field.
|
||||
*
|
||||
* @param type $data
|
||||
* @dataProvider serializedRepresentationDataProvider
|
||||
*/
|
||||
public function testDeserializeSingleChoiceWithOther($data)
|
||||
{
|
||||
$customField = $this->generateCustomField(array(
|
||||
CustomFieldChoice::ALLOW_OTHER => true,
|
||||
CustomFieldChoice::MULTIPLE => false
|
||||
));
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(array('_other' => '', '_choices' => 'my-value'), $deserialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Other cases :
|
||||
*
|
||||
* - Test if the selected value is '_other
|
||||
* - Test with null data
|
||||
*
|
||||
* @param type $data
|
||||
*/
|
||||
public function testDeserializeSingleChoiceWithOtherOtherCases()
|
||||
{
|
||||
$customField = $this->generateCustomField(array(
|
||||
CustomFieldChoice::ALLOW_OTHER => true,
|
||||
CustomFieldChoice::MULTIPLE => false
|
||||
));
|
||||
|
||||
// from a single to a single
|
||||
$data = array('_other' => 'something', '_choices' => '_other');
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(array('_other' => 'something', '_choices' => '_other'), $deserialized);
|
||||
|
||||
|
||||
// from a multiple to a single
|
||||
$data = array('_other' => 'something', '_choices' => array('some', '_other'));
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(array('_other' => 'something', '_choices' => '_other'), $deserialized);
|
||||
|
||||
//test with null data
|
||||
//from a single to a single :
|
||||
$data = array('_other' => 'something', '_choices' => null);
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(array('_other' => 'something', '_choices' => null), $deserialized);
|
||||
|
||||
$data = array('_other' => 'something', '_choices' => '');
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(array('_other' => 'something', '_choices' => ''), $deserialized);
|
||||
|
||||
// from a multiple to a signle
|
||||
$data = array('_other' => 'something', '_choices' => array());
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(array('_other' => 'something', '_choices' => ''), $deserialized);
|
||||
|
||||
$data = array('_other' => 'something', '_choices' => array(''));
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(array('_other' => 'something', '_choices' => ''), $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(array('_other' => null, '_choices' => null), $customField);
|
||||
$this->assertSame(array('_other' => '', '_choices' => null), $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(array('_other' => null, '_choices' => ''), $customField);
|
||||
$this->assertSame(array('_other' => '', '_choices' => ''), $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(array(null), $customField);
|
||||
$this->assertSame(array('_other' => '', '_choices' => null), $deserialized);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the representation of the data is deserialized to an array text
|
||||
* with an "allow_other" field.
|
||||
*
|
||||
* This test does not covers the case when the selected value is `_other`
|
||||
*
|
||||
* @param type $data
|
||||
* @dataProvider serializedRepresentationDataProvider
|
||||
*/
|
||||
public function testDeserializeMultipleChoiceWithOther($data)
|
||||
{
|
||||
$customField = $this->generateCustomField(array(
|
||||
CustomFieldChoice::ALLOW_OTHER => true,
|
||||
CustomFieldChoice::MULTIPLE => true
|
||||
));
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(array('_other' => '', '_choices' => array('my-value')),
|
||||
$deserialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the representation of the data is deserialized to an array text
|
||||
* with an "allow_other" field.
|
||||
*
|
||||
* This test covers :
|
||||
* - the case when the selected value is `_other`
|
||||
* - result is null
|
||||
*
|
||||
* @param type $data
|
||||
*/
|
||||
public function testDeserializeMultipleChoiceWithOtherOtherCases()
|
||||
{
|
||||
$customField = $this->generateCustomField(array(
|
||||
CustomFieldChoice::ALLOW_OTHER => true,
|
||||
CustomFieldChoice::MULTIPLE => true
|
||||
));
|
||||
|
||||
// selected value is _other
|
||||
// from single to multiple
|
||||
$data = array('_other' => 'something', '_choices' => '_other');
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(array('_other' => 'something', '_choices' => array('_other')),
|
||||
$deserialized);
|
||||
|
||||
// from multiple to multiple
|
||||
$data = array('_other' => 'something', '_choices' => array('_other', 'something'));
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(array('_other' => 'something', '_choices' => array('_other', 'something')),
|
||||
$deserialized);
|
||||
|
||||
// test with null value
|
||||
// from single to multiple
|
||||
$data = array('_other' => '', '_choices' => '');
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(array('_other' => '', '_choices' => array('')),
|
||||
$deserialized);
|
||||
|
||||
// from multiple to multiple
|
||||
$data = array('_other' => '', '_choices' => array());
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(array('_other' => '', '_choices' => array()),
|
||||
$deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(array('_other' => null, '_choices' => null), $customField);
|
||||
$this->assertSame(array('_other' => '', '_choices' => array(null)), $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(array('_other' => null, '_choices' => ''), $customField);
|
||||
$this->assertSame(array('_other' => '', '_choices' => array('')), $deserialized);
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize(array(null), $customField);
|
||||
$this->assertSame(array('_other' => '', '_choices' => array(null)), $deserialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the representation of the data is deserialized to an array text
|
||||
* **without** an "allow_other" field.
|
||||
*
|
||||
*
|
||||
* @param type $data
|
||||
* @dataProvider serializedRepresentationDataProvider
|
||||
*/
|
||||
public function testDeserializeMultipleChoiceWithoutOther($data)
|
||||
{
|
||||
$customField = $this->generateCustomField(array(
|
||||
CustomFieldChoice::ALLOW_OTHER => false,
|
||||
CustomFieldChoice::MULTIPLE => true
|
||||
));
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
|
||||
$this->assertSame(array('my-value'), $deserialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the representation of the data is deserialized to an array text
|
||||
* **without** an "allow_other" field.
|
||||
*
|
||||
* Covered cases :
|
||||
* - NULL values
|
||||
*
|
||||
*
|
||||
* @param type $data
|
||||
*/
|
||||
public function testDeserializeMultipleChoiceWithoutOtherOtherCases()
|
||||
{
|
||||
$customField = $this->generateCustomField(array(
|
||||
CustomFieldChoice::ALLOW_OTHER => false,
|
||||
CustomFieldChoice::MULTIPLE => true
|
||||
));
|
||||
|
||||
// from single to multiple
|
||||
$data = 'my-value';
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(array('my-value'), $deserialized);
|
||||
|
||||
// from multiple to multiple
|
||||
$data = array('my-value');
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(array('my-value'), $deserialized);
|
||||
|
||||
// from multiple
|
||||
$data = array(null);
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(array(null), $deserialized);
|
||||
|
||||
$data = array('_other' => null, '_choices' => array(null));
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(array(null), $deserialized);
|
||||
|
||||
//from single
|
||||
$data = array('_other' => null, '_choices' => null);
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$this->assertSame(array(null), $deserialized);
|
||||
}
|
||||
|
||||
public function serializedRepresentationDataProvider()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
// multiple => false, allow_other => false
|
||||
'my-value'
|
||||
),
|
||||
array(
|
||||
// multiple => true, allow_ther => false
|
||||
array('my-value')
|
||||
),
|
||||
array(
|
||||
// multiple => false, allow_other => true, current value not in other
|
||||
array('_other' => '', '_choices' => 'my-value')
|
||||
),
|
||||
array(
|
||||
// multiple => true, allow_other => true, current value not in other
|
||||
array('_other' => '', '_choices'=> array('my-value'))
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////
|
||||
//
|
||||
// test function isEmptyValue
|
||||
//
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* test the not empty with the not-empty data provider
|
||||
*
|
||||
* @param mixed $data
|
||||
* @dataProvider serializedRepresentationDataProvider
|
||||
*/
|
||||
public function testIsEmptyValueNotEmpty($data)
|
||||
{
|
||||
$customField = $this->generateCustomField(array(
|
||||
CustomFieldChoice::ALLOW_OTHER => false,
|
||||
CustomFieldChoice::MULTIPLE => true
|
||||
));
|
||||
|
||||
$deserialized = $this->cfChoice->deserialize($data, $customField);
|
||||
$isEmpty = $this->cfChoice->isEmptyValue($deserialized, $customField);
|
||||
|
||||
$this->assertFalse($isEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider emptyDataProvider
|
||||
* @param mixed $data deserialized data
|
||||
*/
|
||||
public function testIsEmptyValueEmpty($data)
|
||||
{
|
||||
$customField = $this->generateCustomField(array(
|
||||
CustomFieldChoice::ALLOW_OTHER => false,
|
||||
CustomFieldChoice::MULTIPLE => true
|
||||
));
|
||||
|
||||
$isEmpty = $this->cfChoice->isEmptyValue($data, $customField);
|
||||
|
||||
$this->assertTrue($isEmpty);
|
||||
}
|
||||
|
||||
/**
|
||||
* provide empty data in different possible representations.
|
||||
* Those data are supposed to be deserialized.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function emptyDataProvider()
|
||||
{
|
||||
return array(
|
||||
// 0
|
||||
array(
|
||||
// signle
|
||||
''
|
||||
),
|
||||
// 1
|
||||
array(
|
||||
// single
|
||||
null
|
||||
),
|
||||
// 2
|
||||
array(
|
||||
// signle with allow other
|
||||
array('_other' => 'something', '_choices' => '')
|
||||
),
|
||||
// 3
|
||||
array(
|
||||
// multiple
|
||||
array()
|
||||
),
|
||||
// 4
|
||||
array(
|
||||
// multiple with allow other
|
||||
array('_other' => 'something', '_choices' => array())
|
||||
),
|
||||
// 5
|
||||
array(
|
||||
// multiple with allow other
|
||||
array('_other' => '', '_choices' => array())
|
||||
),
|
||||
// 6
|
||||
array(
|
||||
// empty
|
||||
array('_other' => null, '_choices' => null)
|
||||
),
|
||||
// 7
|
||||
array(
|
||||
// empty
|
||||
array(null)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,45 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Tests;
|
||||
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldNumber;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\CustomFieldsBundle\Form\CustomFieldsGroupType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
/**
|
||||
* Test CustomFieldsNumber
|
||||
* Test CustomFieldsNumber.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class CustomFieldsNumberTest extends \Symfony\Bundle\FrameworkBundle\Test\WebTestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldNumber
|
||||
*/
|
||||
private $customFieldNumber;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var FormBuilderInterface
|
||||
*/
|
||||
private $formBuilder;
|
||||
@@ -49,26 +36,135 @@ class CustomFieldsNumberTest extends \Symfony\Bundle\FrameworkBundle\Test\WebTes
|
||||
self::bootKernel();
|
||||
|
||||
$this->customFieldNumber = self::$kernel->getContainer()
|
||||
->get('chill.custom_field.number');
|
||||
->get('chill.custom_field.number');
|
||||
|
||||
$this->formBuilder = self::$kernel->getContainer()
|
||||
->get('form.factory')
|
||||
->createBuilder('form', null, array(
|
||||
'csrf_protection' => false,
|
||||
'csrf_field_name' => '_token'
|
||||
));
|
||||
->get('form.factory')
|
||||
->createBuilder('form', null, [
|
||||
'csrf_protection' => false,
|
||||
'csrf_field_name' => '_token',
|
||||
]);
|
||||
|
||||
$request = new \Symfony\Component\HttpFoundation\Request();
|
||||
$request->setLocale('fr');
|
||||
|
||||
self::$kernel->getContainer()
|
||||
->get('request_stack')
|
||||
->push($request);
|
||||
->get('request_stack')
|
||||
->push($request);
|
||||
}
|
||||
|
||||
public function testCreateInvalidFormValueGreaterThanMaximum()
|
||||
{
|
||||
$cf = $this->createCustomFieldNumber([
|
||||
'min' => null,
|
||||
'max' => 10,
|
||||
'scale' => null,
|
||||
'post_text' => null,
|
||||
]);
|
||||
|
||||
$this->customFieldNumber->buildForm($this->formBuilder, $cf);
|
||||
|
||||
$form = $this->formBuilder->getForm();
|
||||
|
||||
$form->submit(['default' => 100]);
|
||||
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
$this->assertFalse($form->isValid());
|
||||
$this->assertEquals(1, count($form['default']->getErrors()));
|
||||
}
|
||||
|
||||
public function testCreateInvalidFormValueLowerThanMinimum()
|
||||
{
|
||||
$cf = $this->createCustomFieldNumber([
|
||||
'min' => 1000,
|
||||
'max' => null,
|
||||
'scale' => null,
|
||||
'post_text' => null,
|
||||
]);
|
||||
|
||||
$this->customFieldNumber->buildForm($this->formBuilder, $cf);
|
||||
|
||||
$form = $this->formBuilder->getForm();
|
||||
|
||||
$form->submit(['default' => 100]);
|
||||
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
$this->assertFalse($form->isValid());
|
||||
$this->assertEquals(1, count($form['default']->getErrors()));
|
||||
}
|
||||
|
||||
public function testCreateValidForm()
|
||||
{
|
||||
$cf = $this->createCustomFieldNumber([
|
||||
'min' => null,
|
||||
'max' => null,
|
||||
'scale' => null,
|
||||
'post_text' => null,
|
||||
]);
|
||||
|
||||
$this->customFieldNumber->buildForm($this->formBuilder, $cf);
|
||||
|
||||
$form = $this->formBuilder->getForm();
|
||||
|
||||
$form->submit(['default' => 10]);
|
||||
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
$this->assertEquals(10, $form['default']->getData());
|
||||
}
|
||||
|
||||
public function testRequiredFieldIsFalse()
|
||||
{
|
||||
$cf = $this->createCustomFieldNumber([
|
||||
'min' => 1000,
|
||||
'max' => null,
|
||||
'scale' => null,
|
||||
'post_text' => null,
|
||||
]);
|
||||
$cf->setRequired(false);
|
||||
|
||||
$cfGroup = (new \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup())
|
||||
->addCustomField($cf);
|
||||
|
||||
$form = static::$kernel->getContainer()->get('form.factory')
|
||||
->createBuilder(\Chill\CustomFieldsBundle\Form\Type\CustomFieldType::class, [], [
|
||||
'group' => $cfGroup,
|
||||
])
|
||||
->getForm();
|
||||
|
||||
$this->assertFalse(
|
||||
$form['default']->isRequired(),
|
||||
'The field should not be required'
|
||||
);
|
||||
}
|
||||
|
||||
public function testRequiredFieldIsTrue()
|
||||
{
|
||||
$cf = $this->createCustomFieldNumber([
|
||||
'min' => 1000,
|
||||
'max' => null,
|
||||
'scale' => null,
|
||||
'post_text' => null,
|
||||
]);
|
||||
$cf->setRequired(true);
|
||||
|
||||
$cfGroup = (new \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup())
|
||||
->addCustomField($cf);
|
||||
|
||||
$form = static::$kernel->getContainer()->get('form.factory')
|
||||
->createBuilder(\Chill\CustomFieldsBundle\Form\Type\CustomFieldType::class, [], [
|
||||
'group' => $cfGroup,
|
||||
])
|
||||
->getForm();
|
||||
|
||||
$this->assertTrue(
|
||||
$form['default']->isRequired(),
|
||||
'The field should be required'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param mixed[] $options
|
||||
*
|
||||
* @return CustomField
|
||||
*/
|
||||
private function createCustomFieldNumber($options)
|
||||
@@ -78,114 +174,7 @@ class CustomFieldsNumberTest extends \Symfony\Bundle\FrameworkBundle\Test\WebTes
|
||||
->setActive(true)
|
||||
->setOrdering(10)
|
||||
->setSlug('default')
|
||||
->setName(array('fr' => 'default'))
|
||||
->setName(['fr' => 'default'])
|
||||
->setOptions($options);
|
||||
}
|
||||
|
||||
public function testCreateValidForm()
|
||||
{
|
||||
$cf = $this->createCustomFieldNumber(array(
|
||||
'min' => null,
|
||||
'max' => null,
|
||||
'scale' => null,
|
||||
'post_text' => null
|
||||
));
|
||||
|
||||
$this->customFieldNumber->buildForm($this->formBuilder, $cf);
|
||||
|
||||
$form = $this->formBuilder->getForm();
|
||||
|
||||
$form->submit(array('default' => 10));
|
||||
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
$this->assertEquals(10, $form['default']->getData());
|
||||
}
|
||||
|
||||
public function testCreateInvalidFormValueGreaterThanMaximum()
|
||||
{
|
||||
$cf = $this->createCustomFieldNumber(array(
|
||||
'min' => null,
|
||||
'max' => 10,
|
||||
'scale' => null,
|
||||
'post_text' => null
|
||||
));
|
||||
|
||||
$this->customFieldNumber->buildForm($this->formBuilder, $cf);
|
||||
|
||||
$form = $this->formBuilder->getForm();
|
||||
|
||||
$form->submit(array('default' => 100));
|
||||
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
$this->assertFalse($form->isValid());
|
||||
$this->assertEquals(1, count($form['default']->getErrors()));
|
||||
}
|
||||
|
||||
public function testCreateInvalidFormValueLowerThanMinimum()
|
||||
{
|
||||
$cf = $this->createCustomFieldNumber(array(
|
||||
'min' => 1000,
|
||||
'max' => null,
|
||||
'scale' => null,
|
||||
'post_text' => null
|
||||
));
|
||||
|
||||
$this->customFieldNumber->buildForm($this->formBuilder, $cf);
|
||||
|
||||
$form = $this->formBuilder->getForm();
|
||||
|
||||
$form->submit(array('default' => 100));
|
||||
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
$this->assertFalse($form->isValid());
|
||||
$this->assertEquals(1, count($form['default']->getErrors()));
|
||||
}
|
||||
|
||||
public function testRequiredFieldIsFalse()
|
||||
{
|
||||
$cf = $this->createCustomFieldNumber(array(
|
||||
'min' => 1000,
|
||||
'max' => null,
|
||||
'scale' => null,
|
||||
'post_text' => null
|
||||
));
|
||||
$cf->setRequired(false);
|
||||
|
||||
$cfGroup = (new \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup())
|
||||
->addCustomField($cf);
|
||||
|
||||
$form = static::$kernel->getContainer()->get('form.factory')
|
||||
->createBuilder(\Chill\CustomFieldsBundle\Form\Type\CustomFieldType::class, array(), array(
|
||||
'group' => $cfGroup
|
||||
))
|
||||
->getForm();
|
||||
|
||||
$this->assertFalse($form['default']->isRequired(),
|
||||
"The field should not be required");
|
||||
}
|
||||
|
||||
public function testRequiredFieldIsTrue()
|
||||
{
|
||||
$cf = $this->createCustomFieldNumber(array(
|
||||
'min' => 1000,
|
||||
'max' => null,
|
||||
'scale' => null,
|
||||
'post_text' => null
|
||||
));
|
||||
$cf->setRequired(true);
|
||||
|
||||
$cfGroup = (new \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup())
|
||||
->addCustomField($cf);
|
||||
|
||||
$form = static::$kernel->getContainer()->get('form.factory')
|
||||
->createBuilder(\Chill\CustomFieldsBundle\Form\Type\CustomFieldType::class, array(), array(
|
||||
'group' => $cfGroup
|
||||
))
|
||||
->getForm();
|
||||
|
||||
$this->assertTrue($form['default']->isRequired(),
|
||||
"The field should be required");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -1,106 +1,93 @@
|
||||
<?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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Tests;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
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;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class CustomFieldsTextTest extends WebTestCase
|
||||
{
|
||||
use CustomFieldTestHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Chill\CustomFieldsBundle\Service\CustomFieldProvider
|
||||
* @var \Chill\CustomFieldsBundle\Service\CustomFieldProvider
|
||||
*/
|
||||
private $customFieldProvider;
|
||||
|
||||
use CustomFieldTestHelper;
|
||||
|
||||
|
||||
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);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
'Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface',
|
||||
$customField
|
||||
);
|
||||
$this->assertInstanceOf(
|
||||
'Chill\CustomFieldsBundle\CustomFields\CustomFieldText',
|
||||
$customField
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testFormTextNew()
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$crawler = $client->request('GET', '/fr/admin/customfield/new?type=text');
|
||||
|
||||
$this->assertTrue($client->getResponse()->isSuccessful());
|
||||
|
||||
$form = $crawler->selectButton('custom_field_choice_submit')->form();
|
||||
$this->assertTrue($form->has('custom_field_choice[options][maxLength]'));
|
||||
}
|
||||
|
||||
public function testPublicFormRenderingLengthLessThan256()
|
||||
{
|
||||
$customField = new CustomField();
|
||||
$customField->setType('text')
|
||||
->setOptions(array(CustomFieldText::MAX_LENGTH => 255))
|
||||
->setOptions([CustomFieldText::MAX_LENGTH => 255])
|
||||
->setSlug('slug')
|
||||
->setOrdering(10)
|
||||
->setActive(true)
|
||||
->setName(array('en' => 'my label'));
|
||||
|
||||
->setName(['en' => 'my label']);
|
||||
|
||||
$crawler = $this->getCrawlerForField($customField);
|
||||
|
||||
$this->assertCount(1, $crawler->filter("input[type=text]"));
|
||||
$this->assertCount(1, $crawler->filter("label:contains('my label')"));
|
||||
|
||||
$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))
|
||||
->setOptions([CustomFieldText::MAX_LENGTH => 256])
|
||||
->setSlug('slug')
|
||||
->setOrdering(10)
|
||||
->setActive(true)
|
||||
->setName(array('en' => 'my label'));
|
||||
|
||||
->setName(['en' => 'my label']);
|
||||
|
||||
$crawler = $this->getCrawlerForField($customField);
|
||||
|
||||
$this->assertCount(1, $crawler->filter("textarea"));
|
||||
$this->assertCount(1, $crawler->filter("label:contains('my label')"));
|
||||
|
||||
$this->assertCount(1, $crawler->filter('textarea'));
|
||||
$this->assertCount(1, $crawler->filter("label:contains('my label')"));
|
||||
}
|
||||
|
||||
public function testFormTextNew()
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$crawler = $client->request('GET', '/fr/admin/customfield/new?type=text');
|
||||
|
||||
$this->assertTrue($client->getResponse()->isSuccessful());
|
||||
|
||||
$form = $crawler->selectButton('custom_field_choice_submit')->form();
|
||||
$this->assertTrue($form->has('custom_field_choice[options][maxLength]'));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,20 +1,10 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Champs-Libres <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFields\Tests\Form\Extension;
|
||||
@@ -23,14 +13,14 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
|
||||
|
||||
/**
|
||||
* Test the post-text extension
|
||||
* Test the post-text extension.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class PostTextIntegerExtensionTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var \Symfony\Component\Form\FormBuilderInterface
|
||||
*/
|
||||
private $formBuilder;
|
||||
@@ -42,18 +32,17 @@ class PostTextIntegerExtensionTest extends KernelTestCase
|
||||
$container = self::$kernel->getContainer();
|
||||
|
||||
$this->formBuilder = $container->get('form.factory')
|
||||
->createBuilder('form', null);
|
||||
->createBuilder('form', null);
|
||||
}
|
||||
|
||||
public function testCreateView()
|
||||
{
|
||||
$form = $this->formBuilder->add('test', IntegerType::class, array(
|
||||
'post_text' => 'my text'
|
||||
))->getForm();
|
||||
$form = $this->formBuilder->add('test', IntegerType::class, [
|
||||
'post_text' => 'my text',
|
||||
])->getForm();
|
||||
|
||||
$view = $form->createView();
|
||||
|
||||
$this->assertEquals('my text', $view['test']->vars['post_text']);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,20 +1,10 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Champs-Libres <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFields\Tests\Form\Extension;
|
||||
@@ -23,14 +13,14 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||
|
||||
/**
|
||||
* Test the post-text extension
|
||||
* Test the post-text extension.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class PostTextNumberExtensionTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var \Symfony\Component\Form\FormBuilderInterface
|
||||
*/
|
||||
private $formBuilder;
|
||||
@@ -42,18 +32,17 @@ class PostTextNumberExtensionTest extends KernelTestCase
|
||||
$container = self::$kernel->getContainer();
|
||||
|
||||
$this->formBuilder = $container->get('form.factory')
|
||||
->createBuilder('form', null);
|
||||
->createBuilder('form', null);
|
||||
}
|
||||
|
||||
public function testCreateView()
|
||||
{
|
||||
$form = $this->formBuilder->add('test', NumberType::class, array(
|
||||
'post_text' => 'my text'
|
||||
))->getForm();
|
||||
$form = $this->formBuilder->add('test', NumberType::class, [
|
||||
'post_text' => 'my text',
|
||||
])->getForm();
|
||||
|
||||
$view = $form->createView();
|
||||
|
||||
$this->assertEquals('my text', $view['test']->vars['post_text']);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,21 +1,10 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.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/>.
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFieldsBundle\Tests;
|
||||
@@ -24,22 +13,22 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Test that routes are correctly loaded
|
||||
* Test that routes are correctly loaded.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class RoutingLoaderTest extends WebTestCase
|
||||
{
|
||||
public function testRoutesAreLoaded()
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$client->request('GET','/fr/admin/customfield/');
|
||||
|
||||
|
||||
$client->request('GET', '/fr/admin/customfield/');
|
||||
|
||||
$this->assertEquals(
|
||||
Response::HTTP_OK,
|
||||
$client->getResponse()->getStatusCode()
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,92 +1,77 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2015 Champs Libres <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFields\Tests\Service;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldsHelper;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldsHelper;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* Tests for custom fields helper
|
||||
* Tests for custom fields helper.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @author Champs Libres <info@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class CustomFieldsHelperTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldsHelper
|
||||
*/
|
||||
private $cfHelper;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @var CustomField
|
||||
*/
|
||||
private $randomCFText;
|
||||
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
|
||||
$container = self::$kernel->getContainer();
|
||||
|
||||
|
||||
$this->cfHelper = $container->get('chill.custom_field.helper');
|
||||
|
||||
|
||||
$this->randomCFText = (new CustomField())
|
||||
->setSlug('my-slug')
|
||||
->setActive(true)
|
||||
->setName(array('fr' => 'my cf'))
|
||||
->setOptions(array('maxLength' => 1000))
|
||||
->setType('text')
|
||||
;
|
||||
->setSlug('my-slug')
|
||||
->setActive(true)
|
||||
->setName(['fr' => 'my cf'])
|
||||
->setOptions(['maxLength' => 1000])
|
||||
->setType('text');
|
||||
}
|
||||
|
||||
public function testRenderCustomField()
|
||||
{
|
||||
$data = array(
|
||||
$this->randomCFText->getSlug() => 'Sample text'
|
||||
);
|
||||
|
||||
$text = $this->cfHelper->renderCustomField($data, $this->randomCFText);
|
||||
|
||||
$this->assertContains('Sample text', $text);
|
||||
}
|
||||
|
||||
|
||||
public function testIsEmptyValue()
|
||||
{
|
||||
// not empty value
|
||||
$data = array(
|
||||
$this->randomCFText->getSlug() => 'Sample text'
|
||||
);
|
||||
|
||||
$data = [
|
||||
$this->randomCFText->getSlug() => 'Sample text',
|
||||
];
|
||||
|
||||
$this->assertFalse($this->cfHelper->isEmptyValue($data, $this->randomCFText));
|
||||
|
||||
|
||||
//empty value
|
||||
$data = array(
|
||||
$this->randomCFText->getSlug() => ''
|
||||
);
|
||||
|
||||
$data = [
|
||||
$this->randomCFText->getSlug() => '',
|
||||
];
|
||||
|
||||
$this->assertTrue($this->cfHelper->isEmptyValue($data, $this->randomCFText));
|
||||
}
|
||||
|
||||
|
||||
public function testRenderCustomField()
|
||||
{
|
||||
$data = [
|
||||
$this->randomCFText->getSlug() => 'Sample text',
|
||||
];
|
||||
|
||||
$text = $this->cfHelper->renderCustomField($data, $this->randomCFText);
|
||||
|
||||
$this->assertContains('Sample text', $text);
|
||||
}
|
||||
}
|
||||
|
@@ -1,128 +1,118 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2015 Champs Libres <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFields\Tests\Templating\Twig;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\CustomFieldsBundle\Templating\Twig\CustomFieldRenderingTwig;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\CustomFieldsBundle\Templating\Twig\CustomFieldRenderingTwig;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* Test the rendering of twig function which renders custom fields
|
||||
* Test the rendering of twig function which renders custom fields.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @author Champs Libres <info@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class CustomFieldRenderingTwigTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldRenderingTwig
|
||||
*/
|
||||
private $cfRendering;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldProvider
|
||||
*/
|
||||
private $cfProvider;
|
||||
|
||||
|
||||
/**
|
||||
* @var CustomFieldRenderingTwig
|
||||
*/
|
||||
private $cfRendering;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
self::bootKernel();
|
||||
$this->cfRendering = self::$kernel->getContainer()
|
||||
->get('chill.custom_field.twig.custom_fields_rendering')
|
||||
;
|
||||
|
||||
->get('chill.custom_field.twig.custom_fields_rendering');
|
||||
|
||||
$this->cfProvider = self::$kernel->getContainer()
|
||||
->get('chill.custom_field.provider');
|
||||
|
||||
->get('chill.custom_field.provider');
|
||||
|
||||
// set locale to fr
|
||||
$prophet = new \Prophecy\Prophet;
|
||||
$prophet = new \Prophecy\Prophet();
|
||||
$request = $prophet->prophesize();
|
||||
$request->willExtend('Symfony\Component\HttpFoundation\Request');
|
||||
$request->getLocale()->willReturn('fr');
|
||||
self::$kernel->getContainer()->get('request_stack')
|
||||
->push($request->reveal());
|
||||
->push($request->reveal());
|
||||
}
|
||||
|
||||
|
||||
public function testIsEmpty()
|
||||
{
|
||||
$cf = $this->getSimpleCustomFieldText();
|
||||
|
||||
// value is not empty
|
||||
$fields = [
|
||||
'test' => 'My tailor is rich',
|
||||
];
|
||||
|
||||
$result = $this->cfRendering->isEmptyValue($fields, $cf);
|
||||
|
||||
$this->assertFalse($result);
|
||||
|
||||
// value is empty
|
||||
$fields = [
|
||||
'text' => '',
|
||||
];
|
||||
|
||||
$result = $this->cfRendering->isEmptyValue($fields, $cf);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testLabelRendering()
|
||||
{
|
||||
$cf = $this->getSimpleCustomFieldText();
|
||||
|
||||
$text = $this->cfRendering->renderLabel($cf);
|
||||
|
||||
$this->assertContains(
|
||||
'Test',
|
||||
$text,
|
||||
"The rendering text should contains the 'test' text"
|
||||
);
|
||||
}
|
||||
|
||||
public function testWidgetRendering()
|
||||
{
|
||||
$cf = $this->getSimpleCustomFieldText();
|
||||
$fields = [
|
||||
'test' => 'My tailor is rich',
|
||||
];
|
||||
|
||||
$text = $this->cfRendering->renderWidget($fields, $cf);
|
||||
|
||||
$this->assertContains(
|
||||
'My tailor is rich',
|
||||
$text,
|
||||
"The rendering text should contains the 'test' text"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return CustomField
|
||||
*/
|
||||
private function getSimpleCustomFieldText()
|
||||
{
|
||||
return (new CustomField())
|
||||
->setSlug('test')
|
||||
->setName(array('fr' => 'Test'))
|
||||
->setType('text')
|
||||
->setOrdering(10)
|
||||
->setOptions(array("maxLength" => 255))
|
||||
->setActive(true)
|
||||
;
|
||||
}
|
||||
|
||||
public function testLabelRendering()
|
||||
{
|
||||
$cf = $this->getSimpleCustomFieldText();
|
||||
|
||||
$text = $this->cfRendering->renderLabel($cf);
|
||||
|
||||
$this->assertContains('Test', $text,
|
||||
"The rendering text should contains the 'test' text");
|
||||
}
|
||||
|
||||
public function testWidgetRendering()
|
||||
{
|
||||
$cf = $this->getSimpleCustomFieldText();
|
||||
$fields = array(
|
||||
'test' => "My tailor is rich"
|
||||
);
|
||||
|
||||
$text = $this->cfRendering->renderWidget($fields, $cf);
|
||||
|
||||
$this->assertContains('My tailor is rich', $text,
|
||||
"The rendering text should contains the 'test' text");
|
||||
}
|
||||
|
||||
public function testIsEmpty()
|
||||
{
|
||||
$cf = $this->getSimpleCustomFieldText();
|
||||
|
||||
// value is not empty
|
||||
$fields = array(
|
||||
'test' => "My tailor is rich"
|
||||
);
|
||||
|
||||
$result = $this->cfRendering->isEmptyValue($fields, $cf);
|
||||
|
||||
$this->assertFalse($result);
|
||||
|
||||
// value is empty
|
||||
$fields = array(
|
||||
'text' => ''
|
||||
);
|
||||
|
||||
$result = $this->cfRendering->isEmptyValue($fields, $cf);
|
||||
|
||||
$this->assertTrue($result);
|
||||
->setName(['fr' => 'Test'])
|
||||
->setType('text')
|
||||
->setOrdering(10)
|
||||
->setOptions(['maxLength' => 255])
|
||||
->setActive(true);
|
||||
}
|
||||
}
|
||||
|
@@ -1,130 +1,115 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2015 Champs Libres <info@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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\CustomFields\Tests\Templating\Twig;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\CustomFieldsBundle\Templating\Twig\CustomFieldsGroupRenderingTwig;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\CustomFieldsBundle\Templating\Twig\CustomFieldsGroupRenderingTwig;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* Test the rendering of a custom fields group through
|
||||
* the `chill_custom_fields_group_widget`
|
||||
* the `chill_custom_fields_group_widget`.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @author Champs Libres <info@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class CustomFieldsGroupRenderingTwigTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldsGroupRenderingTwig
|
||||
*/
|
||||
private $cfRendering;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var CustomFieldProvider
|
||||
*/
|
||||
private $cfProvider;
|
||||
|
||||
|
||||
/**
|
||||
* @var CustomFieldsGroupRenderingTwig
|
||||
*/
|
||||
private $cfRendering;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
self::bootKernel();
|
||||
$this->cfRendering = self::$kernel->getContainer()
|
||||
->get('chill.custom_field.twig.custom_fields_group_rendering')
|
||||
;
|
||||
|
||||
->get('chill.custom_field.twig.custom_fields_group_rendering');
|
||||
|
||||
$this->cfProvider = self::$kernel->getContainer()
|
||||
->get('chill.custom_field.provider');
|
||||
|
||||
->get('chill.custom_field.provider');
|
||||
|
||||
// set locale to fr
|
||||
$prophet = new \Prophecy\Prophet;
|
||||
$prophet = new \Prophecy\Prophet();
|
||||
$request = $prophet->prophesize();
|
||||
$request->willExtend('Symfony\Component\HttpFoundation\Request');
|
||||
$request->getLocale()->willReturn('fr');
|
||||
self::$kernel->getContainer()->get('request_stack')
|
||||
->push($request->reveal());
|
||||
->push($request->reveal());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return CustomField
|
||||
*/
|
||||
private function getSimpleCustomFieldText($slug, $name)
|
||||
{
|
||||
return (new CustomField())
|
||||
->setSlug($slug)
|
||||
->setName(array('fr' => $name))
|
||||
->setType('text')
|
||||
->setOrdering(10)
|
||||
->setOptions(array("maxLength" => 255))
|
||||
->setActive(true)
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return CustomFieldsGroup
|
||||
*/
|
||||
private function getCustomFieldsGroup()
|
||||
{
|
||||
return (new CustomFieldsGroup())
|
||||
->setEntity('\Dummy')
|
||||
->setName(array("fr" => "A cf group"))
|
||||
->addCustomField($this->getSimpleCustomFieldText("horses", "Do you like horses ?."))
|
||||
->addCustomField($this->getSimpleCustomFieldText("sure", "Are you sure ?"))
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
public function testRenderingWidget()
|
||||
{
|
||||
$cfGroup = $this->getCustomFieldsGroup();
|
||||
|
||||
$text = $this->cfRendering->renderWidget(array(
|
||||
'horses' => 'I like horses',
|
||||
'sure' => 'Yes !'
|
||||
), $cfGroup);
|
||||
|
||||
|
||||
$text = $this->cfRendering->renderWidget([
|
||||
'horses' => 'I like horses',
|
||||
'sure' => 'Yes !',
|
||||
], $cfGroup);
|
||||
|
||||
$this->assertContains('Do you like horses', $text);
|
||||
$this->assertContains('I like horses', $text);
|
||||
$this->assertContains('Are you sure', $text);
|
||||
$this->assertContains('Yes', $text);
|
||||
}
|
||||
|
||||
|
||||
public function testRenderingWidgetDoNotShowEmpty()
|
||||
{
|
||||
$cfGroup = $this->getCustomFieldsGroup();
|
||||
$cfGroup->addCustomField($this->getSimpleCustomFieldText('empty', 'Do not answer'));
|
||||
|
||||
$text = $this->cfRendering->renderWidget(array(
|
||||
'horses' => 'I like horses',
|
||||
'sure' => 'Yes !'
|
||||
), $cfGroup, 'html', array('show_empty' => false));
|
||||
|
||||
|
||||
$text = $this->cfRendering->renderWidget([
|
||||
'horses' => 'I like horses',
|
||||
'sure' => 'Yes !',
|
||||
], $cfGroup, 'html', ['show_empty' => false]);
|
||||
|
||||
$this->assertContains('Do you like horses', $text);
|
||||
$this->assertContains('I like horses', $text);
|
||||
$this->assertContains('Are you sure', $text);
|
||||
$this->assertContains('Yes', $text);
|
||||
$this->assertNotContains('Do not answer', $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CustomFieldsGroup
|
||||
*/
|
||||
private function getCustomFieldsGroup()
|
||||
{
|
||||
return (new CustomFieldsGroup())
|
||||
->setEntity('\Dummy')
|
||||
->setName(['fr' => 'A cf group'])
|
||||
->addCustomField($this->getSimpleCustomFieldText('horses', 'Do you like horses ?.'))
|
||||
->addCustomField($this->getSimpleCustomFieldText('sure', 'Are you sure ?'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $slug
|
||||
* @param mixed $name
|
||||
*
|
||||
* @return CustomField
|
||||
*/
|
||||
private function getSimpleCustomFieldText($slug, $name)
|
||||
{
|
||||
return (new CustomField())
|
||||
->setSlug($slug)
|
||||
->setName(['fr' => $name])
|
||||
->setType('text')
|
||||
->setOrdering(10)
|
||||
->setOptions(['maxLength' => 255])
|
||||
->setActive(true);
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,14 @@
|
||||
<?php
|
||||
|
||||
if (!is_file($autoloadFile = __DIR__.'/../vendor/autoload.php')) {
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
if (!is_file($autoloadFile = __DIR__ . '/../vendor/autoload.php')) {
|
||||
throw new \LogicException('Could not find autoload.php in vendor/. Did you run "composer install --dev"?');
|
||||
}
|
||||
|
||||
require $autoloadFile;
|
||||
|
||||
|
@@ -1,34 +1,39 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\Migrations\CustomFields;
|
||||
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
class Version20141128195430 extends AbstractMigration
|
||||
{
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql("CREATE SEQUENCE CustomField_id_seq INCREMENT BY 1 MINVALUE 1 START 1;");
|
||||
$this->addSql("CREATE SEQUENCE CustomFieldsDefaultGroup_id_seq INCREMENT BY 1 MINVALUE 1 START 1;");
|
||||
$this->addSql("CREATE SEQUENCE CustomFieldsGroup_id_seq INCREMENT BY 1 MINVALUE 1 START 1;");
|
||||
$this->addSql("CREATE TABLE CustomField (id INT NOT NULL, name JSON NOT NULL, slug VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, active BOOLEAN NOT NULL, ordering DOUBLE PRECISION NOT NULL, options JSON NOT NULL, customFieldGroup_id INT DEFAULT NULL, PRIMARY KEY(id));");
|
||||
$this->addSql("CREATE INDEX IDX_40FB5D6DFEC418B ON CustomField (customFieldGroup_id);");
|
||||
$this->addSql("CREATE TABLE CustomFieldsDefaultGroup (id INT NOT NULL, entity VARCHAR(255) NOT NULL, customFieldsGroup_id INT DEFAULT NULL, PRIMARY KEY(id));");
|
||||
$this->addSql("CREATE INDEX IDX_286DC95DF53B66 ON CustomFieldsDefaultGroup (customFieldsGroup_id);");
|
||||
$this->addSql("CREATE UNIQUE INDEX unique_entity ON CustomFieldsDefaultGroup (entity);");
|
||||
$this->addSql("CREATE TABLE CustomFieldsGroup (id INT NOT NULL, name JSON NOT NULL, entity VARCHAR(255) NOT NULL, PRIMARY KEY(id));");
|
||||
$this->addSql("ALTER TABLE CustomField ADD CONSTRAINT FK_40FB5D6DFEC418B FOREIGN KEY (customFieldGroup_id) REFERENCES CustomFieldsGroup (id) NOT DEFERRABLE INITIALLY IMMEDIATE;");
|
||||
$this->addSql("ALTER TABLE CustomFieldsDefaultGroup ADD CONSTRAINT FK_286DC95DF53B66 FOREIGN KEY (customFieldsGroup_id) REFERENCES CustomFieldsGroup (id) NOT DEFERRABLE INITIALLY IMMEDIATE;");
|
||||
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql('CREATE SEQUENCE CustomField_id_seq INCREMENT BY 1 MINVALUE 1 START 1;');
|
||||
$this->addSql('CREATE SEQUENCE CustomFieldsDefaultGroup_id_seq INCREMENT BY 1 MINVALUE 1 START 1;');
|
||||
$this->addSql('CREATE SEQUENCE CustomFieldsGroup_id_seq INCREMENT BY 1 MINVALUE 1 START 1;');
|
||||
$this->addSql('CREATE TABLE CustomField (id INT NOT NULL, name JSON NOT NULL, slug VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, active BOOLEAN NOT NULL, ordering DOUBLE PRECISION NOT NULL, options JSON NOT NULL, customFieldGroup_id INT DEFAULT NULL, PRIMARY KEY(id));');
|
||||
$this->addSql('CREATE INDEX IDX_40FB5D6DFEC418B ON CustomField (customFieldGroup_id);');
|
||||
$this->addSql('CREATE TABLE CustomFieldsDefaultGroup (id INT NOT NULL, entity VARCHAR(255) NOT NULL, customFieldsGroup_id INT DEFAULT NULL, PRIMARY KEY(id));');
|
||||
$this->addSql('CREATE INDEX IDX_286DC95DF53B66 ON CustomFieldsDefaultGroup (customFieldsGroup_id);');
|
||||
$this->addSql('CREATE UNIQUE INDEX unique_entity ON CustomFieldsDefaultGroup (entity);');
|
||||
$this->addSql('CREATE TABLE CustomFieldsGroup (id INT NOT NULL, name JSON NOT NULL, entity VARCHAR(255) NOT NULL, PRIMARY KEY(id));');
|
||||
$this->addSql('ALTER TABLE CustomField ADD CONSTRAINT FK_40FB5D6DFEC418B FOREIGN KEY (customFieldGroup_id) REFERENCES CustomFieldsGroup (id) NOT DEFERRABLE INITIALLY IMMEDIATE;');
|
||||
$this->addSql('ALTER TABLE CustomFieldsDefaultGroup ADD CONSTRAINT FK_286DC95DF53B66 FOREIGN KEY (customFieldsGroup_id) REFERENCES CustomFieldsGroup (id) NOT DEFERRABLE INITIALLY IMMEDIATE;');
|
||||
}
|
||||
}
|
||||
|
@@ -1,22 +1,29 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\Migrations\CustomFields;
|
||||
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Add an option column to customfieldsgroup
|
||||
* Add an option column to customfieldsgroup.
|
||||
*/
|
||||
class Version20150224164531 extends AbstractMigration
|
||||
{
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql('ALTER TABLE customfieldsgroup ADD options JSON DEFAULT \'{}\'::json');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql('ALTER TABLE CustomFieldsGroup DROP options');
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql('ALTER TABLE customfieldsgroup ADD options JSON DEFAULT \'{}\'::json');
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +1,30 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\Migrations\CustomFields;
|
||||
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
class Version20151210155904 extends AbstractMigration
|
||||
{
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
|
||||
|
||||
$this->addSql('ALTER TABLE CustomField DROP required');
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
// this up() migration is auto-generated, please modify it to your needs
|
||||
@@ -20,16 +32,4 @@ class Version20151210155904 extends AbstractMigration
|
||||
|
||||
$this->addSql('ALTER TABLE customfield ADD required BOOLEAN DEFAULT FALSE');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
|
||||
|
||||
$this->addSql('ALTER TABLE CustomField DROP required');
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +1,32 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\Migrations\CustomFields;
|
||||
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
class Version20151210205610 extends AbstractMigration
|
||||
{
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
|
||||
|
||||
$this->addSql('ALTER TABLE custom_field_long_choice_options DROP CONSTRAINT cf_long_choice_self_referencing');
|
||||
$this->addSql('DROP SEQUENCE custom_field_long_choice_options_id_seq CASCADE');
|
||||
$this->addSql('DROP TABLE custom_field_long_choice_options');
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
|
||||
@@ -29,19 +43,5 @@ class Version20151210205610 extends AbstractMigration
|
||||
$this->addSql('ALTER TABLE custom_field_long_choice_options ADD CONSTRAINT cf_long_choice_self_referencing '
|
||||
. 'FOREIGN KEY (parent_id) REFERENCES custom_field_long_choice_options (id) '
|
||||
. 'NOT DEFERRABLE INITIALLY IMMEDIATE');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
|
||||
|
||||
$this->addSql('ALTER TABLE custom_field_long_choice_options DROP CONSTRAINT cf_long_choice_self_referencing');
|
||||
$this->addSql('DROP SEQUENCE custom_field_long_choice_options_id_seq CASCADE');
|
||||
$this->addSql('DROP TABLE custom_field_long_choice_options');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user