1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* The controller for the order entity containing the CRUD operations.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* The controller class for orders contains methods for the order CRUD
|
10
|
* operations. The load method is inherited from the default controller.
|
11
|
*/
|
12
|
class CommerceOrderEntityController extends DrupalCommerceEntityController {
|
13
|
|
14
|
/**
|
15
|
* Create a default order.
|
16
|
*
|
17
|
* @param array $values
|
18
|
* An array of values to set, keyed by property name.
|
19
|
*
|
20
|
* @return
|
21
|
* An order object with all default fields initialized.
|
22
|
*/
|
23
|
public function create(array $values = array()) {
|
24
|
$values += array(
|
25
|
'order_id' => NULL,
|
26
|
'order_number' => NULL,
|
27
|
'revision_id' => NULL,
|
28
|
'uid' => '',
|
29
|
'mail' => ( !empty($values['uid']) && ($account = user_load($values['uid'])) ) ? $account->mail : '',
|
30
|
'data' => array(),
|
31
|
'created' => '',
|
32
|
'changed' => '',
|
33
|
'placed' => '',
|
34
|
'hostname' => '',
|
35
|
);
|
36
|
|
37
|
return parent::create($values);
|
38
|
}
|
39
|
|
40
|
/**
|
41
|
* Saves an order.
|
42
|
*
|
43
|
* When saving an order without an order ID, this function will create a new
|
44
|
* order at that time. For new orders, it will also determine and save the
|
45
|
* order number and then save the initial revision of the order. Subsequent
|
46
|
* orders that should be saved as new revisions should set $order->revision to
|
47
|
* TRUE and include a log string in $order->log.
|
48
|
*
|
49
|
* @param $order
|
50
|
* The full order object to save.
|
51
|
* @param $transaction
|
52
|
* An optional transaction object.
|
53
|
*
|
54
|
* @return
|
55
|
* SAVED_NEW or SAVED_UPDATED depending on the operation performed.
|
56
|
*/
|
57
|
public function save($order, DatabaseTransaction $transaction = NULL) {
|
58
|
if (!isset($transaction)) {
|
59
|
$transaction = db_transaction();
|
60
|
$started_transaction = TRUE;
|
61
|
}
|
62
|
|
63
|
try {
|
64
|
global $user;
|
65
|
|
66
|
// Determine if we will be inserting a new order.
|
67
|
$order->is_new = empty($order->order_id);
|
68
|
|
69
|
// Set the timestamp fields.
|
70
|
if ($order->is_new) {
|
71
|
if (empty($order->created)) {
|
72
|
$order->created = REQUEST_TIME;
|
73
|
}
|
74
|
if (empty($order->changed)) {
|
75
|
$order->changed = REQUEST_TIME;
|
76
|
}
|
77
|
if (empty($order->revision_timestamp)) {
|
78
|
$order->revision_timestamp = REQUEST_TIME;
|
79
|
}
|
80
|
if (empty($order->hostname)) {
|
81
|
$order->hostname = ip_address();
|
82
|
}
|
83
|
}
|
84
|
else {
|
85
|
// Otherwise if the order is not new but comes from an entity_create()
|
86
|
// or similar function call that initializes the created timestamp, uid,
|
87
|
// and hostname values to empty strings, unset them to prevent
|
88
|
// destroying existing data in those properties on update.
|
89
|
if ($order->created === '') {
|
90
|
unset($order->created);
|
91
|
}
|
92
|
if ($order->uid === '') {
|
93
|
unset($order->uid);
|
94
|
}
|
95
|
if ($order->hostname === '') {
|
96
|
unset($order->hostname);
|
97
|
}
|
98
|
|
99
|
$time = time();
|
100
|
$order->changed = $time;
|
101
|
$order->revision_timestamp = $time;
|
102
|
}
|
103
|
|
104
|
$order->revision_hostname = ip_address();
|
105
|
$order->revision_uid = $user->uid;
|
106
|
|
107
|
// Recalculate the order total using the current line item data.
|
108
|
commerce_order_calculate_total($order);
|
109
|
|
110
|
if ($order->is_new || !empty($order->revision)) {
|
111
|
// When inserting either a new order or revision, $order->log must be set
|
112
|
// because {commerce_order_revision}.log is a text column and therefore
|
113
|
// cannot have a default value. However, it might not be set at this
|
114
|
// point, so we ensure that it is at least an empty string in that case.
|
115
|
if (!isset($order->log)) {
|
116
|
$order->log = '';
|
117
|
}
|
118
|
}
|
119
|
elseif (empty($order->log)) {
|
120
|
// If we are updating an existing order without adding a new revision,
|
121
|
// we need to make sure $order->log is unset whenever it is empty. As
|
122
|
// long as $order->log is unset, drupal_write_record() will not attempt
|
123
|
// to update the existing database column when re-saving the revision.
|
124
|
unset($order->log);
|
125
|
}
|
126
|
|
127
|
return parent::save($order, $transaction);
|
128
|
}
|
129
|
catch (Exception $e) {
|
130
|
if (!empty($started_transaction)) {
|
131
|
$transaction->rollback();
|
132
|
watchdog_exception($this->entityType, $e);
|
133
|
}
|
134
|
throw $e;
|
135
|
}
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Unserializes the data property of loaded orders.
|
140
|
*/
|
141
|
public function attachLoad(&$queried_orders, $revision_id = FALSE) {
|
142
|
foreach ($queried_orders as $order_id => &$order) {
|
143
|
$order->data = unserialize($order->data);
|
144
|
}
|
145
|
|
146
|
// Call the default attachLoad() method. This will add fields and call
|
147
|
// hook_commerce_order_load().
|
148
|
parent::attachLoad($queried_orders, $revision_id);
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* Deletes multiple orders by ID.
|
153
|
*
|
154
|
* @param $order_ids
|
155
|
* An array of order IDs to delete.
|
156
|
* @param $transaction
|
157
|
* An optional transaction object.
|
158
|
*
|
159
|
* @return boolean
|
160
|
* TRUE on success, FALSE otherwise.
|
161
|
*/
|
162
|
public function delete($order_ids, DatabaseTransaction $transaction = NULL) {
|
163
|
if (!empty($order_ids)) {
|
164
|
$orders = $this->load($order_ids, array());
|
165
|
|
166
|
// Ensure the orders can actually be deleted.
|
167
|
foreach ((array) $orders as $order_id => $order) {
|
168
|
if (in_array(FALSE, module_invoke_all('commerce_order_can_delete', $order))) {
|
169
|
unset($orders[$order_id]);
|
170
|
}
|
171
|
}
|
172
|
|
173
|
// If none of the specified orders can be deleted, return FALSE.
|
174
|
if (empty($orders)) {
|
175
|
return FALSE;
|
176
|
}
|
177
|
|
178
|
parent::delete($order_ids, $transaction);
|
179
|
return TRUE;
|
180
|
}
|
181
|
else {
|
182
|
return FALSE;
|
183
|
}
|
184
|
}
|
185
|
}
|