I tried arrong s code and it works well, however if you want to use the native gravity forms functionality, I basically copied the save_lead function to the functions.php file, removed the admin only access to the function and changed its path (see below). To make it work you just pass the form object as $form and let it deal with $lead it self so it inserts the proper lead id).
create_lead doesnt input the lead id so if you pass that as the $lead parameter it might not work.
The function Im using:
// Taken from the GF forms_model.php file, removed admin only access and replace self:: with RGFormsModel::
function local_save_lead($form, &$lead){
global $wpdb;
GFCommon::log_debug("Saving entry.");
/*
if(IS_ADMIN && !GFCommon::current_user_can_any("gravityforms_edit_entries"))
die(__("You don't have adequate permission to edit entries.", "gravityforms"));
*/
$lead_detail_table = RGFormsModel::get_lead_details_table_name();
//Inserting lead if null
if($lead == null){
global $current_user;
$user_id = $current_user && $current_user->ID ? $current_user->ID : 'NULL';
$lead_table = RGFormsModel::get_lead_table_name();
$user_agent = strlen($_SERVER["HTTP_USER_AGENT"]) > 250 ? substr($_SERVER["HTTP_USER_AGENT"], 0, 250) : $_SERVER["HTTP_USER_AGENT"];
$currency = GFCommon::get_currency();
$wpdb->query($wpdb->prepare("INSERT INTO $lead_table(form_id, ip, source_url, date_created, user_agent, currency, created_by) VALUES(%d, %s, %s, utc_timestamp(), %s, %s, {$user_id})", $form["id"], RGFormsModel::get_ip(), RGFormsModel::get_current_page_url(), $user_agent, $currency));
//reading newly created lead id
$lead_id = $wpdb->insert_id;
$lead = array("id" => $lead_id);
GFCommon::log_debug("Entry record created in the database. ID: {$lead_id}");
}
$current_fields = $wpdb->get_results($wpdb->prepare("SELECT id, field_number FROM $lead_detail_table WHERE lead_id=%d", $lead["id"]));
$original_post_id = rgget("post_id", $lead);
$total_field = null;
$calculation_fields = array();
$recalculate_total = false;
GFCommon::log_debug("Saving entry fields.");
foreach($form["fields"] as $field){
//Ignore fields that are marked as display only
if(rgget("displayOnly", $field) && $field["type"] != "password"){
continue;
}
//ignore pricing fields in the entry detail
if(RG_CURRENT_VIEW == "entry" && GFCommon::is_pricing_field($field["type"])){
continue;
}
//process total field after all fields have been saved
if($field["type"] == "total"){
$total_field = $field;
continue;
}
//only save fields that are not hidden (except on entry screen)
if(RG_CURRENT_VIEW == "entry" || !RGFormsModel::is_field_hidden($form, $field, array()) ){
// process calculation fields after all fields have been saved (moved after the is hidden check)
if( GFCommon::has_field_calculation($field) ) {
$calculation_fields[] = $field;
continue;
}
GFCommon::log_debug("Saving field {$field["label"]}");
if($field['type'] == 'post_category')
$field = GFCommon::add_categories_as_choices($field, '');
if(isset($field["inputs"]) && is_array($field["inputs"])){
foreach($field["inputs"] as $input)
RGFormsModel::save_input($form, $field, $lead, $current_fields, $input["id"]);
}
else{
RGFormsModel::save_input($form, $field, $lead, $current_fields, $field["id"]);
}
}
}
if(!empty($calculation_fields)) {
foreach($calculation_fields as $calculation_field) {
GFCommon::log_debug("Saving calculated field {$calculation_field["label"]}");
if(isset($calculation_field["inputs"]) && is_array($calculation_field["inputs"])){
foreach($calculation_field["inputs"] as $input) {
RGFormsModel::save_input($form, $calculation_field, $lead, $current_fields, $input["id"]);
RGFormsModel::refresh_lead_field_value($lead["id"], $input["id"]);
}
}
else{
RGFormsModel::save_input($form, $calculation_field, $lead, $current_fields, $calculation_field["id"]);
RGFormsModel::refresh_lead_field_value($lead["id"], $calculation_field["id"]);
}
}
RGFormsModel::refresh_product_cache($form, $lead = RGFormsModel::get_lead($lead['id']));
}
//saving total field as the last field of the form.
if($total_field) {
GFCommon::log_debug("Saving total field.");
RGFormsModel::save_input($form, $total_field, $lead, $current_fields, $total_field["id"]);
}
}
Posted 11 years ago on Thursday May 30, 2013 |
Permalink