Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace CORE\services;
- use CORE\dao\FormDao;
- use \Slim\Container;
- class FormBuilderService
- {
- private $_Form;
- private $_Types = [];
- private $_ElementNames = [];
- private $_ChildrenStructs = [];
- private $_InvisibleElements = [];
- private $_ValidationTypes = [];
- private $breakElements = true;
- private $_UserForm;
- public function __construct(Container $container, FormDao $formDao)
- {
- $form = $formDao->GetAllForms();
- $this->_Form = $form;
- }
- public function BuildUserForm($formInput){
- $form = $this->_Form;
- $userForm = $formInput;
- $this->_UserForm = $userForm;
- foreach($userForm as $formInput){
- $this->BuildUserFormNested($form, $formInput);
- }
- return $form;
- }
- private function BuildUserFormNested(&$array, &$arrayToCompare, $nestedChild = 0)
- {
- if($nestedChild == 1){
- foreach($array as &$child){
- foreach ($child as &$nestedChild){
- foreach($this->_UserForm as $userForm){
- if($nestedChild['name'] === $userForm['name']){
- $nestedChild['value'] = $userForm['value'];
- }
- }
- }
- }
- }
- foreach ($array as $section => &$element) {
- foreach ($element as &$child) {
- if ($child['name'] === $arrayToCompare['name']) {
- if (isset($child['children'])) {
- if(isset($child['children']['checked'])){
- $this->BuildUserFormNested($child['children'], $arrayToCompare, 1);
- }
- else{
- $child['value'] = $arrayToCompare['value'];
- }
- } else {
- $child['value'] = $arrayToCompare['value'];
- }
- }
- }
- }
- }
- private function GenerateEnvironment($array)
- {
- foreach ($array as $element) {
- if (isset($element['type']) and !in_array($element['type'], $this->_Types)) {
- array_push($this->_Types, $element['type']);
- }
- if (isset($element['name']) and !in_array($element['name'], $this->_ElementNames)) {
- array_push($this->_ElementNames, $element['name']);
- }
- if (isset($element['children'])) {
- $this->_ChildrenStructs[$element['name']] = $element['children'];
- }
- if (isset($element['validation'])) {
- $this->_ValidationTypes[$element['name']] = $element['validation'];
- } elseif (is_array($element)) {
- $this->GenerateEnvironment($element);
- }
- }
- }
- public function BuildSection(...$singleSection)
- {
- $result = $this->_Form;
- if (is_string($result)) {
- return $result;
- }
- $formHTML = "";
- $this->GenerateEnvironment($result);
- if(func_num_args() >= 1){
- foreach($singleSection as $sectionName){
- foreach ($result as $section => &$item){
- if($section == $sectionName){
- $formHTML .= $this->BuildForm($item, true, $section);
- }
- }
- }
- } else{
- $formHTML .= $this->BuildForm($result);
- }
- return $formHTML;
- }
- private function BuildForm($array, $singleSection = false, $parent = null){
- $formHTML = "";
- if($singleSection === true){
- $formHTML .= '<div class="form-group" data-section="' . $parent . '">' . '<h1>' . $parent . '</h1>';
- foreach($array as $sectionItem){
- $formHTML .= $this->BuildElements($sectionItem);
- if ($this->breakElements) {
- $formHTML .= '<br>';
- }
- }
- $formHTML .= '</div>';
- } else{
- foreach ($array as $section => $item) {
- $formHTML .= '<div class="form-group" data-section="' . $section . '">' .
- '<h1>' . $section . '</h1>';
- foreach ($item as $formElement) {
- $formHTML .=
- $this->BuildElements($formElement);
- if ($this->breakElements) {
- $formHTML .= '<br>';
- }
- }
- $formHTML .= '</div>';
- }
- }
- return $formHTML;
- }
- private function BuildElements($array)
- {
- $formHTML = "";
- if ($array['type'] === 'dropdown') {
- $formHTML .= $this->BuildDropdowns($array);
- } else {
- $formHTML .= $this->BuildInputs($array);
- }
- if (isset($array['children']['checked'])) {
- $formHTML .= $this->BuildVisibility($array['name'], $array);
- }
- return $formHTML;
- }
- private function BuildDeepChildren($array, $parent)
- {
- $html = "";
- foreach ($array as $child) {
- $html .= $this->BuildElements($child);
- if (isset($child['children'])) {
- $this->BuildDeepChildren($child['children'], $child['name']);
- }
- }
- return $html;
- }
- private function BuildInputs($array)
- {
- $html = "";
- $html .=
- (isset($array['name']) ? '<label for=' . $array['name'] . '>' . $array['title'] . '</label>' : "");
- if($array['type'] == 'checkbox'){
- $html .= '<br><input class="form-check-input"';
- } else{
- $html .= '<br><input class="form-control"';
- }
- $html .= (isset($array['name']) ? ' name=' . $array['name'] . ' id=' . $array['name'] : "") .
- (isset($array['type']) ? ' type=' . $array['type'] : "") .
- (isset($array['placeholder']) ? ' placeholder="' . $array['placeholder'] . '"' : "") .
- (isset($array['minimum']) ? ' min=' . $array['minimum'] : "") .
- (isset($array['maximum']) ? ' max=' . $array['maximum'] : "") .
- (isset($array['validation']) ? ' data-validate=' . $array['validation'] : "") .
- (isset($array['required']) ? ' required' : "") .
- '>';
- return $html;
- }
- private function BuildDropdowns($children)
- {
- $html = "";
- $html .= '<label for="' . $children['name'] . '">' . $children['name'] . '</label>' .
- '<select id=' . $children['name'] . ' name=' . $children['name'] . ' class="form-control">';
- foreach ($children['children'] as $child) {
- switch ($child['type']) {
- case 'option':
- $html .= $this->BuildOptions($child);
- break;
- }
- }
- $html .= '</select>';
- return $html;
- }
- private function BuildOptions($array)
- {
- $html = "";
- $html .= '<option ' .
- (isset($array['name']) ? ' value=' . $array['name'] : "") .
- (isset($array['default']) ? ' selected=' . $array['title'] : "") .
- '>' .
- (isset($array['title']) ? $array['title'] : $array['name']) .
- '</option>';
- return $html;
- }
- private function BuildVisibility($name, $arrayObject)
- {
- $html = "";
- $visibility = $arrayObject['default'];
- $this->_InvisibleElements[$name] = array('default' => $arrayObject['default'], 'parent' => $name, 'children' => $arrayObject['children']);
- switch ($visibility) {
- case 1:
- $html .= '<div class="checkbox" style="display: block" id=' . $name . '-checked' . '>' .
- $this->BuildDeepChildren($arrayObject['children']['checked'], $name) .
- '</div>' .
- '<div class="checkbox" style="display: none" id=' . $name . '-unchecked' . '>' .
- $this->BuildDeepChildren($arrayObject['children']['unchecked'], $name) .
- '</div>';
- break;
- case 0:
- $html .= '<div class="checkbox" style="display: block" id=' . $name . '-unchecked' . '>' .
- $this->BuildDeepChildren($arrayObject['children']['unchecked'], $name) .
- '</div>' .
- '<div class="checkbox" style="display: none" id=' . $name . '-checked' . '>' .
- $this->BuildDeepChildren($arrayObject['children']['checked'], $name) .
- '</div>';
- break;
- }
- return $html;
- }
- public function ConfigureCheckboxDefaults()
- {
- $html = "";
- foreach ($this->_InvisibleElements as $element => $values) {
- $default = $values['default'] ? true : false;
- $html .= 'CheckedElement("' . $element . '-checked' . '", "' . $element . '-unchecked' . '","' . $values['parent'] . '","' . $default . '");';
- }
- return $html;
- }
- public function GetValidationTypes()
- {
- return $this->_ValidationTypes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement