PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Programmatically create entry

  1. Is there a way to submit entries for a form using a function?
    I have looked at RGFormsModel::create_lead and RGFormsModel::save_lead, but am having a hard time finding the correct format of the data that needs to be passed as arguments.
    Or perhaps there is a wpdb SQL call that would work for this?

    My use case is the collection of data in one form, with an "opt in" from another part of the site that would automatically create an entry.

    Any and all help here is appreciated.

    Posted 11 years ago on Thursday May 2, 2013 | Permalink
  2. David Peralty

    I've asked our developers to take a look, and maybe they can direct you on where to find the code you need.

    Posted 11 years ago on Thursday May 2, 2013 | Permalink
  3. Any progress here? Thanks.

    Posted 11 years ago on Monday May 6, 2013 | Permalink
  4. Unfortunately there is no easy way of manually creating an entry using a function. The RGFormsModel::create_lead() function creates an entry for use in PHP, but doesn't actually save it to the database. The RGFormsModel::save_lead() function saves the entry to the database, but gets its values from the POST variables.
    The only suggestion I have for you is to study the save_lead() function and make a copy of it, changing the pieces you need.
    Sorry I couldn't be of much help this time.

    Posted 11 years ago on Monday May 6, 2013 | Permalink
  5. I have been able to do this. This is part of larger code so please ensure you run this at or after the 'init' action so that the 'GFFormsModel' class is available.

    public static function add_leads($form_id, $qty, $start = 1){
            global $wpdb;
            $table = $wpdb->prefix.'rg_lead';
            $tableDetail = $wpdb->prefix.'rg_lead_detail';
            $form = GFFormsModel::get_form_meta($form_id);
            $lead = GFFormsModel::create_lead($form);
            $itt = $start;
            for($j=0;$j<$qty;$j++){
                foreach ($form['fields'] as $field) {
                    if($field['type'] == 'email'){
                        $lead[$field['id']] = md5($itt).'@md5.gen';
                    } elseif($field['type'] == 'text') {
                        $lead[$field['id']] = md5($itt);
                    }
                }
                // var_dump($lead);
                unset($lead['id']);
                unset($lead['post_id']);
                $lead['date_created'] = date("Y-m-d H:i:s");
                $leadMeta = array();
                $leadDetail = array();
                $leadDetails = array();
                foreach ($lead as $key => $value) {
                    if(gettype($key) == 'integer'){
                        $leadDetail['field_number'] = $key;
                        $leadDetail['value'] = $value;
                        $leadDetail['form_id'] = $form_id;
                        $leadDetails[] = $leadDetail;
                    } else {
                        $leadMeta[$key] = $value;
                    }
                }
                $wpdb->insert($table, $leadMeta, array('%s','%d','%s','%s','%s','%s','%d',));
                $lead_id = $wpdb->insert_id;
                for($i = 0; $i < count($leadDetails); $i++) {
                    $leadDetails[$i]['lead_id'] = $lead_id;
                }
                // var_dump($leadDetails);
                foreach ($leadDetails as $leadDetail) {
                    $wpdb->insert($tableDetail, $leadDetail, array('%d','%s','%d','%d',));
                }
                $itt++;
            }
        }

    what this does for me (when you call it) is just add garbage entries where the text fields are and MD5 hash of an increasing number and emails are that same hash @md5.com this is really so i can test things with 1000s of entries. you could obviously do something more eloquent. message me if you need clarification.

    Posted 11 years ago on Wednesday May 29, 2013 | Permalink
  6. ixistudio
    Member

    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