1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Inventory\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
6
|
|
|
use SilverStripe\Forms\FieldList; |
7
|
|
|
use SilverStripe\Forms\ReadonlyField; |
8
|
|
|
use SilverStripe\ORM\DataObject; |
9
|
|
|
use SilverStripe\Security\Permission; |
10
|
|
|
|
11
|
|
|
class CartReservation extends DataObject |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private static $singular_name = 'Cart Reservation'; |
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private static $plural_name = 'Cart Reservations'; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private static $table_name = 'FoxyCartReservation'; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private static $db = [ |
|
|
|
|
32
|
|
|
'ReservationCode' => 'Varchar(255)', |
33
|
|
|
'CartProductID' => 'Int', |
34
|
|
|
'Code' => 'Varchar(255)', |
35
|
|
|
'Expires' => 'DBDatetime', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private static $has_one = [ |
|
|
|
|
42
|
|
|
'Product' => SiteTree::class, |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private static $indexes = [ |
|
|
|
|
49
|
|
|
'foxy-reservation-code' => [ |
50
|
|
|
'type' => 'unique', |
51
|
|
|
'columns' => ['ReservationCode'], |
52
|
|
|
], |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private static $summary_fields = [ |
|
|
|
|
59
|
|
|
'ReservationCode' => 'Reservation', |
60
|
|
|
'Expires.Nice' => 'Expires', |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return FieldList |
65
|
|
|
*/ |
66
|
|
|
public function getCMSFields() |
67
|
|
|
{ |
68
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
69
|
|
|
$fields->addFieldToTab('Root.Main', ReadonlyField::create('ReservationCode')->setTitle('Reservation Code')); |
70
|
|
|
}); |
71
|
|
|
return parent::getCMSFields(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
*/ |
77
|
|
|
public function onBeforeWrite() |
78
|
|
|
{ |
79
|
|
|
parent::onBeforeWrite(); |
80
|
|
|
if (!$this->ProductID) { |
81
|
|
|
if ($product = SiteTree::get()->filter('Code', $this->Code)->first()) { |
|
|
|
|
82
|
|
|
$this->ProductID = $product->ID; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param null $member |
|
|
|
|
89
|
|
|
* @param array $context |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function canCreate($member = null, $context = []) |
93
|
|
|
{ |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param null $member |
|
|
|
|
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function canEdit($member = null) |
102
|
|
|
{ |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param null $member |
|
|
|
|
108
|
|
|
* @return bool|int |
109
|
|
|
*/ |
110
|
|
|
public function canDelete($member = null) |
111
|
|
|
{ |
112
|
|
|
return Permission::check('ADMIN', 'any', \Security::getCurrentUser()); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param null $member |
|
|
|
|
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public function canView($member = null) |
120
|
|
|
{ |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|