title = $title; $this->icon = $icon; $this->action = $action; $this->method = $method; } /** * Set the title of the form. * @param string $title */ public function setTitle(string $title) { $this->title = $title; } /** * Set the icon for the form. * @param string $icon FontAwesome icon (example: "fas fa-toilet-paper") */ public function setIcon(string $icon) { $this->icon = $icon; } /** * Set the URL the form will submit to. * @param string $action */ public function setAction(string $action) { $this->action = $action; } /** * Set the form submission method (GET, POST, etc) * @param string $method */ public function setMethod(string $method = "POST") { $this->method = $method; } /** * Set the form ID. * @param string $id */ public function setID(string $id = "editform") { $this->id = $id; } /** * Add an input to the form. * * @param string $name Element name * @param string $value Element value * @param string $type Input type (text, number, date, select, tel...) * @param bool $required If the element is required for form submission. * @param string $id Element ID * @param array $options Array of [value => text] pairs for a select element * @param string $label Text label to display near the input * @param string $icon FontAwesome icon (example: "fas fa-toilet-paper") * @param int $width Bootstrap column width for the input, out of 12. * @param int $minlength Minimum number of characters for the input. * @param int $maxlength Maximum number of characters for the input. * @param string $pattern Regex pattern for custom client-side validation. * @param string $error Message to show if the input doesn't validate. */ public function addInput(string $name, string $value = "", string $type = "text", bool $required = true, string $id = null, array $options = null, string $label = "", string $icon = "", int $width = 4, int $minlength = 1, int $maxlength = 100, string $pattern = "", string $error = "") { $item = [ "name" => $name, "value" => $value, "type" => $type, "required" => $required, "label" => $label, "icon" => $icon, "width" => $width, "minlength" => $minlength, "maxlength" => $maxlength ]; if (!empty($id)) { $item["id"] = $id; } if (!empty($options) && $type == "select") { $item["options"] = $options; } if (!empty($pattern)) { $item["pattern"] = $pattern; } if (!empty($error)) { $item["error"] = $error; } $this->items[] = $item; } /** * Add a text input. * * @param string $name Element name * @param string $value Element value * @param bool $required If the element is required for form submission. * @param string $id Element ID * @param string $label Text label to display near the input * @param string $icon FontAwesome icon (example: "fas fa-toilet-paper") * @param int $width Bootstrap column width for the input, out of 12. * @param int $minlength Minimum number of characters for the input. * @param int $maxlength Maximum number of characters for the input. * @param string $pattern Regex pattern for custom client-side validation. * @param string $error Message to show if the input doesn't validate. */ public function addTextInput(string $name, string $value = "", bool $required = true, string $id = "", string $label = "", string $icon = "", int $width = 4, int $minlength = 1, int $maxlength = 100, string $pattern = "", string $error = "") { $this->addInput($name, $value, "text", $required, $id, null, $label, $icon, $width, $minlength, $maxlength, $pattern, $error); } /** * Add a select dropdown. * * @param string $name Element name * @param string $value Element value * @param bool $required If the element is required for form submission. * @param string $id Element ID * @param array $options Array of [value => text] pairs for a select element * @param string $label Text label to display near the input * @param string $icon FontAwesome icon (example: "fas fa-toilet-paper") * @param int $width Bootstrap column width for the input, out of 12. */ public function addSelect(string $name, string $value = "", bool $required = true, string $id = null, array $options = null, string $label = "", string $icon = "", int $width = 4) { $this->addInput($name, $value, "select", $required, $id, $options, $label, $icon, $width); } /** * Add a button to the form. * * @param string $text Text string to show on the button. * @param string $icon FontAwesome icon to show next to the text. * @param string $href If not null, the button will actually be a hyperlink. * @param string $type Usually "button" or "submit". Ignored if $href is set. * @param string $id The element ID. * @param string $name The element name for the button. * @param string $value The form value for the button. Ignored if $name is null. * @param string $class The CSS classes for the button, if a standard success-colored one isn't right. */ public function addButton(string $text, string $icon = "", string $href = null, string $type = "button", string $id = null, string $name = null, string $value = "", string $class = "btn btn-success") { $button = [ "text" => $text, "icon" => $icon, "class" => $class, "type" => $type, "id" => $id, "href" => $href, "name" => $name, "value" => $value ]; $this->buttons[] = $button; } /** * Add a hidden input. * @param string $name * @param string $value */ public function addHiddenInput(string $name, string $value) { $this->hiddenitems[$name] = $value; } /** * Generate the form HTML. * @param bool $echo If false, returns HTML string instead of outputting it. */ public function generate(bool $echo = true) { $html = <<

$this->title

HTMLTOP; foreach ($this->items as $item) { $required = $item["required"] ? "required" : ""; $id = empty($item["id"]) ? "" : "id=\"$item[id]\""; $pattern = empty($item["pattern"]) ? "" : "pattern=\"$item[pattern]\""; if (empty($item['type'])) { $item['type'] = "text"; } $itemhtml = ""; $itemlabel = ""; if ($item['type'] == "textarea") { $itemlabel = ""; } else if ($item['type'] != "checkbox") { $itemlabel = ""; } $strippedlabel = strip_tags($item['label']); $itemhtml .= <<
$itemlabel ITEMTOP; $inputgrouptop = <<
INPUTG; switch ($item['type']) { case "select": $itemhtml .= $inputgrouptop; $itemhtml .= <<"; break; case "checkbox": $itemhtml .= $inputgrouptop; $itemhtml .= <<
CHECKBOX; break; case "textarea": $val = htmlentities($item['value']); $itemhtml .= << TEXTAREA; break; default: $itemhtml .= $inputgrouptop; $itemhtml .= << INPUT; break; } if (!empty($item["error"])) { $itemhtml .= << $item[error]
ERROR; } if ($item["type"] != "textarea") { $itemhtml .= "\n
"; } $itemhtml .= <<
\n ITEMBOTTOM; $html .= $itemhtml; } $html .= << HTMLBOTTOM; if (!empty($this->buttons)) { $html .= "\n "; } $html .= "\n "; foreach ($this->hiddenitems as $name => $value) { $value = htmlentities($value); $html .= "\n "; } $html .= "\n\n"; if ($echo) { echo $html; } return $html; } }