1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class DMSDocumentCartExtension |
5
|
|
|
* |
6
|
|
|
* @property Boolean AllowedInCart |
7
|
|
|
* @property Int PrintRequestCount |
8
|
|
|
* |
9
|
|
|
* @property DMSDocument owner |
10
|
|
|
*/ |
11
|
|
|
class DMSDocumentCartExtension extends DataExtension |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var DMSDocumentCartController |
15
|
|
|
*/ |
16
|
|
|
private $cartController; |
17
|
|
|
|
18
|
|
|
private static $db = array( |
|
|
|
|
19
|
|
|
'AllowedInCart' => 'Boolean', |
20
|
|
|
// Running total of print requests on this document |
21
|
|
|
'PrintRequestCount' => 'Int', |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Returns if a Document is permitted to reflect in a cart |
26
|
|
|
* |
27
|
|
|
* @return boolean |
28
|
|
|
*/ |
29
|
|
|
public function isAllowedInCart() |
30
|
|
|
{ |
31
|
|
|
return $this->owner->AllowedInCart; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function updateCMSFields(FieldList $fields) |
35
|
|
|
{ |
36
|
|
|
$fields->insertBefore(CheckboxField::create( |
37
|
|
|
'AllowedInCart', |
38
|
|
|
_t('DMSDocumentCart.ALLOWED_IN_CART', 'Allowed in document cart') |
39
|
|
|
), 'Description'); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Increments the number of times a document was printed |
44
|
|
|
* |
45
|
|
|
* @return DMSDocument |
46
|
|
|
*/ |
47
|
|
|
public function incrementPrintRequest() |
48
|
|
|
{ |
49
|
|
|
$this->owner->PrintRequestCount++; |
50
|
|
|
$this->owner->write(); |
51
|
|
|
|
52
|
|
|
return $this->owner; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Checks if a given document already exists within the Cart. True if it does, false otherwise |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function isInCart() |
61
|
|
|
{ |
62
|
|
|
return (bool) $this->getCart()->isInCart($this->owner->ID); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Builds and returns a valid DMSDocumentController URL from the given $action link |
67
|
|
|
* |
68
|
|
|
* @param string $action Can be either 'add', 'remove' or 'checkout |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
* |
72
|
|
|
* @throws InvalidArgumentException if the provided $action is not allowed. |
73
|
|
|
*/ |
74
|
|
|
public function getActionLink($action = 'add') |
75
|
|
|
{ |
76
|
|
|
$action = strtolower($action); |
77
|
|
|
$allowedActions = array_merge($this->getCartController()->allowedActions(), array('checkout')); |
78
|
|
|
if (!in_array($action, $allowedActions)) { |
79
|
|
|
throw new InvalidArgumentException("{$action} is not accepted for this method."); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($action !== 'checkout') { |
83
|
|
|
$result = Controller::join_links('documentcart', $action, $this->owner->ID); |
84
|
|
|
} else { |
85
|
|
|
$result = SiteTree::get_one('DMSDocumentCartCheckoutPage')->Link(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $result; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Retrieves a DMSDocumentCartController handle |
93
|
|
|
* |
94
|
|
|
* @return DMSDocumentCartController |
95
|
|
|
*/ |
96
|
|
|
public function getCartController() |
97
|
|
|
{ |
98
|
|
|
if (!$this->cartController) { |
99
|
|
|
$this->cartController = DMSDocumentCartController::create(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->cartController; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Retrieves a DMSDocumentCart handle |
107
|
|
|
* |
108
|
|
|
* @return DMSDocumentCart |
109
|
|
|
*/ |
110
|
|
|
public function getCart() |
111
|
|
|
{ |
112
|
|
|
return $this->getCartController()->getCart(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.