mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
N°8771 - Add Symfony form component to iTop core
- IO
This commit is contained in:
@@ -490,8 +490,10 @@ return array(
|
||||
'Combodo\\iTop\\Forms\\Converter\\OqlToClassConverter' => $baseDir . '/sources/Forms/Converter/OqlToClassConverter.php',
|
||||
'Combodo\\iTop\\Forms\\Converter\\StringToAttributeConverter' => $baseDir . '/sources/Forms/Converter/StringToAttributeConverter.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\DependencyHandler' => $baseDir . '/sources/Forms/FormBuilder/DependencyHandler.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\DependencyMap' => $baseDir . '/sources/Forms/FormBuilder/DependencyMap.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\FormBuilder' => $baseDir . '/sources/Forms/FormBuilder/FormBuilder.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\FormBuilderException' => $baseDir . '/sources/Forms/FormBuilder/FormBuilderException.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\FormHelper' => $baseDir . '/sources/Forms/FormBuilder/FormHelper.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\FormTypeExtension' => $baseDir . '/sources/Forms/FormBuilder/FormTypeExtension.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\ResolvedFormType' => $baseDir . '/sources/Forms/FormBuilder/ResolvedFormType.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\ResolvedFormTypeFactory' => $baseDir . '/sources/Forms/FormBuilder/ResolvedFormTypeFactory.php',
|
||||
|
||||
@@ -871,8 +871,10 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f
|
||||
'Combodo\\iTop\\Forms\\Converter\\OqlToClassConverter' => __DIR__ . '/../..' . '/sources/Forms/Converter/OqlToClassConverter.php',
|
||||
'Combodo\\iTop\\Forms\\Converter\\StringToAttributeConverter' => __DIR__ . '/../..' . '/sources/Forms/Converter/StringToAttributeConverter.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\DependencyHandler' => __DIR__ . '/../..' . '/sources/Forms/FormBuilder/DependencyHandler.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\DependencyMap' => __DIR__ . '/../..' . '/sources/Forms/FormBuilder/DependencyMap.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\FormBuilder' => __DIR__ . '/../..' . '/sources/Forms/FormBuilder/FormBuilder.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\FormBuilderException' => __DIR__ . '/../..' . '/sources/Forms/FormBuilder/FormBuilderException.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\FormHelper' => __DIR__ . '/../..' . '/sources/Forms/FormBuilder/FormHelper.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\FormTypeExtension' => __DIR__ . '/../..' . '/sources/Forms/FormBuilder/FormTypeExtension.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\ResolvedFormType' => __DIR__ . '/../..' . '/sources/Forms/FormBuilder/ResolvedFormType.php',
|
||||
'Combodo\\iTop\\Forms\\FormBuilder\\ResolvedFormTypeFactory' => __DIR__ . '/../..' . '/sources/Forms/FormBuilder/ResolvedFormTypeFactory.php',
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
namespace Combodo\iTop\Forms\FormBuilder;
|
||||
|
||||
use Combodo\iTop\Forms\Block\FormBlock;
|
||||
use Symfony\Component\Form\Event\PostSetDataEvent;
|
||||
use Symfony\Component\Form\Event\PostSubmitEvent;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
@@ -23,10 +22,10 @@ class DependencyHandler
|
||||
private FormBuilder $oFormBuilder;
|
||||
|
||||
/** @var array dependant blocks */
|
||||
private array $aDependentBlocks = [];
|
||||
private array $aDependentBlocks;
|
||||
|
||||
/** @var array dependencies map */
|
||||
private array $aDependenciesMap = [];
|
||||
/** @var DependencyMap dependencies map */
|
||||
private DependencyMap $aDependenciesMap;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -55,63 +54,20 @@ class DependencyHandler
|
||||
{
|
||||
// Initialize the dependencies listeners once the form is built
|
||||
$this->oFormBuilder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
|
||||
$oForm = $event->getForm();
|
||||
$this->InitializeDependenciesMap($oForm);
|
||||
|
||||
// dependencies map
|
||||
$this->aDependenciesMap = new DependencyMap($this->aDependentBlocks);
|
||||
|
||||
/** Iterate throw listened blocks */
|
||||
foreach ($this->aDependenciesMap->GetListenedBlockNames() as $sOutputBlockName) {
|
||||
|
||||
// Listen the output block POST_SET_DATA & POST_SUBMIT
|
||||
$this->oFormBuilder->get($sOutputBlockName)->addEventListener(FormEvents::POST_SET_DATA, $this->GetEventListeningCallback());
|
||||
$this->oFormBuilder->get($sOutputBlockName)->addEventListener(FormEvents::POST_SUBMIT, $this->GetEventListeningCallback());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize dependencies map.
|
||||
*
|
||||
* @param FormInterface $oForm
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function InitializeDependenciesMap(FormInterface $oForm): void
|
||||
{
|
||||
// Connections from output pov
|
||||
$aDependenciesMap = [];
|
||||
|
||||
/** iterate throw dependents blocks... @var FormBlock $oDependentBlock */
|
||||
foreach ($this->aDependentBlocks as $oDependentBlock) {
|
||||
|
||||
/** iterate throw the block inputs connections... **/
|
||||
foreach ($oDependentBlock->GetInputsConnections() as $sInputName => $aConnections) {
|
||||
|
||||
/** Iterate throw the input connections... */
|
||||
foreach ($aConnections as $aConnection) {
|
||||
|
||||
// connection information
|
||||
$sOutputBlock = $aConnection['output_block'];
|
||||
$sOutputBlockName = $sOutputBlock->getName();
|
||||
$sOutputName = $aConnection['output'];
|
||||
|
||||
// initialize map
|
||||
if (!isset($aDependenciesMap[$sOutputBlockName])) {
|
||||
$aDependenciesMap[$sOutputBlockName] = [];
|
||||
}
|
||||
if (!isset($aDependenciesMap[$sOutputBlockName][$sOutputName])) {
|
||||
$aDependenciesMap[$sOutputBlockName][$sOutputName] = [];
|
||||
}
|
||||
|
||||
// add to map
|
||||
$aDependenciesMap[$sOutputBlockName][$sOutputName][] = ['input_block' => $oDependentBlock, 'input_name' => $sInputName];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// store the dependencies map
|
||||
$this->aDependenciesMap = $aDependenciesMap;
|
||||
|
||||
/** Iterate throw output blocks */
|
||||
foreach (array_keys($aDependenciesMap) as $sOutputBlockName) {
|
||||
|
||||
// Listen the output block POST_SET_DATA & POST_SUBMIT
|
||||
$this->oFormBuilder->get($sOutputBlockName)->addEventListener(FormEvents::POST_SET_DATA, $this->GetEventListeningCallback());
|
||||
$this->oFormBuilder->get($sOutputBlockName)->addEventListener(FormEvents::POST_SUBMIT, $this->GetEventListeningCallback());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the listening callback.
|
||||
@@ -121,8 +77,9 @@ class DependencyHandler
|
||||
private function GetEventListeningCallback(): callable
|
||||
{
|
||||
return function (FormEvent $oEvent) {
|
||||
|
||||
// Get the event type
|
||||
$sEventType = $this->GetEventType($oEvent);
|
||||
$sEventType = FormHelper::GetEventType($oEvent);
|
||||
|
||||
// Get the form
|
||||
$oForm = $oEvent->getForm();
|
||||
@@ -130,7 +87,7 @@ class DependencyHandler
|
||||
/** Iterate throw dependencies map... */
|
||||
$sOutputBlockName = $oForm->getName();
|
||||
$oOutputBlock = $this->oFormBuilder->GetFormBlock($sOutputBlockName);
|
||||
foreach (array_keys($this->aDependenciesMap[$sOutputBlockName]) as $sOutputName) {
|
||||
foreach ($this->aDependenciesMap->GetOutputsForBlock($sOutputBlockName) as $sOutputName) {
|
||||
$oOutput = $oOutputBlock->GetOutput($sOutputName);
|
||||
$oOutput->UpdateOutputValue($oEvent->getData(), $sEventType);
|
||||
}
|
||||
@@ -140,22 +97,5 @@ class DependencyHandler
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the event type.
|
||||
*
|
||||
* @param FormEvent $event
|
||||
*
|
||||
* @return string
|
||||
* @throws FormBuilderException
|
||||
*/
|
||||
private function GetEventType(FormEvent $event): string
|
||||
{
|
||||
if ($event instanceof PostSetDataEvent) {
|
||||
return FormEvents::POST_SET_DATA;
|
||||
} else if ($event instanceof PostSubmitEvent) {
|
||||
return FormEvents::POST_SUBMIT;
|
||||
}
|
||||
|
||||
throw new FormBuilderException(sprintf('Unknown event type %s', get_class($event)));
|
||||
}
|
||||
}
|
||||
82
sources/Forms/FormBuilder/DependencyMap.php
Normal file
82
sources/Forms/FormBuilder/DependencyMap.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/*
|
||||
* @copyright Copyright (C) 2010-2025 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Forms\FormBuilder;
|
||||
|
||||
use Combodo\iTop\Forms\Block\FormBlock;
|
||||
use Symfony\Component\Form\Event\PostSetDataEvent;
|
||||
use Symfony\Component\Form\Event\PostSubmitEvent;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
|
||||
/**
|
||||
* Dependencies handler.
|
||||
*
|
||||
*/
|
||||
class DependencyMap
|
||||
{
|
||||
/** @var array dependant blocks */
|
||||
private array $aDependentBlocks = [];
|
||||
|
||||
/** @var array dependencies map */
|
||||
private array $aDependenciesMap = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $aDependentBlocks
|
||||
*/
|
||||
public function __construct(array $aDependentBlocks)
|
||||
{
|
||||
$this->aDependentBlocks = $aDependentBlocks;
|
||||
|
||||
// Initialization
|
||||
$this->Init();
|
||||
}
|
||||
|
||||
private function Init(): void
|
||||
{
|
||||
/** iterate throw dependents blocks... @var FormBlock $oDependentBlock */
|
||||
foreach ($this->aDependentBlocks as $oDependentBlock) {
|
||||
|
||||
/** iterate throw the block inputs connections... **/
|
||||
foreach ($oDependentBlock->GetInputsConnections() as $sInputName => $aConnections) {
|
||||
|
||||
/** Iterate throw the input connections... */
|
||||
foreach ($aConnections as $aConnection) {
|
||||
|
||||
// connection information
|
||||
$sOutputBlock = $aConnection['output_block'];
|
||||
$sOutputBlockName = $sOutputBlock->getName();
|
||||
$sOutputName = $aConnection['output'];
|
||||
|
||||
// initialize map
|
||||
if (!isset($this->aDependenciesMap[$sOutputBlockName])) {
|
||||
$this->aDependenciesMap[$sOutputBlockName] = [];
|
||||
}
|
||||
if (!isset($this->aDependenciesMap[$sOutputBlockName][$sOutputName])) {
|
||||
$this->aDependenciesMap[$sOutputBlockName][$sOutputName] = [];
|
||||
}
|
||||
|
||||
// add to map
|
||||
$this->aDependenciesMap[$sOutputBlockName][$sOutputName][] = ['input_block' => $oDependentBlock, 'input_name' => $sInputName];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function GetListenedBlockNames(): array
|
||||
{
|
||||
return array_keys($this->aDependenciesMap);
|
||||
}
|
||||
|
||||
public function GetOutputsForBlock(string $sBlockName): array
|
||||
{
|
||||
return array_keys($this->aDependenciesMap[$sBlockName]);
|
||||
}
|
||||
}
|
||||
30
sources/Forms/FormBuilder/FormHelper.php
Normal file
30
sources/Forms/FormBuilder/FormHelper.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Combodo\iTop\Forms\FormBuilder;
|
||||
|
||||
use Symfony\Component\Form\Event\PostSetDataEvent;
|
||||
use Symfony\Component\Form\Event\PostSubmitEvent;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
|
||||
class FormHelper
|
||||
{
|
||||
/**
|
||||
* Get the event type.
|
||||
*
|
||||
* @param FormEvent $event
|
||||
*
|
||||
* @return string
|
||||
* @throws FormBuilderException
|
||||
*/
|
||||
public static function GetEventType(FormEvent $event): string
|
||||
{
|
||||
if ($event instanceof PostSetDataEvent) {
|
||||
return FormEvents::POST_SET_DATA;
|
||||
} else if ($event instanceof PostSubmitEvent) {
|
||||
return FormEvents::POST_SUBMIT;
|
||||
}
|
||||
|
||||
throw new FormBuilderException(sprintf('Unknown event type %s', get_class($event)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user