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