mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
101 lines
2.6 KiB
PHP
101 lines
2.6 KiB
PHP
<?php
|
|
/*
|
|
* @copyright Copyright (C) 2010-2025 Combodo SARL
|
|
* @license http://opensource.org/licenses/AGPL-3.0
|
|
*/
|
|
|
|
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;
|
|
use Symfony\Component\Form\FormInterface;
|
|
|
|
/**
|
|
* Dependencies handler.
|
|
*
|
|
*/
|
|
class DependencyHandler
|
|
{
|
|
/** @var FormBuilder form builder */
|
|
private FormBuilder $oFormBuilder;
|
|
|
|
/** @var array dependant blocks */
|
|
private array $aDependentBlocks;
|
|
|
|
/** @var DependencyMap dependencies map */
|
|
private DependencyMap $aDependenciesMap;
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param FormBuilder $oFormBuilder
|
|
* @param array $aDependentBlocks
|
|
*/
|
|
public function __construct(FormBuilder $oFormBuilder, array $aDependentBlocks)
|
|
{
|
|
$this->aDependentBlocks = $aDependentBlocks;
|
|
$this->oFormBuilder = $oFormBuilder;
|
|
|
|
// add form ready listener
|
|
$this->AddFormReadyListener();
|
|
}
|
|
|
|
/**
|
|
* Add form ready listener.
|
|
*
|
|
* Listen the form PRE_SET_DATA
|
|
* First event from Symfony framework, we know that the form is built at this step.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function AddFormReadyListener(): void
|
|
{
|
|
// Initialize the dependencies listeners once the form is built
|
|
$this->oFormBuilder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
|
|
|
|
// 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());
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
/**
|
|
* Get the listening callback.
|
|
*
|
|
* @return callable
|
|
*/
|
|
private function GetEventListeningCallback(): callable
|
|
{
|
|
return function (FormEvent $oEvent) {
|
|
|
|
// Get the event type
|
|
$sEventType = FormHelper::GetEventType($oEvent);
|
|
|
|
// Get the form
|
|
$oForm = $oEvent->getForm();
|
|
|
|
/** Iterate throw dependencies map... */
|
|
$sOutputBlockName = $oForm->getName();
|
|
$oOutputBlock = $this->oFormBuilder->GetFormBlock($sOutputBlockName);
|
|
foreach ($this->aDependenciesMap->GetOutputsForBlock($sOutputBlockName) as $sOutputName) {
|
|
$oOutput = $oOutputBlock->GetOutput($sOutputName);
|
|
$oOutput->UpdateOutputValue($oEvent->getData(), $sEventType);
|
|
}
|
|
|
|
return;
|
|
};
|
|
|
|
}
|
|
|
|
|
|
} |