-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormBuilderProcessorTripleseat.module
140 lines (118 loc) · 4.05 KB
/
FormBuilderProcessorTripleseat.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php namespace ProcessWire;
class FormBuilderProcessorTripleseat extends FormBuilderProcessorAction implements Module
{
public static function getModuleInfo()
{
return array(
'title' => 'Tripleseat Lead Form API Integration',
'version' => 1,
'summary' => 'Forwards forms to Tripleseat using their Lead Form API',
"icon" => "smile-o",
"requires" => "FormBuilder>=0.4.5"
);
}
public function textToArray($text)
{
$value = explode("\n", $text);
$newValue = [];
foreach($value as $row){
$pos = strpos($row, '=');
if($pos) {
$key = trim(substr($row, 0, $pos));
$value = trim(substr($row, $pos+1));
$newValue[$key] = $value;
}
}
return $newValue;
}
public function processReady()
{
$this->addHookAfter('FormBuilderProcessor::saveForm', function ($event) {
/** @var $processor FormBuilderProcessor **/
$processor = $event->object;
$entryId = $event->return;
$form = $event->arguments(0);
$data = $processor->formToEntry($form, $entryId);
$url = $this->get('tripleseat_api_endpoint');
$form_id = $this->get('tripleseat_lead_form_id');
// The required fields
if (!$url || !$form_id) {
return;
}
$fields_to_format = $this->textToArray($this->get('tripleseat_date_formatter'));
$fields_to_add = $this->textToArray($this->get('tripleseat_add'));
$fields_to_rename = $this->textToArray($this->get('tripleseat_rename'));
// add fields
foreach($fields_to_add as $name => $value) {
$data[$name] = $value;
}
// rename fields
foreach($fields_to_rename as $name => $newName) {
if(!array_key_exists($name, $data)) continue;
$value = $data[$name];
unset($data[$name]);
$data[$newName] = $value;
}
// field formatting
foreach($data as $key => $v) {
if(array_key_exists($key, $fields_to_format)){
$data[$key] = date($fields_to_format[$key], $v);
}else{
$data[$key] = $v;
}
}
$post_value = ['lead' => [] ];
foreach($data as $key => $v){
$post_value['lead'][$key] = $v;
}
$post_value['lead_form_id'] = $form_id;
$http = new WireHttp();
$data = json_encode($post_value);
$http->setHeader('Content-Type' ,'application/json');
$r = $http->post($url, $data, [
'curl_setopt' => array(
CURLOPT_POSTFIELDS => $data,
),
]);
$c = $http->getHttpCode();
if($c != 200){
wire('log')->save('tripleaseat-fb-action-error', "Error in forms endpoint: {$r}");
}else{
wire('log')->save('tripleaseat-fb-action-forwarder', $r);
}
});
}
public function getConfigInputfields(InputfieldWrapper $inputfields)
{
$f = $this->modules->get('InputfieldText');
$f->attr('name', 'tripleseat_api_endpoint');
$f->label = 'API endpoint';
$f->val($this->get('tripleseat_api_endpoint'));
$inputfields->add($f);
$f = $this->modules->get('InputfieldText');
$f->attr('name', 'tripleseat_lead_form_id');
$f->label = 'Form ID';
$f->val($this->get('tripleseat_lead_form_id'));
$inputfields->add($f);
$f = $this->modules->get('InputfieldTextarea');
$f->attr('name', 'tripleseat_add');
$f->label = 'Add fields';
$f->description = 'Add fields that are not set in the form';
$f->val($this->get('tripleseat_add'));
$inputfields->add($f);
$f = $this->modules->get('InputfieldTextarea');
$f->attr('name', 'tripleseat_rename');
$f->label = 'Rename fields';
$f->description = 'Rename fields, enter one per line below in the format: [field_name]=[date_format]';
$f->val($this->get('tripleseat_rename'));
$inputfields->add($f);
$f = $this->modules->get('InputfieldTextarea');
$f->attr('name', 'tripleseat_date_formatter');
$f->label = 'Format date fields';
$f->description = 'Specify a format for date fields , enter one per line below in the format: [field_name]=[date_format]';
$f->notes = 'IMPORTANT: This happens AFTER the rename options have been applied. Take that into account when naming the field in the this configuration.
Get [Date Formats in php.net](https://www.php.net/manual/en/datetime.format.php)';
$f->val($this->get('tripleseat_date_formatter'));
$inputfields->add($f);
}
}