1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SForce\Wsdl\SaveResult; |
4
|
|
|
|
5
|
|
|
class SalesForceContact extends DataObject |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private static $fields_to_send_to_sales_force = [ |
9
|
|
|
'FirstName', |
10
|
|
|
'LastName', |
11
|
|
|
'Email', |
12
|
|
|
]; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Singular name for CMS |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private static $singular_name = 'Sales Force Contact'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Plural name for CMS |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private static $plural_name = 'Sales Force Contacts'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* SS4 prep! |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private static $table_name = 'SalesForceContact'; |
31
|
|
|
|
32
|
|
|
private static $db = [ |
33
|
|
|
'SalesForceIdentifier' => 'Varchar(40)', |
34
|
|
|
'FirstName' => 'Varchar(255)', |
35
|
|
|
'LastName' => 'Varchar(255)', |
36
|
|
|
'Email' => 'Varchar(255)', |
37
|
|
|
'SentToSalesForce' => 'Boolean', |
38
|
|
|
'CommsErrors' => 'Text', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
private static $default_sort = [ |
43
|
|
|
'ID' => 'DESC' |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Defines a default list of filters for the search context |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private static $searchable_fields = [ |
51
|
|
|
'SentToSalesForce' => 'ExactMatchFilter', |
52
|
|
|
'SalesForceIdentifier' => 'PartialMatchField', |
53
|
|
|
'FirstName' => 'PartialMatchField', |
54
|
|
|
'LastName' => 'PartialMatchField', |
55
|
|
|
'Email' => 'PartialMatchField', |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Defines summary fields commonly used in table columns |
60
|
|
|
* as a quick overview of the data for this dataobject |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
private static $summary_fields = [ |
64
|
|
|
'SentToSalesForce' => 'Sent to SF', |
65
|
|
|
'SalesForceIdentifier' => 'SF ID', |
66
|
|
|
'FirstName' => 'First Name', |
67
|
|
|
'LastName' => 'Last Name', |
68
|
|
|
'Email' => 'Email', |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
private static $indexes = [ |
72
|
|
|
'Created' => true, |
73
|
|
|
'LastEdited' => true, |
74
|
|
|
'SentToSalesForce' => true, |
75
|
|
|
'SalesForceIdentifier' => true, |
76
|
|
|
'FirstName' => true, |
77
|
|
|
'LastName' => true, |
78
|
|
|
'Email' => true, |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
protected $onAfterWriteDone = false; |
83
|
|
|
/** |
84
|
|
|
* Event handler called after writing to the database. |
85
|
|
|
*/ |
86
|
|
|
public function onAfterWrite() |
87
|
|
|
{ |
88
|
|
|
parent::onAfterWrite(); |
89
|
|
|
if($this->onAfterWriteDone === false) { |
90
|
|
|
if(! $this->SentToSalesForce) { |
|
|
|
|
91
|
|
|
$outcomes = MySalesForcePartnerAPI::create_contact( |
92
|
|
|
$this->prepareFieldsToSendToSalesForce() |
93
|
|
|
); |
94
|
|
|
if(count($outcomes) === 1) { |
|
|
|
|
95
|
|
|
foreach($outcomes as $outcome) { |
96
|
|
|
$this->CommsErrors .= $outcome->getErrors()." ||| "; |
|
|
|
|
97
|
|
|
$this->SentToSalesForce .= $outcome->getSuccess() ? true : false; |
98
|
|
|
if(! $this->SalesForceIdentifier) { |
99
|
|
|
$this->SalesForceIdentifier = $outcome->getId(); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
$this->write(); |
103
|
|
|
$this->onAfterWriteDone = true; |
104
|
|
|
} else { |
105
|
|
|
user_error('unexpected number of outcomes: '.count($outcomes)); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function prepareFieldsToSendToSalesForce() |
112
|
|
|
{ |
113
|
|
|
$return = []; |
114
|
|
|
$fields = Config::inst()->get('SalesForceContact', 'fields_to_send_to_sales_force'); |
115
|
|
|
foreach($fields as $field) { |
116
|
|
|
if($this->$field) { |
117
|
|
|
$return[$field] = $this->$field; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* CMS Fields |
126
|
|
|
* @return FieldList |
127
|
|
|
*/ |
128
|
|
|
public function getCMSFields() |
129
|
|
|
{ |
130
|
|
|
$fields = parent::getCMSFields(); |
131
|
|
|
$fields->replaceField( |
132
|
|
|
'SalesForceIdentifier', |
133
|
|
|
ReadonlyField::create( |
134
|
|
|
'SalesForceIdentifier', |
135
|
|
|
'Sales Force Identifier' |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
$fields->replaceField( |
139
|
|
|
'SentToSalesForce', |
140
|
|
|
ReadonlyField::create( |
141
|
|
|
'SentToSalesForce', |
142
|
|
|
'Sent to Sales Force' |
143
|
|
|
)->setRightTitle('Set to TRUE (1) if sent successfully.') |
144
|
|
|
); |
145
|
|
|
$fields->replaceField( |
146
|
|
|
'CommsErrors', |
147
|
|
|
ReadonlyField::create( |
148
|
|
|
'CommsErrors', |
149
|
|
|
'Communication Errors' |
150
|
|
|
)->setRightTitle('Separated by three ||| symbols.') |
151
|
|
|
); |
152
|
|
|
$fields->addFieldToTab( |
153
|
|
|
'Root.Advanced', |
154
|
|
|
LiteralField::create( |
155
|
|
|
'WhatWeSend', |
156
|
|
|
' |
157
|
|
|
<h2>What is Send</h2>, |
158
|
|
|
<pre>'.print_r($this->prepareFieldsToSendToSalesForce(), 1).'</pre> |
159
|
|
|
' |
160
|
|
|
) |
161
|
|
|
); |
162
|
|
|
return $fields; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
|