1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\OrdersAdmin\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
6
|
|
|
use SilverStripe\Forms\DropdownField; |
7
|
|
|
use SilverStripe\Control\Email\Email; |
8
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
9
|
|
|
|
10
|
|
|
class Notification extends DataObject |
11
|
|
|
{ |
12
|
|
|
private static $table_name = 'OrderNotification'; |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @config |
16
|
|
|
*/ |
17
|
|
|
private static $db = [ |
|
|
|
|
18
|
|
|
"Status" => "Varchar", |
19
|
|
|
"SendNotificationTo" => "Enum('Customer,Vendor,Both','Customer')", |
20
|
|
|
"CustomSubject" => "Varchar(255)", |
21
|
|
|
"FromEmail" => "Varchar", |
22
|
|
|
"VendorEmail" => "Varchar" |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @config |
27
|
|
|
*/ |
28
|
|
|
private static $has_one = [ |
|
|
|
|
29
|
|
|
"Parent" => SiteConfig::class |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @config |
34
|
|
|
*/ |
35
|
|
|
private static $summary_fields = [ |
|
|
|
|
36
|
|
|
"Status", |
37
|
|
|
"SendNotificationTo", |
38
|
|
|
"FromEmail", |
39
|
|
|
"VendorEmail", |
40
|
|
|
"CustomSubject" |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
public function getCMSFields() |
44
|
|
|
{ |
45
|
|
|
$this->beforeUpdateCMSFields(function ($fields) { |
46
|
|
|
$status_field = DropdownField::create( |
47
|
|
|
"Status", |
|
|
|
|
48
|
|
|
$this->fieldLabel("Status"), |
49
|
|
|
Invoice::config()->get("statuses") |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$fields->replaceField("Status", $status_field); |
53
|
|
|
|
54
|
|
|
$vendor = $fields->dataFieldByName("VendorEmail"); |
55
|
|
|
|
56
|
|
|
if ($vendor) { |
57
|
|
|
$vendor->setDescription(_t( |
58
|
|
|
"Orders.VendorEmailDescription", |
59
|
|
|
"Only needed when notification sent to vendor (or both)" |
60
|
|
|
)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$subject = $fields->dataFieldByName("CustomSubject"); |
64
|
|
|
|
65
|
|
|
if ($subject) { |
66
|
|
|
$subject->setDescription(_t( |
67
|
|
|
"Orders.CustomSubjectDescription", |
68
|
|
|
"Overwrite the default subject created in the notification email" |
69
|
|
|
)); |
70
|
|
|
} |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
return parent::getCMSFields(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Deal with sending a notification. This is assumed to be an email |
78
|
|
|
* by default, but can be extended through "augmentSend" to allow |
79
|
|
|
* adding of additional notification types (such as SMS, XML, etc) |
80
|
|
|
* |
81
|
|
|
*/ |
82
|
|
|
public function sendNotification($order) |
83
|
|
|
{ |
84
|
|
|
// Deal with customer email |
85
|
|
|
if ($order->Email && ($this->SendNotificationTo == 'Customer' || $this->SendNotificationTo == "Both")) { |
|
|
|
|
86
|
|
|
if ($this->CustomSubject) { |
|
|
|
|
87
|
|
|
$subject = $this->CustomSubject; |
88
|
|
|
} else { |
89
|
|
|
$subject = _t('Orders.Order', 'Order') . " {$order->OrderNumber} {$order->Status}"; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$email = Email::create() |
93
|
|
|
->setSubject($subject) |
94
|
|
|
->setTo($order->Email) |
95
|
|
|
->setHTMLTemplate("\\SilverCommerce\\OrdersAdmin\\Email\\OrderNotificationEmail_Customer") |
96
|
|
|
->setData([ |
97
|
|
|
"Order" => $order, |
98
|
|
|
"SiteConfig" => $this->Parent(), |
|
|
|
|
99
|
|
|
"Notification" => $this |
100
|
|
|
]); |
101
|
|
|
|
102
|
|
|
if ($this->FromEmail) { |
|
|
|
|
103
|
|
|
$email->setFrom($this->FromEmail); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->extend("augmentEmailCustomer", $email, $order); |
107
|
|
|
|
108
|
|
|
$email->send(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Deal with vendor email |
112
|
|
|
if ($this->VendorEmail && ($this->SendNotificationTo == 'Vendor' || $this->SendNotificationTo == "Both")) { |
|
|
|
|
113
|
|
|
if ($this->CustomSubject) { |
114
|
|
|
$subject = $this->CustomSubject; |
115
|
|
|
} else { |
116
|
|
|
$subject = _t('Orders.Order', 'Order') . " {$order->OrderNumber} {$order->Status}"; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$email = Email::create() |
120
|
|
|
->setSubject($subject) |
121
|
|
|
->setTo($this->VendorEmail) |
122
|
|
|
->setHTMLTemplate("\\SilverCommerce\\OrdersAdmin\\Email\\OrderNotificationEmail_Vendor") |
123
|
|
|
->setData([ |
124
|
|
|
"Order" => $order, |
125
|
|
|
"Notification" => $this |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
if ($this->FromEmail) { |
129
|
|
|
$email->setFrom($this->FromEmail); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$this->extend("augmentEmailVendor", $email, $order); |
133
|
|
|
|
134
|
|
|
$email->send(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->extend("augmentSend", $order); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|