|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author nicolaas [at] sunnysideup.co.nz |
|
5
|
|
|
* |
|
6
|
|
|
* @description: this represents a sub group of a list, otherwise known as a segment |
|
7
|
|
|
* |
|
8
|
|
|
**/ |
|
9
|
|
|
|
|
10
|
|
|
class CampaignMonitorCustomField extends DataObject |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
private static $db = array( |
|
|
|
|
|
|
13
|
|
|
"Code" => "Varchar(64)", |
|
14
|
|
|
"Title" => "Varchar(64)", |
|
15
|
|
|
"Type" => "Varchar(32)", |
|
16
|
|
|
"Options" => "Text", |
|
17
|
|
|
"Visible" => "Boolean", |
|
18
|
|
|
"ListID" => "Varchar(32)" |
|
19
|
|
|
); |
|
20
|
|
|
|
|
21
|
|
|
private static $casting = array( |
|
|
|
|
|
|
22
|
|
|
"Key" => "Varchar" |
|
23
|
|
|
); |
|
24
|
|
|
|
|
25
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
26
|
|
|
"Title" => "Title", |
|
27
|
|
|
"Visible.Nice" => "Visible" |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
private static $indexes = array( |
|
|
|
|
|
|
31
|
|
|
"ListID" => true, |
|
32
|
|
|
"Code" => true |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
36
|
|
|
"CampaignMonitorSignupPage" => "CampaignMonitorSignupPage" |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
private static $default_sort = array( |
|
|
|
|
|
|
40
|
|
|
"Visible" => "DESC" |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* form field matcher between CM and SS CMField => SSField |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
private static $field_translator = array( |
|
|
|
|
|
|
48
|
|
|
"MultiSelectOne" => "OptionSetField", |
|
49
|
|
|
"Text" => "TextField", |
|
50
|
|
|
"Number" => "NumericField", |
|
51
|
|
|
"MultiSelectMany" => "CheckboxSetField", |
|
52
|
|
|
"Date" => "DateField" |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
public function canCreate($member = null) |
|
57
|
|
|
{ |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function canDelete($member = null) |
|
62
|
|
|
{ |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function canEdit($member = null) |
|
67
|
|
|
{ |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function onBeforeWrite() |
|
72
|
|
|
{ |
|
73
|
|
|
parent::onBeforeWrite(); |
|
74
|
|
|
$this->Code = self::key_to_code($this->Code); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getKey() |
|
78
|
|
|
{ |
|
79
|
|
|
return "[".$this->Code."]"; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getOptionsAsArray() |
|
83
|
|
|
{ |
|
84
|
|
|
return array("" => _t("CampaignMonitor.PLEASE_SELECT", "-- please select --"))+explode(",", $this->Options); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* |
|
89
|
|
|
* @return CampaignMonitorCustomField |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function create_from_campaign_monitor_object($customFieldsObject, $listID) |
|
92
|
|
|
{ |
|
93
|
|
|
$filterOptions = array( |
|
94
|
|
|
"ListID" => $listID, |
|
95
|
|
|
"Code" => self::key_to_code($customFieldsObject->Key) |
|
96
|
|
|
); |
|
97
|
|
|
$obj = CampaignMonitorCustomField::get()->filter($filterOptions)->first(); |
|
98
|
|
|
if (!$obj) { |
|
99
|
|
|
$obj = CampaignMonitorCustomField::create($filterOptions); |
|
100
|
|
|
} |
|
101
|
|
|
$page = CampaignMonitorSignupPage::get()->filter(array("ListID" => $listID))->first(); |
|
102
|
|
|
if ($page) { |
|
103
|
|
|
$obj->CampaignMonitorSignupPageID = $page->ID; |
|
104
|
|
|
} |
|
105
|
|
|
$obj->ListID = $listID; |
|
106
|
|
|
$obj->Code = self::key_to_code($customFieldsObject->Key); |
|
107
|
|
|
$obj->Title = $customFieldsObject->FieldName; |
|
108
|
|
|
$obj->Type = $customFieldsObject->DataType; |
|
109
|
|
|
$obj->Options = implode(",", $customFieldsObject->FieldOptions); |
|
|
|
|
|
|
110
|
|
|
$obj->Visible = $customFieldsObject->VisibleInPreferenceCenter; |
|
111
|
|
|
$obj->write(); |
|
112
|
|
|
return $obj; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private static function key_to_code($key) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
return str_replace(array("[", "]"), "", $key); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @var array |
|
122
|
|
|
*/ |
|
123
|
|
|
private $_fieldTranslator = array(); |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $namePrefix |
|
127
|
|
|
* @param string $nameAppendix |
|
128
|
|
|
* @param string $title |
|
129
|
|
|
* @param null | array $options |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getFormField($namePrefix = "", $nameAppendix = "", $title = "", $options = null) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
//sort out names, title |
|
134
|
|
|
if (!$title) { |
|
135
|
|
|
$title = $this->Title; |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
$name = $namePrefix.$this->Code.$nameAppendix; |
|
|
|
|
|
|
138
|
|
|
//create field |
|
139
|
|
|
if (!count($this->_fieldTranslator)) { |
|
140
|
|
|
$this->_fieldTranslator = $this->Config()->get("field_translator"); |
|
141
|
|
|
} |
|
142
|
|
|
$fieldName = $this->_fieldTranslator[$this->Type]; |
|
|
|
|
|
|
143
|
|
|
$field = $fieldName::create($name, $title); |
|
144
|
|
|
//add options |
|
145
|
|
|
if (!$options && $this->Options) { |
|
|
|
|
|
|
146
|
|
|
$optionsArray = explode(",", $this->Options); |
|
|
|
|
|
|
147
|
|
|
$optionsArray = array_combine($optionsArray, $optionsArray); |
|
148
|
|
|
$field->setSource($optionsArray); |
|
149
|
|
|
} |
|
150
|
|
|
return $field; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.