mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 18:48:51 +02:00
N°8771 - Add Symfony form component to iTop core
- IO
This commit is contained in:
@@ -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)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user