1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* The controller for the product entity containing the CRUD operations.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* The controller class for products contains methods for the product CRUD operations.
|
10
|
*
|
11
|
* Mainly relies on the EntityAPIController class provided by the Entity
|
12
|
* module, just overrides specific features.
|
13
|
*/
|
14
|
class CommerceProductEntityController extends DrupalCommerceEntityController {
|
15
|
|
16
|
/**
|
17
|
* Create a default product.
|
18
|
*
|
19
|
* @param array $values
|
20
|
* An array of values to set, keyed by property name.
|
21
|
* @return
|
22
|
* A product object with all default fields initialized.
|
23
|
*/
|
24
|
public function create(array $values = array()) {
|
25
|
$values += array(
|
26
|
'product_id' => NULL,
|
27
|
'is_new' => TRUE,
|
28
|
'sku' => '',
|
29
|
'revision_id' => NULL,
|
30
|
'title' => '',
|
31
|
'uid' => '',
|
32
|
'status' => 1,
|
33
|
'created' => '',
|
34
|
'changed' => '',
|
35
|
);
|
36
|
|
37
|
return parent::create($values);
|
38
|
}
|
39
|
|
40
|
/**
|
41
|
* Saves a product.
|
42
|
*
|
43
|
* @param $product
|
44
|
* The full product object to save.
|
45
|
* @param $transaction
|
46
|
* An optional transaction object.
|
47
|
*
|
48
|
* @return
|
49
|
* SAVED_NEW or SAVED_UPDATED depending on the operation performed.
|
50
|
*/
|
51
|
public function save($product, DatabaseTransaction $transaction = NULL) {
|
52
|
global $user;
|
53
|
|
54
|
// Hardcode the changed time.
|
55
|
$product->changed = REQUEST_TIME;
|
56
|
|
57
|
if (empty($product->{$this->idKey}) || !empty($product->is_new)) {
|
58
|
// Set the creation timestamp if not set, for new entities.
|
59
|
if (empty($product->created)) {
|
60
|
$product->created = REQUEST_TIME;
|
61
|
}
|
62
|
}
|
63
|
else {
|
64
|
// Otherwise if the product is not new but comes from an entity_create()
|
65
|
// or similar function call that initializes the created timestamp and uid
|
66
|
// value to empty strings, unset them to prevent destroying existing data
|
67
|
// in those properties on update.
|
68
|
if ($product->created === '') {
|
69
|
unset($product->created);
|
70
|
}
|
71
|
if ($product->uid === '') {
|
72
|
unset($product->uid);
|
73
|
}
|
74
|
}
|
75
|
|
76
|
$product->revision_timestamp = REQUEST_TIME;
|
77
|
$product->revision_uid = $user->uid;
|
78
|
|
79
|
// Determine if we will be inserting a new product.
|
80
|
$product->is_new = empty($product->product_id);
|
81
|
|
82
|
if ($product->is_new || !empty($product->revision)) {
|
83
|
// When inserting either a new product or revision, $entity->log must be set
|
84
|
// because {commerce_product_revision}.log is a text column and therefore
|
85
|
// cannot have a default value. However, it might not be set at this
|
86
|
// point, so we ensure that it is at least an empty string in that case.
|
87
|
if (!isset($product->log)) {
|
88
|
$product->log = '';
|
89
|
}
|
90
|
}
|
91
|
elseif (empty($product->log)) {
|
92
|
// If we are updating an existing product without adding a new revision,
|
93
|
// we need to make sure $entity->log is unset whenever it is empty. As
|
94
|
// long as $entity->log is unset, drupal_write_record() will not attempt
|
95
|
// to update the existing database column when re-saving the revision.
|
96
|
unset($product->log);
|
97
|
}
|
98
|
|
99
|
// Remove price components from any price fields attached to the product.
|
100
|
// Default price components should instead be rebuilt each load using
|
101
|
// hook_field_attach_load().
|
102
|
foreach (field_info_instances('commerce_product', $product->type) as $field_name => $instance) {
|
103
|
// Load the instance's field data.
|
104
|
$field = field_info_field($instance['field_name']);
|
105
|
|
106
|
// If the instance is a price field with data on this product...
|
107
|
if ($field['type'] == 'commerce_price' && !empty($product->{$field_name})) {
|
108
|
// Remove the price components from every price value.
|
109
|
foreach ($product->{$field_name} as $langcode => &$items) {
|
110
|
foreach ($items as $delta => &$item) {
|
111
|
if (!empty($item['data'])) {
|
112
|
$item['data']['components'] = array();
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
}
|
117
|
}
|
118
|
|
119
|
return parent::save($product, $transaction);
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Unserializes the data property of loaded products.
|
124
|
*/
|
125
|
public function attachLoad(&$queried_products, $revision_id = FALSE) {
|
126
|
foreach ($queried_products as $product_id => &$product) {
|
127
|
$product->data = unserialize($product->data);
|
128
|
}
|
129
|
|
130
|
// Call the default attachLoad() method. This will add fields and call
|
131
|
// hook_commerce_product_load().
|
132
|
parent::attachLoad($queried_products, $revision_id);
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* Deletes multiple products by ID.
|
137
|
*
|
138
|
* @param $product_ids
|
139
|
* An array of product IDs to delete.
|
140
|
* @param $transaction
|
141
|
* An optional transaction object.
|
142
|
*
|
143
|
* @return
|
144
|
* TRUE on success, FALSE otherwise.
|
145
|
*/
|
146
|
public function delete($product_ids, DatabaseTransaction $transaction = NULL) {
|
147
|
if (!empty($product_ids)) {
|
148
|
$products = $this->load($product_ids, array());
|
149
|
|
150
|
// Ensure the products can actually be deleted.
|
151
|
foreach ((array) $products as $product_id => $product) {
|
152
|
if (!commerce_product_can_delete($product)) {
|
153
|
unset($products[$product_id]);
|
154
|
}
|
155
|
}
|
156
|
|
157
|
// If none of the specified products can be deleted, return FALSE.
|
158
|
if (empty($products)) {
|
159
|
return FALSE;
|
160
|
}
|
161
|
|
162
|
parent::delete(array_keys($products), $transaction);
|
163
|
return TRUE;
|
164
|
}
|
165
|
else {
|
166
|
return FALSE;
|
167
|
}
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* Builds a structured array representing the entity's content.
|
172
|
*
|
173
|
* The content built for the entity will vary depending on the $view_mode
|
174
|
* parameter.
|
175
|
*
|
176
|
* @param $entity
|
177
|
* An entity object.
|
178
|
* @param $view_mode
|
179
|
* View mode, e.g. 'full', 'teaser'...
|
180
|
* @param $langcode
|
181
|
* (optional) A language code to use for rendering. Defaults to the global
|
182
|
* content language of the current request.
|
183
|
* @return
|
184
|
* The renderable array.
|
185
|
*/
|
186
|
public function buildContent($product, $view_mode = 'full', $langcode = NULL, $content = array()) {
|
187
|
// Prepare a reusable array representing the CSS file to attach to the view.
|
188
|
$attached = array(
|
189
|
'css' => array(drupal_get_path('module', 'commerce_product') . '/theme/commerce_product.theme.css'),
|
190
|
);
|
191
|
|
192
|
// Add the default fields inherent to the product entity.
|
193
|
$content['sku'] = array(
|
194
|
'#markup' => theme('commerce_product_sku', array('sku' => $product->sku, 'label' => t('SKU:'), 'product' => $product)),
|
195
|
'#attached' => $attached,
|
196
|
);
|
197
|
$content['title'] = array(
|
198
|
'#markup' => theme('commerce_product_title', array('title' => $product->title, 'label' => t('Title:'), 'product' => $product)),
|
199
|
'#attached' => $attached,
|
200
|
);
|
201
|
$content['status'] = array(
|
202
|
'#markup' => theme('commerce_product_status', array('status' => $product->status, 'label' => t('Status:'), 'product' => $product)),
|
203
|
'#attached' => $attached,
|
204
|
);
|
205
|
|
206
|
return parent::buildContent($product, $view_mode, $langcode, $content);
|
207
|
}
|
208
|
}
|