Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private function getDataFromDb() {
- $app = JFactory::getApplication();
- $catid = $app -> getUserState('com.dwp.user.ad.anwendung_id');
- // $catid = 5;
- $db = JFactory::getDbo();
- $doc = JFactory::getDocument();
- $query = $db -> getQuery(true);
- // get the fields
- $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');
- $query -> from('#__dwp_ads_fields AS a');
- // Inner join on the mapping table
- $query -> join('INNER', '#__dwp_ads_fields_cats_map AS m ON m.field_id = a.field_id AND m.cat_id = ' . $catid);
- // join over tabs
- $query -> select('t.tab_name, t.tab_id, t.tab_ordering');
- $query -> join('LEFT', '#__dwp_ads_tabs AS t ON t.tab_id = m.tab_id');
- // join over options
- $query -> select('o.option_id, o.option_name');
- $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 . ')');
- // status
- $query -> where('a.field_status = 1');
- // ordering
- $query -> order('a.field_step, t.tab_ordering, a.field_ordering, o.option_ordering');
- // echo $query->dump();
- $db -> setQuery($query);
- $results = $db -> loadObjectList();
- return $results;
- }
- private function setupArray() {
- $results = $this -> getDataFromDb();
- $steps = array();
- foreach ($results as $i => $result) {
- if (!array_key_exists($result -> field_step, $steps)) {
- $steps[$result -> field_step] = array();
- }
- if (!array_key_exists($result -> tab_name, $steps[$result -> field_step])) {
- $steps[$result -> field_step][$result -> tab_name] = array();
- }
- if (!array_key_exists($result -> field_id, $steps[$result -> field_step][$result -> tab_name])) {
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['label'] = JText::_($result -> field_name);
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['name'] = JText::_($result -> field_dbname);
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['type'] = $result->field_type != 'list-multiple' ? JText::_($result -> field_type) : 'list';
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['id'] = JText::_($result -> field_html_id);
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['required'] = JText::_($result -> field_required);
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['class'] = 'inputbox' . JText::_($result -> field_class);
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['validate'] = JText::_($result -> field_validate);
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['description'] = JText::_($result -> field_desc);
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['multiple'] = $result -> field_type == 'list-multiple' ? 'true' : 'false';
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['class'] .= $result -> field_type == 'list-multiple' ? ' multiple' : '';
- if ($result -> field_default) {
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['default'] = 0;
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['options'][0] = JText::_($result -> field_default);
- }
- if($result->field_type == 'calendar') {
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['format'] = '%d.%m.%Y';
- }
- }
- if ($result -> field_option == '1' && !array_key_exists($result -> option_id, $steps[$result -> field_step][$result -> tab_name][$result -> field_id])) {
- $steps[$result -> field_step][$result -> tab_name][$result -> field_id]['options'][$result -> option_id] = $result -> option_name;
- }
- }
- return $steps;
- }
- private function getXml() {
- $steps = $this -> setupArray();
- $xml = new DOMDocument('1.0', 'utf-8');
- $form = $xml -> createElement('form');
- $form = $xml -> appendChild($form);
- // build the xml
- foreach ($steps as $i => $groupArray) {
- $group = $xml -> createElement('fields');
- $group -> setAttribute('name', 'step' . $i);
- foreach ($groupArray as $fieldsetName => $fieldsetArray) {
- $fieldset = $xml -> createElement('fieldset');
- $fieldset -> setAttribute('name', $fieldsetName);
- $group -> appendChild($fieldset);
- foreach ($fieldsetArray as $fieldsArray) {
- $field = $xml -> createElement('field');
- foreach ($fieldsArray as $attribute_name => $attribute_value) {
- if ($attribute_name == 'options') {
- foreach ($attribute_value as $option_id => $option_name) {
- if ($option_id == 0) {
- $option_id = '';
- }
- $option = $xml -> createElement('option');
- $option -> setAttribute('value', $option_id);
- $option_text = $xml -> createTextNode($option_name);
- $option -> appendChild($option_text);
- $field -> appendChild($option);
- }
- continue;
- }
- $field -> setAttribute($attribute_name, $attribute_value);
- }
- $fieldset -> appendChild($field);
- }
- }
- $form -> appendChild($group);
- }
- $string = $xml -> saveXML();
- // dump($string);
- return $string;
- }
- public function getForm($data = array(), $loadData = false) {
- // dump($this->state);
- // Get the form.
- $xml = $this -> getXml();
- $form = $this -> loadForm('com_dwp.ad', $xml, array('control' => 'jform', 'load_data' => $loadData));
- if (empty($form)) {
- return false;
- }
- return $form;
- }
Add Comment
Please, Sign In to add comment