Updated Form handler

This commit is contained in:
Florian Bouillon 2019-04-17 01:31:12 +02:00
parent ca0150ca0b
commit 0a724f9dc6

View File

@ -4,33 +4,8 @@ namespace AdminPanel;
class Form class Form
{ {
private static $knownOptions = array(
'type',
'choices',
'label',
'entity'
);
// TODO: switch to scandir to get actual templates
// & maybe allow the adition of other custom templates
private static $knownTypes = array(
'choice',
'entity',
'text',
'date'
);
public function __construct(string $formname, $entity = null) public function __construct(string $formname, $entity = null)
{ {
//check if entity is linked and $entity instanceof entity
// do something
// check if datas are in the entity
// loop through the content of the form
// check if type is in the db
// load the template from the type
// else
// load the _default
// put it in the variable with his name
$ap = AdminPanel::getInstance(); $ap = AdminPanel::getInstance();
$cache = $ap->getCache(); $cache = $ap->getCache();
$forms = $cache->get("forms"); $forms = $cache->get("forms");
@ -39,43 +14,48 @@ class Form
if (isset($form->entity) && (!isset($entity) || !($entity instanceof $form->entity))) { if (isset($form->entity) && (!isset($entity) || !($entity instanceof $form->entity))) {
$entity = new $form->entity(); $entity = new $form->entity();
} }
// dump($form->fields);
foreach ($form->fields as $name => $settings) { foreach ($form->fields as $name => $settings) {
// get class
if (strstr($settings->type, "\\") === false) {
$settings->type = "\\AdminPanel\\Form\\" . ucfirst($settings->type) . "Input";
}
/** @var \AdminPanel\Form\Input */
$field = new $settings->type();
$func = "get" . ucfirst($name);
$options = array( $options = array(
'name' => $name, 'name' => $name,
'label' => null, 'type' => $settings->type,
'attr' => array(), 'attr' => array(),
'value' => null, 'label' => isset($settings->label) ? $settings->label : null,
'type' => null, //used in case of formtype not found 'value' => $entity !== null && $entity->$func !== null ? $entity->$func : null
'choices' => null //choiceType
); );
if ($entity !== null) {
$func = "get" . ucfirst($name); $fieldOptions = array_merge(
if ($entity->$func() !== null) { $field->getOptions(),
$options["value"] = $entity->$func(); array_keys($options)
} );
}
/*
$settings
$fieldOptions
$options
$field
*/
foreach ($settings as $opt => $value) { foreach ($settings as $opt => $value) {
// dump($settings); if (in_array($opt, $fieldOptions)) {
if (in_array($opt, Form::$knownOptions)) { $options = array_merge(
if ($opt === "entity") { $options,
// dd($ap->getEm()->getRepository($value)->findAll()); $field->processOption($opt, $value)
$options["entities"] = $ap->getEm()->getRepository($value)->findAll(); );
} else {
$options[$opt] = $value;
}
} else { } else {
$options["attr"][$opt] = $value; $options["attr"][$opt] = $value;
} }
} }
if (in_array($options["type"], Form::$knownTypes)) { $this->$name = $ap->getTwig()->display($field->getTemplate(), $options);
$this->$name = $ap->getTwig()->display("@AdminPanel/Form/" . $options["type"] . ".twig", $options);
} else {
$this->$name = $ap->getTwig()->display("@AdminPanel/Form/_default.twig", $options);
}
} }
} else {
// TODO: IDK
} }
} }
} }