Guest User

Untitled

a guest
Jul 16th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.80 KB | None | 0 0
  1.     private function getDataFromDb() {
  2.         $app = JFactory::getApplication();
  3.         $catid = $app -> getUserState('com.dwp.user.ad.anwendung_id');
  4.         // $catid = 5;
  5.         $db = JFactory::getDbo();
  6.         $doc = JFactory::getDocument();
  7.  
  8.         $query = $db -> getQuery(true);
  9.  
  10.         // get the fields
  11.         $query -> select('a.field_id, a.field_type, a.field_name, a.field_showlabel, a.field_newline, a.field_aftertext, a.field_default, a.field_step, a.field_html_id, a.field_option, a.field_height, a.field_width, a.field_required, a.field_class, a.field_validate, a.field_desc, a.field_dbname, a.field_deps');
  12.         $query -> from('#__dwp_ads_fields AS a');
  13.  
  14.         // Inner join on the mapping table
  15.         $query -> join('INNER', '#__dwp_ads_fields_cats_map AS m ON m.field_id = a.field_id AND m.cat_id = ' . $catid);
  16.  
  17.         // join over tabs
  18.         $query -> select('t.tab_name, t.tab_id, t.tab_ordering');
  19.         $query -> join('LEFT', '#__dwp_ads_tabs AS t ON t.tab_id = m.tab_id');
  20.  
  21.         // join over options
  22.         $query -> select('o.option_id, o.option_name');
  23.         $query -> join('LEFT', '#__dwp_ads_fields_options AS o ON o.field_id = a.field_id AND (o.option_category = 0 OR o.option_category = ' . $catid . ')');
  24.  
  25.         // status
  26.         $query -> where('a.field_status = 1');
  27.  
  28.         // ordering
  29.         $query -> order('a.field_step, t.tab_ordering, a.field_ordering, o.option_ordering');
  30.         // echo $query->dump();
  31.         $db -> setQuery($query);
  32.         $results = $db -> loadObjectList();
  33.  
  34.         return $results;
  35.     }
  36.  
  37.     private function setupArray() {
  38.         $results = $this -> getDataFromDb();
  39.  
  40.         $steps = array();
  41.  
  42.         foreach ($results as $i => $result) {
  43.  
  44.  
  45.             if (!array_key_exists($result -> field_step, $steps)) {
  46.                 $steps[$result -> field_step] = array();
  47.             }
  48.             if (!array_key_exists($result -> tab_name, $steps[$result -> field_step])) {
  49.                 $steps[$result -> field_step][$result -> tab_name] = array();
  50.             }
  51.             if (!array_key_exists($result -> field_id, $steps[$result -> field_step][$result -> tab_name])) {
  52.                 $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['label'] = JText::_($result -> field_name);
  53.                 $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['name'] = JText::_($result -> field_dbname);
  54.                 $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['type'] = $result->field_type != 'list-multiple' ? JText::_($result -> field_type) : 'list';
  55.                 $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['id'] = JText::_($result -> field_html_id);
  56.                 $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['required'] = JText::_($result -> field_required);
  57.                 $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['class'] = 'inputbox' . JText::_($result -> field_class);
  58.                 $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['validate'] = JText::_($result -> field_validate);
  59.                 $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['description'] = JText::_($result -> field_desc);
  60.                 $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['multiple'] = $result -> field_type == 'list-multiple' ? 'true' : 'false';
  61.                 $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['class'] .= $result -> field_type == 'list-multiple' ? ' multiple' : '';
  62.                 if ($result -> field_default) {
  63.                     $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['default'] = 0;
  64.                     $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['options'][0] = JText::_($result -> field_default);
  65.                 }
  66.                 if($result->field_type == 'calendar') {
  67.                     $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['format'] = '%d.%m.%Y';
  68.                 }
  69.  
  70.             }
  71.             if ($result -> field_option == '1' && !array_key_exists($result -> option_id, $steps[$result -> field_step][$result -> tab_name][$result -> field_id])) {
  72.                 $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['options'][$result -> option_id] = $result -> option_name;
  73.             }
  74.         }
  75.         return $steps;
  76.  
  77.     }
  78.  
  79.     private function getXml() {
  80.         $steps = $this -> setupArray();
  81.         $xml = new DOMDocument('1.0', 'utf-8');
  82.         $form = $xml -> createElement('form');
  83.         $form = $xml -> appendChild($form);
  84.  
  85.         // build the xml
  86.         foreach ($steps as $i => $groupArray) {
  87.             $group = $xml -> createElement('fields');
  88.             $group -> setAttribute('name', 'step' . $i);
  89.  
  90.             foreach ($groupArray as $fieldsetName => $fieldsetArray) {
  91.                 $fieldset = $xml -> createElement('fieldset');
  92.                 $fieldset -> setAttribute('name', $fieldsetName);
  93.                 $group -> appendChild($fieldset);
  94.                 foreach ($fieldsetArray as $fieldsArray) {
  95.                     $field = $xml -> createElement('field');
  96.                     foreach ($fieldsArray as $attribute_name => $attribute_value) {
  97.                         if ($attribute_name == 'options') {
  98.                             foreach ($attribute_value as $option_id => $option_name) {
  99.                                 if ($option_id == 0) {
  100.                                     $option_id = '';
  101.                                 }
  102.                                 $option = $xml -> createElement('option');
  103.                                 $option -> setAttribute('value', $option_id);
  104.                                 $option_text = $xml -> createTextNode($option_name);
  105.                                 $option -> appendChild($option_text);
  106.                                 $field -> appendChild($option);
  107.                             }
  108.                             continue;
  109.                         }
  110.                         $field -> setAttribute($attribute_name, $attribute_value);
  111.  
  112.                     }
  113.                     $fieldset -> appendChild($field);
  114.                 }
  115.             }
  116.             $form -> appendChild($group);
  117.         }
  118.         $string = $xml -> saveXML();
  119.         // dump($string);
  120.  
  121.         return $string;
  122.     }
  123.  
  124.     public function getForm($data = array(), $loadData = false) {
  125.         // dump($this->state);
  126.         // Get the form.
  127.         $xml = $this -> getXml();
  128.         $form = $this -> loadForm('com_dwp.ad', $xml, array('control' => 'jform', 'load_data' => $loadData));
  129.         if (empty($form)) {
  130.             return false;
  131.         }
  132.  
  133.         return $form;
  134.     }
Add Comment
Please, Sign In to add comment