mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
78 lines
2.3 KiB
PHP
78 lines
2.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* @copyright Copyright (C) 2010-2025 Combodo SARL
|
|
* @license http://opensource.org/licenses/AGPL-3.0
|
|
*/
|
|
|
|
namespace Combodo\iTop\Forms\Block\DataModel;
|
|
|
|
use Combodo\iTop\Service\DependencyInjection\DIService;
|
|
use Combodo\iTop\Forms\Block\Base\ChoiceFormBlock;
|
|
use Combodo\iTop\Forms\IO\Converter\StringToAttributeConverter;
|
|
use Combodo\iTop\Forms\IO\Format\AttributeIOFormat;
|
|
use Combodo\iTop\Forms\IO\Format\ClassIOFormat;
|
|
use Combodo\iTop\Forms\Register\IORegister;
|
|
use Combodo\iTop\Forms\Register\OptionsRegister;
|
|
use utils;
|
|
|
|
/**
|
|
* Form block for choice of class attributes.
|
|
*
|
|
* @package DataModel
|
|
*/
|
|
class AttributeChoiceFormBlock extends ChoiceFormBlock
|
|
{
|
|
// inputs
|
|
public const INPUT_CLASS_NAME = 'class_name';
|
|
|
|
// outputs
|
|
public const OUTPUT_ATTRIBUTE = 'attribute';
|
|
|
|
/** @inheritdoc */
|
|
protected function RegisterOptions(OptionsRegister $oOptionsRegister): void
|
|
{
|
|
parent::RegisterOptions($oOptionsRegister);
|
|
$oOptionsRegister->SetOption('placeholder', 'Select an attribute...');
|
|
}
|
|
|
|
/** @inheritdoc */
|
|
protected function RegisterIO(IORegister $oIORegister): void
|
|
{
|
|
parent::RegisterIO($oIORegister);
|
|
$oIORegister->AddInput(self::INPUT_CLASS_NAME, ClassIOFormat::class);
|
|
$oIORegister->AddOutput(self::OUTPUT_ATTRIBUTE, AttributeIOFormat::class, new StringToAttributeConverter());
|
|
}
|
|
|
|
/**
|
|
* @param \Combodo\iTop\Forms\Register\OptionsRegister $oOptionsRegister
|
|
*
|
|
* @throws \Combodo\iTop\Service\DependencyInjection\DIException
|
|
* @throws \Combodo\iTop\Forms\Block\FormBlockException
|
|
* @throws \Combodo\iTop\Forms\Register\RegisterException
|
|
*/
|
|
public function UpdateOptions(OptionsRegister $oOptionsRegister): void
|
|
{
|
|
parent::UpdateOptions($oOptionsRegister);
|
|
|
|
$sClass = strval($this->GetInputValue(self::INPUT_CLASS_NAME));
|
|
|
|
if (utils::IsNullOrEmptyString($sClass)) {
|
|
$oOptionsRegister->SetOption('choices', []);
|
|
return;
|
|
}
|
|
|
|
/** @var \ModelReflection $oModelReflection */
|
|
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection');
|
|
$aAttributeCodes = array_keys($oModelReflection->ListAttributes($sClass));
|
|
|
|
$aAttributes = [];
|
|
foreach ($aAttributeCodes as $sAttCode) {
|
|
$sLabel = $oModelReflection->GetLabel($sClass, $sAttCode);
|
|
$aAttributes[$sLabel] = $sAttCode;
|
|
}
|
|
|
|
$oOptionsRegister->SetOption('choices', $aAttributes);
|
|
}
|
|
}
|