diff --git a/sources/FormType/Orm/AttCodeGroupByType.php b/sources/FormType/Orm/AttCodeGroupByType.php index e59f21dc5..531012d3a 100644 --- a/sources/FormType/Orm/AttCodeGroupByType.php +++ b/sources/FormType/Orm/AttCodeGroupByType.php @@ -12,7 +12,6 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SymfonyChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; -use Symfony\Component\Form\FormInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class AttCodeGroupByType extends AbstractType @@ -39,24 +38,19 @@ class AttCodeGroupByType extends AbstractType ->setAllowedTypes('hook_type', 'string'); } - public static function BuildSubField(FormInterface $oForm, string $sName, array $aData, array $aFormOptions = []): void - { - \IssueLog::Info('AttCodeGroupByType BuildSubField data: '.var_export($aData, true)); - - $aFormOptions['choices'] = self::GetGroupByOptions($aData['query']); - $aFormOptions['multiple'] = false; - $oForm->add($sName, AttCodeGroupByType::class, $aFormOptions); - } - - protected static function GetGroupByOptions($sOql) + protected function GetGroupByOptions($sClassOrOql) { $oModelReflection = new \ModelReflectionRuntime(); $aGroupBy = array(); try { - $oQuery = $oModelReflection->GetQuery($sOql); - $sClass = $oQuery->GetClass(); + if ($oModelReflection->IsValidClass($sClassOrOql)) { + $sClass = $sClassOrOql; + } else { + $oQuery = $oModelReflection->GetQuery($sClassOrOql); + $sClass = $oQuery->GetClass(); + } foreach($oModelReflection->ListAttributes($sClass) as $sAttCode => $sAttType) { // For external fields, find the real type of the target @@ -110,4 +104,29 @@ class AttCodeGroupByType extends AbstractType } return array_flip($aGroupBy); } + + public function BuildOptions(array $aUserOptions, array $aModelData): ?array + { + $oModelReflection = new \ModelReflectionRuntime(); + $sClassOrOql = $aModelData[$aUserOptions['source_class']]; + if ($oModelReflection->IsValidClass($sClassOrOql)) { + $sClass = $sClassOrOql; + } else { + try { + $oQuery = $oModelReflection->GetQuery($sClassOrOql); + } catch (Exception $e) { + return null; + } + $sClass = $oQuery->GetClass(); + } + $aFormOptions['choices'] = $this->GetGroupByOptions($sClass); + $aFormOptions['multiple'] = false; + + return $aFormOptions; + } + + public function GetPrerequisites(array $aUserOptions): ?array + { + return [$aUserOptions['source_class']]; + } } \ No newline at end of file diff --git a/sources/FormType/Orm/ValuesFromAttcodeType.php b/sources/FormType/Orm/ValuesFromAttcodeType.php index 47ae54923..d4f56c270 100644 --- a/sources/FormType/Orm/ValuesFromAttcodeType.php +++ b/sources/FormType/Orm/ValuesFromAttcodeType.php @@ -11,12 +11,13 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SymfonyChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; -use Symfony\Component\Form\FormInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use utils; class ValuesFromAttcodeType extends AbstractType { + + public function buildForm(FormBuilderInterface $builder, array $options) { $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { @@ -44,18 +45,22 @@ class ValuesFromAttcodeType extends AbstractType $resolver->setDefault('attr', ['class' => 'ibo-input ibo-input-select']); } - public static function BuildSubField(FormInterface $oForm, string $sName, array $aData, array $aFormOptions = []): void + public function BuildOptions(array $aUserOptions, array $aModelData): ?array { - \IssueLog::Info("ValuesFromAttcodeType BuildSubField data: ".var_export($aData, true)); - - - if (utils::IsNullOrEmptyString($aData['group_by'] ?? null)) { - return; + $sAttCode = $aModelData[$aUserOptions['source_attcode']] ?? null; + if (utils::IsNullOrEmptyString($sAttCode)) { + return null; } else { $oModelReflection = new \ModelReflectionRuntime(); - $oQuery = $oModelReflection->GetQuery($aData['query']); - $sClass = $oQuery->GetClass(); - $sAttCode = $aData['group_by']; + $sClass = $aModelData[$aUserOptions['source_class']]; + if (! $oModelReflection->IsValidClass($sClass)) { + try { + $oQuery = $oModelReflection->GetQuery($sClass); + } catch (\Exception $e) { + return null; + } + $sClass = $oQuery->GetClass(); + } if (\MetaModel::IsValidAttCode($sClass, $sAttCode)) { $aAllowed = $oModelReflection->GetAllowedValues_att($sClass, $sAttCode); if (is_array($aAllowed)) @@ -72,7 +77,14 @@ class ValuesFromAttcodeType extends AbstractType $aFormOptions['multiple'] = true; $aFormOptions['required'] = false; - $oForm->add($sName, ValuesFromAttcodeType::class, $aFormOptions); + return $aFormOptions; } + public function GetPrerequisites(array $aUserOptions): ?array + { + return [ + $aUserOptions['source_class'], + $aUserOptions['source_attcode'], + ]; + } } \ No newline at end of file