1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* this can be linked to pages / other objects using many_many relationships |
5
|
|
|
* so that you can send default record values to Salesforce |
6
|
|
|
*/ |
7
|
|
|
class SalesforceDefaultContactField extends DataObject |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* contact fields that should be created by default... |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private static $defaults_records = []; |
|
|
|
|
15
|
|
|
|
16
|
|
|
private static $site_wide_fields_to_send = []; |
|
|
|
|
17
|
|
|
|
18
|
|
|
private static $site_wide_filter_values = []; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* @param array|DataList|null $mixed fields to send |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public static function get_fields_to_send($mixed = null) |
27
|
|
|
{ |
28
|
|
|
$array = self::mixed_to_array($mixed); |
29
|
|
|
|
30
|
|
|
return array_merge( |
31
|
|
|
Config::inst()->get('SalesforceDefaultContactField', 'site_wide_fields_to_send'), |
|
|
|
|
32
|
|
|
$array |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @param array|DataList|null $mixed fields to send |
39
|
|
|
* |
40
|
|
|
* @return array|DataList|null |
41
|
|
|
*/ |
42
|
|
|
public static function get_fields_for_filter($mixed = null) |
43
|
|
|
{ |
44
|
|
|
$array = self::mixed_to_array($mixed); |
45
|
|
|
|
46
|
|
|
return array_merge( |
47
|
|
|
Config::inst()->get('SalesforceDefaultContactField', 'site_wide_filter_values'), |
|
|
|
|
48
|
|
|
$array |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Singular name for CMS |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private static $singular_name = 'Default Contact Field'; |
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Plural name for CMS |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private static $plural_name = 'Default Contact Fields'; |
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
private static $db = [ |
|
|
|
|
69
|
|
|
'Key' => 'Varchar', |
70
|
|
|
'Value' => 'Varchar', |
71
|
|
|
'IsFilter' => 'Boolean', |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Defines summary fields commonly used in table columns |
76
|
|
|
* as a quick overview of the data for this dataobject |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
private static $summary_fields = [ |
|
|
|
|
80
|
|
|
'Key' => 'Field Name', |
81
|
|
|
'Value' => 'Field Value', |
82
|
|
|
'IsFilter.Nice' => 'Is Filter', |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getTitle() |
90
|
|
|
{ |
91
|
|
|
return $this->Key . ' = '.$this->Value; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
public function requireDefaultRecords() |
96
|
|
|
{ |
97
|
|
|
foreach($this->Config()->get('default_records') as $key => $value) { |
98
|
|
|
$filter = [ |
99
|
|
|
'Key' => $key |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
$obj = SalesforceDefaultContactField::get()->filter($filter)->first(); |
103
|
|
|
if(! $obj) { |
104
|
|
|
$obj = SalesforceDefaultContactField::create($filter); |
105
|
|
|
$obj->Value = $value; |
106
|
|
|
$obj->write(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
|
|
public function BetterValue() |
116
|
|
|
{ |
117
|
|
|
if(strtolower($this->Value) === 'true') { |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
if(strtolower($this->Value) === 'false') { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
if(floatval($this->Value)) { |
124
|
|
|
return floatval($this->Value); |
125
|
|
|
} |
126
|
|
|
if(intval($this->Value)) { |
127
|
|
|
return intval($this->Value); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return trim($this->Value); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* |
136
|
|
|
* @param DataList|array|null $mixed |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
protected static function mixed_to_array($mixed = null) : array |
140
|
|
|
{ |
141
|
|
|
if($mixed === null) { |
142
|
|
|
$array = []; |
143
|
|
|
} |
144
|
|
|
elseif($mixed instanceof DataList) { |
145
|
|
|
$array = []; |
146
|
|
|
foreach($mixed as $object) { |
147
|
|
|
$array[trim($object->Key)] = $object->BetterValue(); |
148
|
|
|
} |
149
|
|
|
} elseif(is_array($mixed)) { |
|
|
|
|
150
|
|
|
$array = $mixed; |
151
|
|
|
} else { |
152
|
|
|
user_error('Variable '.vardump($mixed).'Should be an array'); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $array; |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
|
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|