<?php
function send_to_gripp($inputs)
{
include_once 'api.class.php';
$token = 'SNNNHPJEos5vDVibFm3c45IvWdNJvO';
$API = new com_gripp_API($token);
$company_data = array(
"relationtype" => "PRIVATEPERSON",
"lastname" => $inputs['name'],
"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,
"message" => implode("\n", $description),
"company" => $company_id,
"task" => $task_id,
);
$API->timelineentry_create($timeline_data);
}
}
}
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;
}
add_action(
'gform_after_submission',
function ($entry, $form) {
$barasso_forms = [
2 => [
'name' => 1,
'email' => 2,
'phone' => 3,
'message' => 4,
'place' => 6,
'measurement' => 8,
],
4 => [
'name' => 1,
'email' => 2,
'phone' => 3,
'message' => 4,
'measurement' => 9,
'place' => 11,
'model' => 17,
],
5 => [
'name' => 1,
'email' => 2,
'phone' => 3,
'street' => 6,
'zip' => 11,
'place' => 18,
],
1 => [
'business' => 1,
'salution' => 7,
'name' => 8,
'email' => 2,
'phone' => 3,
'message' => 4,
'contact' => 5,
]
];
$entry_data = [
'date_created',
'post_id',
'source_url',
];
if (in_array($form['id'], array_keys($barasso_forms))) {
$inputs = [];
foreach ($barasso_forms[$form['id']] as $field_label => $field_id) {
$inputs[$field_label] = rgar($entry, (string) $field_id);
}
foreach ($entry_data as $attribute) {
$inputs[$attribute] = $entry[$attribute];
}
$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
);