|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class EmailAFriendForm extends Form |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @var String |
|
8
|
|
|
*/ |
|
9
|
|
|
private static $friend_email_address_label = "Friend#039;s Email Address"; |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var String |
|
13
|
|
|
*/ |
|
14
|
|
|
private static $message_label = "Message"; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var String |
|
18
|
|
|
*/ |
|
19
|
|
|
private static $your_email_address_label = "Your email address"; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var String |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $send_label = 'Send'; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* yes or no |
|
28
|
|
|
* @var String |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $mail_to_site_owner_only = 'no'; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param Controller $controller |
|
34
|
|
|
* @param String $name |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($controller, $name) |
|
37
|
|
|
{ |
|
38
|
|
|
Requirements::themedCSS("EmailReferral", "emailreferral"); |
|
39
|
|
|
Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js"); |
|
40
|
|
|
Requirements::javascript("emailreferral/javascript/EmailReferralForm.js"); |
|
41
|
|
|
|
|
42
|
|
|
$fields[] = new HiddenField('PageID', 'PageID', $controller->dataRecord->ID); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$fields[] = new EmailField('YourMailAddress', $this->Config()->get("your_email_address_label")); |
|
45
|
|
|
$fields[] = new TextareaField( |
|
46
|
|
|
'Message', |
|
47
|
|
|
$this->Config()->get("message_label"), |
|
48
|
|
|
$this->Config()->get("EmailAFriendExtension", "default_message") |
|
49
|
|
|
); |
|
50
|
|
|
if ($this->Config()->get("mail_to_site_owner_only") != 'yes') { |
|
51
|
|
|
$fields[] = new LiteralField('AdditionalMessage', '<div id="additionalMessageStuff"><p>'.Director::absoluteURL($controller->Link()).'</p><p>Sent by: <span id="emailReplacer">[your email address]</span></p></div>'); |
|
52
|
|
|
$fields[] = new EmailField('To', $this->Config()->get("friend_email_address_label")); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$fields = new FieldList($fields); |
|
56
|
|
|
|
|
57
|
|
|
$actions = new FieldList(new FormAction('sendemailafriend', $this->Config()->get("send_label"))); |
|
58
|
|
|
if ($this->Config()->get("mail_to_site_owner_only") != 'yes') { |
|
59
|
|
|
$requiredFields = new RequiredFields(array('YourMailAddress', 'To', 'Message')); |
|
60
|
|
|
} else { |
|
61
|
|
|
$requiredFields = new RequiredFields(array('YourMailAddress', 'Message')); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
parent::__construct($controller, $name, $fields, $actions, $requiredFields); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* |
|
69
|
|
|
* @param Array |
|
70
|
|
|
* @param EmailAFriendForm |
|
71
|
|
|
*/ |
|
72
|
|
|
public function sendemailafriend($RAW_data, $form) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$adminEmail = Config::inst()->get("Email", "admin_email"); |
|
75
|
|
|
$data = Convert::raw2sql($RAW_data); |
|
76
|
|
|
if ($page = Page::get()->byID(intval($data['PageID']))) { |
|
77
|
|
|
$pageLink = $page->AbsoluteLink(); |
|
78
|
|
|
} |
|
79
|
|
|
$toList = array(); |
|
80
|
|
|
if ($this->Config()->get("mail_to_site_owner_only") != 'yes') { |
|
81
|
|
|
$tos = explode(',', $data['To']); |
|
82
|
|
|
foreach ($tos as $to) { |
|
83
|
|
|
$toList = array_merge($toList, explode(';', $to)); |
|
84
|
|
|
} |
|
85
|
|
|
} else { |
|
86
|
|
|
$toList[] = $adminEmail; |
|
87
|
|
|
} |
|
88
|
|
|
if ($data['YourMailAddress']) { |
|
89
|
|
|
$toList[] = $data['YourMailAddress']; |
|
90
|
|
|
} |
|
91
|
|
|
$ip = EmailAFriendExtension::get_ip_user(); |
|
92
|
|
|
$count = 0; |
|
93
|
|
|
if ($this->Config()->get("EmailAFriendExtension", "max_message_phour_pip")) { |
|
94
|
|
|
$anHourAgo = date('Y-m-d H:i:s', mktime(date('G') - 1, date('i'), date('s'), date('n'), date('j'), date('Y'))); |
|
95
|
|
|
$count = FriendEmail::get()->filter( |
|
96
|
|
|
array( |
|
97
|
|
|
"IPAddress" => $ip, |
|
98
|
|
|
"Created:GreaterThan" => $anHourAgo |
|
99
|
|
|
) |
|
100
|
|
|
)->count(); |
|
101
|
|
|
} |
|
102
|
|
|
if ($this->Config()->get("mail_to_site_owner_only") != 'yes') { |
|
103
|
|
|
$mailFrom = $data['YourMailAddress']; |
|
104
|
|
|
} else { |
|
105
|
|
|
if ($this->Config()->get("EmailAFriendExtension", "sender_name")) { |
|
106
|
|
|
$mailFrom = $this->Config()->get("EmailAFriendExtension", "sender_name"); |
|
107
|
|
|
if ($this->Config()->get("EmailAFriendRole", "sender_email_address")) { |
|
108
|
|
|
$mailFrom .= ' <' . $this->Config()->get("EmailAFriendExtension", "sender_email_address") . '>'; |
|
109
|
|
|
} |
|
110
|
|
|
} elseif ($this->Config()->get("EmailAFriendExtension", "sender_email_address")) { |
|
111
|
|
|
$mailFrom = $this->Config()->get("EmailAFriendExtension", "sender_email_address"); |
|
112
|
|
|
} else { |
|
113
|
|
|
$mailFrom = $adminEmail; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
foreach ($toList as $index => $to) { |
|
117
|
|
|
$messagesPerHour = $this->Config()->get("EmailAFriendExtension", "max_message_phour_pip"); |
|
118
|
|
|
if ($messagesPerHour && $count > $messagesPerHour) { |
|
119
|
|
|
$stopIndex = $index; |
|
120
|
|
|
break; |
|
121
|
|
|
} else { |
|
122
|
|
|
$friendEmail = new FriendEmail(); |
|
123
|
|
|
$friendEmail->To = $to; |
|
|
|
|
|
|
124
|
|
|
$friendEmail->Message = $data['Message']; |
|
|
|
|
|
|
125
|
|
|
$friendEmail->From = $data['YourMailAddress']; |
|
|
|
|
|
|
126
|
|
|
$friendEmail->IPAddress = $ip; |
|
|
|
|
|
|
127
|
|
|
$friendEmail->PageID = $data['PageID']; |
|
|
|
|
|
|
128
|
|
|
$friendEmail->write(); |
|
129
|
|
|
$subject = $this->Config()->get("EmailAFriendExtension", "mail_subject"); |
|
130
|
|
|
$subject .= ' | sent by '.$data['YourMailAddress']; |
|
131
|
|
|
$email = new Email( |
|
132
|
|
|
$mailFrom, |
|
133
|
|
|
$to, |
|
134
|
|
|
$subject, |
|
135
|
|
|
Convert::raw2xml($data['Message']) . '<br/><br/>Page Link : ' . $pageLink. '<br /><br />Sent by: '.$data['YourMailAddress'] |
|
|
|
|
|
|
136
|
|
|
); |
|
137
|
|
|
$outcome = $email->send(); |
|
138
|
|
|
if ($outcome) { |
|
|
|
|
|
|
139
|
|
|
$count++; |
|
140
|
|
|
} else { |
|
141
|
|
|
unset($toList[$index]); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
if (count($toList) > 0) { |
|
147
|
|
|
$content = ''; |
|
148
|
|
|
$endIndex = isset($stopIndex) ? $stopIndex : count($toList); |
|
149
|
|
|
if (! isset($stopIndex) || $stopIndex > 0) { |
|
150
|
|
|
$content .= '<p class="message good">This page has been successfully e-mailed to the following addresses :</p><ul>'; |
|
151
|
|
View Code Duplication |
for ($i = 0; $i < $endIndex; $i++) { |
|
|
|
|
|
|
152
|
|
|
$content .= '<li>' . $toList[$i] . '</li>'; |
|
153
|
|
|
} |
|
154
|
|
|
$content .= '</ul>'; |
|
155
|
|
|
} |
|
156
|
|
|
if ($endIndex < count($toList)) { |
|
157
|
|
|
$content .= '<p class="message required">This page could not be e-mailed to the following addresses :</p><ul>'; |
|
158
|
|
View Code Duplication |
for ($i = $endIndex; $i < count($toList); $i++) { |
|
|
|
|
|
|
159
|
|
|
$content .= '<li>' . $toList[$i] . '</li>'; |
|
160
|
|
|
} |
|
161
|
|
|
$content .= '</ul>'; |
|
162
|
|
|
} |
|
163
|
|
|
} else { |
|
164
|
|
|
$content = '<p class="message required bad">This page has not been e-mailed to anyone.</p>'; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$content .= '<br/><p><a href="' . $this->controller->Link() . '">Send more?</a>.</p>'; |
|
168
|
|
|
|
|
169
|
|
|
$templateData = array("EmailAFriendForm" => null, "EmailAFriendThankYouContent" => $content); |
|
170
|
|
|
return $this->customise($templateData)->renderWith('EmailAFriendHolder'); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
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.