function form_get_profile_data_from_form_post()
{
    $form_id = get_input('form_id', 0);
    $flexprofile_data = array();
    $data = array();
    $images = array();
    $maps = form_get_maps($form_id);
    // does not handle image uploads or required data right now, but should
    // best to use the function in the form plugin instead? Not sure ...
    if ($maps) {
        foreach ($maps as $map) {
            $field = get_entity($map->field_id);
            $flexprofile_data[$field->internal_name] = get_input('form_data_' . $field->internal_name, '');
            if ($field->field_type == 'tags') {
                // KJ - I reverse the array to fix an annoying Elgg tag order bug
                // I will remove this workaround when the bug is fixed
                $flexprofile_data[$field->internal_name] = array_reverse(string_to_tag_array($flexprofile_data[$field->internal_name]));
            } else {
                if ($field->field_type == 'image_upload') {
                    // special handling for images
                    $images[] = $field->internal_name;
                }
            }
        }
    }
    $profile = get_entity($form_id)->profile;
    if ($profile == 1) {
        $flexprofile_access = get_input('flexprofile_access', '');
    }
    foreach ($flexprofile_data as $name => $value) {
        if (!in_array($name, $images)) {
            $data[$name] = new StdClass();
            $data[$name]->value = $value;
            if ($profile == 1) {
                $data[$name]->access_id = $flexprofile_access[$name];
            } else {
                $data[$name]->access_id = ACCESS_PUBLIC;
            }
        }
    }
    // not sure if this should go here
    foreach ($images as $image) {
        $formfieldname = 'form_data_' . $image;
        // don't do anything if this field is blank
        if (isset($_FILES[$formfieldname]['name']) && trim($_FILES[$formfieldname]['name'])) {
            // should probably delete the old image data somehow, if it exists
            $data[$image] = new StdClass();
            if ($profile == 1) {
                $data[$image]->access_id = $flexprofile_access[$image];
            } else {
                $data[$image]->access_id = ACCESS_PUBLIC;
            }
            $data[$image]->value = form_handle_file_upload($formfieldname, $flexprofile_access[$image]);
        }
    }
    return $data;
}
function form_set_data_from_form($form_data_id = 0)
{
    global $CONFIG;
    $form_id = get_input('form_id');
    $form = get_entity($form_id);
    $maps = form_get_maps($form_id);
    if ($maps) {
        // fields are no longer stored in an array
        //$form_data = get_input('form_data',array());
        if ($form_data_id) {
            $fd = get_entity($form_data_id);
        } else {
            $fd = new ElggObject();
            $fd->subtype = 'form_data';
            if (isloggedin()) {
                $fd->owner_guid = $_SESSION['user']->getGUID();
            } else {
                // no owner
                $fd->owner_guid = 0;
            }
            // Same as form unless this is changed by the form submit
            $fd->access_id = $form->access_id;
            $fd->form_id = $form_id;
        }
        $form_data = array();
        $images = array();
        $invite_box_name = '';
        $result = new StdClass();
        $result->error_status = false;
        $result->missing = array();
        $result_form_data = array();
        foreach ($maps as $map) {
            $field = get_entity($map->field_id);
            $internalname = $field->internal_name;
            $item = new StdClass();
            $item->value = '';
            $form_data[$internalname] = get_input('form_data_' . $internalname, '');
            if ($field->field_type == 'tags') {
                // KJ - I reverse the array to fix an annoying Elgg tag order bug
                // I will remove this workaround when the bug is fixed
                $form_data[$internalname] = array_reverse(string_to_tag_array($form_data[$internalname]));
            }
            if ($field->field_type == 'image_upload') {
                // special handling for images
                $images[] = $internalname;
            } else {
                if ($field->field_type == 'invite_box') {
                    // special handling for invite box
                    $invite_box_name = 'form_data_' . $internalname;
                } else {
                    if ($field->field_type == 'access') {
                        // set both values in case the internal name is not "access_id"
                        $fd->{$internalname} = $form_data[$internalname];
                        $fd->access_id = $form_data[$internalname];
                        $item->value = $form_data[$internalname];
                    } else {
                        if (isset($form_data[$internalname])) {
                            $fd->{$internalname} = $form_data[$internalname];
                            $item->value = $form_data[$internalname];
                        } else {
                            $fd->{$internalname} = '';
                        }
                    }
                }
            }
            if ($field->required && (!isset($form_data[$internalname]) || trim($form_data[$internalname]) === '')) {
                $result->error_status = true;
                $result->error_reason = 'missing';
                $result->missing[] = $field;
            }
            $result_form_data[$internalname] = $item;
        }
        $result->form_data = $result_form_data;
        if ($result->error_status) {
            return $result;
        } else {
            // looks good
            if ($form->email_form) {
                form_send_results($form, $result, $fd->owner_guid);
                return $result;
            } else {
                // set language and save
                if (empty($CONFIG->language)) {
                    $fd->_language = 'en';
                } else {
                    $fd->_language = $CONFIG->language;
                }
                if ($fd->save()) {
                    // success, so save images
                    foreach ($images as $image) {
                        $formfieldname = 'form_data_' . $image;
                        // don't do anything if this field is blank
                        if (isset($_FILES[$formfieldname]['name']) && trim($_FILES[$formfieldname]['name'])) {
                            if ($fd->{$image}) {
                                // delete the old file
                                form_delete_file($fd->{$image});
                            }
                            $fd->{$image} = form_handle_file_upload($formfieldname, $fd->access_id);
                        }
                    }
                    // added to keep the river happy
                    if (!trim($fd->title)) {
                        $fd->title = sprintf(elgg_echo('form:river:title'), $form->title);
                    }
                    if (!$form_data_id) {
                        add_to_river('river/object/form_data/create', 'create', get_loggedin_userid(), $fd->getGUID());
                    }
                    // TODO: avoid this second save
                    $fd->save();
                    // handle invitations if required
                    if ($invite_box_name) {
                        form_send_invitations($invite_box_name, $fd->getGUID());
                    }
                    return $result;
                } else {
                    $result->error_status = true;
                    $result->error_reason = 'save_failed';
                    return result;
                }
            }
        }
    }
}