diff --git a/src/AdminPanel/Form.php b/src/AdminPanel/Form.php index 03cbf11..4261067 100644 --- a/src/AdminPanel/Form.php +++ b/src/AdminPanel/Form.php @@ -4,33 +4,8 @@ namespace AdminPanel; 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) { - //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(); $cache = $ap->getCache(); $forms = $cache->get("forms"); @@ -39,43 +14,48 @@ class Form if (isset($form->entity) && (!isset($entity) || !($entity instanceof $form->entity))) { $entity = new $form->entity(); } - // dump($form->fields); 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( 'name' => $name, - 'label' => null, + 'type' => $settings->type, 'attr' => array(), - 'value' => null, - 'type' => null, //used in case of formtype not found - 'choices' => null //choiceType + 'label' => isset($settings->label) ? $settings->label : null, + 'value' => $entity !== null && $entity->$func !== null ? $entity->$func : null ); - if ($entity !== null) { - $func = "get" . ucfirst($name); - if ($entity->$func() !== null) { - $options["value"] = $entity->$func(); - } - } + + $fieldOptions = array_merge( + $field->getOptions(), + array_keys($options) + ); + + /* + $settings + $fieldOptions + $options + $field + */ + foreach ($settings as $opt => $value) { - // dump($settings); - if (in_array($opt, Form::$knownOptions)) { - if ($opt === "entity") { - // dd($ap->getEm()->getRepository($value)->findAll()); - $options["entities"] = $ap->getEm()->getRepository($value)->findAll(); - } else { - $options[$opt] = $value; - } + if (in_array($opt, $fieldOptions)) { + $options = array_merge( + $options, + $field->processOption($opt, $value) + ); } else { $options["attr"][$opt] = $value; } } - if (in_array($options["type"], Form::$knownTypes)) { - $this->$name = $ap->getTwig()->display("@AdminPanel/Form/" . $options["type"] . ".twig", $options); - } else { - $this->$name = $ap->getTwig()->display("@AdminPanel/Form/_default.twig", $options); - } + $this->$name = $ap->getTwig()->display($field->getTemplate(), $options); } - } else { - // TODO: IDK } } }