Hi Alex,
I've added the code below. The original method comes from Gravity Forms v1.7.6. I made two other modifications in this version to prevent problems with the value returned by the filter.
Instead of checking the $lead value to be NULL I've made it to check if the $lead value has an ID. To do so I had to cast $lead to an array (isset($lead["id"]) would have worked on any type except for objects which would have caused a fatal error for using them as an array).
public static function 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 = apply_filters("gform_pre_save_lead", $lead);
$lead_detail_table = self::get_lead_details_table_name();
$lead = (array) $lead;
//Inserting lead if null
if(!isset($lead["id"])) {
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"], self::get_ip(), self::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)
self::save_input($form, $field, $lead, $current_fields, $input["id"]);
}
else{
self::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) {
self::save_input($form, $calculation_field, $lead, $current_fields, $input["id"]);
self::refresh_lead_field_value($lead["id"], $input["id"]);
}
}
else{
self::save_input($form, $calculation_field, $lead, $current_fields, $calculation_field["id"]);
self::refresh_lead_field_value($lead["id"], $calculation_field["id"]);
}
}
self::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.");
self::save_input($form, $total_field, $lead, $current_fields, $total_field["id"]);
}
}
Posted 11 years ago on Tuesday July 9, 2013 |
Permalink