public function edit($object_name, $instance_id, $linked_id = false)
 {
     # Get object / field / whatever infoz
     $object = DB::table(DB_OBJECTS)->where('name', $object_name)->first();
     $fields = DB::table(DB_FIELDS)->where('object_id', $object->id)->where('visibility', '<>', 'hidden')->orderBy('precedence')->get();
     $instance = DB::table($object->name)->where('id', $instance_id)->first();
     # Add return var to the queue
     if ($linked_id) {
         $return_to = action('InstanceController@edit', [self::getRelatedObjectName($object), $linked_id]);
     } elseif (URL::previous()) {
         $return_to = URL::previous();
     } else {
         $return_to = action('InstanceController@index', $object->name);
     }
     //format instance values for form
     foreach ($fields as &$field) {
         if ($field->type == 'datetime') {
             if (!empty($instance->{$field->name})) {
                 $instance->{$field->name} = date('m/d/Y h:i A', strtotime($instance->{$field->name}));
             }
         } elseif ($field->type == 'checkboxes' || $field->type == 'select') {
             //load options for checkboxes or selects
             $related_object = self::getRelatedObject($field->related_object_id);
             $field->options = DB::table($related_object->name)->orderBy($related_object->order_by, $related_object->direction)->lists($related_object->field->name, 'id');
             //indent nested selects
             if ($field->type == 'select' && !empty($related_object->group_by_field)) {
                 $grouped_field = DB::table(DB_FIELDS)->where('id', $related_object->group_by_field)->first();
                 if ($grouped_field->object_id == $grouped_field->related_object_id) {
                     $field->options = $parents = array();
                     $options = DB::table($related_object->name)->orderBy($related_object->order_by, $related_object->direction)->get();
                     foreach ($options as $option) {
                         if (!empty($option->{$grouped_field->name})) {
                             //calculate indent
                             if (in_array($option->{$grouped_field->name}, $parents)) {
                                 $parents = array_slice($parents, 0, array_search($option->{$grouped_field->name}, $parents) + 1);
                             } else {
                                 $parents[] = $option->{$grouped_field->name};
                             }
                             $option->{$related_object->field->name} = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', count($parents)) . $option->{$related_object->field->name};
                         } elseif (count($parents)) {
                             $parents = array();
                         }
                         $field->options[$option->id] = $option->{$related_object->field->name};
                     }
                 }
             }
             //select might be nullable
             if ($field->type == 'select' && !$field->required) {
                 $field->options = ['' => ''] + $field->options;
             }
             //get checkbox values todo make a function for consistently getting these checkbox column names
             if ($field->type == 'checkboxes') {
                 $table_key = Str::singular($object->name) . '_id';
                 $foreign_key = Str::singular($related_object->name) . '_id';
                 $instance->{$field->name} = DB::table($field->name)->where($table_key, $instance->id)->lists($foreign_key);
             }
         } elseif ($field->type == 'image') {
             $instance->{$field->name} = DB::table(DB_FILES)->where('id', $instance->{$field->name})->first();
             if (!empty($instance->{$field->name}->width) && !empty($instance->{$field->name}->height)) {
                 $field->width = $instance->{$field->name}->width;
                 $field->height = $instance->{$field->name}->height;
             }
             list($field->screen_width, $field->screen_height) = FileController::getImageDimensions($field->width, $field->height);
         } elseif ($field->type == 'images') {
             $instance->{$field->name} = DB::table(DB_FILES)->where('field_id', $field->id)->where('instance_id', $instance->id)->orderBy('precedence', 'asc')->get();
             foreach ($instance->{$field->name} as &$image) {
                 if (!empty($image->width) && !empty($image->height)) {
                     $image->screen_width = $image->width;
                     $image->screen_width = $image->height;
                 }
             }
             list($field->screen_width, $field->screen_height) = FileController::getImageDimensions($field->width, $field->height);
         } elseif ($field->type == 'slug') {
             if ($field->required && empty($instance->{$field->name}) && $field->related_field_id) {
                 //slugify related field to populate this one
                 foreach ($fields as $related_field) {
                     if ($related_field->id == $field->related_field_id) {
                         $instance->{$field->name} = Str::slug($instance->{$related_field->name});
                     }
                 }
             }
         } elseif ($field->type == 'user') {
             $field->options = DB::table(DB_USERS)->orderBy('name')->lists('name', 'id');
             if (!$field->required) {
                 $field->options = ['' => ''] + $field->options;
             }
         } elseif ($field->type == 'us_state') {
             $field->options = FieldController::usStates();
             if (!$field->required) {
                 $field->options = ['' => ''] + $field->options;
             }
         }
     }
     # Get linked objects
     $links = DB::table(DB_OBJECT_LINKS)->where('object_id', $object->id)->join(DB_OBJECTS, DB_OBJECT_LINKS . '.linked_id', '=', DB_OBJECTS . '.id')->lists(DB_OBJECTS . '.name');
     foreach ($links as &$link) {
         $link = self::index($link, $instance_id, $linked_id);
     }
     return View::make('avalon::instances.edit', compact('object', 'fields', 'instance', 'links', 'linked_id', 'return_to'));
 }