SalesforceContactLog::confirmContactLog()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 23
nc 24
nop 2
dl 0
loc 30
rs 8.9297
c 0
b 0
f 0
1
<?php
2
3
4
class SalesforceContactLog extends DataObject
5
{
6
    /**
7
     * Singular name for CMS
8
     * @var string
9
     */
10
    private static $singular_name = 'Sales Force Contact Log';
11
12
    /**
13
     * Plural name for CMS
14
     * @var string
15
     */
16
    private static $plural_name = 'Sales Force Contact Log Entries';
17
18
    /**
19
     * SS4 prep!
20
     * @var string
21
     */
22
    private static $table_name = 'SalesforceContactLog';
23
24
    private static $db = [
25
        'SalesforceIdentifier' => 'Varchar(40)',
26
        'Type' => 'Enum("Created,Updated")',
27
        'Email' => 'Varchar',
28
        'Phone' => 'Varchar',
29
        'FirstName' => 'Varchar',
30
        'LastName' => 'Varchar',
31
        'Executed' => 'Boolean',
32
        'FieldsSent' => 'Text',
33
        'Filters' => 'Text',
34
        'HasError' => 'Boolean',
35
        'Errors' => 'Text',
36
    ];
37
38
    private static $default_sort = [
39
        'ID' => 'DESC',
40
    ];
41
42
    /**
43
     * Defines a default list of filters for the search context
44
     * @var array
45
     */
46
    private static $searchable_fields = [
47
        'Type' => 'ExactMatchFilter',
48
        'Executed' => 'ExactMatchFilter',
49
        'HasError' => 'ExactMatchFilter',
50
        'SalesforceIdentifier' => 'PartialMatchField',
51
        'Email' => 'PartialMatchField',
52
        'Phone' => 'PartialMatchField',
53
        'FirstName' => 'PartialMatchField',
54
        'LastName' => 'PartialMatchField',
55
        'FieldsSent' => 'PartialMatchField',
56
        'Filters' => 'PartialMatchField',
57
        'Errors' => 'PartialMatchField',
58
    ];
59
60
    /**
61
     * Defines summary fields commonly used in table columns
62
     * as a quick overview of the data for this dataobject
63
     * @var array
64
     */
65
    private static $summary_fields = [
66
        'Created.Nice' => 'When',
67
        'SalesforceIdentifier' => 'Code',
68
        'Type' => 'Type',
69
        'Email' => 'Email',
70
        'Phone' => 'Phone',
71
        'FirstName' => 'First',
72
        'LastName' => 'Last',
73
        'Executed.Nice' => 'Executed',
74
        'HasError.Nice' => 'Errors',
75
    ];
76
77
    private static $indexes = [
78
        'SalesforceIdentifier' => true,
79
        'Email' => 'Email',
80
        'Phone' => 'Phone',
81
    ];
82
83
    /**
84
     * create contact log
85
     * @param  string $type
86
     * @param  array $fieldsSent
87
     * @param  array $filters
88
     *
89
     * @return SalesforceContactLog|null
90
     */
91
    public static function create_contact_log($type, array $fieldsSent, array $filters)
92
    {
93
        //basic fields
94
        $email = isset($fieldsSent['Email']) ? $fieldsSent['Email'] : '';
95
        $phone = isset($fieldsSent['Phone']) ? $fieldsSent['Phone'] : '';
96
        $firstName = isset($fieldsSent['FirstName']) ? $fieldsSent['FirstName'] : '';
97
        $lastName = isset($fieldsSent['LastName']) ? $fieldsSent['LastName'] : '';
98
99
        //serialize
100
        $fieldsSent = serialize($fieldsSent);
101
        $filters = serialize($filters);
102
        $obj = self::create(
103
            [
104
                'Type' => $type,
105
                'FieldsSent' => $fieldsSent,
106
                'Filters' => $filters,
107
                'Email' => $email,
108
                'Phone' => $phone,
109
                'FirstName' => $firstName,
110
                'LastName' => $lastName,
111
            ]
112
        );
113
        $id = $obj->write();
114
115
        return self::get()->byID($id);
116
    }
117
118
    /**
119
     * returns true if the contact update was successful
120
     * @param  mixed $response
121
     *
122
     * @return bool
123
     */
124
    public function confirmContactLog($response, $error = '')
125
    {
126
        $id = '';
127
        $errors = '';
128
        $hasError = false;
129
        if (is_array($response)) {
130
            $response = $response[0];
131
        }
132
        if ($error) {
133
            $hasError = true;
134
            $errors = $error;
135
        }
136
        if ($response instanceof \SForce\Wsdl\SaveResult) {
137
            $id = $response->getId();
138
            if ($response->getSuccess()) {
139
                $hasError = false;
140
            } else {
141
                $errors = serialize($response->getErrors());
142
                $hasError = true;
143
            }
144
        } else {
145
            $errors .= '||| Unexpected response: ' . serialize($response);
146
        }
147
        $this->SalesforceIdentifier = $id;
148
        $this->Errors = $errors;
149
        $this->HasError = $hasError;
150
        $this->Executed = true;
151
        $this->write();
152
153
        return $this->HasError ? false : true;
154
    }
155
156
    /**
157
     * CMS Fields
158
     * @return FieldList
159
     */
160
    public function getCMSFields()
161
    {
162
        $fields = parent::getCMSFields();
163
        $fields->removeFieldsFromTab(
164
            'Root.Main',
165
            [
166
                'FieldsSent',
167
                'Filters',
168
                'HasError',
169
                'Executed',
170
            ]
171
        );
172
        $fields->addFieldsToTab(
173
            'Root.Main',
174
            [
175
                ReadonlyField::create(
176
                    'Created',
177
                    'Created',
178
                    DBField::create_field('SS_DateTime', $this->Created)->Nice()
179
                ),
180
                ReadonlyField::create(
181
                    'Type',
182
                    'Type'
183
                ),
184
                ReadonlyField::create(
185
                    'SalesforceIdentifier',
186
                    'Sales Force Identifier'
187
                ),
188
            ]
189
        );
190
        $fields->addFieldsToTab(
191
            'Root.Debug',
192
            [
193
                LiteralField::create(
194
                    'FieldsSentNice',
195
                    '<h2>Fields Sent</h2>' . $this->serializedToHTML($this->FieldsSent)
196
                ),
197
                LiteralField::create(
198
                    'FiltersNice',
199
                    '<h2>Filters</h2>' . $this->serializedToHTML($this->Filters)
200
                ),
201
                ReadonlyField::create(
202
                    'ExecutedNice',
203
                    'Executed',
204
                    DBField::create_field('Boolean', $this->Executed)->Nice()
205
                ),
206
                ReadonlyField::create(
207
                    'HasErrorNice',
208
                    'HasError',
209
                    DBField::create_field('Boolean', $this->HasError)->Nice()
210
                ),
211
                ReadonlyField::create(
212
                    'Errors',
213
                    'Communication Errors'
214
                )->setRightTitle('Separated by three ||| symbols.'),
215
            ]
216
        );
217
218
        return $fields;
219
    }
220
221
    /**
222
     * DataObject create permissions
223
     * @param Member $member
224
     * @return bool
225
     */
226
    public function canCreate($member = null)
227
    {
228
        return false;
229
    }
230
231
    /**
232
     * DataObject edit permissions
233
     * @param Member $member
234
     * @return bool
235
     */
236
    public function canEdit($member = null)
237
    {
238
        return false;
239
    }
240
241
    /**
242
     * DataObject delete permissions
243
     * @param Member $member
244
     * @return bool
245
     */
246
    public function canDelete($member = null)
247
    {
248
        return false;
249
    }
250
251
    /**
252
     * @param  string $serializedString (serialised data)
253
     * @return string (html)
254
     */
255
    protected function serializedToHTML($serializedString)
256
    {
257
        $s = unserialize($serializedString);
258
259
        return '<pre>' . print_r($s, 1) . '</pre>';
260
    }
261
}
262