|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
*@author: nicolaas[at]sunnysideup.co.nz |
|
5
|
|
|
*@description: individual staff profile |
|
6
|
|
|
* |
|
7
|
|
|
**/ |
|
8
|
|
|
|
|
9
|
|
|
class StaffProfile extends DataObject |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
private static $db = array( |
|
|
|
|
|
|
12
|
|
|
"Name" => "Varchar(255)", |
|
13
|
|
|
"Position" => "Varchar(255)", |
|
14
|
|
|
"Description" => "Text", |
|
15
|
|
|
"Email" => "Varchar(255)", |
|
16
|
|
|
"SubjectLine" => "Varchar(255)", |
|
17
|
|
|
"Sort" => "Int" |
|
18
|
|
|
); |
|
19
|
|
|
|
|
20
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
21
|
|
|
"ProfilePicture" => "Image", |
|
22
|
|
|
"Parent" => "StaffProfilesPage" |
|
23
|
|
|
); |
|
24
|
|
|
|
|
25
|
|
|
//database related settings |
|
26
|
|
|
private static $indexes = array( |
|
|
|
|
|
|
27
|
|
|
"Sort" => true |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
//formatting |
|
31
|
|
|
private static $searchable_fields = array("Name" => "PartialMatchFilter"); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
private static $field_labels = array( |
|
|
|
|
|
|
34
|
|
|
"SortNumber" => "Sort Index Number for Sorting (lower numbers first)", |
|
35
|
|
|
"Subjectline" => "Optional Subject Line" |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
private static $summary_fields = array("Name" => "Name", "Email" => "Email", "Title" => "Title"); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
private static $singular_name = "Staff Profile"; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
private static $plural_name = "Staff Profiles"; |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
private static $default_sort = "Sort ASC, Name ASC"; |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
private static $defaults = array( |
|
|
|
|
|
|
47
|
|
|
"Sort" => 100 |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* replacement placeholders |
|
52
|
|
|
* [xxx] => yyy |
|
53
|
|
|
* where xxx is the string the CMS user types |
|
54
|
|
|
* and yyy the replacement field / relation. |
|
55
|
|
|
* |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
private static $subject_place_holders = array( |
|
|
|
|
|
|
59
|
|
|
"Name" => "Name", |
|
60
|
|
|
"Email" => "Email", |
|
61
|
|
|
"Position" => "Position", |
|
62
|
|
|
"PageTitle" => "Parent.Title", |
|
63
|
|
|
"PageLink" => "Parent.Link" |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
public function populateDefaults() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->Sort = 100; |
|
|
|
|
|
|
69
|
|
|
parent::populateDefaults(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getCMSFields() |
|
73
|
|
|
{ |
|
74
|
|
|
$fields = parent::getCMSFields(); |
|
75
|
|
|
$field = $fields->dataFieldByName("SubjectLine"); |
|
76
|
|
|
$field->setRightTitle( |
|
77
|
|
|
_t("StaffProfile.PLACEHOLDER_EXPLANATION", "you can use the following placeholders") |
|
78
|
|
|
. ": [" |
|
79
|
|
|
. implode("], [", array_keys($this->Config()->get("subject_place_holders"))) |
|
80
|
|
|
. "]" |
|
81
|
|
|
); |
|
82
|
|
|
if (class_exists("DataObjectSorterController") && $this->hasExtension("DataObjectSorterController")) { |
|
83
|
|
|
$fields->addFieldToTab("Root.Sort", new LiteralField("InvitationToSort", $this->dataObjectSorterPopupLink())); |
|
|
|
|
|
|
84
|
|
|
$fields->removeFieldFromTab("Root.Main", "Sort"); |
|
85
|
|
|
} |
|
86
|
|
|
return $fields; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function fieldLabels($includeRelations = true) |
|
90
|
|
|
{ |
|
91
|
|
|
$labels = parent::fieldLabels($includeRelations); |
|
92
|
|
|
return $labels; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Obscure all email links in StringField. |
|
97
|
|
|
* Matches mailto:[email protected] as well as [email protected] |
|
98
|
|
|
* |
|
99
|
|
|
* @return string | Null |
|
100
|
|
|
*/ |
|
101
|
|
|
public function EncodedEmailLink() |
|
102
|
|
|
{ |
|
103
|
|
|
if ($email = $this->getBestEmail()) { |
|
104
|
|
|
$obj = $this->retrieveEmailObject(); |
|
105
|
|
|
if ($obj) { |
|
106
|
|
|
return $obj->MailTo; |
|
107
|
|
|
} else { |
|
108
|
|
|
return "mailto:".$email; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Obscure all email links in StringField. |
|
115
|
|
|
* Matches mailto:[email protected] as well as [email protected] |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function EncodedEmailText() |
|
120
|
|
|
{ |
|
121
|
|
|
if ($email = $this->getBestEmail()) { |
|
122
|
|
|
$obj = $this->retrieveEmailObject(); |
|
123
|
|
|
if ($obj) { |
|
124
|
|
|
return $obj->Text; |
|
125
|
|
|
} else { |
|
126
|
|
|
return $email; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function onBeforeWrite() |
|
132
|
|
|
{ |
|
133
|
|
|
parent::onBeforeWrite(); |
|
134
|
|
|
if (!$this->Sort) { |
|
|
|
|
|
|
135
|
|
|
$this->Sort = 100; |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
if (!$this->ParentID) { |
|
|
|
|
|
|
138
|
|
|
$page = StaffProfilesPage::get()->First(); |
|
139
|
|
|
$this->ParentID = $page->ID; |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* puts together a subject line with replacements |
|
145
|
|
|
* |
|
146
|
|
|
* @return String |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function SubjectLineCreator() |
|
149
|
|
|
{ |
|
150
|
|
|
if ($this->SubjectLine) { |
|
|
|
|
|
|
151
|
|
|
$str = $this->SubjectLine; |
|
|
|
|
|
|
152
|
|
|
} else { |
|
153
|
|
|
$str = $this->Parent()->SubjectLine; |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
if (!$str) { |
|
156
|
|
|
$str = "Enquiry from [PageLink] for [Name]"; |
|
157
|
|
|
} |
|
158
|
|
|
$replace = $this->Config()->get("subject_place_holders"); |
|
159
|
|
|
foreach ($replace as $findKey => $replaceField) { |
|
160
|
|
|
if (strpos($str, $findKey) !== null) { |
|
161
|
|
|
if (strpos($replaceField, ".")) { |
|
162
|
|
|
$replaceFieldParts = explode(".", $replaceField); |
|
163
|
|
|
$method1 = $replaceFieldParts[0]; |
|
164
|
|
|
$method2 = $replaceFieldParts[1]; |
|
165
|
|
|
$relationalObject = $this->$method1(); |
|
166
|
|
|
if ($relationalObject) { |
|
167
|
|
|
if ($relationalObject->hasMethod($method2)) { |
|
168
|
|
|
$replaceValue = $relationalObject->$method2(); |
|
169
|
|
|
} elseif ($relationalObject->hasMethod("get".$method2)) { |
|
170
|
|
|
$method2 = "get".$method2; |
|
171
|
|
|
$replaceValue = $relationalObject->$method2(); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} else { |
|
175
|
|
|
$replaceValue = $this->$replaceField; |
|
176
|
|
|
} |
|
177
|
|
|
$str = str_ireplace("[".$findKey."]", $replaceValue, $str); |
|
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
return $str; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @var EmailObject |
|
186
|
|
|
*/ |
|
187
|
|
|
protected $emailObject = null; |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* |
|
191
|
|
|
* @return EmailObject | NULL |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function retrieveEmailObject() |
|
194
|
|
|
{ |
|
195
|
|
|
if (!$this->emailObject) { |
|
196
|
|
|
if (class_exists("HideMailto")) { |
|
197
|
|
|
if ($email = $this->getBestEmail()) { |
|
198
|
|
|
$this->emailObject = HideMailto::convert_email($email, $this->SubjectLineCreator()); |
|
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
} else { |
|
201
|
|
|
user_error("This module requires Sunnysideup/hidemailto, but it can function without it", E_USER_NOTICE); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
return $this->emailObject; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* finds the best email available. |
|
209
|
|
|
* |
|
210
|
|
|
* @return String |
|
211
|
|
|
*/ |
|
212
|
|
|
protected function getBestEmail() |
|
213
|
|
|
{ |
|
214
|
|
|
if ($this->Email) { |
|
|
|
|
|
|
215
|
|
|
$email = $this->Email; |
|
|
|
|
|
|
216
|
|
|
} else { |
|
217
|
|
|
$email = $this->Parent()->DefaultEmail; |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
return $email; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
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.