Skip to content

Commit

Permalink
Added several features
Browse files Browse the repository at this point in the history
  • Loading branch information
dekyfin committed Jan 22, 2018
1 parent 88bf897 commit 4c4fbe0
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor/
tests
165 changes: 134 additions & 31 deletions src/DF/ArrayForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* ArrayForm Class
*
* Responsible for building forms
* Usage
*
* @param array $elements renderable array containing form elements
*
Expand All @@ -13,11 +14,15 @@

class ArrayForm {

public $elements;
public $form_number = 1;
protected $elements;
protected $formData = [];
protected $formNo = 0;
protected $id;

public function __construct( $elements ){
$this->elements = $elements;
public function __construct( $formData, $elements ){
$this->formData = $formData;
$this->elements = $elements;
$this->id = rand (9,1000);
}

/**
Expand All @@ -29,7 +34,7 @@ public function __construct( $elements ){
*
*/
public function dumpData() {
var_dump($this->elements);
var_dump( $this->elements, $this->formData );
}

/**
Expand All @@ -39,38 +44,136 @@ public function dumpData() {
* @return string $output contains the form as HTML
*
*/
function build() {
$output = '';
public function build() {

// For multiple forms, create a counter.
$this->form_number++;
$this->formNo++;

$input = $this->renderInputs();
return $this->printOutput( $input );
}

protected function renderInputs(){

// Loop through each form element and render it.
foreach ($this->elements as $name => $elements) {
$label = '<label>' . $elements['title'] . '</label>';
switch ($elements['type']) {
case 'textarea':
$input = '<textarea name="' . $name . '" ></textarea>';
break;
case 'submit':
$input = '<input type="submit" name="' . $name . '" value="' . $elements['title'] . '">';
$label = '';
break;
default:
$input = '<input type="' . $elements['type'] . '" name="' . $name . '" />';
break;
}
$output .= $label . '<p>' . $input . '</p>';
}
foreach ($this->elements as $element ) {

//Values to be reset for each input
$opts = $input = $label = $attributes = "";


//Extract key data from attributes
$id = htmlspecialchars( $element["id"] );
$name = htmlspecialchars( $element["name"] );
$options = $element["options"] ;
$default = htmlspecialchars( $element["default"] );
$type = htmlspecialchars( $element["type"] );



unset(
$element["id"],
$element["name"],
$element["options"],
$element["type"],
$element["default"]
);

//Input ID
$inputID = "DFF$this->id-$this->formNo-$id";

//Create Attributes
foreach( $element as $attr=> $val ){

$val = htmlspecialchars( $val );

// Wrap a form around the inputs.
$output = '
<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
<input type="hidden" name="action" value="submit_' . $this->form_number . '" />
' . $output . '
</form>';
if( gettype( $val ) === true ){
$attributes .= "$attr='$attr' ";
}
if( gettype( $val ) === false ){

}
else{
$attributes .= "$attr='$val' ";
}
}

//Create options
switch ( $type ) {
case 'textarea':
$input = "<textarea id='$inputID' name='$id' value='$default' $attributes></textarea>";
break;
case "select":
foreach( $options as $key => $val ){
if( is_numeric($key) ){
$value = "";
}
else{
$key = htmlspecialchars( $key );
$value = "value='$key'";
}
$opts .= "\n <option $value>$val</option>";
}
$input = "<select id='$inputID' name='$id' $attributes value='$default'>$opts\n </select>";

break;
case "radio":
case "checkbox":
foreach( $options as $key=>$val ){
if( is_numeric( $key ) ){
$val = htmlspecialchars( $val );
$value = "value='$val'";
}
else{
$key = htmlspecialchars( $key );
$value = "value='$key'";
}
$input .= "\n<label><input type='$type' name='$id' $attributes $value/>$val</label>";
}

break;
case 'submit':
$input = '<input type="submit" name="' . $id . '" value="' . $name . '">';
$label = '';
break;
default:
$input = "<input id='$inputID' type='$type' name='$id' $attributes />";
break;
}

switch ( $this->formData["display"] ){

case "table":
$output .=<<<INPUT
<tr>
<td class="DFForm-input-title"><label for="$inputID">$name</label></td>
<td class="DFForm-input-content">$input</td>
</tr>
INPUT;
break;

default:
$output .=<<<INPUT
<div class="DFForm-input">
<div><label for="$inputID">$name</label></div>
<div>$input</div>
</div>
INPUT;

}

}

// Return the form.
return $output;
}

protected function printOutput( $output ){
//HTML wrapper for all inputs
$wrapper = ( $this->formData["display"] == "table" ) ? ["<table class='DFForm-table'>","</table>"] : ["",""];

return "
<form id='DFF$this->id-$this->formNo' class='DFForm'>
$wrapper[0] $output $wrapper[1]
</form>";
}
}
9 changes: 0 additions & 9 deletions tests/test.php

This file was deleted.

0 comments on commit 4c4fbe0

Please sign in to comment.