mirror of
https://github.com/Aviortheking/DeltaCMS.git
synced 2025-04-22 10:52:11 +00:00
Added support for forms
still work in progress
This commit is contained in:
parent
783f33381a
commit
bf80fe298a
81
src/AdminPanel/Form.php
Normal file
81
src/AdminPanel/Form.php
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
14
src/AdminPanel/Twig/Form/_default.twig
Normal file
14
src/AdminPanel/Twig/Form/_default.twig
Normal 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 %}
|
17
src/AdminPanel/Twig/Form/choice.twig
Normal file
17
src/AdminPanel/Twig/Form/choice.twig
Normal 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 }}" {% for index, item in attr %}{{index}}="{{item}}" {% 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 %}
|
7
src/AdminPanel/Twig/Form/date.twig
Normal file
7
src/AdminPanel/Twig/Form/date.twig
Normal 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 %}
|
||||
/>
|
13
src/AdminPanel/Twig/Form/entity.twig
Normal file
13
src/AdminPanel/Twig/Form/entity.twig
Normal 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" {% for index, item in attr %}{{index}}="{{item}}" {% endfor %}/>
|
||||
<datalist id="{{name}}_list">
|
||||
{% for entity in entities %}
|
||||
<option>{{entity.name}}</option>
|
||||
{% endfor %}
|
||||
</datalist>
|
||||
{% endblock %}
|
6
src/AdminPanel/Twig/Form/text.twig
Normal file
6
src/AdminPanel/Twig/Form/text.twig
Normal 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}}" {% for index, item in attr %}{{index}}="{{item}}" {% endfor %}/>
|
||||
{% endblock %}
|
Loading…
x
Reference in New Issue
Block a user