cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -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;
}
}

View File

@@ -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.');
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}