N°7063 - Forms SDK - Add Symfony forms component

error forms issue
This commit is contained in:
Benjamin Dalsass
2023-12-27 16:53:39 +01:00
parent d6970f6486
commit 2bcc4d9989
25 changed files with 450 additions and 370 deletions

View File

@@ -0,0 +1,134 @@
<?php
namespace Combodo\iTop\FormImplementation\Helper;
use Combodo\iTop\Controller\AbstractAppController;
use Combodo\iTop\FormSDK\Service\FormManager;
use DateTime;
use MetaModel;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Regex;
class FormHelper
{
/**
* Create a sample form factory for demo purpose.
*
* @param \Combodo\iTop\FormSDK\Service\FormManager $oFormManager
* @param \Symfony\Component\Routing\RouterInterface $oRouter
*
* @return \Combodo\iTop\FormSDK\Service\FormFactory
* @throws \ArchivedObjectException
* @throws \CoreException
*/
static public function CreateSampleFormFactory(FormManager $oFormManager, RouterInterface $oRouter)
{
// create a factory
$oFormFactory = $oFormManager->CreateFactory();
// add X person forms...
for($i = 0 ; $i < 2 ; $i++){
// retrieve person
$oPerson = MetaModel::GetObject('Person', $i+1);
// create object adapter
$oObjectPlugin = $oFormFactory->CreateObjectAdapter($oPerson, true);
$oObjectPlugin->AddAttribute('name');
$oObjectPlugin->AddAttribute('mobile_phone');
}
// city - text
$oFormFactory->AddTextField('city', [
'label' => 'Ma ville',
'help' => 'This is where you live',
'constraints' => new Length(['min' => 3])
], 'Autun');
// tel - text with pattern
$oFormFactory->AddTextField('tel', [
'label' => 'Tel',
'constraints' => new Regex(['pattern' => '+{33}(0) 00 00 00 00']),
// 'constraints' => new Regex(['pattern' => '/^[1-6]\d{0,5}$/']),
'required' => false
], null);
// birthday - date
$oFormFactory->AddDateField('birthday', [
'label' => 'Anniversaire',
'widget' => 'single_text',
'required' => false
], new DateTime('1979/06/27'));
// ready
$oFormFactory->AddSwitchField('notify', [
'label' => 'Veuillez m\'avertir en cas de changement',
], true);
// blog - date
$oFormFactory->AddAreaField('blog', [
'label' => 'Blog',
'required' => false
], 'Your story');
// language - select with static data
$oFormFactory->AddSelectField('language', [
'label' => 'Ma langue',
'choices' => SelectDataProvider::GetApplicationLanguages()
], 'FR FR');
// dog - select with ajax API
$oFormFactory->AddSelectAjaxField('dog', [
'label' => 'Mon Chien',
'placeholder' => 'Sélectionnez un chien',
'required' => false,
], [
'url' => 'http://localhost' . $oRouter->generate('formSDK_ajax_select'),
'query_parameter' => 'query',
'value_field' => 'breed',
'label_field' => 'breed',
'search_field' => 'breed',
'threshold' => 20,
'max_items' => 1
]);
// friends - select with OQL
$oFormFactory->AddSelectOqlField('friends', [
'label' => 'Ma personne',
'required' => false
], 'Person', 'SELECT Person', [], '', 20);
// requests - select with OQL
$oFormFactory->AddSelectOqlField('requests', [
'label' => 'Tickets',
'required' => false
], 'UserRequest', 'SELECT UserRequest', [], '', 20);
// mode - select with static data
$oFormFactory->AddSelectField('mode', [
'label' => 'Mon mode',
'choices' => SelectDataProvider::GetModes(),
'expanded' => true,
'multiple' => false,
'label_attr' => [
'class' => 'radio-inline'
]
], '1');
// options - select with static data
$oFormFactory->AddSelectField('option', [
'label' => 'Mes options',
'choices' => SelectDataProvider::GetOptions(),
'expanded' => true,
'multiple' => true,
'label_attr' => [
'class' => 'checkbox-inline'
]
], ['0', '2','4']);
return $oFormFactory;
}
}

View File

@@ -0,0 +1,78 @@
<?php
/*
* Copyright (C) 2013-2023 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
namespace Combodo\iTop\FormImplementation\Helper;
use Dict;
/**
*
* @package FormSDK
* @since 3.2.0
*/
class SelectDataProvider
{
/**
* Return array of application languages.
*
* @return array
*/
static public function GetApplicationLanguages() : array
{
$aAvailableLanguages = Dict::GetLanguages();
$aLanguageCodes = array();
foreach($aAvailableLanguages as $sLangCode => $aInfo){
$sLanguageLabel = $aInfo['description'].' ('.$aInfo['localized_description'].')';
$aLanguageCodes[$sLanguageLabel] = $sLangCode;
}
return $aLanguageCodes;
}
/**
* Return array of modes.
*
* @return array
*/
static public function GetModes() : array
{
return [
'Minimal' => 0,
'Optimal' => 1,
"Maximal" => 2
];
}
/**
* Return array of options.
*
* @return array
*/
static public function GetOptions() : array
{
return [
'Option A' => 0,
'Option B' => 1,
"Option C" => 2,
"Option D" => 3,
"Option E" => 4,
"Option F" => 5
];
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Combodo\iTop\FormImplementation\Helper;
use MetaModel;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Helper to construct assets paths.
*
*/
class TwigHelper extends AbstractExtension
{
/** @var string|mixed application root */
private string $sAppRoot;
/**
* Constructor.
*
*/
public function __construct()
{
$this->sAppRoot = MetaModel::GetConfig()->Get('app_root_url');
}
/** @inheritdoc */
public function getFunctions() : array
{
return [
new TwigFunction('asset_js', [$this, 'asset_js']),
new TwigFunction('asset_css', [$this, 'asset_css']),
new TwigFunction('asset_image', [$this, 'asset_image']),
new TwigFunction('asset_node', [$this, 'asset_node']),
];
}
/**
* @param $name
*
* @return string
*/
public function asset_js($name) : string
{
return $this->sAppRoot . 'js/' . $name;
}
/**
* @param $name
*
* @return string
*/
public function asset_css($name) : string
{
return $this->sAppRoot . 'css/' . $name;
}
/**
* @param $name
*
* @return string
*/
public function asset_image($name) : string
{
return $this->sAppRoot . 'images/' . $name;
}
/**
* @param $name
*
* @return string
*/
public function asset_node($name) : string
{
return $this->sAppRoot . 'node_modules/' . $name;
}
}