Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.27 KB | None | 0 0
  1. <?php
  2.  
  3. namespace CORE\services;
  4.  
  5. use CORE\dao\FormDao;
  6. use \Slim\Container;
  7.  
  8. class FormBuilderService
  9. {
  10.     private $_Form;
  11.     private $_Types = [];
  12.     private $_ElementNames = [];
  13.     private $_ChildrenStructs = [];
  14.     private $_InvisibleElements = [];
  15.     private $_ValidationTypes = [];
  16.     private $breakElements = true;
  17.     private $_UserForm;
  18.  
  19.     public function __construct(Container $container, FormDao $formDao)
  20.     {
  21.         $form = $formDao->GetAllForms();
  22.         $this->_Form = $form;
  23.     }
  24.  
  25.     public function BuildUserForm($formInput){
  26.         $form = $this->_Form;
  27.         $userForm = $formInput;
  28.  
  29.         $this->_UserForm = $userForm;
  30.         foreach($userForm as $formInput){
  31.             $this->BuildUserFormNested($form, $formInput);
  32.         }
  33.  
  34.         return $form;
  35.  
  36.     }
  37.  
  38.     private function BuildUserFormNested(&$array, &$arrayToCompare, $nestedChild = 0)
  39.     {
  40.         if($nestedChild == 1){
  41.             foreach($array as &$child){
  42.                 foreach ($child as &$nestedChild){
  43.                     foreach($this->_UserForm as $userForm){
  44.                         if($nestedChild['name'] === $userForm['name']){
  45.                             $nestedChild['value'] = $userForm['value'];
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.  
  52.         foreach ($array as $section => &$element) {
  53.             foreach ($element as &$child) {
  54.                 if ($child['name'] === $arrayToCompare['name']) {
  55.                     if (isset($child['children'])) {
  56.                         if(isset($child['children']['checked'])){
  57.                             $this->BuildUserFormNested($child['children'], $arrayToCompare, 1);
  58.                         }
  59.                         else{
  60.                             $child['value'] = $arrayToCompare['value'];
  61.                         }
  62.                     } else {
  63.                         $child['value'] = $arrayToCompare['value'];
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.     }
  69.  
  70.     private function GenerateEnvironment($array)
  71.     {
  72.         foreach ($array as $element) {
  73.             if (isset($element['type']) and !in_array($element['type'], $this->_Types)) {
  74.                 array_push($this->_Types, $element['type']);
  75.             }
  76.             if (isset($element['name']) and !in_array($element['name'], $this->_ElementNames)) {
  77.                 array_push($this->_ElementNames, $element['name']);
  78.             }
  79.             if (isset($element['children'])) {
  80.                 $this->_ChildrenStructs[$element['name']] = $element['children'];
  81.             }
  82.             if (isset($element['validation'])) {
  83.                 $this->_ValidationTypes[$element['name']] = $element['validation'];
  84.             } elseif (is_array($element)) {
  85.                 $this->GenerateEnvironment($element);
  86.             }
  87.         }
  88.     }
  89.  
  90.     public function BuildSection(...$singleSection)
  91.     {
  92.         $result = $this->_Form;
  93.  
  94.         if (is_string($result)) {
  95.             return $result;
  96.         }
  97.        
  98.         $formHTML = "";
  99.  
  100.         $this->GenerateEnvironment($result);
  101.  
  102.         if(func_num_args() >= 1){
  103.             foreach($singleSection as $sectionName){
  104.                 foreach ($result as $section => &$item){
  105.                     if($section == $sectionName){
  106.                         $formHTML .= $this->BuildForm($item, true, $section);
  107.                     }
  108.                 }
  109.             }
  110.         } else{
  111.             $formHTML .= $this->BuildForm($result);
  112.         }
  113.  
  114.         return $formHTML;
  115.     }
  116.  
  117.     private function BuildForm($array, $singleSection = false, $parent = null){
  118.  
  119.         $formHTML = "";
  120.  
  121.         if($singleSection === true){
  122.             $formHTML .= '<div class="form-group" data-section="' . $parent . '">' . '<h1>' . $parent . '</h1>';
  123.             foreach($array as $sectionItem){
  124.                 $formHTML .= $this->BuildElements($sectionItem);
  125.                 if ($this->breakElements) {
  126.                     $formHTML .= '<br>';
  127.                 }
  128.             }
  129.             $formHTML .= '</div>';
  130.         } else{
  131.             foreach ($array as $section => $item) {
  132.                 $formHTML .= '<div class="form-group" data-section="' . $section . '">' .
  133.                     '<h1>' . $section . '</h1>';
  134.                 foreach ($item as $formElement) {
  135.                     $formHTML .=
  136.                         $this->BuildElements($formElement);
  137.                     if ($this->breakElements) {
  138.                         $formHTML .= '<br>';
  139.                     }
  140.                 }
  141.                 $formHTML .= '</div>';
  142.             }
  143.         }
  144.  
  145.         return $formHTML;
  146.     }
  147.  
  148.     private function BuildElements($array)
  149.     {
  150.         $formHTML = "";
  151.  
  152.         if ($array['type'] === 'dropdown') {
  153.             $formHTML .= $this->BuildDropdowns($array);
  154.         } else {
  155.             $formHTML .= $this->BuildInputs($array);
  156.         }
  157.  
  158.         if (isset($array['children']['checked'])) {
  159.             $formHTML .= $this->BuildVisibility($array['name'], $array);
  160.         }
  161.  
  162.         return $formHTML;
  163.     }
  164.  
  165.     private function BuildDeepChildren($array, $parent)
  166.     {
  167.         $html = "";
  168.         foreach ($array as $child) {
  169.             $html .= $this->BuildElements($child);
  170.             if (isset($child['children'])) {
  171.                 $this->BuildDeepChildren($child['children'], $child['name']);
  172.             }
  173.         }
  174.  
  175.         return $html;
  176.     }
  177.  
  178.     private function BuildInputs($array)
  179.     {
  180.         $html = "";
  181.         $html .=
  182.             (isset($array['name']) ? '<label for=' . $array['name'] . '>' . $array['title'] . '</label>' : "");
  183.             if($array['type'] == 'checkbox'){
  184.                 $html .= '<br><input class="form-check-input"';
  185.             } else{
  186.                 $html .= '<br><input class="form-control"';
  187.             }
  188.             $html .= (isset($array['name']) ? ' name=' . $array['name'] . ' id=' . $array['name'] : "") .
  189.             (isset($array['type']) ? ' type=' . $array['type'] : "") .
  190.             (isset($array['placeholder']) ? ' placeholder="' . $array['placeholder'] . '"' : "") .
  191.             (isset($array['minimum']) ? ' min=' . $array['minimum'] : "") .
  192.             (isset($array['maximum']) ? ' max=' . $array['maximum'] : "") .
  193.             (isset($array['validation']) ? ' data-validate=' . $array['validation'] : "") .
  194.             (isset($array['required']) ? ' required' : "") .
  195.             '>';
  196.         return $html;
  197.     }
  198.  
  199.     private function BuildDropdowns($children)
  200.     {
  201.         $html = "";
  202.         $html .= '<label for="' . $children['name'] . '">' . $children['name'] . '</label>' .
  203.                  '<select id=' . $children['name'] . ' name=' . $children['name'] . ' class="form-control">';
  204.         foreach ($children['children'] as $child) {
  205.             switch ($child['type']) {
  206.                 case 'option':
  207.                     $html .= $this->BuildOptions($child);
  208.                     break;
  209.             }
  210.         }
  211.         $html .= '</select>';
  212.         return $html;
  213.     }
  214.  
  215.     private function BuildOptions($array)
  216.     {
  217.         $html = "";
  218.         $html .= '<option ' .
  219.             (isset($array['name']) ? ' value=' . $array['name'] : "") .
  220.             (isset($array['default']) ? ' selected=' . $array['title'] : "") .
  221.             '>' .
  222.             (isset($array['title']) ? $array['title'] : $array['name']) .
  223.             '</option>';
  224.         return $html;
  225.     }
  226.  
  227.     private function BuildVisibility($name, $arrayObject)
  228.     {
  229.  
  230.         $html = "";
  231.         $visibility = $arrayObject['default'];
  232.         $this->_InvisibleElements[$name] = array('default' => $arrayObject['default'], 'parent' => $name, 'children' => $arrayObject['children']);
  233.  
  234.         switch ($visibility) {
  235.             case 1:
  236.                 $html .= '<div class="checkbox" style="display: block" id=' . $name . '-checked' . '>' .
  237.                     $this->BuildDeepChildren($arrayObject['children']['checked'], $name) .
  238.                     '</div>' .
  239.                     '<div class="checkbox" style="display: none" id=' . $name . '-unchecked' . '>' .
  240.                     $this->BuildDeepChildren($arrayObject['children']['unchecked'], $name) .
  241.                     '</div>';
  242.                 break;
  243.             case 0:
  244.                 $html .= '<div class="checkbox" style="display: block" id=' . $name . '-unchecked' . '>' .
  245.                     $this->BuildDeepChildren($arrayObject['children']['unchecked'], $name) .
  246.                     '</div>' .
  247.                     '<div class="checkbox" style="display: none" id=' . $name . '-checked' . '>' .
  248.                     $this->BuildDeepChildren($arrayObject['children']['checked'], $name) .
  249.                     '</div>';
  250.                 break;
  251.         }
  252.  
  253.         return $html;
  254.  
  255.     }
  256.  
  257.     public function ConfigureCheckboxDefaults()
  258.     {
  259.         $html = "";
  260.         foreach ($this->_InvisibleElements as $element => $values) {
  261.             $default = $values['default'] ? true : false;
  262.             $html .= 'CheckedElement("' . $element . '-checked' . '", "' . $element . '-unchecked' . '","' . $values['parent'] . '","' . $default . '");';
  263.         }
  264.         return $html;
  265.     }
  266.  
  267.     public function GetValidationTypes()
  268.     {
  269.         return $this->_ValidationTypes;
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement