Skip to content

Instantly share code, notes, and snippets.

@adamfairholm
Created April 25, 2013 18:08
Show Gist options
  • Save adamfairholm/5461807 to your computer and use it in GitHub Desktop.
Save adamfairholm/5461807 to your computer and use it in GitHub Desktop.
public function add_field_to_stream($field_id, $stream_id, $data, $create_column = true)
{
// -------------------------------------
// Get the field data
// -------------------------------------
$field = $this->fields_m->get_field($field_id);
if ( ! $field) {
die('no field');
return false;
}
// -------------------------------------
// Get stream info
// -------------------------------------
$stream = $this->get_stream($stream_id);
if ( ! $stream) {
die('no stream');
return false;
}
// -------------------------------------
// Load the field type
// -------------------------------------
$field_type = $this->type->types->{$field->field_type};
if ( ! $field_type) {
die('no field type');
return false;
}
// Do we have a pre-add function?
if (method_exists($field_type, 'field_assignment_construct')) {
$field_type->field_assignment_construct($field, $stream);
}
// -------------------------------------
// Create database column
// -------------------------------------
$this->load->dbforge();
$field_data = array();
$field_data['field_slug'] = $field->field_slug;
if (isset($field->field_data['max_length']))
{
$field_data['max_length'] = $field->field_data['max_length'];
}
if (isset($field->field_data['default_value']))
{
$field_data['default_value'] = $field->field_data['default_value'];
}
$field_to_add[$field->field_slug] = $this->fields_m->field_data_to_col_data($field_type, $field_data);
if ($field_type->db_col_type !== false and $create_column === true)
{
if ( ! $this->dbforge->add_column($stream->stream_prefix.$stream->stream_slug, $field_to_add)) return false;
}
// -------------------------------------
// Check for title column
// -------------------------------------
// See if this should be made the title column
// -------------------------------------
if (isset($data['title_column']) and $data['title_column'] == 'yes')
{
$update_data['title_column'] = $field->field_slug;
$this->db->where('id', $stream->id );
$this->db->update(STREAMS_TABLE, $update_data);
}
// -------------------------------------
// Create record in assignments
// -------------------------------------
$insert_data['stream_id'] = $stream_id;
$insert_data['field_id'] = $field_id;
if (isset($data['instructions']))
{
$insert_data['instructions'] = $data['instructions'];
}
else
{
$insert_data['instructions'] = null;
}
// +1 for ordering.
$this->db->select('MAX(sort_order) as top_num')->where('stream_id', $stream->id);
$query = $this->db->get(ASSIGN_TABLE);
if ($query->num_rows() == 0)
{
// First one! Make it 1
$insert_data['sort_order'] = 1;
}
else
{
$row = $query->row();
$insert_data['sort_order'] = $row->top_num+1;
}
// Is Required
if (isset($data['is_required']) and $data['is_required'] == 'yes')
{
$insert_data['is_required'] = 'yes';
}
// Unique
if (isset($data['is_unique']) and $data['is_unique'] == 'yes')
{
$insert_data['is_unique'] = 'yes';
}
if ( ! $this->db->insert(ASSIGN_TABLE, $insert_data))
{
die('failed insert.');
return false;
}
else
{
return $this->db->insert_id();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment