1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class DMSDocumentCartController extends Controller |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
private static $url_handlers = array( |
|
|
|
|
6
|
|
|
'$Action//$ID' => 'handleAction', |
7
|
|
|
); |
8
|
|
|
|
9
|
|
|
private static $allowed_actions = array( |
|
|
|
|
10
|
|
|
'DocumentCartForm', |
11
|
|
|
'add', |
12
|
|
|
'deduct', |
13
|
|
|
'remove', |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* See {@link DMSDocumentCart::getItems()} |
18
|
|
|
* |
19
|
|
|
* @return ArrayList |
20
|
|
|
*/ |
21
|
|
|
public function items() |
22
|
|
|
{ |
23
|
|
|
return $this->getCart()->getItems(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Prepares receiver info for the template. |
28
|
|
|
* Additionally it uses Zend_Locale to retrieve the localised spelling of the Country |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public function getReceiverInfo() |
33
|
|
|
{ |
34
|
|
|
$receiverInfo = $this->getCart()->getReceiverInfo(); |
35
|
|
|
|
36
|
|
|
if (isset($receiverInfo['DeliveryAddressCountry']) && $receiverInfo['DeliveryAddressCountry']) { |
37
|
|
|
$source = Zend_Locale::getTranslationList('territory', $receiverInfo['DeliveryAddressCountry'], 2); |
38
|
|
|
$receiverInfo['DeliveryAddressCountryLiteral'] = $source[$receiverInfo['DeliveryAddressCountry']]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!empty($receiverInfo)) { |
42
|
|
|
$result = $receiverInfo; |
43
|
|
|
} else { |
44
|
|
|
$result = array('Result' => 'no data'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $result; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* See DMSDocumentCart::isCartEmpty() |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function getIsCartEmpty() |
56
|
|
|
{ |
57
|
|
|
return $this->getCart()->isCartEmpty(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Add quantity to an item that exists in {@link DMSDocumentCart}. |
62
|
|
|
* If the item does nt exist, try to add a new item of the particular |
63
|
|
|
* class given the URL parameters available. |
64
|
|
|
* |
65
|
|
|
* @param SS_HTTPRequest $request |
66
|
|
|
* |
67
|
|
|
* @return SS_HTTPResponse|string |
68
|
|
|
*/ |
69
|
|
|
public function add(SS_HTTPRequest $request) |
70
|
|
|
{ |
71
|
|
|
$quantity = ($request->requestVar('quantity')) ? intval($request->requestVar('quantity')) : 1; |
72
|
|
|
$documentId = (int)$request->param('ID'); |
73
|
|
|
if ($doc = DMSDocument::get()->byID($documentId)) { |
74
|
|
|
if ($doc->isAllowedInCart() && $doc->canView()) { |
75
|
|
|
if ($this->getCart()->getItem($documentId)) { |
76
|
|
|
$this->getCart()->updateItemQuantity($documentId, $quantity); |
77
|
|
|
} else { |
78
|
|
|
$requestItem = DMSRequestItem::create()->setDocument($doc)->setQuantity($quantity); |
|
|
|
|
79
|
|
|
$this->getCart()->addItem($requestItem); |
80
|
|
|
} |
81
|
|
|
$backURL = $request->getVar('BackURL'); |
82
|
|
|
// make sure that backURL is a relative path (starts with /) |
83
|
|
|
if (isset($backURL) && preg_match('/^\//', $backURL)) { |
84
|
|
|
$this->getCart()->setBackUrl($backURL); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
if ($request->isAjax()) { |
|
|
|
|
90
|
|
|
$this->response->addHeader('Content-Type', 'application/json'); |
91
|
|
|
|
92
|
|
|
return Convert::raw2json(array('result' => true)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($backURL = $request->getVar('BackURL')) { |
96
|
|
|
return $this->redirect($backURL); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->redirectBack(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Deduct quantity from an item that exists in {@link DMSDocumentCart} |
104
|
|
|
* |
105
|
|
|
* @param SS_HTTPRequest $request |
106
|
|
|
* |
107
|
|
|
* @return SS_HTTPResponse|string |
108
|
|
|
*/ |
109
|
|
|
public function deduct(SS_HTTPRequest $request) |
110
|
|
|
{ |
111
|
|
|
$quantity = ($request->requestVar('quantity')) ? intval($request->requestVar('quantity')) : 1; |
112
|
|
|
$this->getCart()->updateItemQuantity((int)$request->param('ID'), $quantity); |
113
|
|
|
$this->redirectBack(); |
114
|
|
|
|
115
|
|
View Code Duplication |
if ($request->isAjax()) { |
|
|
|
|
116
|
|
|
$this->response->addHeader('Content-Type', 'application/json'); |
117
|
|
|
|
118
|
|
|
return Convert::raw2json(array('result' => true)); |
119
|
|
|
} |
120
|
|
|
if ($backURL = $request->getVar('BackURL')) { |
121
|
|
|
return $this->redirect($backURL); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->redirectBack(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Completely remove an item that exists in {@link DMSDocumentCart} |
129
|
|
|
* |
130
|
|
|
* @param SS_HTTPRequest $request |
131
|
|
|
* |
132
|
|
|
* @return string |
|
|
|
|
133
|
|
|
*/ |
134
|
|
|
public function remove(SS_HTTPRequest $request) |
135
|
|
|
{ |
136
|
|
|
$this->getCart()->removeItemByID(intval($request->param('ID'))); |
137
|
|
|
|
138
|
|
|
if ($request->isAjax()) { |
139
|
|
|
$this->response->addHeader('Content-Type', 'application/json'); |
140
|
|
|
|
141
|
|
|
return Convert::raw2json(array('result' => !$this->getIsCartEmpty())); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->redirectBack(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Retrieves a {@link DMSDocumentCart} instance |
149
|
|
|
* |
150
|
|
|
* @return DMSDocumentCart |
151
|
|
|
*/ |
152
|
|
|
public function getCart() |
153
|
|
|
{ |
154
|
|
|
return singleton('DMSDocumentCart'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.