diff --git a/src/AdminPanel/Form/AbstractInput.php b/src/AdminPanel/Form/AbstractInput.php index 7934d92..a459c11 100644 --- a/src/AdminPanel/Form/AbstractInput.php +++ b/src/AdminPanel/Form/AbstractInput.php @@ -2,20 +2,84 @@ namespace AdminPanel\Form; -class AbstractInput implements Input +use AdminPanel\AdminPanel; + +/** + * this abstract class has default functionnality for all working + * before doing `parent::function` please see what it does + */ +abstract class AbstractInput implements Input { + protected $attributes = array(); + protected $options = array(); + + public function __construct() + { + $arr = explode('\\', get_class($this)); + $this->setOption("type", strtolower( + str_replace( + "Input", + "", + $arr[count($arr) - 1] + ) + )); + } + + public function getAttributesList(): array + { + return array( + 'name', + 'type', + 'id', + 'value', + 'placeholder', + 'style' + ); + } + + + public function getOptionsList(): array + { + return array( + 'label', + 'value' + ); + } + + public function getAttributes(): array + { + return $this->attributes; + } + public function getOptions(): array { - return array(); + return $this->options; } - public function processOption(string $optionName, $value): array + public function getOption(string $name) { - return array($optionName => $value); + return $this->options[$name]; } - public function getTemplate(): string + public function setOption(string $name, $value) + { + if (in_array($name, $this->getOptionsList())) { + $this->options[$name] = $value; + } + if (in_array($name, $this->getAttributesList())) { + if (in_array($name, array("id", "name")) && + !array_key_exists("id", $this->attributes) && + !array_key_exists("name", $this->attributes) + ) { + $this->attributes["name"] = $value; + $this->attributes["id"] = $value; + } + $this->attributes[$name] = $value; + } + } + + public function render(): string { $arr = explode('\\', get_class($this)); $tpName = strtolower( @@ -25,6 +89,17 @@ class AbstractInput implements Input $arr[count($arr) - 1] ) ); - return "@AdminPanel/form/" . $tpName . ".twig"; + return $this->getTwig()->render( + "@AdminPanel/form/" . $tpName . ".twig", + array( + 'attributes' => $this->attributes, + 'options' => $this->options + ) + ); + } + + protected function getTwig() + { + return AdminPanel::getInstance()->getTwig(); } } diff --git a/src/AdminPanel/Form/ChoiceInput.php b/src/AdminPanel/Form/ChoiceInput.php index d96865d..db99650 100644 --- a/src/AdminPanel/Form/ChoiceInput.php +++ b/src/AdminPanel/Form/ChoiceInput.php @@ -2,12 +2,25 @@ namespace AdminPanel\Form; +use AdminPanel\AdminPanel; + class ChoiceInput extends AbstractInput { - public function getOptions(): array + public function getOptionsList(): array + { + return array_merge( + parent::getOptionsList(), + array( + 'choices' + ) + ); + } + + public function getAttributesList(): array { return array( - 'choices' + 'name', + 'id' ); } } diff --git a/src/AdminPanel/Form/DateInput.php b/src/AdminPanel/Form/DateInput.php index e468c5f..78dd31f 100644 --- a/src/AdminPanel/Form/DateInput.php +++ b/src/AdminPanel/Form/DateInput.php @@ -2,6 +2,26 @@ namespace AdminPanel\Form; +use DateTime; + class DateInput extends AbstractInput { + + public function getOption(string $name) + { + if ($name === "value") { + return new DateTime($this->options["name"]); + } else { + return parent::getOption($name); + } + } + + public function setOption($name, $value) + { + if ($name === "value") { + $this->attributes["value"] = (new DateTime($value))->format('Y-m-d'); + } else { + parent::setOption($name, $value); + } + } } diff --git a/src/AdminPanel/Form/EntityInput.php b/src/AdminPanel/Form/EntityInput.php index 18c2a7b..178a99f 100644 --- a/src/AdminPanel/Form/EntityInput.php +++ b/src/AdminPanel/Form/EntityInput.php @@ -6,22 +6,40 @@ use AdminPanel\AdminPanel; class EntityInput extends AbstractInput { - public function getOptions(): array + public function __construct() { - return array( - 'entity' + $this->setOption("type", "text"); + } + + public function getOptionsList(): array + { + return array_merge( + parent::getOptionsList(), + array( + 'entity' + ) ); } - public function processOption(string $optionName, $value): array + public function setOption(string $name, $value) { - if ($optionName === 'entity') { - return array( - 'entities' => AdminPanel::getInstance()->getEm()->getRepository($value)->findAll() - ); + if ($name === 'entity') { + $this->options["entities"] = AdminPanel::getInstance()->getEm()->getRepository($value)->findAll(); + } elseif ($name === "name") { + $this->attributes["list"] = $value . "_list"; + } + parent::setOption($name, $value); + } + + public function getOption($name) + { + if ($name === "value") { + $repo = AdminPanel::getInstance()->getEm()->getRepository($this->options["entity"]); + return $repo->findOneBy(array( + 'name' => $this->options["value"] + )); + } else { + return parent::getOption($name); } - return array( - $optionName => $value - ); } } diff --git a/src/AdminPanel/Form/Input.php b/src/AdminPanel/Form/Input.php index a60e56e..6e638d9 100644 --- a/src/AdminPanel/Form/Input.php +++ b/src/AdminPanel/Form/Input.php @@ -4,29 +4,62 @@ namespace AdminPanel\Form; interface Input { + /** - * get definable options in form - * some are processed elsewhere like - * name, value + * Get list of used in this input * - * @return array + * @return array array containing options + */ + public function getAttributesList(): array; + + /** + * get options that will be passed into the final object + * + * @return void + */ + public function getAttributes(): array; + + /** + * Get list of options usable by this input + * + * @return array array containing options + */ + public function getOptionsList(): array; + + /** + * get options that will be passed into the final object + * + * @return void */ public function getOptions(): array; /** - * run things when you got option + * get a single option that will be passed in the final object + * (option can either go in attributes or options) * - * @param string $optionName - * @param mixed $value + * @param string $name option's name * - * @return array + * @return mixed option's value */ - public function processOption(string $optionName, $value): array; + public function getOption(string $name); /** - * Get template file + * Process the options given by php or json + * + * (you can do whatever you want with it) + * + * @param string $name name of the option + * @param mixed $value value of element + * + * @return void + */ + public function setOption(string $name, $value); + + /** + * return the html to render the current input + * * * @return string */ - public function getTemplate(): string; + public function render(): string; } diff --git a/src/AdminPanel/Form/TextInput.php b/src/AdminPanel/Form/TextInput.php index 350cee8..abf5b07 100644 --- a/src/AdminPanel/Form/TextInput.php +++ b/src/AdminPanel/Form/TextInput.php @@ -2,30 +2,6 @@ namespace AdminPanel\Form; -class TextInput implements Input +class TextInput extends AbstractInput { - - public function getOptions(): array - { - return array( - ); - } - - public function processOption(string $optionName, $value): array - { - return array($optionName => $value); - } - - public function getTemplate(): string - { - $arr = explode('\\', self::class); - $tpName = strtolower( - str_replace( - "Input", - "", - $arr[count($arr) - 1] - ) - ); - return "@AdminPanel/form/" . $tpName . ".twig"; - } }