BetterValueHumanReadable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
 * It is NOT for user specific ones, but for generic fields.
7
 *
8
 * This class knows what format salesforce expects.
9
 */
10
class SalesforceDefaultContactField extends DataObject
11
{
12
    /**
13
     * contact fields that should be created by default...
14
     * @var array
15
     */
16
    private static $defaults_records = [];
17
18
    /**
19
     * Singular name for CMS
20
     * @var string
21
     */
22
    private static $singular_name = 'Default Contact Field';
23
24
    /**
25
     * Plural name for CMS
26
     * @var string
27
     */
28
    private static $plural_name = 'Default Contact Fields';
29
30
    /**
31
     * @var array
32
     */
33
    private static $db = [
34
        'Key' => 'Varchar',
35
        'Value' => 'Varchar',
36
        'ValueType' => 'Enum("String,Number,YesOrTrue,NoOrFalse,CurrentDate,CurrentDateAndTime", "String")',
37
    ];
38
39
    /**
40
     * Defines summary fields commonly used in table columns
41
     * as a quick overview of the data for this dataobject
42
     * @var array
43
     */
44
    private static $summary_fields = [
45
        'Key' => 'Field Name',
46
        'Value' => 'Field Value',
47
        'ValueType' => 'Value Type',
48
        'BetterValue' => 'Calculated Value',
49
    ];
50
51
    /**
52
     * Defines summary fields commonly used in table columns
53
     * as a quick overview of the data for this dataobject
54
     * @var array
55
     */
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
     * @return string
69
     */
70
    public function getTitle()
71
    {
72
        return $this->Key . ' = ' . $this->BetterValueHumanReadable() . ' (' . $this->ValueType . ')';
73
    }
74
75
    public function requireDefaultRecords()
76
    {
77
        foreach ($this->Config()->get('default_records') as $key => $details) {
78
            $filter = [
79
                'Key' => $key,
80
            ];
81
82
            $obj = self::get()->filter($filter)->first();
83
            if (! $obj) {
84
                $obj = self::create($filter);
85
                // get values
86
                $value = isset($details['Value']) ? $details['Value'] : 'please set';
87
                $type = isset($details['ValueType']) ? $details['ValueType'] : 'String';
88
                // set values
89
                $obj->Value = $value;
90
                $obj->ValueType = $type;
91
                $obj->write();
92
            }
93
        }
94
    }
95
96
    /**
97
     * @return mixed
98
     */
99
    public function BetterValue()
100
    {
101
        switch ($this->ValueType) {
102
            case 'Number':
103
                return floatval($this->Value);
104
            case 'CurrentDate':
105
                return Date('Y-m-d');
106
            case 'CurrentDateAndTime':
107
                return Date('Y-m-d h:i:s');
108
            case 'YesOrTrue':
109
                return 'true';
110
            case 'NoOrFalse':
111
                return 'false';
112
            default:
113
                return trim($this->Value);
114
        }
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120
    public function BetterValueHumanReadable()
121
    {
122
        return $this->BetterValue();
123
    }
124
125
    /**
126
     * CMS Fields
127
     * @return FieldList
128
     */
129
    public function getCMSFields()
130
    {
131
        $fields = parent::getCMSFields();
132
        $fields->addFieldsToTab(
133
            'Root.Main',
134
            [
135
                TextField::create(
136
                    'Value',
137
                    'Value'
138
                ),
139
                DropdownField::create(
140
                    'ValueType',
141
                    'Type / Predefined Value',
142
                    $this->dbObject('ValueType')->enumValues()
143
                ),
144
                ReadonlyField::create(
145
                    'BetterValueHumanReadableNice',
146
                    'Calculated Value',
147
                    $this->BetterValueHumanReadable()
148
                ),
149
            ]
150
        );
151
        switch ($this->ValueType) {
152
            case 'CurrentDate':
153
            case 'CurrentDateAndTime':
154
            case 'YesOrTrue':
155
            case 'NoOrFalse':
156
                $fields->removeFieldFromTab(
157
                    'Root.Main',
158
                    'Value'
159
                );
160
                // no break
161
            case 'Number':
162
                $fields->removeFieldFromTab(
163
                    'Root.Main',
164
                    NumericField::create('Value', 'Value')
165
                );
166
        }
167
        return $fields;
168
    }
169
}
170