form dependency

This commit is contained in:
Benjamin Dalsass
2025-10-07 09:25:51 +02:00
parent 51a97ad28f
commit 9200883bc3
11 changed files with 172 additions and 3 deletions

View File

@@ -412,10 +412,16 @@ return array(
'Combodo\\iTop\\Controller\\Form\\Dependency\\MutableStatusEnumeration' => $baseDir . '/sources/Controller/Form/Dependency/MutableStatusEnumeration.php',
'Combodo\\iTop\\Controller\\Form\\FakeDataProvider' => $baseDir . '/sources/Controller/Form/FakeDataProvider.php',
'Combodo\\iTop\\Controller\\Form\\FormController' => $baseDir . '/sources/Controller/Form/FormController.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\AttributeChoiceType' => $baseDir . '/sources/Controller/Form/Type/Dependency/AttributeChoiceType.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\AttributeChoiceTypeData' => $baseDir . '/sources/Controller/Form/Type/Dependency/AttributeChoiceTypeData.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\ClassProviderInterface' => $baseDir . '/sources/Controller/Form/Type/Dependency/ClassProviderInterface.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\ContactType' => $baseDir . '/sources/Controller/Form/Type/Dependency/ContactType.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\ContactTypeData' => $baseDir . '/sources/Controller/Form/Type/Dependency/ContactTypeData.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\FakeChoiceType' => $baseDir . '/sources/Controller/Form/Type/Dependency/FakeChoiceType.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\FakeChoiceTypeData' => $baseDir . '/sources/Controller/Form/Type/Dependency/FakeChoiceTypeData.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\OQLData' => $baseDir . '/sources/Controller/Form/Type/Dependency/OQLData.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\OQLDataTransformer' => $baseDir . '/sources/Controller/Form/Type/Dependency/OQLDataTransformer.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\OQLType' => $baseDir . '/sources/Controller/Form/Type/Dependency/OQLType.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\OfficeType' => $baseDir . '/sources/Controller/Form/Type/Dependency/OfficeType.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\OfficeTypeData' => $baseDir . '/sources/Controller/Form/Type/Dependency/OfficeTypeData.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Form\\MailType' => $baseDir . '/sources/Controller/Form/Type/Form/MailType.php',

View File

@@ -828,10 +828,16 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f
'Combodo\\iTop\\Controller\\Form\\Dependency\\MutableStatusEnumeration' => __DIR__ . '/../..' . '/sources/Controller/Form/Dependency/MutableStatusEnumeration.php',
'Combodo\\iTop\\Controller\\Form\\FakeDataProvider' => __DIR__ . '/../..' . '/sources/Controller/Form/FakeDataProvider.php',
'Combodo\\iTop\\Controller\\Form\\FormController' => __DIR__ . '/../..' . '/sources/Controller/Form/FormController.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\AttributeChoiceType' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Dependency/AttributeChoiceType.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\AttributeChoiceTypeData' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Dependency/AttributeChoiceTypeData.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\ClassProviderInterface' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Dependency/ClassProviderInterface.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\ContactType' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Dependency/ContactType.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\ContactTypeData' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Dependency/ContactTypeData.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\FakeChoiceType' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Dependency/FakeChoiceType.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\FakeChoiceTypeData' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Dependency/FakeChoiceTypeData.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\OQLData' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Dependency/OQLData.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\OQLDataTransformer' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Dependency/OQLDataTransformer.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\OQLType' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Dependency/OQLType.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\OfficeType' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Dependency/OfficeType.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Dependency\\OfficeTypeData' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Dependency/OfficeTypeData.php',
'Combodo\\iTop\\Controller\\Form\\Type\\Form\\MailType' => __DIR__ . '/../..' . '/sources/Controller/Form/Type/Form/MailType.php',

View File

@@ -36,8 +36,7 @@ if ($issues) {
echo 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . str_replace('You are running '.PHP_VERSION.'.', '', implode(PHP_EOL, $issues)) . PHP_EOL.PHP_EOL;
}
}
trigger_error(
'Composer detected issues in your platform: ' . implode(' ', $issues),
E_USER_ERROR
throw new \RuntimeException(
'Composer detected issues in your platform: ' . implode(' ', $issues)
);
}

View File

@@ -2,6 +2,8 @@
namespace Combodo\iTop\Controller\Form;
use Combodo\iTop\Controller\Form\Type\Dependency\AttributeChoiceType;
use Combodo\iTop\Controller\Form\Type\Dependency\AttributeChoiceTypeData;
use Combodo\iTop\Controller\Form\Type\Dependency\ContactTypeData;
use Combodo\iTop\Controller\Form\Type\Dependency\FakeChoiceTypeData;
use Combodo\iTop\Controller\Form\Dependency\FormDependencyManager;
@@ -9,6 +11,7 @@ use Combodo\iTop\Controller\Form\Type\Dependency\OfficeTypeData;
use Combodo\iTop\Controller\Form\Type\Dependency\ContactType;
use Combodo\iTop\Controller\Form\Type\Dependency\FakeChoiceType;
use Combodo\iTop\Controller\Form\Type\Dependency\OfficeType;
use Combodo\iTop\Controller\Form\Type\Dependency\OQLType;
use Combodo\iTop\Controller\Form\Type\Form\Form2;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
@@ -71,6 +74,14 @@ class FormController extends AbstractController
],
])
->add('contact', ContactType::class)
->add('oql', OQLType::class, [
'bindings' => [
'object_class' => AttributeChoiceTypeData::OBJECT_CLASS
],
])
->add('object_class', AttributeChoiceType::class, [
'required' => false,
])
->getForm();
$form->handleRequest($request);

View File

@@ -0,0 +1,49 @@
<?php
namespace Combodo\iTop\Controller\Form\Type\Dependency;
use Combodo\iTop\Controller\Form\Dependency\AbstractMutableDependentType;
use Combodo\iTop\Controller\Form\Dependency\MutableStatusEnumeration;
use Combodo\iTop\Controller\Form\Dependency\SlotEnumeration;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormInterface;
class AttributeChoiceType extends AbstractMutableDependentType
{
public function getParent(): string
{
return ChoiceType::class;
}
public function mutate(FormInterface $form, array $data): array
{
$options = $form->getConfig()->getOptions();
$value = $data[AttributeChoiceTypeData::OBJECT_CLASS->name]['value'];
$options['choices'] = [];
if($value !== null){
// attributes of the selected class
$objectClass = $value->GetObjectClass();
foreach (\MetaModel::GetAttributesList($objectClass) as $attCode){
$options['choices'][$attCode] = $attCode;
}
// mute type
return [
'status' => MutableStatusEnumeration::MUTATION,
'type' => ChoiceType::class,
'options' => $options
];
}
else{
// delete type
return [
'status' => MutableStatusEnumeration::SUPPRESSION,
];
}
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace Combodo\iTop\Controller\Form\Type\Dependency;
enum AttributeChoiceTypeData
{
case OBJECT_CLASS;
}

View File

@@ -0,0 +1,8 @@
<?php
namespace Combodo\iTop\Controller\Form\Type\Dependency;
interface ClassProviderInterface
{
public function GetObjectClass();
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Combodo\iTop\Controller\Form\Type\Dependency;
class OQLData implements ClassProviderInterface
{
public function __construct(private string $sOQL)
{
}
public function GetOQL(): string
{
return $this->sOQL;
}
public function GetObjectClass(): string
{
return 'Contact';
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Combodo\iTop\Controller\Form\Type\Dependency;
use Symfony\Component\Form\DataTransformerInterface;
class OQLDataTransformer implements DataTransformerInterface
{
public function transform(mixed $value): string
{
if (null === $value) {
return '';
}
return $value->getOQL();
}
public function reverseTransform(mixed $value): ?OQLData
{
if (!$value) {
return null;
}
return new OQLData($value);
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Combodo\iTop\Controller\Form\Type\Dependency;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OQLType extends FormType
{
public function getParent(): string
{
return TextType::class;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addViewTransformer(new OQLDataTransformer());
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => OQLData::class,
]);
}
}

View File

@@ -33,10 +33,13 @@ class OfficeType extends AbstractMutableDependentType
$builder->add('office', FakeChoiceType::class,[
'fake_collection' => 'office',
'help' => '⚡ Will mutate place',
// on office post submit
'post_submit_callback' => function (FormEvent $event) {
// add place field to parent form
$event->getForm()->getParent()->add('place', IntegerType::class,[
'required' => false,
'disabled' => empty($event->getData()),
// preset place default value
'pre_set_data_callback' => function (FormEvent $event) {
if($event->getData() === null){
$event->setData(1); // prefer set this by model layer (not a form responsibility)