1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\OrdersAdmin\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataExtension; |
6
|
|
|
use SilverStripe\Forms\FieldList; |
7
|
|
|
use SilverStripe\Forms\DropdownField; |
8
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm; |
9
|
|
|
use SilverStripe\Core\Config\Config; |
10
|
|
|
use SilverStripe\Security\Member; |
11
|
|
|
use SilverCommerce\OrdersAdmin\Forms\GridField\OrdersDetailForm; |
12
|
|
|
use SilverCommerce\OrdersAdmin\Model\Invoice; |
13
|
|
|
use SilverCommerce\OrdersAdmin\Model\Estimate; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Add additional functions to a contact |
17
|
|
|
*/ |
18
|
|
|
class ContactExtension extends DataExtension |
19
|
|
|
{ |
20
|
|
|
private static $has_one = [ |
|
|
|
|
21
|
|
|
"Member" => Member::class |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
private static $has_many = [ |
|
|
|
|
25
|
|
|
"Invoices" => Invoice::class, |
26
|
|
|
"Estimates"=> Estimate::class |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
public function updateCMSFields(FieldList $fields) |
30
|
|
|
{ |
31
|
|
|
// Filter the invoice list |
32
|
|
|
$invoices_field = $fields->dataFieldByName("Invoices"); |
33
|
|
|
$estimate_field = $fields->dataFieldByName("Estimates"); |
34
|
|
|
|
35
|
|
|
if ($invoices_field) { |
36
|
|
|
$invoices_field |
37
|
|
|
->getConfig() |
38
|
|
|
->removeComponentsByType(GridFieldDetailForm::class) |
39
|
|
|
->addComponent(OrdersDetailForm::create()); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($estimate_field) { |
43
|
|
|
$list = $estimate_field->getList(); |
44
|
|
|
$list = $list->filter("ClassName", Estimate::class); |
45
|
|
|
$estimate_field->setList($list); |
46
|
|
|
$estimate_field |
47
|
|
|
->getConfig() |
48
|
|
|
->removeComponentsByType(GridFieldDetailForm::class) |
49
|
|
|
->addComponent(OrdersDetailForm::create()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Add a dropdown to select user account |
53
|
|
|
$fields->addFieldToTab( |
54
|
|
|
"Root.Main", |
55
|
|
|
DropdownField::create( |
56
|
|
|
"MemberID", |
|
|
|
|
57
|
|
|
_t( |
58
|
|
|
"OrdersAdmin.LinkContactToAccount", |
59
|
|
|
"Link this contact to a user account" |
60
|
|
|
), |
61
|
|
|
Member::get()->map() |
|
|
|
|
62
|
|
|
)->setEmptyString("") |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get all orders that have been generated and are marked |
68
|
|
|
* as paid or processing |
69
|
|
|
* |
70
|
|
|
* @return DataList |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function OutstandingInvoices() |
73
|
|
|
{ |
74
|
|
|
return $this |
75
|
|
|
->owner |
76
|
|
|
->Invoices() |
77
|
|
|
->filter("Status", Config::inst()->get(Invoice::class, "outstanding_statuses")); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get all orders that have been generated and are marked |
82
|
|
|
* as dispatched or canceled |
83
|
|
|
* |
84
|
|
|
* @return DataList |
85
|
|
|
*/ |
86
|
|
|
public function HistoricInvoices() |
87
|
|
|
{ |
88
|
|
|
return $this |
89
|
|
|
->owner |
90
|
|
|
->Invoices() |
91
|
|
|
->filter("Status", Config::inst()->get(Invoice::class, "historic_statuses")); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function canView($member) |
95
|
|
|
{ |
96
|
|
|
// Members can view their own records |
97
|
|
|
if ($member && $member->exists() && $member->ID == $this->owner->MemberID) { |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function canEdit($member) |
103
|
|
|
{ |
104
|
|
|
// Members can edit their own records |
105
|
|
|
if ($member && $member->exists() && $member->ID == $this->owner->MemberID) { |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function canDelete($member) |
111
|
|
|
{ |
112
|
|
|
// Members can delete their own records |
113
|
|
|
if ($member && $member->exists() && $member->ID == $this->owner->MemberID) { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |