/** * Adds a custom field using a field object. * * @param TemplateField $field * * @return bool */ public function addFieldObject(&$field) { $GLOBALS['log']->debug('adding field'); $object_name = $this->module; $db_name = $field->name; $fmd = new FieldsMetaData(); $id = $fmd->retrieve($object_name . $db_name, true, false); $is_update = false; $label = strtoupper($field->label); if (!empty($id)) { $is_update = true; } else { $db_name = $this->getDBName($field->name); $field->name = $db_name; } $this->createCustomTable(); $fmd->id = $object_name . $db_name; $fmd->custom_module = $object_name; $fmd->name = $db_name; $fmd->vname = $label; $fmd->type = $field->type; $fmd->help = $field->help; if (!empty($field->len)) { $fmd->len = $field->len; } // tyoung bug 15407 - was being set to $field->size so changes weren't being saved $fmd->required = $field->required ? 1 : 0; $fmd->default_value = $field->default; $fmd->ext1 = $field->ext1; $fmd->ext2 = $field->ext2; $fmd->ext3 = $field->ext3; $fmd->ext4 = isset($field->ext4) ? $field->ext4 : ''; $fmd->comments = $field->comment; $fmd->massupdate = $field->massupdate; $fmd->importable = isset($field->importable) ? $field->importable : null; $fmd->duplicate_merge = $field->duplicate_merge; $fmd->audited = $field->audited; $fmd->inline_edit = $field->inline_edit; $fmd->reportable = $field->reportable ? 1 : 0; if (!$is_update) { $fmd->new_with_id = true; } if ($field) { if (!$is_update) { //Do two SQL calls here in this case //The first is to create the column in the custom table without the default value //The second is to modify the column created in the custom table to set the default value //We do this so that the existing entries in the custom table don't have the default value set $field->default = ''; $field->default_value = ''; // resetting default and default_value does not work for multienum and causes trouble for MsSQL // so using a temporary variable here to indicate that we don't want default for this query $field->no_default = 1; $query = $field->get_db_add_alter_table($this->bean->table_name . '_cstm'); // unset temporary member variable unset($field->no_default); if (!empty($query)) { $this->db->query($query, true, 'Cannot create column'); $field->default = $fmd->default_value; $field->default_value = $fmd->default_value; $query = $field->get_db_modify_alter_table($this->bean->table_name . '_cstm'); if (!empty($query)) { $this->db->query($query, true, 'Cannot set default'); } } } else { $query = $field->get_db_modify_alter_table($this->bean->table_name . '_cstm'); if (!empty($query)) { $this->db->query($query, true, 'Cannot modify field'); } } $fmd->save(); $this->buildCache($this->module); $this->saveExtendedAttributes($field, array_keys($fmd->field_defs)); } return true; }