mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
N°8772 - Refactor
This commit is contained in:
@@ -7,13 +7,14 @@
|
||||
namespace Combodo\iTop\Forms\Block;
|
||||
|
||||
use Combodo\iTop\Forms\Block\Base\FormBlock;
|
||||
use Combodo\iTop\Forms\IFormBlock;
|
||||
use Combodo\iTop\Forms\IO\AbstractFormIO;
|
||||
use Combodo\iTop\Forms\IO\Converter\AbstractConverter;
|
||||
use Combodo\iTop\Forms\IO\Format\RawFormat;
|
||||
use Combodo\iTop\Forms\IO\FormInput;
|
||||
use Combodo\iTop\Forms\IO\FormOutput;
|
||||
use Combodo\iTop\Forms\Register\IORegister;
|
||||
use Combodo\iTop\Forms\Register\OptionsRegister;
|
||||
use Combodo\iTop\Forms\IFormBlock;
|
||||
|
||||
/**
|
||||
* Abstract form block.
|
||||
@@ -187,21 +188,6 @@ abstract class AbstractFormBlock implements IFormBlock
|
||||
*/
|
||||
public function AddInput(string $sName, string $sType): AbstractFormBlock
|
||||
{
|
||||
// Check name validity
|
||||
if (preg_match('/(?<name>\w+)/', $sName, $aMatches)) {
|
||||
$sParsedName = $aMatches['name'];
|
||||
if ($sParsedName !== $sName) {
|
||||
$sName = json_encode($sName);
|
||||
$sParsedName = json_encode($sParsedName);
|
||||
$sBlockName = json_encode($this->getName());
|
||||
Throw new FormBlockException("Input $sName does not match $sParsedName for block $sBlockName.");
|
||||
}
|
||||
} else {
|
||||
$sName = json_encode($sName);
|
||||
$sBlockName = json_encode($this->getName());
|
||||
Throw new FormBlockException("Input $sName is not valid for block $sBlockName.");
|
||||
}
|
||||
// Name is valid
|
||||
$this->oIORegister->AddInput($sName, $sType);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -72,10 +72,6 @@ class CollectionBlock extends AbstractTypeFormBlock
|
||||
$sBlockEntryOptions = $this->GetOption('block_entry_options');
|
||||
$this->oPrototypeBlock = new ($sBlockEntryType)('prototype', $sBlockEntryOptions);
|
||||
|
||||
// $this->HandleBlockDependencies();
|
||||
// $this->oBlock->SetParent($this->GetParent());
|
||||
// $oBlock->DependsOn('company', 'company', AbstractFormBlock::OUTPUT_VALUE);
|
||||
|
||||
$oOptionsRegister->SetOption('entry_type', $this->oPrototypeBlock->GetFormType());
|
||||
$oOptionsRegister->SetOption('entry_options', $this->oPrototypeBlock->GetOptions());
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ namespace Combodo\iTop\Forms\Block\Base;
|
||||
use Combodo\iTop\Forms\Block\AbstractFormBlock;
|
||||
use Combodo\iTop\Forms\Block\AbstractTypeFormBlock;
|
||||
use Combodo\iTop\Forms\Block\FormBlockException;
|
||||
use Combodo\iTop\Forms\FormType\Base\FormType;
|
||||
use Combodo\iTop\Forms\Register\OptionsRegister;
|
||||
use Combodo\iTop\Forms\FormBuilder\DependencyMap;
|
||||
use Combodo\iTop\Forms\FormsException;
|
||||
use Combodo\iTop\Forms\FormType\Base\FormType;
|
||||
use Combodo\iTop\Forms\Register\OptionsRegister;
|
||||
use Exception;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
@@ -43,8 +43,8 @@ class FormBlock extends AbstractTypeFormBlock
|
||||
// Build the form
|
||||
$this->BuildForm();
|
||||
}
|
||||
catch (Exception $ex) {
|
||||
throw new FormsException('Unable to construct demonstrator form.', 0, $ex);
|
||||
catch (Exception $e) {
|
||||
throw new FormBlockException('Unable to construct form', 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,6 +72,7 @@ class FormBlock extends AbstractTypeFormBlock
|
||||
*
|
||||
* @return $this
|
||||
* @throws ReflectionException
|
||||
* @throws \Combodo\iTop\Forms\Block\FormBlockException
|
||||
*/
|
||||
public function Add(string $sName, string $sType, array $aSymfonyOptions, array $aOptions = []): AbstractFormBlock
|
||||
{
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
namespace Combodo\iTop\Forms\Block\DataModel;
|
||||
|
||||
use Combodo\iTop\Forms\Block\Base\ChoiceFormBlock;
|
||||
use Combodo\iTop\Forms\Block\FormBlockException;
|
||||
use Combodo\iTop\Forms\IO\Format\AttributeIOFormat;
|
||||
use Combodo\iTop\Forms\IO\Format\ClassIOFormat;
|
||||
use Combodo\iTop\Forms\IO\Format\RawFormat;
|
||||
@@ -47,7 +48,9 @@ class AttributeValueChoiceFormBlock extends ChoiceFormBlock
|
||||
$oIORegister->AddOutput(self::OUTPUT_VALUE, RawFormat::class);
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc
|
||||
* @throws \Combodo\iTop\Forms\Block\FormBlockException
|
||||
*/
|
||||
public function UpdateOptions(OptionsRegister $oOptionsRegister): void
|
||||
{
|
||||
parent::UpdateOptions($oOptionsRegister);
|
||||
@@ -55,13 +58,15 @@ class AttributeValueChoiceFormBlock extends ChoiceFormBlock
|
||||
$oClassName = $this->GetInputValue(self::INPUT_CLASS_NAME);
|
||||
$oAttribute = $this->GetInputValue(self::INPUT_ATTRIBUTE);
|
||||
|
||||
try{
|
||||
try {
|
||||
$oAttDef = MetaModel::GetAttributeDef(strval($oClassName), strval($oAttribute));
|
||||
$aValues = $oAttDef->GetAllowedValues();
|
||||
|
||||
$oOptionsRegister->SetOption('choices', array_flip($aValues ?? []));
|
||||
}
|
||||
catch(Exception){}
|
||||
catch (Exception $e) {
|
||||
throw new FormBlockException('Update option failed for '.json_encode($this->GetName()), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -137,10 +137,10 @@ class DependencyHandler
|
||||
$oFormBlock = $this->oFormBlock->Get($oForm->getName());
|
||||
|
||||
// Compute the block outputs with the data
|
||||
try{
|
||||
try {
|
||||
$oFormBlock->ComputeOutputs($sEventType, $oForm->getData());
|
||||
}
|
||||
catch(Exception $e){
|
||||
catch (Exception $e) {
|
||||
$oForm->addError(new FormError($e->getMessage()));
|
||||
}
|
||||
|
||||
@@ -150,19 +150,19 @@ class DependencyHandler
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param FormInterface|FormBuilderInterface $oForm
|
||||
* @param string|null $sOutputBlock
|
||||
* @param string|null $sEventType
|
||||
*
|
||||
* @return void
|
||||
* @throws \Combodo\iTop\Forms\Block\FormBlockException
|
||||
*/
|
||||
private function CheckDependencies(FormInterface|FormBuilderInterface $oForm, string $sOutputBlock = null, string $sEventType = null): void
|
||||
{
|
||||
$aImpactedBlocks = $this->aDependentBlocks;
|
||||
if($sOutputBlock !== null){
|
||||
$aImpactedBlocks = $this->oDependenciesMap->GetBlocksImpactedBy($sOutputBlock, function(AbstractFormBlock $oBlock) use ($sEventType) {
|
||||
if ($sOutputBlock !== null) {
|
||||
$aImpactedBlocks = $this->oDependenciesMap->GetBlocksImpactedBy($sOutputBlock, function (AbstractFormBlock $oBlock) use ($sEventType) {
|
||||
return $oBlock instanceof AbstractTypeFormBlock;
|
||||
});
|
||||
}
|
||||
@@ -213,9 +213,7 @@ class DependencyHandler
|
||||
// Add event
|
||||
$this->AddEvent('form.remove', $oDependentBlock->getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -46,7 +46,6 @@ class FormTypeExtension extends AbstractTypeExtension
|
||||
$builder->addEventListener(FormEvents::POST_SET_DATA, $options['builder_listener']);
|
||||
$builder->addEventListener(FormEvents::POST_SUBMIT, $options['builder_listener']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
@@ -58,7 +57,5 @@ class FormTypeExtension extends AbstractTypeExtension
|
||||
$oFormBlock = $options['form_block'];
|
||||
$view->vars['trigger_form_submit_on_modify'] = $oFormBlock->ImpactDependentsBlocks();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -40,7 +40,7 @@ class AbstractFormIO
|
||||
*/
|
||||
public function __construct(string $sName, string $sType, AbstractFormBlock $oOwnerBlock)
|
||||
{
|
||||
$this->sName = $sName;
|
||||
$this->SetName($sName);
|
||||
$this->sType = $sType;
|
||||
$this->oOwnerBlock = $oOwnerBlock;
|
||||
}
|
||||
@@ -71,9 +71,26 @@ class AbstractFormIO
|
||||
* @param string $sName
|
||||
*
|
||||
* @return self
|
||||
* @throws \Combodo\iTop\Forms\IO\FormBlockIOException
|
||||
*/
|
||||
public function SetName(string $sName): self
|
||||
{
|
||||
// Check name validity
|
||||
if (preg_match('/(?<name>\w+)/', $sName, $aMatches)) {
|
||||
$sParsedName = $aMatches['name'];
|
||||
if ($sParsedName !== $sName) {
|
||||
$sName = json_encode($sName);
|
||||
$sParsedName = json_encode($sParsedName);
|
||||
$sBlockName = json_encode($this->getName());
|
||||
Throw new FormBlockIOException("Input $sName does not match $sParsedName for block $sBlockName.");
|
||||
}
|
||||
} else {
|
||||
$sName = json_encode($sName);
|
||||
$sBlockName = json_encode($this->getName());
|
||||
Throw new FormBlockIOException("Input $sName is not valid for block $sBlockName.");
|
||||
}
|
||||
|
||||
// Name is valid
|
||||
$this->sName = $sName;
|
||||
|
||||
return $this;
|
||||
@@ -210,7 +227,7 @@ class AbstractFormIO
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function Attach(FormBinding $oFormBinding)
|
||||
public function Attach(FormBinding $oFormBinding): void
|
||||
{
|
||||
$this->oBinding = $oFormBinding;
|
||||
}
|
||||
|
||||
@@ -7,15 +7,17 @@
|
||||
namespace Combodo\iTop\Forms\IO\Converter;
|
||||
|
||||
use Combodo\iTop\Forms\IO\Format\ClassIOFormat;
|
||||
use Combodo\iTop\Forms\IO\FormBlockIOException;
|
||||
use MetaModel;
|
||||
use Symfony\Component\Filesystem\Exception\IOException;
|
||||
|
||||
/**
|
||||
* OQL expression to class converter.
|
||||
*/
|
||||
class OqlToClassConverter extends AbstractConverter
|
||||
{
|
||||
/** @inheritdoc */
|
||||
/** @inheritdoc
|
||||
* @throws \Combodo\iTop\Forms\IO\FormBlockIOException
|
||||
*/
|
||||
public function Convert(mixed $oData): ?ClassIOFormat
|
||||
{
|
||||
if ($oData === null) {
|
||||
@@ -29,12 +31,12 @@ class OqlToClassConverter extends AbstractConverter
|
||||
if (isset($aMatches[1])) {
|
||||
$sSelectedClass = $aMatches[1];
|
||||
if (!MetaModel::IsValidClass($sSelectedClass)) {
|
||||
throw new IOException("Class `$sSelectedClass` not found");
|
||||
throw new FormBlockIOException('Class '.json_encode($sSelectedClass).' not found');
|
||||
}
|
||||
|
||||
return new ClassIOFormat($aMatches[1]);
|
||||
} else {
|
||||
throw new IOException('Incorrect OQL sentence');
|
||||
throw new FormBlockIOException('Incorrect OQL sentence '.json_encode($oData));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ class IORegister
|
||||
* @param string $sOutputBlockName
|
||||
* @param string $sOutputName
|
||||
*
|
||||
* @return AbstractFormBlock
|
||||
* @return $this
|
||||
* @throws FormBlockException
|
||||
*/
|
||||
public function AddInputDependsOn(string $sName, string $sOutputBlockName, string $sOutputName): self
|
||||
@@ -171,7 +171,7 @@ class IORegister
|
||||
public function GetOutput(string $sName): FormOutput
|
||||
{
|
||||
if (!array_key_exists($sName, $this->aOutputs)) {
|
||||
throw new FormBlockException('Missing output '.$sName.' for '.$this->oFormBlock->GetName());
|
||||
throw new FormBlockException('Missing output '.json_encode($sName).' for '.json_encode($this->oFormBlock->GetName()));
|
||||
}
|
||||
|
||||
return $this->aOutputs[$sName];
|
||||
|
||||
Reference in New Issue
Block a user