Beispiel #1
0
 /**
  * Method to get a unique code_name.
  *
  * @param   array   $data			The data where the original name is stored
  * @param   string  $field			Name of field which allows for handling both code_name and plural_code_name
  *
  * @return	string  $code_name		The modified code_name.
  */
 protected function importFields($data)
 {
     $db = JFactory::getDBO();
     $source_table = $data['source_table'];
     $component_id = $data['component_id'];
     $component_object_id = $data['id'];
     // Get the default fieldset
     $query = $db->getQuery(true);
     // Construct the query
     $query->select($db->quoteName('a.default_fieldset_id'));
     $query->from($db->quoteName('#__componentarchitect_componentobjects') . ' AS a ');
     $query->where($db->quoteName('a.id') . ' = ' . $component_object_id);
     // Setup the query
     $db->setQuery($query->__toString());
     // Return the result
     $default_fieldset_id = $db->loadResult();
     $query->clear();
     $db->setQuery('SHOW COLUMNS FROM ' . $source_table);
     if ($table_fields = $db->loadAssocList()) {
         $query->clear();
         // Construct the query
         $query->select($db->quoteName('a.code_name') . ' AS code_name');
         $query->from($db->quoteName('#__componentarchitect_fields') . ' AS a ');
         $query->where($db->quoteName('a.component_object_id') . ' = ' . $component_object_id);
         // Setup the query
         $db->setQuery($query->__toString());
         // Return the result
         $current_fields = $db->loadColumn();
         $field_type_lookup = array();
         // Get all field types to get the Field Type ID for the field
         $field_types_model = JModelLegacy::getInstance('fieldtypes', 'ComponentarchitectModel', array('ignore_request' => true));
         $field_types_model->setState('list.ordering', 'a.name');
         $field_types_model->setState('list.direction', 'ASC');
         $field_types_model->setState('list.select', 'a.*');
         $field_types = $field_types_model->getItems();
         foreach ($field_types as $field_type) {
             $field_type_lookup[] = $field_type->code_name;
         }
         foreach ($table_fields as $table_field) {
             $field = array();
             $table_field['Field'] = JString::strtolower(preg_replace('/[^a-zA-Z0-9_]/', '_', $table_field['Field']));
             $field['code_name'] = $table_field['Field'];
             // Only process fields if they do not already exist
             if (!in_array($field['code_name'], $current_fields)) {
                 $field['id'] = 0;
                 $field['name'] = JString::ucWords(str_replace('-', ' ', str_replace('_', ' ', $table_field['Field'])));
                 $type_array = explode('(', $table_field['Type']);
                 if (preg_match('/unsigned/i', $table_field['Type'])) {
                     $unsigned = true;
                 } else {
                     $unsigned = false;
                 }
                 $field['mysql_datatype'] = strtoupper(trim(preg_replace('/\\)|unsigned/i', '', $type_array[0])));
                 if (!is_null($table_field['Default']) and $table_field['Default'] != '') {
                     $field['mysql_default'] = "'" . $table_field['Default'] . "'";
                 } else {
                     if ($table_field['Null'] == 'YES' and is_null($table_field['Default'])) {
                         $field['mysql_default'] = "'NULL'";
                     } else {
                         $field['mysql_default'] = '';
                     }
                 }
                 // Set field maxlength
                 if (isset($type_array[1])) {
                     $field['mysql_size'] = trim(preg_replace('/\\)|unsigned/i', '', $type_array[1]));
                 } else {
                     $field['mysql_size'] = '';
                 }
                 switch ($field['mysql_datatype']) {
                     case 'VARCHAR':
                     case 'CHAR':
                         if ((int) $field['mysql_size'] <= 255) {
                             $type = 'text';
                         } else {
                             $type = 'textarea';
                         }
                         $field['maxlength'] = $field['mysql_size'];
                         $field['php_variable_type'] = 'string';
                         if ((int) $field['mysql_size'] <= 100) {
                             $field['size'] = $field['mysql_size'];
                         }
                         if ($field['mysql_default'] == '' and $field['mysql_datatype'] != 'TINYTEXT') {
                             $field['mysql_default'] = "''";
                         }
                         break;
                     case 'INT':
                     case 'BIGINT':
                     case 'SERIAL':
                         // Most likely a foreign key so check if we want to make it modal
                         if ((int) $field['mysql_size'] == 10 and $table_field['Key'] != '') {
                             // Get all the component object names if one matches the field code name (without '_id'_
                             $query->clear();
                             // Construct the query
                             $query->select($db->quoteName('a.id') . ' AS id');
                             $query->from($db->quoteName('#__componentarchitect_componentobjects') . ' AS a ');
                             $query->where($db->quote($field['code_name']) . ' REGEXP ' . $db->quoteName('a.code_name'));
                             // Setup the query
                             $db->setQuery($query->__toString());
                             // Return the result
                             $foreign_object_id = $db->loadRow();
                             if ($foreign_object_id[0] > 0) {
                                 $type = 'modal';
                                 $field['foreign_object_id'] = $foreign_object_id[0];
                             } else {
                                 $type = 'text';
                                 $field['validate'] = 1;
                                 $field['validation_type'] = 'numeric';
                             }
                             if ($field['mysql_default'] == '') {
                                 $field['mysql_default'] = "'0'";
                             }
                             $field['php_variable_type'] = 'int';
                             break;
                         }
                     case 'TINYINT':
                     case 'SMALLINT':
                         if ((int) $field['mysql_size'] == 1) {
                             $type = 'checkbox';
                         } else {
                             $type = 'text';
                             $field['validate'] = 1;
                             $field['validation_type'] = 'numeric';
                         }
                         if ($field['mysql_default'] == '') {
                             $field['mysql_default'] = "'0'";
                         }
                         $field['php_variable_type'] = 'int';
                         break;
                     case 'BOOL':
                     case 'BOOLEAN':
                         $type = 'checkbox';
                         $field['php_variable_type'] = 'int';
                         break;
                     case 'FLOAT':
                     case 'DOUBLE':
                     case 'REAL':
                     case 'DECIMAL':
                         $type = 'text';
                         $field['validate'] = 1;
                         $field['validation_type'] = 'numeric';
                         $field['php_variable_type'] = 'float';
                         if ($field['mysql_default'] == '') {
                             $field['mysql_default'] = "'0'";
                         }
                         break;
                     case 'DATE':
                     case 'DATETIME':
                     case 'TIMESTAMP':
                         $type = 'calendar';
                         break;
                     case 'TIME':
                         $type = 'text';
                         $field['format'] = 'H:M:S';
                         break;
                     case 'YEAR':
                         $type = 'text';
                         $field['format'] = 'Y';
                         break;
                     case 'TEXT':
                     case 'MEDIUMTEXT':
                     case 'LONGTEXT':
                         $type = 'editor';
                         $field['mysql_default'] = '';
                         break;
                     case 'TINYTEXT':
                         $type = 'textarea';
                         $field['mysql_default'] = '';
                         break;
                     default:
                         $type = 'text';
                         if ($field['mysql_default'] == '') {
                             $field['mysql_default'] = "''";
                         }
                         break;
                 }
                 // Set values for any defaults for the field type
                 $index = array_search($type, $field_type_lookup);
                 $optional_attributes = array('class', 'size', 'maxlength', 'width', 'height', 'cols', 'rows', 'format', 'first', 'last', 'step', 'hide_none', 'hide_default', 'buttons', 'hide_buttons', 'field_filter', 'max_file_size', 'exclude_files', 'accept_file_types', 'directory', 'link', 'translate', 'client', 'stripext', 'preview', 'autocomplete', 'onclick', 'onchange', 'mysql_size', 'mysql_datatype', 'mysql_default');
                 foreach ($optional_attributes as $attribute) {
                     // Temporary overrider as the Joomla Calendar field seems to only work with the full format date as below.
                     if ($type == 'calendar' and $attribute == 'format' and !isset($field['format'])) {
                         $field[$attribute] = 'Y-m-d H:M:S';
                     } else {
                         if ($attribute == 'mysql_size' or $attribute == 'mysql_datatype' or $attribute == 'mysql_default') {
                             if ($attribute == 'mysql_size' and $field['mysql_size'] == '' and in_array($field['mysql_datatype'], array('FLOAT', 'DOUBLE', 'REAL', 'DECIMAL'))) {
                                 $field['mysql_size'] = '';
                             } else {
                                 if (!isset($field[$attribute]) or $field[$attribute] == '') {
                                     $attribute_default = $attribute . '_default';
                                     if (isset($field_types[$index]->{$attribute_default}) and $field_types[$index]->{$attribute_default} != '') {
                                         $field[$attribute] = $field_types[$index]->{$attribute_default};
                                     }
                                 }
                             }
                         } else {
                             if ($field_types[$index]->{$attribute}) {
                                 if (!isset($field[$attribute]) or $field[$attribute] == '') {
                                     $attribute_default = $attribute . '_default';
                                     if (isset($field_types[$index]->{$attribute_default}) and $field_types[$index]->{$attribute_default} != '') {
                                         $field[$attribute] = $field_types[$index]->{$attribute_default};
                                     }
                                 }
                             }
                         }
                     }
                 }
                 // Set fields for field
                 $field['component_id'] = $component_id;
                 $field['component_object_id'] = $component_object_id;
                 $field['fieldset_id'] = $default_fieldset_id;
                 $field['fieldtype_id'] = $field_types[$index]->id;
                 $field['language'] = '*';
                 $field['state'] = 1;
                 $field['access'] = 1;
                 $field['predefined_field'] = 0;
                 $field_model = JModelLegacy::getInstance('field', 'ComponentarchitectModel');
                 if (!$field_model->save($field)) {
                     $this->setError(JText::sprintf('COM_COMPONENTARCHITECT_COMPONENTOBJECTS_ERROR_PROBLEM_IMPORTING_SOURCE_TABLE', $field['code_name'], $source_table));
                     return false;
                 }
             }
         }
         return true;
     } else {
         $this->setError(JText::sprintf('COM_COMPONENTARCHITECT_COMPONENTOBJECTS_ERROR_SOURCE_TABLE_NOT_ACCESSIBLE', $source_table));
         return false;
     }
 }