Submission to gripp

<?php

// barasso
/**
 * Send a complete Gravity forms submission to separate GRIPP objects
 *
 * @param array $inputs
 * @link  https://api.gripp.com/public/api3.php#offer
 */
function send_to_gripp($inputs)
{
	include_once 'api.class.php';
	$token = 'SNNNHPJEos5vDVibFm3c45IvWdNJvO';
	$API = new com_gripp_API($token);

	// create company
	$company_data = array(
	"relationtype" => "PRIVATEPERSON", // mandatory
	"lastname" => $inputs['name'], // mandatory
	"companyroles" => ["LEAD"],
	"active" => true,
	"email" => $inputs['email'],
	"phone" => $inputs['phone'],
	);
	if (isset($inputs['street'])) {
		$company_data['postaddress_street'] = $inputs['street'];
	}
	if (isset($inputs['zip'])) {
		$company_data['postaddress_zipcode'] = $inputs['zip'];
	}
	if (isset($inputs['salution'])) {
		$company_data['salution'] = $inputs['salution'];
	}
	if (isset($inputs['business'])) {
		$company_data['companyname'] = $inputs['business'];
	}
	if (isset($inputs['place'])) {
		$company_data['postaddress_city'] = $inputs['place'];
		$company_data['visitingaddress_city'] = $inputs['place'];
	}

	$company_response = $API->company_create($company_data);
	if ($company_id = validate_response($company_response)) {
		$description = [
		'Bericht: ' . $inputs['message'],
		'Afmeting: ' . $inputs['measurement'],
		];
		if (isset($inputs['model'])) {
			$description[] = 'Model: ' . $inputs['model'];
		}
		if (isset($inputs['additional'])) {
			$description[] = 'Extra: ' . $inputs['additional'];
		}
		if (isset($inputs['source_url'])) {
			$description[] =   'URL: ' . $inputs['source_url'];
		}
		if (isset($inputs['contact'])) {
			$description[] =   'Telefonisch contact opnemen: ' . $inputs['contact'];
		}

		$task_data = array(
		"type" => 10,
		"phase" => 1,
		"deadlinedate" => date('Y-m-d'),
		"deadlinetime" => date('H:i'),
		"description" => implode("\n", $description),
		"content" => "Opvolgen nieuwe lead",
		"company" => $company_id,
		"estimatedhours" => 0,
		);
		$task_response = $API->task_create($task_data);
		if ($task_id = validate_response($task_response)) {
			$timeline_data = array(
			"employee" => 95629, // Machiel
			"message" => implode("\n", $description),
			"company" => $company_id,
			// "offer" => $offer_id,
			"task" => $task_id,
			);
			$API->timelineentry_create($timeline_data);
		}
	}
}

/**
 * validate the returned GRIPP results and return the created recordid or false
 *
 * @param array $response
 */
function validate_response($response)
{
	if (isset($response[0]) && isset($response[0]['result'])) {
		$response = $response[0]['result'];
		if (isset($response['success']) && $response['success'] == true) {
			return $response['recordid'];
		}
	}
	return false;
}

/**
 * Custom gravity forms hook which hooks after a submission is created.
 * All entry data will be formatted and sent to GRIPP
 *
 * @param "https://docs.gravityforms.com/entry-object/"      $entry
 * @param "https://docs.gravityforms.com/form-object/"       $form
 * @link  https://docs.gravityforms.com/gform_after_submission/
 */
add_action(
	'gform_after_submission',
	function ($entry, $form) {
		// only send these forms + inputs to gripp
		$barasso_forms = [
		2 => [                  // form id
		'name' => 1,          // input id, eq
		'email' => 2,
		'phone' => 3,
		'message' => 4,
		'place' => 6,
		'measurement' => 8,
		],
		4 => [ // offererte (landingspage)
		'name' => 1,
		'email' => 2,
		'phone' => 3,
		'message' => 4,
		'measurement' => 9,
		'place' => 11,
		'model' => 17,
		],
		5 => [ // brochure-aanvragen
		'name' => 1,
		'email' => 2,
		'phone' => 3,
		'street' => 6,
		'zip' => 11,
		'place' => 18,
		],
		1 => [ // contact
		'business' => 1,
		'salution' => 7,
		'name' => 8,
		'email' => 2,
		'phone' => 3,
		'message' => 4,
		'contact' => 5,
		]
		];
		// additional data each entry contains by default
		$entry_data = [
		'date_created',
		'post_id',
		'source_url',
		];

		if (in_array($form['id'], array_keys($barasso_forms))) {
			$inputs = [];

			// first loop through our pre-defined values
			foreach ($barasso_forms[$form['id']] as $field_label => $field_id) {
				$inputs[$field_label] = rgar($entry, (string) $field_id);
			}

			// loop through the default entry data and get it
			foreach ($entry_data as $attribute) {
				$inputs[$attribute] = $entry[$attribute];
			}

			// loop through the entry so we won't miss any data
			$additional = [];
			foreach ($form['fields'] as $field) {
				$field_inputs = $field->get_entry_inputs();
				if (is_array($field_inputs)) {
					foreach ($field_inputs as $input) {
						if (!in_array(rgar($entry, (string) $input['id']), $inputs)) {
							$additional[] = $field->label . ': ' . rgar($entry, (string) $input['id']);
						}
					}
				} else {
					if (!in_array(rgar($entry, (string) $field->id), $inputs)) {
						$additional[] = $field->label . ': ' . rgar($entry, (string) $field->id);
					}
				}
			}
			if (!empty($additional)) {
				$inputs['additional'] = implode(', ', $additional);
			}

			send_to_gripp($inputs);
		}
	},
	10,
	2
);
Last Updated:
Contributors: Niek Vlam, Suite Seven