|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use SilverStripe\Forms\DropdownField; |
|
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* this can be linked to pages / other objects using many_many relationships |
|
6
|
|
|
* so that you can send default record values to Salesforce. |
|
7
|
|
|
* It is NOT for user specific ones, but for generic fields. |
|
8
|
|
|
* |
|
9
|
|
|
* This class knows what format salesforce expects. |
|
10
|
|
|
*/ |
|
11
|
|
|
class SalesforceDefaultContactField extends DataObject |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* contact fields that should be created by default... |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private static $defaults_records = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Singular name for CMS |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $singular_name = 'Default Contact Field'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Plural name for CMS |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $plural_name = 'Default Contact Fields'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private static $db = [ |
|
37
|
|
|
'Key' => 'Varchar', |
|
38
|
|
|
'Value' => 'Varchar', |
|
39
|
|
|
'ValueType' => 'Enum("String,Number,YesOrTrue,NoOrFalse,CurrentDate,CurrentDateAndTime", "String")', |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Defines summary fields commonly used in table columns |
|
44
|
|
|
* as a quick overview of the data for this dataobject |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
private static $summary_fields = [ |
|
48
|
|
|
'Key' => 'Field Name', |
|
49
|
|
|
'Value' => 'Field Value', |
|
50
|
|
|
'ValueType' => 'Value Type', |
|
51
|
|
|
]; |
|
52
|
|
|
/** |
|
53
|
|
|
* Defines summary fields commonly used in table columns |
|
54
|
|
|
* as a quick overview of the data for this dataobject |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
/** |
|
58
|
|
|
* Defines a default list of filters for the search context |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
private static $searchable_fields = [ |
|
62
|
|
|
'Key', |
|
63
|
|
|
'Value', |
|
64
|
|
|
'ValueType', |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getTitle() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->Key . ' = '.$this->BetterValueHumanReadable() . ' ('.$this->ValueType.')'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
public function requireDefaultRecords() |
|
78
|
|
|
{ |
|
79
|
|
|
foreach($this->Config()->get('default_records') as $key => $value) { |
|
80
|
|
|
$filter = [ |
|
81
|
|
|
'Key' => $key |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
$obj = SalesforceDefaultContactField::get()->filter($filter)->first(); |
|
85
|
|
|
if(! $obj) { |
|
86
|
|
|
$obj = SalesforceDefaultContactField::create($filter); |
|
87
|
|
|
$obj->Value = $value; |
|
88
|
|
|
$obj->write(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
*/ |
|
97
|
|
|
public function BetterValue() |
|
98
|
|
|
{ |
|
99
|
|
|
switch($this->ValueType) { |
|
100
|
|
|
case 'Number': |
|
101
|
|
|
return floatval($this->Value); |
|
102
|
|
|
case 'CurrentDate': |
|
103
|
|
|
return Date('Y-m-d'); |
|
104
|
|
|
case 'CurrentDateAndTime': |
|
105
|
|
|
return Date('Y-m-d h:i:s'); |
|
106
|
|
|
case 'YesOrTrue': |
|
107
|
|
|
return 'true'; |
|
108
|
|
|
case 'NoOrFalse': |
|
109
|
|
|
return 'false'; |
|
110
|
|
|
default: |
|
111
|
|
|
return trim($this->Value); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
|
|
public function BetterValueHumanReadable() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->BetterValue(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* CMS Fields |
|
125
|
|
|
* @return FieldList |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getCMSFields() |
|
128
|
|
|
{ |
|
129
|
|
|
$fields = parent::getCMSFields(); |
|
130
|
|
|
$fields->addFieldsToTab( |
|
131
|
|
|
'Root.Main', |
|
132
|
|
|
[ |
|
133
|
|
|
TextField::create( |
|
134
|
|
|
'Value', |
|
135
|
|
|
'Value' |
|
136
|
|
|
), |
|
137
|
|
|
DropdownField::create( |
|
138
|
|
|
'ValueType', |
|
139
|
|
|
'Type / Predefined Value', |
|
140
|
|
|
$this->dbObject('ValueType')->enumValues() |
|
141
|
|
|
), |
|
142
|
|
|
ReadonlyField::create( |
|
143
|
|
|
'BetterValueHumanReadableNice', |
|
144
|
|
|
'Calculated Value', |
|
145
|
|
|
$this->BetterValueHumanReadable() |
|
146
|
|
|
), |
|
147
|
|
|
] |
|
148
|
|
|
); |
|
149
|
|
|
switch($this->ValueType) { |
|
150
|
|
|
case 'CurrentDate': |
|
151
|
|
|
case 'CurrentDateAndTime': |
|
152
|
|
|
case 'YesOrTrue': |
|
153
|
|
|
case 'NoOrFalse': |
|
154
|
|
|
return $fields->removeFieldFromTab( |
|
|
|
|
|
|
155
|
|
|
'Root.Main', |
|
156
|
|
|
'Value' |
|
157
|
|
|
); |
|
158
|
|
|
break; |
|
|
|
|
|
|
159
|
|
|
case 'Number': |
|
160
|
|
|
return $fields->removeFieldFromTab( |
|
|
|
|
|
|
161
|
|
|
'Root.Main', |
|
162
|
|
|
NumericField::create('Value', 'Value') |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
return $fields; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: