Added support for forms

still work in progress
This commit is contained in:
Florian Bouillon 2019-04-16 16:33:45 +02:00
parent 783f33381a
commit bf80fe298a
6 changed files with 138 additions and 0 deletions

81
src/AdminPanel/Form.php Normal file
View File

@ -0,0 +1,81 @@
<?php
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");
if (isset($forms[$formname])) {
$form = $forms[$formname];
if (isset($form->entity) && (!isset($entity) || !($entity instanceof $form->entity))) {
$entity = new $form->entity();
}
// dump($form->fields);
foreach ($form->fields as $name => $settings) {
$options = array(
'name' => $name,
'label' => null,
'attr' => array(),
'value' => null,
'type' => null, //used in case of formtype not found
'choices' => null //choiceType
);
if ($entity !== null) {
$func = "get" . ucfirst($name);
if ($entity->$func() !== null) {
$options["value"] = $entity->$func();
}
}
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;
}
} 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);
}
}
} else {
// TODO: IDK
}
}
}

View File

@ -0,0 +1,14 @@
{# Default label if type is not found #}
{% spaceless %}
{% block form %}
{% if label %}
<label for="{{ name }}">{{label}}</label>
{% endif %}
<input
type="{{ type }}"
name="{{ name }}"
id="{{ name }}"
{% for index, item in attr %}{{index}}="{{item}}" {% endfor %}
/>
{% endblock %}
{% endspaceless %}

View File

@ -0,0 +1,17 @@
{% extends "@AdminPanel/Form/_default.twig" %}
{% block form %}
{% if label %}
<label for="{{ name }}">{{ label }}</label>
{% endif %}
<select name="{{ name }}" id="{{ name }}"&nbsp;{% for index, item in attr %}{{index}}="{{item}}"&nbsp;{% endfor %}>
{% for choice in choices %}
{% if loop.index0 == value %}
<option value="{{ loop.index0 }}" selected>{{ choice }}</option>
{% else %}
<option value="{{ loop.index0 }}">{{ choice }}</option>
{% endif %}
{% endfor %}
</select>
{% endblock %}

View File

@ -0,0 +1,7 @@
<input
type="date"
name="{{name}}"
id="{{name}}"
{% if value is not null %}value="{{value|date("Y-m-d")}}"{% endif %}
{% for index, item in attr %}{{index}}="{{item}}" {% endfor %}
/>

View File

@ -0,0 +1,13 @@
{% extends "@AdminPanel/Form/_default.twig" %}
{% block form %}
{% if label %}
<label for="{{name}}">{{label}}</label>
{% endif %}
<input type="text" name="{{name}}" id="{{name}}" list="{{name}}_list"&nbsp;{% for index, item in attr %}{{index}}="{{item}}"&nbsp;{% endfor %}/>
<datalist id="{{name}}_list">
{% for entity in entities %}
<option>{{entity.name}}</option>
{% endfor %}
</datalist>
{% endblock %}

View File

@ -0,0 +1,6 @@
{% extends "@AdminPanel/Form/_default.twig" %}
{% block form %}
<label for="{{name}}" />{{label}}</label>
<input {% if value is not null %}value="{{value}}"{% endif %} type="text" name="{{name}}" id="{{name}}"&nbsp;{% for index, item in attr %}{{index}}="{{item}}"&nbsp;{% endfor %}/>
{% endblock %}