Passed
Push — master ( ef5075...1d5d39 )
by Nicolaas
02:11
created

SalesforceContactLog::confirmContactLog()   B

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