1 | <?php |
||
2 | |||
3 | namespace Dynamic\FoxyStripe\ORM; |
||
4 | |||
5 | use Dynamic\FoxyStripe\Model\ProductCartReservation; |
||
6 | use SilverStripe\Forms\CheckboxField; |
||
7 | use SilverStripe\Forms\FieldGroup; |
||
8 | use SilverStripe\Forms\FieldList; |
||
9 | use SilverStripe\Forms\GridField\GridField; |
||
10 | use SilverStripe\Forms\GridField\GridFieldConfig_RecordViewer; |
||
11 | use SilverStripe\Forms\NumericField; |
||
12 | use SilverStripe\ORM\DataExtension; |
||
13 | use SilverStripe\ORM\ValidationResult; |
||
14 | |||
15 | /** |
||
16 | * Class ProductPageDataExtension |
||
17 | * @package Dynamic\Sheeps\ProductCartExpiry\ORM |
||
18 | */ |
||
19 | class ProductPageDataExtension extends DataExtension |
||
20 | { |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private static $db = [ |
||
25 | 'CartExpiration' => 'Boolean', |
||
26 | 'ExpirationMinutes' => 'Int', |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private static $has_many = [ |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
33 | 'CartReservations' => ProductCartReservation::class, |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * @param FieldList $fields |
||
38 | */ |
||
39 | 1 | public function updateCMSFields(FieldList $fields) |
|
40 | { |
||
41 | $expirationFields = [ |
||
42 | 1 | CheckboxField::create('CartExpiration') |
|
43 | 1 | ->setTitle('Cart Product Expiration'), |
|
44 | 1 | $duration = NumericField::create('ExpirationMinutes') |
|
45 | 1 | ->setTitle('Expiration In Minutes') |
|
46 | 1 | ->setDescription("After the time listed above in minutes, this product will be removed from the user's cart"), |
|
47 | ]; |
||
48 | |||
49 | 1 | $duration->displayIf('CartExpiration')->isChecked()->end(); |
|
50 | |||
51 | 1 | if ($this->owner->CartReservations()->exists()) { |
|
52 | $expirationGrid = GridField::create( |
||
53 | 'CartReservations', |
||
54 | 'Cart Reservations', |
||
55 | $this->owner->CartReservations() |
||
56 | ->filter('Expires:GreaterThan', date('Y-m-d H:i:s', strtotime('now'))) |
||
57 | ->sort('Created'), |
||
58 | $cartResConfig = GridFieldConfig_RecordViewer::create() |
||
59 | ); |
||
60 | $expirationGrid->displayIf('CartExpiration')->isChecked()->end(); |
||
61 | |||
62 | $expirationFields[] = $expirationGrid; |
||
63 | } |
||
64 | |||
65 | 1 | $fields->addFieldsToTab( |
|
66 | 1 | 'Root.Inventory', |
|
67 | 1 | $expirationFields |
|
68 | ); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param ValidationResult $validationResult |
||
73 | */ |
||
74 | public function validate(ValidationResult $validationResult) |
||
75 | { |
||
76 | if ($this->owner->CartExpiration && $this->owner->ExpirationMinutes < 1) { |
||
77 | $validationResult |
||
78 | ->addFieldError( |
||
79 | 'ExpirationMinutes', |
||
80 | 'You must set the "Expiration In Minutes" or disable "Cart Product Expiration"' |
||
81 | ); |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 |