aboutsummaryrefslogtreecommitdiffhomepage
path: root/main/app/sprinkles/FormGenerator/src
diff options
context:
space:
mode:
Diffstat (limited to 'main/app/sprinkles/FormGenerator/src')
-rwxr-xr-xmain/app/sprinkles/FormGenerator/src/Controller/FormGeneratorController.php26
-rwxr-xr-xmain/app/sprinkles/FormGenerator/src/Element/Alert.php33
-rwxr-xr-xmain/app/sprinkles/FormGenerator/src/Element/BaseInput.php111
-rwxr-xr-xmain/app/sprinkles/FormGenerator/src/Element/Checkbox.php38
-rwxr-xr-xmain/app/sprinkles/FormGenerator/src/Element/Hidden.php32
-rwxr-xr-xmain/app/sprinkles/FormGenerator/src/Element/InputInterface.php19
-rwxr-xr-xmain/app/sprinkles/FormGenerator/src/Element/Select.php41
-rwxr-xr-xmain/app/sprinkles/FormGenerator/src/Element/Text.php37
-rwxr-xr-xmain/app/sprinkles/FormGenerator/src/Element/Textarea.php38
-rwxr-xr-xmain/app/sprinkles/FormGenerator/src/Form.php188
10 files changed, 563 insertions, 0 deletions
diff --git a/main/app/sprinkles/FormGenerator/src/Controller/FormGeneratorController.php b/main/app/sprinkles/FormGenerator/src/Controller/FormGeneratorController.php
new file mode 100755
index 0000000..e731011
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/src/Controller/FormGeneratorController.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * UF Form Generator
+ *
+ * @link https://github.com/lcharette/UF_FormGenerator
+ * @copyright Copyright (c) 2017 Louis Charette
+ * @license https://github.com/lcharette/UF_FormGenerator/blob/master/LICENSE (MIT License)
+ */
+namespace UserFrosting\Sprinkle\FormGenerator\Controller;
+
+use UserFrosting\Sprinkle\Core\Controller\SimpleController;
+
+/**
+ * FormGeneratorController Class
+ *
+ * Controller class for /forms/confirm/* URLs. Handles rendering the confirm dialog
+ */
+class FormGeneratorController extends SimpleController {
+
+ /**
+ * Display the confirmation dialog
+ */
+ public function confirm($request, $response, $args) {
+ $this->ci->view->render($response, 'FormGenerator/confirm.html.twig', $request->getQueryParams());
+ }
+}
diff --git a/main/app/sprinkles/FormGenerator/src/Element/Alert.php b/main/app/sprinkles/FormGenerator/src/Element/Alert.php
new file mode 100755
index 0000000..f848b5c
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/src/Element/Alert.php
@@ -0,0 +1,33 @@
+<?php
+/**
+ * UF Form Generator
+ *
+ * @link https://github.com/lcharette/UF_FormGenerator
+ * @copyright Copyright (c) 2017 Louis Charette
+ * @license https://github.com/lcharette/UF_FormGenerator/blob/master/LICENSE (MIT License)
+ */
+namespace UserFrosting\Sprinkle\FormGenerator\Element;
+
+use UserFrosting\Sprinkle\FormGenerator\Element\BaseInput;
+
+/**
+ * Alert input type class.
+ * Manage the default attributes required to display an alert
+ *
+ * @extends BaseInput
+ */
+class Alert extends BaseInput {
+
+ /**
+ * {@inheritDoc}
+ */
+ protected function applyTransformations()
+ {
+ $this->element = array_merge([
+ "class" => "alert-danger",
+ "icon" => "fa-ban",
+ "value" => $this->value,
+ "name" => $this->name
+ ], $this->element);
+ }
+}
diff --git a/main/app/sprinkles/FormGenerator/src/Element/BaseInput.php b/main/app/sprinkles/FormGenerator/src/Element/BaseInput.php
new file mode 100755
index 0000000..d892001
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/src/Element/BaseInput.php
@@ -0,0 +1,111 @@
+<?php
+/**
+ * UF Form Generator
+ *
+ * @link https://github.com/lcharette/UF_FormGenerator
+ * @copyright Copyright (c) 2017 Louis Charette
+ * @license https://github.com/lcharette/UF_FormGenerator/blob/master/LICENSE (MIT License)
+ */
+namespace UserFrosting\Sprinkle\FormGenerator\Element;
+
+use UserFrosting\Sprinkle\FormGenerator\Element\InputInterface;
+use UserFrosting\Sprinkle\Core\Facades\Translator;
+use UserFrosting\Sprinkle\Core\Facades\Debug;
+
+/**
+ * BaseInput class.
+ *
+ * Parse the schema data for a form input element to add the default
+ * attributes values and transform other attributes.
+ * @abstract
+ * @implements InputInterface
+ */
+abstract class BaseInput implements InputInterface {
+
+ /**
+ * @var String The name of the input.
+ */
+ var $name;
+
+ /**
+ * @var object The input schema data.
+ */
+ var $element;
+
+ /**
+ * @var String The input value.
+ */
+ var $value;
+
+ /**
+ * Constructor.
+ *
+ * @access public
+ * @param String $name
+ * @param object $element
+ * @param mixed $value (default: null)
+ * @return void
+ */
+ public function __construct($name, $element, $value = null)
+ {
+ $this->name = $name;
+ $this->element = $element;
+ $this->value = $value;
+ }
+
+ /**
+ * parse function.
+ *
+ * Return the parsed input attributes
+ * @access public
+ * @return void
+ */
+ public function parse()
+ {
+ $this->applyTransformations();
+ return $this->element;
+ }
+
+ /**
+ * translateArgValue function.
+ *
+ * Translate the value of passed argument using the Translator Facade
+ * @access public
+ * @param String $argument
+ * @return void
+ */
+ public function translateArgValue($argument) {
+ if (isset($this->element[$argument])) {
+ $this->element[$argument] = Translator::translate($this->element[$argument]);
+ }
+ }
+
+ /**
+ * getValue function.
+ *
+ * Return the value of the current input element. If not value is set in
+ * `$this->value`, return the default value (from the schema data), if any.
+ * @access public
+ * @return string The input current value
+ */
+ public function getValue() {
+ if (isset($this->value) && $this->value !== null) {
+ return $this->value;
+ } else if (isset($this->element['default'])) {
+ return $this->element['default'];
+ } else {
+ return "";
+ }
+ }
+
+ /**
+ * applyTransformations function.
+ *
+ * Add defaut attributes to the current input element. Also transform
+ * attributes values passed from the schema
+ * @access protected
+ * @abstract
+ * @return void
+ */
+ abstract protected function applyTransformations();
+}
diff --git a/main/app/sprinkles/FormGenerator/src/Element/Checkbox.php b/main/app/sprinkles/FormGenerator/src/Element/Checkbox.php
new file mode 100755
index 0000000..59e6eaf
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/src/Element/Checkbox.php
@@ -0,0 +1,38 @@
+<?php
+/**
+ * UF Form Generator
+ *
+ * @link https://github.com/lcharette/UF_FormGenerator
+ * @copyright Copyright (c) 2017 Louis Charette
+ * @license https://github.com/lcharette/UF_FormGenerator/blob/master/LICENSE (MIT License)
+ */
+namespace UserFrosting\Sprinkle\FormGenerator\Element;
+
+use UserFrosting\Sprinkle\FormGenerator\Element\BaseInput;
+
+/**
+ * Checkbox input type class.
+ * Manage the default attributes required to display a checkbox input
+ *
+ * @extends BaseInput
+ */
+class Checkbox extends BaseInput {
+
+ /**
+ * {@inheritDoc}
+ */
+ protected function applyTransformations()
+ {
+ $this->element = array_merge([
+ "class" => "js-icheck",
+ "name" => $this->name,
+ "id" => "field_" . $this->name,
+ "binary" => true
+ ], $this->element);
+
+ // We add the check status instead of the value
+ if ($this->element["binary"] !== false && $this->getValue() == 1) {
+ $this->element["checked"] = "checked";
+ }
+ }
+}
diff --git a/main/app/sprinkles/FormGenerator/src/Element/Hidden.php b/main/app/sprinkles/FormGenerator/src/Element/Hidden.php
new file mode 100755
index 0000000..08c22f7
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/src/Element/Hidden.php
@@ -0,0 +1,32 @@
+<?php
+/**
+ * UF Form Generator
+ *
+ * @link https://github.com/lcharette/UF_FormGenerator
+ * @copyright Copyright (c) 2017 Louis Charette
+ * @license https://github.com/lcharette/UF_FormGenerator/blob/master/LICENSE (MIT License)
+ */
+namespace UserFrosting\Sprinkle\FormGenerator\Element;
+
+use UserFrosting\Sprinkle\FormGenerator\Element\BaseInput;
+
+/**
+ * Hidden input type class.
+ * Manage the default attributes required to display an hidden input type
+ *
+ * @extends BaseInput
+ */
+class Hidden extends BaseInput {
+
+ /**
+ * {@inheritDoc}
+ */
+ protected function applyTransformations()
+ {
+ $this->element = array_merge([
+ "value" => $this->getValue(),
+ "name" => $this->name,
+ "id" => "field_" . $this->name
+ ], $this->element);
+ }
+}
diff --git a/main/app/sprinkles/FormGenerator/src/Element/InputInterface.php b/main/app/sprinkles/FormGenerator/src/Element/InputInterface.php
new file mode 100755
index 0000000..7405109
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/src/Element/InputInterface.php
@@ -0,0 +1,19 @@
+<?php
+/**
+ * UF Form Generator
+ *
+ * @link https://github.com/lcharette/UF_FormGenerator
+ * @copyright Copyright (c) 2017 Louis Charette
+ * @license https://github.com/lcharette/UF_FormGenerator/blob/master/LICENSE (MIT License)
+ */
+namespace UserFrosting\Sprinkle\FormGenerator\Element;
+
+/**
+ * InputInterface
+ *
+ * Interface for Form elements classes
+ */
+interface InputInterface {
+ public function __construct($name, $element, $value = null);
+ public function parse();
+}
diff --git a/main/app/sprinkles/FormGenerator/src/Element/Select.php b/main/app/sprinkles/FormGenerator/src/Element/Select.php
new file mode 100755
index 0000000..bb23772
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/src/Element/Select.php
@@ -0,0 +1,41 @@
+<?php
+/**
+ * UF Form Generator
+ *
+ * @link https://github.com/lcharette/UF_FormGenerator
+ * @copyright Copyright (c) 2017 Louis Charette
+ * @license https://github.com/lcharette/UF_FormGenerator/blob/master/LICENSE (MIT License)
+ */
+namespace UserFrosting\Sprinkle\FormGenerator\Element;
+
+use UserFrosting\Sprinkle\FormGenerator\Element\BaseInput;
+
+/**
+ * Select input type class.
+ * Manage the default attributes required to display a select input type
+ *
+ * @extends BaseInput
+ */
+class Select extends BaseInput {
+
+ /**
+ * {@inheritDoc}
+ */
+ protected function applyTransformations()
+ {
+ $this->element = array_merge([
+ "class" => "form-control js-select2",
+ "value" => $this->getValue(),
+ "name" => $this->name,
+ "id" => "field_" . $this->name
+ ], $this->element);
+
+ // Placeholder is required to be in `data-*` for select 2
+ // Plus we translate the placeholder
+ if (isset($this->element["placeholder"])) {
+ $this->element["data-placeholder"] = $this->element["placeholder"];
+ unset($this->element["placeholder"]);
+ $this->translateArgValue('data-placeholder');
+ }
+ }
+}
diff --git a/main/app/sprinkles/FormGenerator/src/Element/Text.php b/main/app/sprinkles/FormGenerator/src/Element/Text.php
new file mode 100755
index 0000000..b936fe2
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/src/Element/Text.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * UF Form Generator
+ *
+ * @link https://github.com/lcharette/UF_FormGenerator
+ * @copyright Copyright (c) 2017 Louis Charette
+ * @license https://github.com/lcharette/UF_FormGenerator/blob/master/LICENSE (MIT License)
+ */
+namespace UserFrosting\Sprinkle\FormGenerator\Element;
+
+use UserFrosting\Sprinkle\FormGenerator\Element\BaseInput;
+
+/**
+ * Text input type class.
+ * Manage the default attributes required to display a text and other html5 input
+ *
+ * @extends BaseInput
+ */
+class Text extends BaseInput {
+
+ /**
+ * {@inheritDoc}
+ */
+ protected function applyTransformations()
+ {
+ $this->element = array_merge([
+ "autocomplete" => "off",
+ "class" => "form-control",
+ "value" => $this->getValue(),
+ "name" => $this->name,
+ "id" => "field_" . $this->name
+ ], $this->element);
+
+ // Translate placeholder
+ $this->translateArgValue('placeholder');
+ }
+}
diff --git a/main/app/sprinkles/FormGenerator/src/Element/Textarea.php b/main/app/sprinkles/FormGenerator/src/Element/Textarea.php
new file mode 100755
index 0000000..bec3a6c
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/src/Element/Textarea.php
@@ -0,0 +1,38 @@
+<?php
+/**
+ * UF Form Generator
+ *
+ * @link https://github.com/lcharette/UF_FormGenerator
+ * @copyright Copyright (c) 2017 Louis Charette
+ * @license https://github.com/lcharette/UF_FormGenerator/blob/master/LICENSE (MIT License)
+ */
+namespace UserFrosting\Sprinkle\FormGenerator\Element;
+
+use UserFrosting\Sprinkle\FormGenerator\Element\BaseInput;
+
+/**
+ * Textarea input type class.
+ * Manage the default attributes required to display a textarea input
+ *
+ * @extends BaseInput
+ */
+class Textarea extends BaseInput {
+
+ /**
+ * {@inheritDoc}
+ */
+ protected function applyTransformations()
+ {
+ $this->element = array_merge([
+ "autocomplete" => "off",
+ "class" => "form-control",
+ "value" => $this->getValue(),
+ "name" => $this->name,
+ "rows" => 3,
+ "id" => "field_" . $this->name
+ ], $this->element);
+
+ // Translate placeholder
+ $this->translateArgValue('placeholder');
+ }
+}
diff --git a/main/app/sprinkles/FormGenerator/src/Form.php b/main/app/sprinkles/FormGenerator/src/Form.php
new file mode 100755
index 0000000..e845e3e
--- /dev/null
+++ b/main/app/sprinkles/FormGenerator/src/Form.php
@@ -0,0 +1,188 @@
+<?php
+/**
+ * UF Form Generator
+ *
+ * @link https://github.com/lcharette/UF_FormGenerator
+ * @copyright Copyright (c) 2017 Louis Charette
+ * @license https://github.com/lcharette/UF_FormGenerator/blob/master/LICENSE (MIT License)
+ */
+namespace UserFrosting\Sprinkle\FormGenerator;
+
+use Illuminate\Contracts\Config\Repository;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Collection;
+use Illuminate\Support\Str;
+use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface;
+
+/**
+ * Form Class
+ *
+ * The FormGenerator class, which is used to return the `form` part from a Fortress
+ * schema for html form generator in Twig.
+ */
+class Form {
+
+ /**
+ * @var RequestSchemaInterface The form fields definition
+ */
+ protected $schema;
+
+ /**
+ * @var array|object The form values
+ */
+ protected $data = [];
+
+ /**
+ * @var string Use this to wrap form fields in top-level array
+ */
+ protected $formNamespace = "";
+
+ /**
+ * Constructor
+ *
+ * @param RequestSchemaInterface $schema
+ * @param array|object $data (default: [])
+ * @return void
+ */
+ public function __construct(RequestSchemaInterface $schema, $data = [])
+ {
+ $this->setSchema($schema);
+ $this->setData($data);
+ }
+
+ /**
+ * Set the form current values
+ *
+ * @param array|object $data The form values
+ */
+ public function setData($data)
+ {
+ if ($data instanceof Collection || $data instanceof Model) {
+ $this->data = $data->toArray();
+ } else if (is_array($data) || $data instanceof Repository) {
+ $this->data = $data;
+ } else {
+ throw new \InvalidArgumentException("Data must be an array, a Collection, a Model or a Repository");
+ }
+ }
+
+ /**
+ * Set the schema for this validator.
+ *
+ * @param RequestSchemaInterface $schema A RequestSchemaInterface object, containing the form definition.
+ */
+ public function setSchema(RequestSchemaInterface $schema)
+ {
+ $this->schema = $schema;
+ }
+
+ /**
+ * Use to define the value of a form input when `setData` is already set
+ *
+ * @param mixed $inputName
+ * @param mixed $value
+ * @return void
+ */
+ public function setValue($inputName, $value)
+ {
+ $this->data[$inputName] = $value;
+ }
+
+ /**
+ * Function used to overwrite the input argument from a schema file
+ * Can also be used to overwrite an argument hardcoded in the Twig file.
+ * Use `setCustomFormData` to set any other tag.
+ *
+ * @param string $inputName The input name where the argument will be added
+ * @param string $property The argument name. Example "data-color"
+ * @param string $data The value of the argument
+ * @return void
+ */
+ public function setInputArgument($inputName, $property, $data)
+ {
+ if ($this->schema->has($inputName)) {
+ // Get the element and force set the property
+ $element = $this->schema->get($inputName);
+ $element['form'][$property] = $data;
+
+ // Push back the modifyed element in the schema
+ $this->schema->set($inputName, $element);
+ }
+ }
+
+ /**
+ * Function used to set options of a select element. Shortcut for using
+ * `setInputArgument` and `setValue`.
+ *
+ * @param string $inputName The select name to add options to
+ * @param array $data An array of `value => label` options
+ * @param string $selected The selected key
+ * @return void
+ */
+ public function setOptions($inputName, $data = [], $selected = null)
+ {
+ // Set opdations
+ $this->setInputArgument($inputName, 'options', $data);
+
+ // Set the value
+ if (!is_null($selected)) {
+ $this->setValue($inputName, $selected);
+ }
+ }
+
+ /**
+ * Function to set the form namespace.
+ * Use the form namespace to wrap the fields name in a top level array.
+ * Useful when using multiple schemas at once or if the names are using dot syntaxt.
+ * See : http://stackoverflow.com/a/20365198/445757
+ *
+ * @param string $namespace
+ * @return void
+ */
+ public function setFormNamespace($namespace)
+ {
+ $this->formNamespace = $namespace;
+ }
+
+ /**
+ * Generate an array contining all nececerry value to generate a form
+ * with Twig.
+ *
+ * @return array The form fields data
+ */
+ public function generate()
+ {
+ $form = collect([]);
+
+ // Loop all the the fields in the schema
+ foreach ($this->schema->all() as $name => $input) {
+
+ // Skip the one that don't have a `form` definition
+ if (isset($input['form'])) {
+
+ // Get the value from the data
+ $value = isset($this->data[$name]) ? $this->data[$name] : null;
+
+ // Add the namespace to the name if it's defined
+ $name = ($this->formNamespace != "") ? $this->formNamespace."[".$name."]" : $name;
+
+ // Get the element class and make sure it exist
+ $type = (isset($input['form']['type'])) ? $input['form']['type'] : "text";
+ $type = "UserFrosting\\Sprinkle\\FormGenerator\\Element\\" . Str::studly($type);
+
+ // If class doesn't esist, default to Text element
+ if (!class_exists($type)) {
+ $type = "UserFrosting\\Sprinkle\\FormGenerator\\Element\\Text";
+ }
+
+ // Create a new instance
+ $element = new $type($name, $input['form'], $value);
+
+ // Push data to `$form`
+ $form->put($name, $element->parse());
+ }
+ }
+
+ return $form->toArray();
+ }
+}