1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author nicolaas [at] sunny side up . co . nz |
5
|
|
|
* this extension of product is for software products (modules) |
6
|
|
|
* |
7
|
|
|
* |
8
|
|
|
**/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class ModuleProductEmail extends DataObject |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
private static $icon = "ecommerce_software/images/treeicons/ModuleProduct"; |
|
|
|
|
14
|
|
|
|
15
|
|
|
private static $db = array( |
|
|
|
|
16
|
|
|
"Subject" => "Varchar", |
17
|
|
|
"Body" => "HTMLText", |
18
|
|
|
"To" => "Varchar(255)", |
19
|
|
|
"Sent" => "Boolean" |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
private static $has_one = array( |
|
|
|
|
23
|
|
|
"ModuleProduct" => "ModuleProduct", |
24
|
|
|
"Member" => "Member" |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
private static $singular_name = "Module Email"; |
|
|
|
|
28
|
|
|
public function i18n_singular_name() |
29
|
|
|
{ |
30
|
|
|
return _t("ModuleProductEmail.MODULEPRODUCTEMAIL", "Module Email"); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private static $plural_name = "Module Emails"; |
|
|
|
|
34
|
|
|
public function i18n_plural_name() |
35
|
|
|
{ |
36
|
|
|
return _t("ModuleProductEmail.MODULEPRODUCTEMAILS", "Module Emails"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function canDelete($member = null) |
40
|
|
|
{ |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function canEdit($member = null) |
45
|
|
|
{ |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function onAfterWrite() |
50
|
|
|
{ |
51
|
|
|
parent::onAfterWrite(); |
52
|
|
|
if (!$this->Sent) { |
|
|
|
|
53
|
|
|
$email = new Email( |
54
|
|
|
$from = Email::getAdminEmail(), |
|
|
|
|
55
|
|
|
$to = $this->To, |
|
|
|
|
56
|
|
|
$subject = $this->Subject, |
|
|
|
|
57
|
|
|
$body = $this->Body, |
|
|
|
|
58
|
|
|
$bounceHandlerURL = null, |
59
|
|
|
$cc = null, |
60
|
|
|
$bcc = Email::getAdminEmail() |
|
|
|
|
61
|
|
|
); |
62
|
|
|
$email->send(); |
63
|
|
|
$this->Sent = 1; |
|
|
|
|
64
|
|
|
$this->write(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
class ModuleProductEmail_Form extends Form |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
public function __construct($controller, $name, $moduleProduct) |
73
|
|
|
{ |
74
|
|
|
$defaults = $moduleProduct->EmailDefaults(); |
75
|
|
|
$fields = new FieldList(); |
76
|
|
|
$fields->push(new TextField('To', 'To', $defaults->To)); |
77
|
|
|
$fields->push(new TextField('Subject', 'Subject', $defaults->Subject)); |
78
|
|
|
$fields->push(new HiddenField('ModuleProductID', 'ModuleProductID', $moduleProduct->ID)); |
79
|
|
|
$fields->push(new HiddenField('MemberID', 'memberID', $moduleProduct->DefaultMemberID())); |
80
|
|
|
$fields->push(new HtmlEditorField('Body', 'Body', $defaults->Body)); |
81
|
|
|
$actions = new FieldList(new FormAction("submit", "submit")); |
82
|
|
|
$validator = new ModuleProductEmail_RequiredFields(array("Subject")); |
83
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator); |
84
|
|
|
$this->loadDataFrom($moduleProduct->emailDefaults()); |
85
|
|
|
return $this; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function submit($data, $form) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$member = Member::currentUser(); |
91
|
|
|
if (!$member || !$member->inGroup("ADMIN")) { |
92
|
|
|
$form->setMessage("You need to be logged as an admin to send this email.", "bad"); |
93
|
|
|
return Controller::curr()->redirectBack(); |
94
|
|
|
} |
95
|
|
|
$data = Convert::raw2sql($data); |
96
|
|
|
$page = null; |
97
|
|
|
if (isset($data["ModuleProductID"])) { |
98
|
|
|
$page = ModuleProduct::get()->byID(intval($data["ModuleProductID"])); |
99
|
|
|
} |
100
|
|
|
if (!$page) { |
101
|
|
|
$form->setMessage("Can not find the right page for saving this email.", "bad"); |
102
|
|
|
return Controller::curr()->redirectBack(); |
103
|
|
|
} |
104
|
|
|
$email = new ModuleProductEmail(); |
105
|
|
|
$form->saveInto($email); |
106
|
|
|
$email->write(); |
107
|
|
|
if (Director::is_ajax()) { |
108
|
|
|
return "mail sent!"; |
109
|
|
|
} else { |
110
|
|
|
return Controller::curr()->redirect($page->Link()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
class ModuleProductEmail_RequiredFields extends RequiredFields |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
public function __construct($array) |
118
|
|
|
{ |
119
|
|
|
parent::__construct($array); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function javascript() |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$js = ''; |
125
|
|
|
Requirements::customScript($js, "ModuleProductEmail"); |
126
|
|
|
return parent::javascript(); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Allows validation of fields via specification of a php function for validation which is executed after |
132
|
|
|
* the form is submitted |
133
|
|
|
*/ |
134
|
|
|
public function php($data) |
135
|
|
|
{ |
136
|
|
|
$valid = true; |
137
|
|
View Code Duplication |
if (!isset($data["Subject"]) || (isset($data["Subject"]) && strlen($data["Subject"])) < 3) { |
|
|
|
|
138
|
|
|
$errorMessage = _t("Form.PLEASEENTERASUBJECT", "Please enter a subject"); |
139
|
|
|
$this->validationError( |
140
|
|
|
$fieldName = "Subject", |
141
|
|
|
$errorMessage, |
142
|
|
|
"required" |
143
|
|
|
); |
144
|
|
|
$valid = false; |
145
|
|
|
} |
146
|
|
View Code Duplication |
if (!isset($data["To"]) || (isset($data["To"]) && strlen($data["To"]) < 3)) { |
|
|
|
|
147
|
|
|
$errorMessage = _t("Form.PLEASEENTERANEMAIL", "Please enter an e=mail"); |
148
|
|
|
$this->validationError( |
149
|
|
|
$fieldName = "To", |
150
|
|
|
$errorMessage, |
151
|
|
|
"required" |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
if (!$valid) { |
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
return parent::php($data); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
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.